/* $Id: RFCcontrol.js,v 1.26 2023/04/12 10:18:27 sqko Exp $ */

function openInNewTab(url) {
    var win = window.open(url, '_blank');
    win.focus();
}

function doAlertLogout(t) {
    var tt = setTimeout('dologout()',t);
}

function dologout()
{
        IsLoggedIn = 0;
        document.logout.Logout.value = 'To front page';
	parent.location = "prelogin.cgi";
        alert('You have been automatically logged out!');
}


function is_int(element)
{
        var val = element.value;
        reg = /^\d+$/;
        if (!reg.test(val))
        {
                alert("'"+val+"' is not an integer!")
                element.focus();
                element.select();
                return false;
        }
        return true;
}

function is_float(element)
{
        var val = element.value;
        reg = /^[\d\.\-\+eE]+$/;
        if (!reg.test(val))
        {
                alert("'"+val+"' is not an number!")
                element.focus();
                element.select();
                return false;
        }
        return true;
}

/* AJAX functions */


function ajax_ok(script,tagname,element){
    var user = document.login_info.name.value;
    var pass = document.login_info.pass.value;
    var time = document.login_info.time.value;
    var rig = document.login_info.rig.value;
    var js = document.login_info.jscheck.value;
    var rnd = "&rnd="+ Math.random();
    if (element.value == null || element.value.length == 0)
    {
        // No need to run query if input is empty
        element.value = '';
        return false;
    }
    var ajaxRequest;   // The variable that makes Ajax possible!
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
             ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
             try{
                  ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (e){
                  // Something went wrong
                  alert("Your browser broke!");
                  return false;
             }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            // This is what actually is done once ajax request has finished
            if (ajaxRequest.responseText != "OK")
            {
                alert(ajaxRequest.responseText);
                element.value = '';
                element.focus();
            }
        }
    }
    // Add a random parameter to query to prevent IE caching of ajax requests !!!!!!!! *ARGH*
    var rnd =  "&random=" + new Date().getTime();
    var hidden = "?ajax=1&name="+user+"&pass="+pass+"&time="+time+rnd+"&rig="+rig+"&jscheck="+js+rnd;
    var query = "@"+tagname+"="+element.value;
    ajaxRequest.open("GET", script+hidden+query, true);
    ajaxRequest.send(null);
}

function raw_ajax(script,query,func){
    var ajaxRequest;  // The variable that makes Ajax possible!
        try{
            // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
                try{
                    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e){
                        // Something went wrong
                            alert("Your browser broke!");
                        return false;
                    }
                }
        }
    // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                // This is what actually is done once ajax request has finished
                    func(ajaxRequest.responseText);
            }
        }
    // Add a random parameter to query to prevent IE caching of ajax requests !!!!!!!! *ARGH*
    var rnd =  "&random=" + new Date().getTime();
    ajaxRequest.open("GET", script+query+rnd, true);
    ajaxRequest.send(null);
}


function remote_log_file(script,id,str,func,file)
{
        var msg = str;
        var rig = document.login_info.rig.value;
        var test = document.login_info.testnr.value;
        if (test >= 0)
        {
                msg = rig+'test'+test+': '+str;
                type = 'Testlog';
        }
        var user = document.login_info.name.value;
        var pass = document.login_info.pass.value;
	var formData = new FormData();
	formData.append('action','new_maint_log');
	formData.append('type','Testlog');
	formData.append('equipid',id);
	formData.append('name',document.login_info.name.value);
	formData.append('pass',document.login_info.pass.value);
	formData.append('description',msg);
	formData.append('filename',file,file.name);
	// Add a random parameter to query to prevent IE caching of ajax requests !!!!!!!! *ARGH*
	var rnd = new Date().getTime();
	formData.append('random',rnd);
	var xhr = new XMLHttpRequest();
	xhr.open('POST', script, true);
	xhr.onload = function () 
	{
  		if (xhr.status === 200) 
		{
			if (func == null || func == '')
			{
                    		ajax_alert(xhr.responseText);
			}
			else
			{
                    		func(xhr.responseText);
			}
		}
		else 
		{
	    		alert('An error occurred!');
  		}
	};
	xhr.send(formData);
}


function remote_log(script,id,str,func)
{
	var msg = str;
	var type = 'Riglog';
	var rig = document.login_info.rig.value;
	var test = document.login_info.testnr.value;
	if (test >= 0)
	{
		msg = rig+'test'+test+': '+str;
		type = 'Testlog';
	}
	var user = document.login_info.name.value;
	var pass = document.login_info.pass.value;
	var query = '&action=new_maint_log&type='+type+'&equipid='+id+'&description='+msg;
	var ajaxRequest;  // The variable that makes Ajax possible!
        try{
            // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
                try{
                    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e){
                        // Something went wrong
                            alert("Your browser broke!");
                        return false;
                    }
                }
        }
    // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                // This is what actually is done once ajax request has finished
		if (func == null || func == '')
		{
                    ajax_alert(ajaxRequest.responseText);
		}
		else
		{
                    func(ajaxRequest.responseText);
		}
            }
        }
    // Add a random parameter to query to prevent IE caching of ajax requests !!!!!!!! *ARGH*
    var rnd =  "&random=" + new Date().getTime();
	var hidden = "?name="+user+"&pass="+pass +rnd;
	ajaxRequest.open("GET", script+hidden+query, true);
	ajaxRequest.send(null);
}


