<commit_before>{% extends sitebase.html %}
{% block head %}
<script type="text/javascript" src="/static/vendor/js/jquery.validate.min.js"></script>
<style>
  .custom-combobox {
    position: relative;
    display: inline-block;
  }
  .custom-combobox-toggle {
    position: absolute;
    top: 0;
    bottom: 0;
    margin-left: -1px;
    padding: 0;
  }
  .custom-combobox-input {
    margin: 0;
    padding: 5px 10px;
  }
</style>

<script>
$(document).ready(function() {
    $("#new_person_div").hide();
     $( "#{{creation_form.principal_investigator.id}}" ).chosen({
        allow_single_deselect: true
     });
    $( "#{{creation_form.lab_person.id}}" ).chosen({
        allow_single_deselect: true
      });
    $("#create_study").validate({
      ignore: "",
      submitHandler: function (form) {
        //from http://stackoverflow.com/a/18265708
        // let's disable the inputs for the duration of the ajax request
        $("#create_study_submit").prop('disabled', true);

        // fire off ajax the request
        request = $.ajax({
          type: "GET",
          url: '/check_study/',
          data: {study_title: $("#study_title")[0].value {% if study %}, old_study_title: '{{study.title}}'{% end %} }
        });

        // callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR) {
            if (response == 'True') {
                form.submit();
            }
            else {
                $("#create_study_submit").prop('disabled', false);
                $("#messagespan").text("Study '" + $("#study_title")[0].value + "' already exists! Please choose a different name.");
                alert($("#messagespan").text());
            }
        });

        // callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown) {
            // log the error to the console
            console.error(
                "The following error occured: " + textStatus, errorThrown);
            $("#create_study_submit").prop('disabled', false);
        });
    },
      rules:{
{% for idx, form_item in enumerate(creation_form, start=1) %}
  {% if form_item.flags.required %}
    {{form_item.name}}: {required:true},
  {% end %}
{% end %}
      },
      messages: {
{% for idx, form_item in enumerate(creation_form, start=1) %}
  {% if form_item.flags.required %}
    {{form_item.name}}: '{{form_item.label.text}} is required',
  {% end %}
{% end %}
      },
      errorLabelContainer: "#errorspan",
      wrapper: "li",
    });
});

counter = -1;

function add_new_person(name, affiliation, email, phone, address) {
    if (name.value == '') {
        $("#new_person_name").focus();
        return false;
    };
    if (affiliation.value == '') {
        $("#new_person_affiliation").focus();
        return false;
    };
    if (email.value == '') {
        $("#new_person_email").focus();
        return false;
    };

    var new_element = '<option value="'+counter+'" "selected">' + name.value + ', ' + affiliation.value + '</option>';

    $("#{{creation_form.principal_investigator.id}}").append(new_element);
    $("#{{creation_form.lab_person.id}}" ).append(new_element);
    $("#{{creation_form.principal_investigator.id}}").trigger("chosen:updated");
    $("#{{creation_form.lab_person.id}}" ).trigger("chosen:updated");
    $("#new_person_div").hide(400);

    $("#create_study").append('<input type="hidden" name="new_people_names" value="' + name.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_affiliations" value="' + affiliation.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_emails" value="' + email.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_phones" value="' + phone.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_addresses" value="' + address.value + '">');

    counter -= 1;
};

{% if study %}
function cancel_edition(){
  if (confirm("Are you sure you want to go to the study description page? None of the changes will be stored")){
    window.location.href = "/study/description/{{study.id}}";
  }
}
{% end %}

</script>

{% end %}

{% block content %}
<h1>{% if study %}Edit study (ID: {{study.id}}) {% else %}Create a new Study{%end%}</h1>
<h3>
    <font color="red">*</font> = Required Field
