<!-- Template for chermocouple callibration -->

<div style="padding:50px;">
<h1>Thermocouple calibration files</h1>
<br>

<table class=thermo><tbody>
<tr><th> Current files </th><th> New file </th></tr>
<form method="post" action="/cgi-bin/celltest/iv_setup.cgi" enctype="multipart/form-data" onsubmit="return check_file()" name="sofc">
[% INCLUDE print_hidden.tt %]
<input type="hidden" name="action" value="">
<tr><td><select name="filelist" onchange="change_val(this)" class="textfield">
<option selected="selected" value=""></option>
[% FOR i IN data.files -%]
<option value="[% i %]" [% IF data.file == i %]selected=selected[% END %]>[% i %]</option>
[% END -%]
</select>
</td><td><input type="text" name="filename" placeholder="Filename for new file" onchange="set_filename_ok()" class="textfield"></td></tr>
<tr><td><textarea name="current_file" rows="20" class="textfield"></textarea></td><td><textarea name="new_file" rows="20" class="textfield"></textarea></td></tr>
<tr><td><input type="button" name="dummy" value="Delete file" onclick="confirm_delete()" id="delbtn" class="hidden"></td><td><input type="button" name="dummy2" value="Save new file" onclick="confirm_save()" id="savebtn" class="hidden"></td></tr>
</tbody>
</table>


</div>

<style>

.hidden {
   display:none;
}

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

.textfield {
   width:400px;
}


</style>


<script>

var filenameok = false;

function check_file()
{
    if (!filenameok)
    {
        alert('Filename not entered');
        return false;
    }
    if (document.sofc.new_file.value.length < 10)
    {
        alert('You must input a calibration file');
        return false;
    }
    return true;
}

function confirm_save()
{
    if (filenameok)
    {
        if (document.sofc.new_file.value.length < 10)
        {
            alert('You must input a calibration file');
            return false;
        }
        document.sofc.action.value = 'save';
        document.sofc.submit();
    }
}

function confirm_delete()
{
    if (confirm('Delete this file'))
    {
        document.sofc.action.value = 'delete';
        document.sofc.submit();
    }
}

function set_filename_ok()
{
    var reg = /^[\w\-\.]+\$/;
    var btn = document.getElementById('savebtn');
    if (!reg.test(document.sofc.filename.value))
    {
        btn.setAttribute("class","hidden");
        btn.setAttribute("className","hidden");
        filenameok = false;
        alert("Illegal filename attempted, only alphanumerics , '-' and '.' are allowed");
    }
    else
    {
        btn.setAttribute("class","rfcbutton");
        btn.setAttribute("className","rfcbutton");
        filenameok = true;
    }
}


function change_val(i)
{
    run_ajax('rig_ajax_run.cgi','&action=get_callibration_file&filename='+i.value,set_file);
}



function set_file(r)
{
    document.sofc.current_file.value = r;
[% IF user_info.admin -%]
    var btn = document.getElementById('delbtn');
    if (r == "")
    {
        btn.setAttribute("class","hidden");
        btn.setAttribute("className","hidden");
    }
    else
    {
        btn.setAttribute("class","rfcbutton");
        btn.setAttribute("className","rfcbutton");
    }
[% END -%]
    if (r != "")
    {
        i = document.sofc.filelist.value;
        run_ajax('rig_ajax_run.cgi','&action=plot_callibration_file&filename='+i,show_plot);
    }
}



function show_plot(r)
{
    RFC_open('../../'+r,500,400,300,200);
}



</script>