function run_ajax(script,query,func){
    var user = document.login_info.name.value;
    var pass = document.login_info.pass.value;
    var time = document.login_info.time.value;
    var rig = document.login_info.rig.value;
    var js = document.login_info.jscheck.value;
    var rnd = "&rnd="+ Math.random();
    var ajaxRequest;  // The variable that makes Ajax possible!
        try{
            // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
                try{
                    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e){
                        // Something went wrong
                            alert("Your browser broke!");
                        return false;
                    }
                }
        }
    // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                // This is what actually is done once ajax request has finished
                    func(ajaxRequest.responseText);
            }
        }
    // Add a random parameter to query to prevent IE caching of ajax requests !!!!!!!! *ARGH*
    var rnd =  "&random=" + new Date().getTime();
    var hidden = "?ajax=1&name="+user+"&pass="+pass+"&time="+time+rnd+"&rig="+rig+"&jscheck="+js+rnd
    ajaxRequest.open("GET", script+hidden+query, true);
    ajaxRequest.send(null);
}



function ajax_alert(s)
{
    if (s == "OK")
    {
        return true;
    }
    alert(s);
    return false;
}


function RFC_open(script,w,h,l,t)
{
    var user = document.login_info.name.value;
    var pass = document.login_info.pass.value;
    var time = document.login_info.time.value;
    var rig = document.login_info.rig.value;
    var js = document.login_info.jscheck.value;
    var hidden = "?name="+user+"&pass="+pass+"&time="+time+"&rig="+rig+"&jscheck="+js
    var myWindow=window.open(script+hidden,'windowRef','width='+w+',height='+h+',left='+l+',top='+t);
    myWindow.location.href = script+hidden;
    if (!myWindow.opener)
        myWindow.opener = self;
    myWindow.resizeTo(w,h);
    myWindow.focus();
}



/*   Fix for broken onblur() in IE *Argh* */

var active_element;
var bIsMSIE = false;

function save_active()
{
	active_element = document.activeElement;
}



/*
In Internet Explorer 'onblur' event is implemented incorrectly (as opposed to Firefox/Mozilla browsers).
It is wrongly fired when focus is switched between HTML elements *inside* a window. As a result, clicking
on any element *inside* a popup window or trying to select something there will also close a popup.
Below is a workaround.
*/

function initiateSelfClosing()
{
	if (navigator.appName == "Microsoft Internet Explorer") {
		active_element = document.activeElement;
		document.onfocusout = closeWnd;
		bIsMSIE = true;
	}
	else 
	{ 
		window.onblur = closeWnd; 
	}
}


function closeWnd()
{
	if (window.opener != null) {
		if (bIsMSIE && (active_element != document.activeElement)) {
			active_element = document.activeElement;
		}
		else 
		{
			window.close();
		}
	}
}


function CPLE_open_org(script,w,h,l,t)
{
    var data = document.sofc.data.value;
    if (data == undefined)
        data = '';
    var fdata = document.sofc.filedata.value;
    if (fdata == undefined)
        fdata = '';
    var model = document.sofc.model.value;
    if (model == undefined)
        model = '';
    var nr = document.sofc.offset.value;
    var length = document.sofc.length.value;
    var text = document.sofc.text.value;
    if (text == undefined)
        text = '';
    var name = document.sofc.filename.value;
    if (name == undefined)
        name = '';
    var type = document.sofc.type.value;
    if (type == undefined)
        type = '';
    var minf = document.sofc.minf.value;
    if (minf == undefined)
        minf = '';
    var maxf = document.sofc.maxf.value;
    if (maxf == undefined)
        maxf = '';
    var fit = document.sofc.fit_res.value;
    if (fit == undefined)
        fit = '';
    var R_L = document.sofc.add_R_L.value;
    if (R_L == undefined)
        R_L = '';
    var hidden = "?filename="+name+"&data="+data+"&model="+model+"&offset="+nr+"&length="+length+"&text="+text+"&type="+type+"&maxf="+maxf+"&minf="+minf+"&fit_res="+fit+"&add_R_L="+R_L;
    /* Fix newlines */
    hidden = hidden.replace(/\\r/g,"\n");
    hidden = hidden.replace(/\n/g,"%0A");
    hidden = hidden.replace(/\+/g,"%2B");
/*    hidden = escape(hidden);*/
    var d = (new Date().getTime());
    var myWindow=window.open(script+hidden,'windowRef'+d,'width='+w+',height='+h+',left='+l+',top='+t);
    myWindow.location.href = script+hidden;
    if (!myWindow.opener)
        myWindow.opener = self;
    myWindow.resizeTo(w,h);
    myWindow.focus();
}

function CPLE_open(script,w,h,l,t)
{
/*    var data = document.sofc.data.value;
    if (data == undefined)
        data = '';
    var fdata = document.sofc.filedata.value;
    if (fdata == undefined)
        fdata = '';
    var nr = document.sofc.offset.value;
    var length = document.sofc.length.value;
    var text = document.sofc.text.value;
    if (text == undefined)
        text = '';
    var name = document.sofc.filename.value;
    if (name == undefined)
        name = '';
    var type = document.sofc.type.value;
    if (type == undefined)
        type = '';
    var hidden = "?filename="+name+"&data="+data+"&offset="+nr+"&length="+length+"&text="+text+"&type="+type;

    hidden = hidden.replace(/\\r/g,"\n");
    hidden = hidden.replace(/\n/g,"%0A");
    hidden = hidden.replace(/\+/g,"%2B");
*/
    var d = (new Date().getTime());
    var myWindow=window.open(script,'windowRef'+d,'width='+w+',height='+h+',left='+l+',top='+t);
    myWindow.location.href = script;
    if (!myWindow.opener)
        myWindow.opener = self;
    myWindow.resizeTo(w,h);
    myWindow.focus();
}


function get_server_name(server,fn,cred)
{
    if (cred == null)
    {
	cred = '';
    }
    raw_ajax(server+'/cgi-bin/celltest/ajax_interface.cgi','?action=server_name'+cred,fn);
}
