#!/usr/bin/perl -T
use strict;
use warnings;
use CGI qw(:standard :netscape);
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use SemaforeFile;
use File::Copy;
use SocketClient;
use IO::Socket::SSL;
use LWP::UserAgent;
use Unicode::String qw(latin1 utf8);
my $ssl = 0;
$ssl = 1 if (CGI::https());

my @list = ();
my $listserver = RFC::Main::get_cv('servers','listserver');

if ($listserver)
{
    chomp $listserver;
    my $str = &RFC::Main::report_client($listserver,'servers');
    chomp $str if ($str);
    my @l = split(/\t/,$str) if ($str && $str !~ /ERROR/);
    foreach (@l)
    {
        if (/([\w\.\-]+)/)
        {
            push @list,$1;
        }
    }
}
my @rigs = ();
foreach (@list)
{
    my $str = &RFC::Main::report_client($_,'rigs');
    next if (!$str || $str =~ /error/i);
    chomp $str;
    chomp $str;
    chomp $str;
    push @rigs,split(/\s+/,$str);
}
@rigs = sort {$a <=> $b} @rigs;
unshift @rigs,'';


&print_header("RFCcontrol");
print center(h1("Load i-V file from RFCcontrol cluster"));
print '<center><table border=0 cellspacing=20><tr>';
print start_form(-name=>'select');
print '<th> Rig: </th><td>'; 
print popup_menu(-name=>'rig',-values=>[@rigs],-default=>'',-force=>1,-onchange=>'select_rig(this.value)');
print '</td><th> Test: </th><td>';
print popup_menu(-name=>'test',-values=>[''],-default=>'',-force=>1,-onchange=>'select_test(document.select.rig.value,this.value)');
print '</td></tr><tr><td colspan=4>';
print popup_menu(-name=>'file',-values=>[''],-default=>'',-force=>1,-class=>'textfield',-onchange=>'send_data()');
print_hidden();
&action('load');
print end_form();
print '</th>';
end_top_bar();
print '</center>';



my $protocol = 'http://';
$protocol = 'https://' if ($ssl);

print <<END;

<script language="javascript">

var proto = '$protocol';
var name = '';
var pass = '';
var time = ''
var cred = '&name='+name+'&pass='+pass+'&jscheck=1&time='+time;
var fserver = '$listserver'+'/cgi-bin/celltest/ajax_interface.cgi';

function send_data()
{
    opener.document.load.loadrig.value = document.select.rig.value;
    opener.document.load.loadtest.value = document.select.test.value;
    opener.document.load.loadiv.value = document.select.file.value;
    opener.load_submit();
    self.close();
}


function select_rig(r)
{
    var query = '&action=run_list&furnace='+r+cred;
    run_ajax(proto+fserver,query,set_test);
}

function select_test(r,t)
{
    var query = '&action=iv_list&furnace='+r+"&runnr="+t+cred;
    run_ajax(proto+fserver,query,set_files);
}

function set_files(res)
{
    var out = res.split("\\n");
    if (out[0] != "OK")
    {
        alert(res);
        return false;
    }
    document.select.file.length = 0;
    var opt = document.createElement("option");
    opt.text = '';
    opt.value = '';
    document.select.file.options.add(opt);
    for (var x = 1; x < out.length; x++)
    {
	var opt = document.createElement("option");
	opt.text = out[x];
	opt.value = out[x];
	document.select.file.options.add(opt);
    }
}

function set_test(res)
{
    var out = res.split("\\n");
    if (out[0] != "OK")
    {
        alert(res);
        return false;
    }
    document.select.test.length = 0;
    var opt = document.createElement("option");
    opt.text = '';
    opt.value = 0;
    document.select.test.options.add(opt);
    for (var x = 1; x < out.length; x++)
    {
	var opt = document.createElement("option");
	opt.text = out[x];
	opt.value = out[x];
	document.select.test.options.add(opt);
    }
}

</script>

END

print end_html();
exit;

