<!-- Common template containing forms and JS for updates -->


<script>

function find_tty()
{
    var query = '&action=list_ttys';
    run_ajax('ajax.cgi',query,alert);
}

function change_include(element)
{
    var val = 0;
    if (element.checked == true)
    {
        val = 1;
    }
    if (val == 0)
    {
        if (!confirm('Exclude $device from data logging'))
        {
            element.checked = true;
            return false;
        }
    }
    var typename = '[% data.class %]';
    var dev = '[% data.name %]';
    element.disabled = true;
    run_ajax('change_config.cgi','&action=include_device&type='+typename+'&device='+dev+'&value='+val+'&key=include',set_checkbox);
}

function run_monitor()
{
    var typename = '[% data.class %]';
    var dev = '[% data.name %]';
    var rig = document.login_info.rig.value;
    var user = document.login_info.name.value;
    var pass = document.login_info.pass.value;
    var time = document.login_info.time.value;
    var js = document.login_info.jscheck.value;
    var hidden = "?rig="+rig+"&type="+typename+"&device="+dev+"&name="+user+"&pass="+pass+"&time="+time+"&jscheck="+js;
    var myWin=window.open('monitor.cgi'+hidden,'windowRef','width=400,height=350,left=100,top=100');
    myWin.location.href = 'monitor.cgi'+hidden;
    if (!myWin.opener)
        myWin.opener = self;
    myWin.resizeTo(450, 400);
    myWin.focus();
}



function plot_data(data)
{
    data = data.replace(/\n/g,",");
    var url = 'plot.cgi?data=' + data +'&size=0.6,0.6';
    var myWin=window.open(url,'windowRef','width=600,height=500,left=100,top=100');
    myWin.location.href = url;
    if (!myWin.opener)
        myWin.opener = self;
    myWin.resizeTo(600, 500);
    myWin.focus();
}


function run_test()
{
    var typename = '[% data.class %]';
    var dev = '[% data.name %]';
    run_ajax('change_config.cgi','&action=test_device&type='+typename+'&device='+dev,alert);
}


function set_checkbox(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;
        var val = arr[2];
        obj.checked = false;
        if (val == 1)
        {
            obj.checked = true;
        }
    }
    else
    {
        alert(arr[0]);
        var obj = document.getElementById(arr[1]);
        obj.disabled = false;
        obj.checked = false;
        var val = arr[2];
        if (val == 1)
        {
            obj.checked = true;
        }
    }
}


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]);
        var obj = document.getElementById(arr[1]);
        obj.disabled = false;
        obj.value = arr[2];
    }
}



function set_type(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]);
        var obj = document.getElementById(arr[1]);
        obj.disabled = false;
        obj.value = arr[2];
    }
    document.device.submit();
}

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

function change_type(element)
{
    var n = element.name;
    var val = element.value;
    var sec = '[% data.section %]';
    element.disabled = true;
    run_ajax('change_config.cgi','&action=change_config_value&section='+sec+'&key='+n+'&value='+val,set_type);
}

function set_spline(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].replace(/,/g,"\n");
    }
    else
    {
        alert(arr[0]);
        var obj = document.getElementById(arr[1]);
        obj.disabled = false;
        obj.value = arr[2].replace(/,/g,"\n");
    }
}


function change_spline(element)
{
    var n = element.name;
    var val = element.value;
    val = val.replace(/\n/g,",");
    var sec = '[% data.section %]';
    element.disabled = true;
    run_ajax('change_config.cgi','&action=change_config_value&section='+sec+'&key='+n+'&value='+val,set_spline);
}



function process_result(res)
{
    var arr = res.split("\n");
    if (arr[0] == "OK")
    {
        arr.shift();
        var reload = false;
        if (arr[arr.length - 1] == 'RELOAD')
        {
            reload = true;
            arr.pop();
        }
        alert(arr.join("\n"));
        if (reload)
        {       
            document.device_select.submit();
        }
    }
    else
    {
        alert(res);
    }
}


function run_args(type,dev,fname,a)
{
    var args = '[% user_info.name %]';
    if (a == 1)
    {
        arg1 = prompt("Input argument to "+fname,'');
        if (arg1 == null)
        {
            return false;
        }
        args = arg1+','+args;
        run_ajax('rig_ajax_run.cgi','&action=run_device_function&type='+type+'&dname='+dev+'&function='+fname+'&arguments='+args,process_result);
    }
}

function run_no_args(type,dev,fname)
{
    var args = '[% user_info.name %]';
    if (confirm('Run function '+fname))
    {
        run_ajax('rig_ajax_run.cgi','&action=run_device_function&type='+type+'&dname='+dev+'&function='+fname+'&arguments='+args,process_result);
    }
}


</script>


<style>

.device_setup {
   text-align:left;
   border-collapse: collapse;
   margin:auto;
}

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

.textfield {
   width:100%;
}


</style>

<!-- end common template -->