<commit_before>var lastState, reqGroups, reqTypes, reqStatuses, dropdownsWereDrawn, isAdmin, dictionariesWereLoaded;

loadDictionaries();
isAdmin = localStorage.getItem("user_role") === "0";
hideOrShowReqCreateButton();

function dateFormatting(date) {
  var d = new Date(date);
  var monthNames = ["Jan", "Feb", "March", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  ];
  return d.getDate() + '-' + monthNames[d.getMonth()] + '-' + d.getFullYear();
};

function formatDateForInput(date) {
  var d = new Date(date);
  var month = d.getMonth() < 10 ? '0' + d.getMonth().toString() : d.getMonth().toString();
  return d.getFullYear() + '-' + month + '-' + d.getDate();
}

function createRequirement(){
  showRequirementCard(null, 'create');
}

function editRequirementCard() {
  changeReqCardState('edit');
}

function changeReqCardState(state) {
  if (state === 'view') {
    $('.view-field').show();
    $('.edit-field').hide();
    $('.edit-row').hide();
    $('.hide-in-edit').show();
  } else {
    showRequirementContentTab();
    $('.view-field').hide();
    $('.edit-field').show();
    $('.edit-row').css('display','flex');
    $('.hide-in-edit').hide();
  }

  if (!isAdmin) $('.btn-edit').hide();
}

function showRequirementCard(id, state) {
  if (lastState === 'create' && state !== 'create') {
    $('.hide-in-create').show();
    $('.show-in-create').hide();
  }

  if (!dropdownsWereDrawn && dictionariesWereLoaded) drawDropdowns();

  changeReqCardState(state);

  document.getElementById('requirement-card').style.display = 'block';

  document.getElementById('req_id').innerHTML = id;
  localStorage.setItem('req_id', id);
  if (state !== 'create') {
    $.post('/api/getRequirementId', {req_id: id}, function(data){
      //https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page
      window.history.pushState("object or string", "Title", "/"+id);

      document.getElementById('req_text').innerHTML =
        document.getElementById('req_text_edit').innerHTML = data.recordset[0].RawDataPlant;
      document.getElementById('req_group').innerHTML = data.recordset[0].Group;
      document.getElementById('req_author').innerHTML = data.recordset[0].Authors;
      document.getElementById('req_authors_edit').value = data.recordset[0].Authors;
      if (data.recordset[0].ElicitationDate) {
        document.getElementById('req_eliciataion').innerHTML = dateFormatting(data.recordset[0].ElicitationDate);
        $('#req_date_edit').val(formatDateForInput(data.recordset[0].ElicitationDate));
      } else {
        document.getElementById('req_eliciataion').innerHTML = 'N/A'
      }
      document.getElementById('req_prior').innerHTML =
        document.getElementById('req_prior_edit').value = data.recordset[0].Priority;
      document.getElementById('req_isreviewed').innerHTML = data.recordset[0].IsReviewed;
      $('#isreviewed-checkbox').prop('checked', data.recordset[0].IsReviewed == 'no' ? false : true)
      document.getElementById('req_status').innerHTML = data.recordset[0].Status;
      $('#status-select').val(data.recordset[0].StatusId);
      $('#group-select').val(data.recordset[0].GroupId);
      $('#type-select').val(data.recordset[0].TypeId);
      document.getElementById('req_BE').innerHTML =
        document.getElementById('req_BE_edit').value = data.recordset[0].BE_Estimate;
      document.getElementById('req_FE').innerHTML =
        document.getElementById('req_FE_edit').value = data.recordset[0].FE_Estimate;
      if (data.recordset[0].ChangeRequestLink) {
        document.getElementById('req_CR').setAttribute("href", data.recordset[0].ChangeRequestLink);
        document.getElementById('req_CR').style.display = 'inline';
      } else {
        document.getElementById('req_CR').style.display = 'none';
      };
      document.getElementById('req_crLink_edit').value = data.recordset[0].ChangeRequestLink;
      document.getElementById('req_sorce').innerHTML =
        document.getElementById('req_source_edit').value = data.recordset[0].Source;
      document.getElementById('req_notes').innerHTML =
        document.getElementById('req_notes_edit').innerHTML = data.recordset[0].Comment;

      $.post('/api/getCommentAmount', {req_id:id}, (data)=>{
        if (data.recordset[0].commentAmount != 0) {
          document.getElementById('commentAmount').innerHTML = data.recordset[0].commentAmount;
        } else {document.getElementById('commentAmount').innerHTML = ''}
      });

      $('select').material_select();

      $.post('/api/getComment', {req_id: id}, (data)=>{
        loadComment(data);
      });

      $.post('/api/getAttachments', {req_id: id}, (data)=>{
        loadAttachments(data);
      });
    });
  } else {
    $('.hide-in-create').hide();
    $('.show-in-create').show();

    document.getElementById('req_id').innerHTML = ''
    document.getElementById('req_text_edit').innerHTML = '';
    document.getElementById('req_authors_edit').value = '';
    document.getElementById('req_prior_edit').value = '';
    document.getElementById('req_BE_edit').value = '';
    document.getElementById('req_FE_edit').value = '';
    document.getElementById('req_date_edit').value = '';
    document.getElementById('req_CR').setAttribute("href", '');

    document.getElementById('req_source_edit').value = '';
    document.getElementById('req_notes_edit').innerHTML = '';
    document.getElementById('req_crLink_edit').value = '';

    $('#status-select').val(0);
    $('#group-select').val(0);
    $('#type-select').val(0);

    $('select').material_select();
  }

  lastState = state;
};