</h3>
<span id="errorspan" name="errorspan" style="color: red"></span>
<span id="messagespan" name="messagespan" style="color: red"></span>
<br />
<form id="create_study" name="create_study" action={%if study%}'/study/edit/{{study.id}}'{%else%}'/study/create/'{%end%} method="POST" onsubmit='return submit_create_study();'>
<table>
{% for idx, form_item in enumerate(creation_form, start=1) %}
    {% set kwargs = {'tabindex': idx, 'class_': 'form-control'} %}
    {% set additional_info = '' %}
    {# form-item-specific rendering #}
    {% if form_item.label.text == 'Environmental Packages' %}
        {% set kwargs['size'] = len(form_item.choices) %}
        {% set additional_info = 'You can select multiple entries by control-clicking (mac: command-clicking)' %}
    {% end %}
  <tr>
    <td width="20%">{% raw form_item.label %} <br /> {{additional_info}}</td>
    <td width="30%">{% raw form_item(**kwargs) %}</td>
    <td width="50%">{% if form_item.flags.required %}<font color="red"><h4>&nbsp;&nbsp;*</h4></font>{% end %}</td>
  </tr>
  {% if form_item.label.text == 'Lab Person' %}
  <tr>
    <td></td>
    <td>Can't find the person you're looking for? <a href="#bot" onclick='$("#new_person_div").show(400); return false;'>Add a person</a></td>
  </tr>
  <tr>
    <td></td>
    <td>
      <div name="new_person_div" id="new_person_div">
        <table><commit_msg>manually update PI field for validator<commit_after>{% extends sitebase.html %}
{% block head %}
<script type="text/javascript" src="/static/vendor/js/jquery.validate.min.js"></script>
<style>
  .custom-combobox {
    position: relative;
    display: inline-block;
  }
  .custom-combobox-toggle {
    position: absolute;
    top: 0;
    bottom: 0;
    margin-left: -1px;
    padding: 0;
  }
  .custom-combobox-input {
    margin: 0;
    padding: 5px 10px;
  }
</style>

<script>
$(document).ready(function() {
    $("#new_person_div").hide();
     $( "#{{creation_form.principal_investigator.id}}" ).chosen({
        allow_single_deselect: true,
     });
    $( "#{{creation_form.lab_person.id}}" ).chosen({
        allow_single_deselect: true
      });
    $("#create_study").validate({
      ignore: "",
      submitHandler: function (form) {
        //from http://stackoverflow.com/a/18265708
        // let's disable the inputs for the duration of the ajax request
        $("#create_study_submit").prop('disabled', true);

        // fire off ajax the request
        request = $.ajax({
          type: "GET",
          url: '/check_study/',
          data: {study_title: $("#study_title")[0].value {% if study %}, old_study_title: '{{study.title}}'{% end %} }
        });

        // callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR) {
            if (response == 'True') {
                form.submit();
            }
            else {
                $("#create_study_submit").prop('disabled', false);
                $("#messagespan").text("Study '" + $("#study_title")[0].value + "' already exists! Please choose a different name.");
                alert($("#messagespan").text());
            }
        });

        // callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown) {
            // log the error to the console
            console.error(
                "The following error occured: " + textStatus, errorThrown);
            $("#create_study_submit").prop('disabled', false);
        });
    },
      rules:{
{% for idx, form_item in enumerate(creation_form, start=1) %}
  {% if form_item.flags.required %}
    {{form_item.name}}: {required:true},
  {% end %}
{% end %}
      },
      messages: {
{% for idx, form_item in enumerate(creation_form, start=1) %}
  {% if form_item.flags.required %}
    {{form_item.name}}: '{{form_item.label.text}} is required',
  {% end %}
{% end %}
      },
      errorLabelContainer: "#errorspan",
      wrapper: "li",
    });

    $('#{{creation_form.principal_investigator.id}}').on('change', function(e) {
      $("#create_study").validate().element("#{{creation_form.principal_investigator.id}}");
    });
});

counter = -1;

function add_new_person(name, affiliation, email, phone, address) {
    if (name.value == '') {
        $("#new_person_name").focus();
        return false;
    };
    if (affiliation.value == '') {
        $("#new_person_affiliation").focus();
        return false;
    };
    if (email.value == '') {
        $("#new_person_email").focus();
        return false;
    };

    var new_element = '<option value="'+counter+'" "selected">' + name.value + ', ' + affiliation.value + '</option>';

    $("#{{creation_form.principal_investigator.id}}").append(new_element);
    $("#{{creation_form.lab_person.id}}" ).append(new_element);
    $("#{{creation_form.principal_investigator.id}}").trigger("chosen:updated");
    $("#{{creation_form.lab_person.id}}" ).trigger("chosen:updated");
    $("#new_person_div").hide(400);

    $("#create_study").append('<input type="hidden" name="new_people_names" value="' + name.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_affiliations" value="' + affiliation.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_emails" value="' + email.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_phones" value="' + phone.value + '">');
    $("#create_study").append('<input type="hidden" name="new_people_addresses" value="' + address.value + '">');

    counter -= 1;
};

{% if study %}
function cancel_edition(){
  if (confirm("Are you sure you want to go to the study description page? None of the changes will be stored")){
    window.location.href = "/study/description/{{study.id}}";
  }
}
{% end %}

</script>

{% end %}

{% block content %}
<h1>{% if study %}Edit study (ID: {{study.id}}) {% else %}Create a new Study{%end%}</h1>
<h3>
    <font color="red">*</font> = Required Field
</h3>
<span id="errorspan" name="errorspan" style="color: red"></span>
<span id="messagespan" name="messagespan" style="color: red"></span>
<br />
<form id="create_study" name="create_study" action={%if study%}'/study/edit/{{study.id}}'{%else%}'/study/create/'{%end%} method="POST">
<table>
{% for idx, form_item in enumerate(creation_form, start=1) %}
    {% set kwargs = {'tabindex': idx, 'class_': 'form-control'} %}
    {% set additional_info = '' %}
    {# form-item-specific rendering #}
    {% if form_item.label.text == 'Environmental Packages' %}
        {% set kwargs['size'] = len(form_item.choices) %}
        {% set additional_info = 'You can select multiple entries by control-clicking (mac: command-clicking)' %}
    {% elif form_item.label.text == 'Principan Investigator'%}
        {% set kwargs['class_'] = kwargs['class_'] + ' chzn-select' %}
    {% end %}
  <tr>
    <td width="20%">{% raw form_item.label %} <br /> {{additional_info}}</td>
    <td width="30%">{% raw form_item(**kwargs) %}</td>
    <td width="50%">{% if form_item.flags.required %}<font color="red"><h4>&nbsp;&nbsp;*</h4></font>{% end %}</td>
  </tr>
  {% if form_item.label.text == 'Lab Person' %}
  <tr>
    <td></td>
    <td>Can't find the person you're looking for? <a href="#bot" onclick='$("#new_person_div").show(400); return false;'>Add a person</a></td>
  </tr>
  <tr>
    <td></td>
    <td>
      <div name="new_person_div" id="new_person_div">
        <table>
{'fraction_non_alphanumeric': 0.19998313090418354, 'fraction_numerical': 0.006916329284750338, 'mean_word_length': 3.2240826505165656, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 109, 'https://': 0, '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': '3850577', 'n_tokens_mistral': 3985, 'n_tokens_neox': 3720, 'n_words': 789}