<!-- Template for starting a new test -->

<div style="margin:30px;">

<h1> Start new test for Rig [% rig_info.name %]</h1>
<br>

<form method="post" action="/cgi-bin/celltest/start_new_test.cgi" enctype="multipart/form-data" name="sofc">
[% INCLUDE print_hidden.tt %]
<input type="hidden" name="action" value="start_test">

<table class="new_test content-container fit-content"><tbody>
<tr><th colspan=2>New test information:</th></tr>
<tr><td title="Email addresses who will get warnings and errors from this test">Warning mails</td><td><input type="text" name="warning_mails" onchange="change_val(this,'main')" id="warning_mails" class="textfield" value="[% data.warning_mails %]" autocomplete=off></td></tr>
<tr><td>Sample number (*):</td><td><input type="text" name="id" class="textfield"></td></tr>
<tr><td>Project number (*):</td><td><input type="text" id=project name="project" [% IF data.project %]onchange="raw_ajax('[% data.project %]'+this.value,'&name=[% user_info.name %]&pass=[% user_info.pass %]',ajax_alert)"[% END %] class="textfield"></td></tr>
<tr><td>Other test numbers:</td><td><input type="text" name="name1" class="textfield bspace">
<!--<br><input type="text" name="name2" class="textfield bspace"><br>
<input type="text" name="name3" class="textfield bspace">-->
</td></tr>
<tr><td>Anode house (*):</td><td><input type="text" name="anode_house" class="textfield"></td></tr>
<tr><td>Cathode house (*):</td><td><input type="text" name="cathode_house" class="textfield"></td></tr>
<tr><td>Anode curent collector (*):</td><td><input type="text" name="anode_current_collector" class="textfield"></td></tr>
<tr><td>Cathode current collector (*):</td><td><input type="text" name="cathode_current_collector" class="textfield"></td></tr>
<tr><td>Sealing on anode:</td><td><input type="text" name="anode_glass" class="textfield"></td></tr>
<tr><td>Sealing on cathode:</td><td><input type="text" name="cathode_glass" class="textfield"></td></tr>
<tr><td>Sample preparation information (*):</td><td><textarea name="sample_info" rows="3" class="textfield2"></textarea></td></tr>
<tr><td>Test plan (*):</td><td><textarea name="testplan" rows="3" class="textfield2"></textarea></td></tr>
<tr><td>Further comments:</td><td><textarea name="comments" rows="5" class="textfield2"></textarea></td></tr>
<tr><td></td><td><input type="button" name="dummy" value="Start new test" class="rfcbutton" style="float:right;" onclick="check_start(this.form)"></td></tr>
</tbody>
</table>

</form>
</div>



<style>
.bspace {
     margin-bottom:3px;
}

.new_test {
     text-align:left;
     margin:auto;
}

.new_test th, .new_test td {
     padding:5px;
}

.textfield {
     width:200px;
}

.textfield2 {
     width:500px;
}


</style>

<script>

function check_start(f)
{
  if ([% IF data.ok %]checkinput()[% ELSE %]checki()[% END %])
  {
    f.submit();
  }
}


function checkinput() {
  if (!confirm('Start new test for Rig [% rig_info.name %]'))
  {
	return false;
  }
  var f = document.sofc;
  var txt = '';
  var newf = '';
  txt = f.id.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"id\" has not been input!");
     return false;
  }
  txt = f.project.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"project\" has not been input!");
     return false;
  }
  reg = /[% data.account_reg %]/;
  if (!reg.test(txt)) {
     alert('Account number not in correct format ([% data.account_reg_display %])');
     return false;
  }
  txt = f.anode_house.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"anode_house\" has not been input!");
     return false;
  }
  txt = f.cathode_house.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"cathode_house\" has not been input!");
     return false;
  }
  txt = f.anode_current_collector.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"anode_current_collector\" has not been input!");
     return false;
  }
  txt = f.cathode_current_collector.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"cathode_current_collector\" has not been input!");
     return false;
  }
  txt = f.sample_info.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"sample_info\" has not been input!");
     return false;
  }
  txt = f.testplan.value;
  if (txt == null || txt.length == 0) {
     alert("Required field \"testplan\" has not been input!");
     return false;
  }
  return true;
}

function alert_test() {
    if(confirm('Warning! Temperature is above 50C, a test is likely already running!\n Continue with new test?')) {
        return true;
    }
    else {
        back();
	return false;
    }
}

function checki() 
{
   var test = alert_test();
   var input = checkinput();
   if (test && input) {
       return true;
   }
   else {
       return false;
   }
}

[% IF global_config.global.project_number_autocomplete_url -%]
 \$("#project").autocomplete({
     source: function (request, response) {
       var name = document.getElementById('project').value;
       jQuery.getJSON("[% global_config.global.project_number_autocomplete_url %]"+name+"&name=[% user_info.name %]&pass=[% user_info.pass %]", {
          query: request.term
       }, function (data) {
          response(data);
       });
     },
     minLength: 3,
     delay: 200
  });

[% END -%]


function set_val(res)
{
    var arr = res.split("\n");
    if (arr[0] == "OK" || arr[0] == "WARN")
    {
        if (arr[0] == "WARN")
        {
            alert(arr[3]);
        }
        var obj = document.getElementById(arr[1]);
        obj.disabled = false;
        obj.value = arr[2];
    }
    else
    {
        alert(arr[0]);
    }
}

function change_val(element,section)
{
    var n = element.name;
    var val = element.value;
    element.disabled = true;
    run_ajax('change_config.cgi','&action=change_config_value&section='+section+'&key='+n+'&value='+val,set_val);
}

[% IF !data.warning_mails || global_config.global.set_warning_email_on_start.lower == 'yes' || config.main.set_warning_email_on_start.lower == 'yes' %]
document.getElementById('warning_mails').value = '[% data.email %]';
change_val(document.getElementById('warning_mails'),'main');
[% END %]

</script>