function saveRequirement() {
  if (!($('#group-select').val() &&
      $('#type-select').val() &&
      $('#req_text_edit').val() &&
      document.getElementById('req_prior_edit').value &&
      $('#status-select').val() &&
      document.getElementById('req_authors_edit').value
    )) {
      alert('Please set Group, Type, Req. text, Priority, Status and Authors');
      return;
    }

  $.post('/api/createOrUpdateRequirement', {
    id: document.getElementById('req_id').innerHTML,
    groupId: $('#group-select').val(),
    typeId: $('#type-select').val(),
    text: $('#req_text_edit').val(),
    priority: document.getElementById('req_prior_edit').value,
    isreviewed: document.getElementById('isreviewed-checkbox').checked ? 1 : 0,
    be: document.getElementById('req_BE_edit').value,
    fe: document.getElementById('req_FE_edit').value,
    statusId: $('#status-select').val(),
    authors: document.getElementById('req_authors_edit').value,
    comment: $('#req_notes_edit').val(),
    source: document.getElementById('req_source_edit').value,
    crLink: document.getElementById('req_crLink_edit').value,
    date: document.getElementById('req_date_edit').value
  }, function(d){
    $.post('/api/getNewRequirementId', function(data) {

        alert('New Requirement has been created with Id: ' + data.recordset[0].newId)

    });
    closeRequirementCard();


    location.reload();
  });
}

function closeRequirementCard(){
  document.getElementById('requirement-card').style.display = 'none';
  window.history.pushState("", "", "/");
};

function showLoginCard(){
  document.getElementById('login-card').style.display = 'block';
};
<commit_msg>fix bug with messgers of a new requirement<commit_after>var lastState, reqGroups, reqTypes, reqStatuses, dropdownsWereDrawn, isAdmin, dictionariesWereLoaded;

loadDictionaries();
isAdmin = localStorage.getItem("user_role") === "0";
hideOrShowReqCreateButton();

function dateFormatting(date) {
  var d = new Date(date);
  var monthNames = ["Jan", "Feb", "March", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  ];
  return d.getDate() + '-' + monthNames[d.getMonth()] + '-' + d.getFullYear();
};

function formatDateForInput(date) {
  var d = new Date(date);
  var month = d.getMonth() < 10 ? '0' + d.getMonth().toString() : d.getMonth().toString();
  return d.getFullYear() + '-' + month + '-' + d.getDate();
}

function createRequirement(){
  showRequirementCard(null, 'create');
}

function editRequirementCard() {
  changeReqCardState('edit');
}

function changeReqCardState(state) {
  if (state === 'view') {
    $('.view-field').show();
    $('.edit-field').hide();
    $('.edit-row').hide();
    $('.hide-in-edit').show();
  } else {
    showRequirementContentTab();
    $('.view-field').hide();
    $('.edit-field').show();
    $('.edit-row').css('display','flex');
    $('.hide-in-edit').hide();
  }

  if (!isAdmin) $('.btn-edit').hide();
}

