<div class=toolbox>
<h3>Toolbox</h3>

[% IF user_info.auth -%]

[% IF data.tempcontrol.size > 0 -%]
<div class=overlay id=temperature>
[% INCLUDE close_btn.tt %]
<h3> Set temperature</h3>
<br>
<table class="new_setpoint">
[% IF data.tempcontrol.size == 1 %]
<tr><td colspan=2>[% data.tempcontrol.0 %]</td></tr>
<input class="device tempcontrol_[% data.tempcontrol.0 %]" type=hidden id="select_tempcontrol" devclass="tempcontrol" devname="[% data.tempcontrol.0 %]" value="[% data.tempcontrol.0 %]">
[% ELSE %]
<tr><td colspan=3>
<select autocomplete=off onchange="set_tempcontrol_line(this)" class="setpoint_input" id="select_tempcontrol" devclass="tempcontrol" devname="[% data.tempcontrol.0 %]">
<option value="">Select controler</option>
[% FOR i IN data.tempcontrol %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Temp</td><td class="[% IF data.tempcontrol.size == 1 %]device tempcontrol_[% data.tempcontrol.0 %]_value[% END %]" id=tempcontrol_value></td><td>&#x2103;</td></tr>
<tr><td>Setpoint</td><td><input class="[% IF data.tempcontrol.size == 1 %]device tempcontrol_[% data.tempcontrol.0 %]_setpoint[% END %]" type=text value="" id=tempcontrol_set style="width:70px;" autocomplete=off></td><td>&#x2103;</td></tr>
<tr><td>Ramp rate</td><td><input class="[% IF data.tempcontrol.size == 1 %]device tempcontrol_[% data.tempcontrol.0 %]_ramprate[% END %]" type=text value="" id=tempcontrol_ramp style="width:70px;" autocomplete=off></td><td>&#x2103;/h</td></tr>
<tr><td colspan=3><button onclick="set_new_temp()" class="setpoint_input">Set new values</button></td></tr>
</table>
</div>
[% END -%]
[% IF data.relay.size -%]
<div class=overlay id=relay>
[% INCLUDE close_btn.tt %]
<h3> Set Relay status</h3>
<br>
<table class="new_setpoint">
[% IF data.relay.size == 1 %]
<tr><td colspan=2>[% data.relay.0 %]</td></tr>
<input type=hidden id="select_relay" devclass="relay" value="[% data.relay.0 %]" devname="[% data.relay.0 %]" class="device relay_[% data.relay.0 %]">
[% ELSE %]
<tr><td colspan=2>
<select autocomplete=off onchange="set_relay_line(this)" class="setpoint_input" id="select_relay">
<option value="">Select relay</option>
[% FOR i IN data.relay %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Status</td><td>
<select class="[% IF data.relay.size == 1 %]device relay_[% data.relay.0 %]_value[% END %]" type=text id=relay_value autocomplete=off devclass="relay" [% IF data.relay.size == 1 %]devname="[% data.relay.0 %]"[% END %]>
<option value=''></option>
<option value='0'>Off</option>
<option value='1'>On</option>
</select>
</td></tr>
<tr><td colspan=2><button onclick="set_new_relay()" class="setpoint_input">Set status</button></td></tr>

</table>
</div>
[% END -%]
[% IF data.water.size -%]
<div class=overlay id=water>
[% INCLUDE close_btn.tt %]
<h3> Set water bubler</h3>
<br>
<table class="new_setpoint">
[% IF data.water.size == 1 %]
<tr><td colspan=2>[% data.water.0 %]</td></tr>
<input type=hidden id="select_water" devclass="water" value="[% data.water.0 %]" devname="[% data.water.0 %]" class="device water_[% data.water.0 %]">
[% ELSE %]
<tr><td colspan=2>
<select autocomplete=off onchange="set_water_line(this)" class="setpoint_input" id="select_water">
<option value="">Select humidifier</option>
[% FOR i IN data.water %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Status</td><td>
<select class="[% IF data.water.size == 1 %]device relay_[% data.water.0 %]_value[% END %]" type=text id=water_value autocomplete=off devclass="relay" [% IF data.water.size == 1 %]devname="[% data.water.0 %]"[% END %]>
<option value=''></option>
<option value='0'>Off</option>
<option value='1'>On</option>
</select>
</td></tr>
<tr><td colspan=2><button onclick="set_new_water()" class="setpoint_input">Set status</button></td></tr>
</table>

</div>
[% END -%]
[% IF data.gas.size -%]
<div class=overlay id=gas>
[% INCLUDE close_btn.tt %]
<h3> Set gas flow</h3>
<table class="new_setpoint">
[% IF data.gas.size == 1 %]
<tr><td colspan=2>[% data.gas.0 %]</td></tr>
<input type=hidden id="select_gas" devclass="gas" value="[% data.gas.0 %]" devname="[% data.gas.0 %]" class="device gas_[% data.gas.0 %]">
[% ELSE %]
<tr><td colspan=3>
<select autocomplete=off onchange="set_gas_line(this)" class="setpoint_input" id="select_gas">
<option value="">Select gas</option>
[% FOR i IN data.gas %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Flow</td><td class="[% IF data.gas.size == 1 %]device gas_[% data.gas.0 %]_value[% END %]" id=gas_value></td><td>L/hour</td></tr>
<tr><td>Setpoint</td><td><input class="[% IF data.gas.size == 1 %]device gas_[% data.gas.0 %]_setpoint[% END %]" type=text value="" id=gas_set style="width:70px;" autocomplete=off></td><td>L/hour</td></tr>
</td></tr>
<tr><td colspan=3><button onclick="set_new_gas()" class="setpoint_input">Set flow</button></td></tr>

</table>
</div>
[% END -%]
[% IF data.multiplex.size -%]
<div class=overlay id=multiplex>
[% INCLUDE close_btn.tt %]
<h3> Set multiplexer </h3>

<table class="new_setpoint">
[% IF data.multiplex.size == 1 %]
<tr><td colspan=2>[% data.multiplex.0.label %]</td></tr>
<input type=hidden id="select_multiplex" devclass="multiplex" value="[% data.multiplex.0.name %]" devname="[% data.multiplex.0.name %]" class="device multiplex_[% data.multiplex.0.name %]">
[% ELSE %]
<tr><td colspan=2>
<select autocomplete=off onchange="set_multiplex_line(this)" class="setpoint_input" id="select_multiplex">
<option value="">Select multiplexer</option>
[% FOR i IN data.multiplex %]<option value="[% i.name %]">[% i.label.replace('_',' ') %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Select</td><td>
<select class="[% IF data.multiplex.size == 1 %]device multiplex_[% data.multiplex.0.name %]_value[% END %]" type=text value="" id=multiplex_set autocomplete=off style="min-width:70px;"> 
[% mp = data.multiplex.0.name %]
<option value=''>Select value</option>
[% IF data.multiplex.size == 1 -%]
[% FOR i IN data.multiplex_gasses.$mp %]<option value="[% i %]">[% i %]</option>[% END %]
[% END -%]
</select>
</td></tr>
</td></tr>
<tr><td colspan=3><button onclick="set_new_multiplex()" class="setpoint_input">Set selection</button></td></tr>
</table>



</div>
[% END -%]
[% IF data.PSU.size -%]
<div class=overlay id=current>
[% INCLUDE close_btn.tt %]
<h3> Set PSU current</h3>

<table class="new_setpoint">
[% IF data.PSU.size == 1 %]
<tr><td colspan=2>[% data.PSU.0 %]</td></tr>
<input type=hidden id="select_PSU_current" devclass="PSU" value="[% data.PSU.0 %]" devname="[% data.PSU.0 %]" class="device PSU_[% data.PSU.0 %]">
[% ELSE %]
<tr><td colspan=3>
<select autocomplete=off onchange="set_PSU_line(this,'current')" class="setpoint_input" id="select_PSU_current">
<option value="">Select PSU</option>
[% FOR i IN data.PSU %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Current</td><td class="[% IF data.PSU.size == 1 %]device PSU_[% data.PSU.0 %]_value[% END %]" id=PSU_current_value></td><td>A</td></tr>
<tr><td>Setpoint</td><td><input class="[% IF data.PSU.size == 1 %]device PSU_[% data.PSU.0 %]_current_setpoint[% END %]" type=text value="" id=PSU_current_set style="width:70px;" autocomplete=off></td><td>A</td></tr>
</td></tr>
<tr><td colspan=3><button onclick="set_new_PSU('current',document.getElementById('PSU_current_set').value,'current')" class="setpoint_input">Set current</button></td></tr>
<tr><td colspan=3><button onclick="set_new_PSU('current','ocv','current')" class="setpoint_input">Set OCV</button></td></tr>
</table>

</div>
<div class=overlay id=voltage>
[% INCLUDE close_btn.tt %]
<h3> Set PSU voltage</h3>
<table class="new_setpoint">
[% IF data.PSU.size == 1 %]
<tr><td colspan=2>[% data.PSU.0 %]</td></tr>
<input type=hidden id="select_PSU_voltage" devclass="PSU" value="[% data.PSU.0 %]" devname="[% data.PSU.0 %]" class="device PSU_[% data.PSU.0 %]">
[% ELSE %]
<tr><td colspan=3>
<select autocomplete=off onchange="set_PSU_line(this,'voltage')" class="setpoint_input" id="select_PSU_voltage">
<option value="">Select PSU</option>
[% FOR i IN data.PSU %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Voltage</td><td class="[% IF data.PSU.size == 1 %]device PSU_[% data.PSU.0 %]_value[% END %]" id=PSU_voltage_value></td><td>V</td></tr>
<tr><td>Setpoint</td><td><input class="[% IF data.PSU.size == 1 %]device PSU_[% data.PSU.0 %]_voltage_setpoint[% END %]" type=text value="" id=PSU_voltage_set style="width:70px;" autocomplete=off></td><td>V</td></tr>
</td></tr>
<tr><td colspan=3><button onclick="set_new_PSU('voltage',document.getElementById('PSU_voltage_set').value,'voltage')" class="setpoint_input">Set voltage</button></td></tr>
<tr><td colspan=3><button onclick="set_new_PSU('voltage','ocv','voltage')" class="setpoint_input">Set OCV</button></td></tr>
</table>
</div>
[% END -%]

[% IF data.PID.size -%]
<div class=overlay id=PID>
[% INCLUDE close_btn.tt %]
<h3> Set PID setpoint</h3>
<table class="new_setpoint">
[% IF data.gas.size == 1 %]
<tr><td colspan=2>[% data.PID.0 %]</td></tr>
<input type=hidden id="select_PID" devclass="PID" value="[% data.PID.0 %]" devname="[% data.PID.0 %]" class="device PID_[% data.PID.0 %]">
[% ELSE %]
<tr><td colspan=3>
<select autocomplete=off onchange="set_PID_line(this)" class="setpoint_input" id="select_PID">
<option value="">Select PID</option>
[% FOR i IN data.PID %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Setpoint</td><td><input class="[% IF data.PID.size == 1 %]device PID_[% data.PID.0 %]_setpoint[% END %]" type=text value="" id=PID_set style="width:70px;" autocomplete=off></td><td></td></tr>
</td></tr>
<tr><td colspan=3><button onclick="set_new_PID()" class="setpoint_input">Set setpoint</button></td></tr>
</table>
</div>
[% END -%]

[% IF data.analog.size -%]
<div class=overlay id=analog>
[% INCLUDE close_btn.tt %]
<h3> Set analog output</h3>


<table class="new_setpoint">
[% IF data.analog.size == 1 %]
<tr><td colspan=2>[% data.analog.0 %]</td></tr>
<input type=hidden id="select_analog" devclass="analog" value="[% data.analog.0 %]" devname="[% data.analog.0 %]" class="device analog_[% data.analog.0 %]">
[% ELSE %]
<tr><td colspan=3>
<select autocomplete=off onchange="set_analog_line(this)" class="setpoint_input" id="select_analog">
<option value="">Select output</option>
[% FOR i IN data.analog %]<option value="[% i %]">[% i %]</option>[% END %]
</select>
</td></tr>
[% END %]
<tr><td>Value</td><td><input class="[% IF data.analog.size == 1 %]device gas_[% data.analog.0 %]_value[% END %]" type=text value="" id=gas_analog style="width:70px;" autocomplete=off></td><td id=aunit></td></tr>
</td></tr>
<tr><td colspan=3><button onclick="set_new_analog()" class="setpoint_input">Set output</button></td></tr>
</table>



</div>
[% END -%]

[% END -%]

<div class=overlay id=impedance>
[% INCLUDE close_btn.tt %]
<h3> Start impedance on remote Elchemea system</h3>

<form method="post" enctype="multipart/form-data">
<table class="new_setpoint"><tbody>
<tr><td></td><td><input type="button" name="dummy" value="Configure impedance" onclick="configure_imp(this.form.server.value)"></td></tr>
<tr><th> Server: </th><td><input type="text" name="server" value="[% config.impedance.server %]" class="textfield"></td></tr>
<tr><th> Port: </th><td><input type="text" name="port" value="[% config.impedance.port %]" class="textfield"></td></tr>
<tr><th> Username: </th><td><input type="text" name="impuser" value="[% config.impedance.user %]" class="textfield"></td></tr>
<tr><th> Session: </th><td><input type="text" name="session" value="[% config.impedance.session %]" class="textfield"></td></tr>
<tr><th> Compensation: </th><td><select name="comp" class="textfield">
<option selected="selected" value="DEFAULT">DEFAULT</option>
</select></td></tr>
<tr><td></td><td><input type="button" name="test" value="Ok" onclick="run_imp()"></td></tr>
</tbody></table>
</form>
</div>

<table class="tool-list">

[% IF user_info.auth -%]
[% IF data.tempcontrol.size > 0 %]<tr><td><button type=button class="tool-btn" onclick="set_option('temperature')">Set temperature</button</td></tr>[% END %]
[% IF data.relay.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('relay')">Set relay status</button></td></tr>[% END %]
[% IF data.water.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('water')">Set humidifier</button></td></tr>[% END %]
[% IF data.gas.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('gas')">Set gas flow</button></td></tr>[% END %]
[% IF data.multiplex.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('multiplex')">Set multiplexer</button></td></tr>[% END %]
[% IF data.PSU.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('current')">Set PSU current</button></td></tr>
<tr><td><button type=button class="tool-btn" onclick="set_option('voltage')">Set PSU voltage</button></td></tr>[% END %]
[% IF data.PID.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('PID')">Set PID setpoint</button></td></tr>[% END %]
[% IF data.analog.size %]<tr><td><button type=button class="tool-btn" onclick="set_option('analog')">Set analog output</button></td></tr>[% END %]
[% IF data.PSU.size %]<tr><td><button type=button id=start_stop class="tool-btn" onclick="iv_control(this)">[% IF data.is_iv %]Stop iV curve[% ELSE %]Start i-V curve[% END %]</button></td></tr>[% END %]
[% END -%]
<tr><td><button type=button class="tool-btn" onclick="set_option('impedance')">Run impedance</button></td></tr>
[% IF config.userinterface.default_layout && !nav.legacy %]<tr><td><form>[% INCLUDE print_hidden.tt %]<input type=hidden name="legacy" value=1><button type=submit class=tool-btn>Legacy view</button></form></td></tr>[% END %]
</table>

</div>

<script>

function configure_imp(server)
{
    var url = 'https://'+server+'/cgi-bin/elchemea/main.cgi?name=[% user_info.name%]&pass=[% user_info.pass %]';
    popupWindow = window.open(url,'popUpWindow','height=500,width=1000,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}

function run_imp()
{
   document.getElementById('impedance').style.display = 'none';
}

[% IF user_info.auth -%]

var mgasses = new Object();
[% FOR i IN data.multiplex -%]
mgasses['[% i.name %]'] = ['[% data.multiplex_gasses.$i.join("','") %]'];
[% END -%]

var amodes = new Object();
[% FOR i IN data.analog -%]
amodes['[% i %]'] = '[% data.analog_modes.$i %]';
[% END -%]

function set_multiplex_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('multiplex_'+e.value);
   e.setAttribute('devname',e.value);
   var e2 = document.getElementById('multiplex_set');
   e2.classList.add('multiplex_'+e.value+'_value');
   e2.options.length = 0;
   var option = document.createElement("option");
   option.value = '';
   option.text = 'Select value';
   e2.appendChild(option);
   for (i = 0; i < mgasses[e.value].length; i++)
   {
      var option = document.createElement("option");
      option.value = mgasses[e.value][i];
      option.text = mgasses[e.value][i];
      e2.appendChild(option);
   }
   e2.value= '';
   var arr = new Object();
   var index = 'multiplex_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'multiplex';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}

function set_analog_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('analog_'+e.value);
   e.setAttribute('devname',e.value);
   var e2 = document.getElementById('analog_set');
   e2.classList.add('analog_'+e.value+'_value');
   e2.value= '';
   var e3 = document.getElementById('aunit');
   e3.innerHTML = amode[e.value];
   var arr = new Object();
   var index = 'analog_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'analog';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}


function set_gas_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('gas_'+e.value);
   e.setAttribute('devname',e.value);
   document.getElementById('gas_value').classList.add('gas_'+e.value+'_value');
   document.getElementById('gas_set').classList.add('gas_'+e.value+'_setpoint');
   document.getElementById('gas_value').innerHTML = '';
   document.getElementById('gas_set').value= '';
   var arr = new Object();
   var index = 'gas_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'gas';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}

function set_PID_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('PID_'+e.value);
   e.setAttribute('devname',e.value);
   document.getElementById('PID_set').classList.add('PID_'+e.value+'_setpoint');
   document.getElementById('PID_set').value= '';
   var arr = new Object();
   var index = 'PID_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'PID';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}

function set_PSU_line(e,mode)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   document.getElementById('select_PSU_current').classList.add('device');
   document.getElementById('select_PSU_current').classList.add('PSU_'+e.value);
   document.getElementById('select_PSU_current').setAttribute('devname',e.value);
   document.getElementById('PSU_current_value').classList.add('PSU_'+e.value+'_value');
   document.getElementById('PSU_current_set').classList.add('PSU_'+e.value+'_setpoint');
   document.getElementById('PSU_current_value').innerHTML = '';
   document.getElementById('PSU_current_set').value= '';
   document.getElementById('select_PSU_voltage').classList.add('device');
   document.getElementById('select_PSU_voltage').classList.add('PSU_'+e.value);
   document.getElementById('select_PSU_voltage').setAttribute('devname',e.value);
   document.getElementById('PSU_voltage_value').classList.add('PSU_'+e.value+'_value');
   document.getElementById('PSU_voltage_set').classList.add('PSU_'+e.value+'_setpoint');
   document.getElementById('PSU_voltage_value').innerHTML = '';
   document.getElementById('PSU_voltage_set').value= '';
   var arr = new Object();
   var index = 'PSU_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'PSU';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}

function set_analog_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('analog_'+e.value);
   e.setAttribute('devname',e.value);
   document.getElementById('analog_value').classList.add('analog_'+e.value+'_value');
   document.getElementById('analog_set').classList.add('analog_'+e.value+'_setpoint');
   document.getElementById('analog_value').innerHTML = '';
   document.getElementById('analog_set').value= '';
   var arr = new Object();
   var index = 'analog_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'analog';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}

function set_tempcontrol_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('tempcontrol_'+e.value);
   e.setAttribute('devname',e.value);
   document.getElementById('tempcontrol_value').classList.add('tempcontrol_'+e.value+'_value');
   document.getElementById('tempcontrol_set').classList.add('tempcontrol_'+e.value+'_setpoint');
   document.getElementById('tempcontrol_ramp').classList.add('tempcontrol_'+e.value+'_ramprate');
   document.getElementById('tempcontrol_value').innerHTML = '';
   document.getElementById('tempcontrol_set').value= '';
   document.getElementById('tempcontrol_ramp').value = '';
   var arr = new Object();
   var index = 'tempcontrol_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'tempcontrol';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}


function set_relay_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('relay_'+e.value);
   e.setAttribute('devname',e.value);
   document.getElementById('relay_value').classList.add('relay_'+e.value+'_value');
   document.getElementById('relay_value').value = '';
   var arr = new Object();
   var index = 'relay_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'relay';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}


function set_water_line(e)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   e.classList.add('device');
   e.classList.add('relay_'+e.value);
   e.setAttribute('devname',e.value);
   document.getElementById('water_value').classList.add('relay_'+e.value+'_value');
   document.getElementById('water_value').value = '';
   var arr = new Object();
   var index = 'relay_'+e.value;
   arr[index] = new Object();
   arr[index].devclass = 'relay';
   arr[index].devname = e.value;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_noblock);
}

function set_new_temp()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_tempcontrol').value == '')
   {
	alert('You have to select a controler');
	return false;
   }
   var temp = document.getElementById('tempcontrol_set').value;
   var ramp = document.getElementById('tempcontrol_ramp').value;
   if (temp == '' || ramp == '')
   {
	alert('You have to specify a setpoint and a ramp rate');
	return false;
   }
   if (ramp <= 0)
   {
	alert('Ramp rate has to be positive (and not zero)');
   }
   var arr = new Object();
   var index = 'tempcontrol_'+document.getElementById('select_tempcontrol').value;
   arr[index] = new Object();
   arr[index].devclass = 'tempcontrol';
   arr[index].devname = document.getElementById('select_tempcontrol').value;
   arr[index].command = 'set_temp';
   var args = new Array();
   args.push(temp);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   arr[index].command = 'set_ramp';
   var args = new Array();
   args.push(ramp);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);
   document.getElementById('temperature').style.display = 'none';
}

function set_new_relay()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_relay').value == '')
   {
	alert('You have to select a controler');
	return false;
   }
   var status = document.getElementById('relay_value').value;
   if (status == '')
   {
	alert('You have to specify a status');
	return false;
   }
   var arr = new Object();
   var index = 'relay_'+document.getElementById('select_relay').value;
   arr[index] = new Object();
   arr[index].devclass = 'relay';
   arr[index].devname = document.getElementById('select_relay').value;
   arr[index].command = 'set';
   var args = new Array();
   args.push(status);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   document.getElementById('relay').style.display = 'none';
}


function set_new_PID()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_PID').value == '')
   {
	alert('You have to select a PID');
	return false;
   }
   var val = document.getElementById('PID_set').value;
   if (val == '')
   {
	alert('You have to specify a setpoint');
	return false;
   }
   var arr = new Object();
   var index = 'PID_'+document.getElementById('select_PID').value;
   arr[index] = new Object();
   arr[index].devclass = 'PID';
   arr[index].devname = document.getElementById('select_PID').value;
   arr[index].command = 'set';
   var args = new Array();
   args.push(val);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   document.getElementById('PID').style.display = 'none';
}


function set_new_multiplex()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_multiplex').value == '')
   {
	alert('You have to select a PID');
	return false;
   }
   var val = document.getElementById('multiplex_set').value;
   if (val == '')
   {
	alert('You have to specify a gas');
	return false;
   }
   var arr = new Object();
   var index = 'multiplex_'+document.getElementById('select_multiplex').value;
   arr[index] = new Object();
   arr[index].devclass = 'multiplex';
   arr[index].devname = document.getElementById('select_multiplex').value;
   arr[index].command = 'set';
   var args = new Array();
   args.push(val);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
   document.getElementById('multiplex').style.display = 'none';
}

function set_new_analog()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_analog').value == '')
   {
	alert('You have to select a analog output');
	return false;
   }
   var val = document.getElementById('analog_value').value;
   if (val == '')
   {
	alert('You have to specify a voltage/current');
	return false;
   }
   var arr = new Object();
   var index = 'analog_'+document.getElementById('select_analog').value;
   arr[index] = new Object();
   arr[index].devclass = 'analog';
   arr[index].devname = document.getElementById('select_analog').value;
   arr[index].command = 'set';
   var args = new Array();
   args.push(val);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   document.getElementById('relay').style.display = 'none';
}

function set_new_PSU(mode,val,cmd)
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_PSU_'+mode).value == '')
   {
	alert('You have to select a PSU');
	return false;
   }
   if (val == '')
   {
	alert('You have to specify a '+mode);
	return false;
   }
   var arr = new Object();
   var index = 'PSU_'+document.getElementById('select_PSU_'+mode).value;
   arr[index] = new Object();
   arr[index].devclass = 'PSU';
   arr[index].devname = document.getElementById('select_PSU_'+mode).value;
   arr[index].command = cmd;
   var args = new Array();
   args.push(val);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   document.getElementById(mode).style.display = 'none';
}

function set_new_water()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_water').value == '')
   {
	alert('You have to select a controler');
	return false;
   }
   var status = document.getElementById('water_value').value;
   if (status == '')
   {
	alert('You have to specify a status');
	return false;
   }
   var arr = new Object();
   var index = 'relay_'+document.getElementById('select_water').value;
   arr[index] = new Object();
   arr[index].devclass = 'water';
   arr[index].devname = document.getElementById('select_water').value;
   arr[index].command = 'setstatus';
   var args = new Array();
   args.push(status);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   document.getElementById('water').style.display = 'none';
}


function set_new_gas()
{
   if (!is_logged_in)
   {
        alert('session has timed out');
	return false;
   }
   if (document.getElementById('select_gas').value == '')
   {
	alert('You have to select a gas line');
	return false;
   }
   var flow = document.getElementById('gas_set').value;
   if (flow == '')
   {
	alert('You have to specify a flow rate');
	return false;
   }
   var arr = new Object();
   var index = 'gas_'+document.getElementById('select_gas').value;
   arr[index] = new Object();
   arr[index].devclass = 'gas';
   arr[index].devname = document.getElementById('select_gas').value;
   arr[index].command = 'setflow';
   var args = new Array();
   args.push(flow);
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,do_nothing);   
   document.getElementById('gas').style.display = 'none';
}

[% END %]

function do_nothing(res)
{
   console.log(res);
[% IF data.PSU.size > 0 -%]
   run_ajax('rig_ajax_run.cgi','&action=is_iv',set_iv);
[% END -%]
   get_logs();
}

function get_proglog()
{
   /* Get last X lines of proglog */
}

function set_option(mode)
{
    var slides = document.getElementsByClassName("overlay");
    for(var i = 0; i < slides.length; i++)
    {
	slides[i].style.display = 'none';
    }
    document.getElementById(mode).style.display='inline-block';
}

[% IF data.is_iv %]var isactive = 1;[% ELSE %]var isactive = 0;[% END %]
var ispopup = false;

function iv_popup()
{
    if (isactive == 0 || ispopup == true)
    {
        return false;
    }
    ispopup = true;
    RFC_open('ivfig.cgi',510,380,100,400);
}

function load_isiv()
{
    if (!is_logged_in)
    {
	return false;
    }
    run_ajax('rig_ajax_run.cgi','&action=is_iv',set_iv);
    setTimeout(load_isiv,10000);    
}

function set_iv(res)
{
    var btn = document.getElementById('start_stop');
    if (btn == null)
       return false;
    if (res == 1)
    {
       btn.innerHTML = "Stop iV curve";
       iv_popup();
    }
    else
    {
       btn.innerHTML = "Start iV curve";
       ispopup = false;
    }
}

[% IF user_info.auth -%]

function iv_control(btn)
{
    if (!is_logged_in)
    {
        alert('session has timed out');
	return false;
    }
    var val = btn.innerHTML;
    var reg = /Stop/;
    if (reg.test(val))
    {
        var ok = confirm("Stop iV curve?");
        if (ok)
        {
            run_ajax('rig_ajax_run.cgi','&action=stop_iv',do_nothing);
	    isactive = 0;
	}
    }
    else
    {
        run_ajax('rig_ajax_run.cgi','&action=start_iv',do_nothing);
	isactive = 1;
	iv_popup();
    }
}

[% END %]

iv_popup();

</script>

<style>

.setpoint_input {
    width:100%;
}

.new_setpoint {
    padding:10px;      
    text-align: left;
    margin:auto;
}

.new_setpoint td {
    padding:3px;
}

.overlay {
    font-size:12px;
    vertical-align:top;
    text-align:left;
    display:none;
    z-index: 10;
    position: absolute;
    background: white;
    border-style:solid;
    padding:5px;
    border: 1px solid #8E2420;
    min-width:200px;
}

.tool-btn {
    width:100%;
    text-align:left;
    background-color: #8E2420;
    color:white;
    border-radius:5px;
    border-style:solid;
    border:none;
    padding:3px;
    font-weight:900;
}

.tool-list {
   margin:auto;
}

.tool-list td, .tool-list th {
   padding:5px;	   
}

</style>