function showRequirementCard(id, state) {
  if (lastState === 'create' && state !== 'create') {
    $('.hide-in-create').show();
    $('.show-in-create').hide();
  }

  if (!dropdownsWereDrawn && dictionariesWereLoaded) drawDropdowns();

  changeReqCardState(state);

  document.getElementById('requirement-card').style.display = 'block';

  document.getElementById('req_id').innerHTML = id;
  localStorage.setItem('req_id', id);
  if (state !== 'create') {
    $.post('/api/getRequirementId', {req_id: id}, function(data){
      //https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page
      window.history.pushState("object or string", "Title", "/"+id);

      document.getElementById('req_text').innerHTML =
        document.getElementById('req_text_edit').innerHTML = data.recordset[0].RawDataPlant;
      document.getElementById('req_group').innerHTML = data.recordset[0].Group;
      document.getElementById('req_author').innerHTML = data.recordset[0].Authors;
      document.getElementById('req_authors_edit').value = data.recordset[0].Authors;
      if (data.recordset[0].ElicitationDate) {
        document.getElementById('req_eliciataion').innerHTML = dateFormatting(data.recordset[0].ElicitationDate);
        $('#req_date_edit').val(formatDateForInput(data.recordset[0].ElicitationDate));
      } else {
        document.getElementById('req_eliciataion').innerHTML = 'N/A'
      }
      document.getElementById('req_prior').innerHTML =
        document.getElementById('req_prior_edit').value = data.recordset[0].Priority;
      document.getElementById('req_isreviewed').innerHTML = data.recordset[0].IsReviewed;
      $('#isreviewed-checkbox').prop('checked', data.recordset[0].IsReviewed == 'no' ? false : true)
      document.getElementById('req_status').innerHTML = data.recordset[0].Status;
      $('#status-select').val(data.recordset[0].StatusId);
      $('#group-select').val(data.recordset[0].GroupId);
      $('#type-select').val(data.recordset[0].TypeId);
      document.getElementById('req_BE').innerHTML =
        document.getElementById('req_BE_edit').value = data.recordset[0].BE_Estimate;
      document.getElementById('req_FE').innerHTML =
        document.getElementById('req_FE_edit').value = data.recordset[0].FE_Estimate;
      if (data.recordset[0].ChangeRequestLink) {
        document.getElementById('req_CR').setAttribute("href", data.recordset[0].ChangeRequestLink);
        document.getElementById('req_CR').style.display = 'inline';
      } else {
        document.getElementById('req_CR').style.display = 'none';
      };
      document.getElementById('req_crLink_edit').value = data.recordset[0].ChangeRequestLink;
      document.getElementById('req_sorce').innerHTML =
        document.getElementById('req_source_edit').value = data.recordset[0].Source;
      document.getElementById('req_notes').innerHTML =
        document.getElementById('req_notes_edit').innerHTML = data.recordset[0].Comment;

      $.post('/api/getCommentAmount', {req_id:id}, (data)=>{
        if (data.recordset[0].commentAmount != 0) {
          document.getElementById('commentAmount').innerHTML = data.recordset[0].commentAmount;
        } else {document.getElementById('commentAmount').innerHTML = ''}
      });

      $('select').material_select();

      $.post('/api/getComment', {req_id: id}, (data)=>{
        loadComment(data);
      });

      $.post('/api/getAttachments', {req_id: id}, (data)=>{
        loadAttachments(data);
      });
    });
  } else {
    $('.hide-in-create').hide();
    $('.show-in-create').show();

    document.getElementById('req_id').innerHTML = ''
    document.getElementById('req_text_edit').innerHTML = '';
    document.getElementById('req_authors_edit').value = '';
    document.getElementById('req_prior_edit').value = '';
    document.getElementById('req_BE_edit').value = '';
    document.getElementById('req_FE_edit').value = '';
    document.getElementById('req_date_edit').value = '';
    document.getElementById('req_CR').setAttribute("href", '');

    document.getElementById('req_source_edit').value = '';
    document.getElementById('req_notes_edit').innerHTML = '';
    document.getElementById('req_crLink_edit').value = '';

    $('#status-select').val(0);
    $('#group-select').val(0);
    $('#type-select').val(0);

    $('select').material_select();
  }

  lastState = state;
};

function saveRequirement() {

  if (!($('#group-select').val() &&
      $('#type-select').val() &&
      $('#req_text_edit').val() &&
      document.getElementById('req_prior_edit').value &&
      $('#status-select').val() &&
      document.getElementById('req_authors_edit').value
    )) {
      alert('Please set Group, Type, Req. text, Priority, Status and Authors');
      return;
    }

    $.post('/api/createOrUpdateRequirement', {
      id: document.getElementById('req_id').innerHTML,
      groupId: $('#group-select').val(),
      typeId: $('#type-select').val(),
      text: $('#req_text_edit').val(),
      priority: document.getElementById('req_prior_edit').value,
      isreviewed: document.getElementById('isreviewed-checkbox').checked ? 1 : 0,
      be: document.getElementById('req_BE_edit').value,
      fe: document.getElementById('req_FE_edit').value,
      statusId: $('#status-select').val(),
      authors: document.getElementById('req_authors_edit').value,
      comment: $('#req_notes_edit').val(),
      source: document.getElementById('req_source_edit').value,
      crLink: document.getElementById('req_crLink_edit').value,
      date: document.getElementById('req_date_edit').value
  }, function(d){
    if (!document.getElementById('req_id').innerHTML) {
      $.post('/api/getNewRequirementId', function(data) {
          alert('New Requirement has been created with Id: ' + data.recordset[0].newId)
      });
    }

    closeRequirementCard();

    location.reload();
  });
}

function closeRequirementCard(){
  document.getElementById('requirement-card').style.display = 'none';
  window.history.pushState("", "", "/");
};

function showLoginCard(){
  document.getElementById('login-card').style.display = 'block';
};

{'fraction_non_alphanumeric': 0.1896271186440678, 'fraction_numerical': 0.005559322033898305, 'mean_word_length': 4.192185850052798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5749368', 'n_tokens_mistral': 4480, 'n_tokens_neox': 4386, 'n_words': 573}