// JavaScript Document

function execJS(t)

{

    var p1 = 0, p2 = 0, p3 = 0, p4 = 0;

    p1 = t.indexOf("<" + "script", 0);

    if(p1 == -1) return t;

    

    p2 = t.indexOf(">", p1 + 7) + 1;

    p3 = t.indexOf("<" + "/script>", p2);

    p4 = p3 + 9;

    

    var c = t.substring(p2, p3);

    var s = document.createElement("script");

    s.type = "text/javascript";

    s.text = c;

    document.getElementsByTagName("head")[0].appendChild(s);

    

    t = t.substring(0, p1) + t.substr(p4);

    return execJS(t);



}


function createRequestObject() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}

	if(!req && window.createRequest)
		req = window.createRequest();

	if (!req) alert("Il browser non supporta AJAX");

	return req;
}


var http = createRequestObject();

var http2 = createRequestObject();

var isBusy ='';
function sndReq(action,idoutput) {
	if (isBusy == true)
	{
		http.onreadystatechange = null;
		http.abort();
	}
	http.open('get', '/include/getgrab.inc.php?'+action+'&idoutput='+idoutput);
	isBusy = true;
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function sndReqnew(action,idoutput) {
	if (isBusy == true)
	{
		http.onreadystatechange = null;
		http.abort();
	}
	http.open('get', '/include/getgrabnew.inc.php?'+action+'&idoutput='+idoutput);
	isBusy = true;
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function sndReqpost2(action,idoutput,operation,vars) {
	document.getElementById(idoutput).innerHTML='<img style="padding:2px; padding-top:4px;" src="/img/n_loader.gif" />';
	http2.open('post', '/include/ajax_php.inc.php?'+action+'&idoutput='+idoutput+'&operation='+operation, true);
	var variabili=vars;
	http2.onreadystatechange = handleResponse2;
	http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http2.send(variabili);
}

function handleResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		var update = new Array();

		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			document.getElementById(update[0]).src = update[1];
			if (update[1]=='/sito/n_loader.gif') {
				/*handleResponse.intervallo = window.setInterval(function () {
				//var intervallo = window.setInterval(function () {
				  sndReq('Url='+update[2],update[0]);
				},5000);*/
			} else {
				//clearInterval(handleResponse.intervallo);
			}
		}
	} else {
		isBusy = false;
	}
}

function handleResponse2() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();

		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			document.getElementById(update[0]).style.display="block";
			outputupdate=execJS(update[1]);
			document.getElementById(update[0]).innerHTML = update[1];
			if (document.getElementById(update[0]+'minus')) document.getElementById(update[0]+'minus').style.display='block';
		}
	} else {
		isBusy = false;
	}
}

function remdiv(divid) {
	document.getElementById(divid+'minus').style.display='none';
	document.getElementById(divid).innerHTML = '';
	//var DIVtoRemove = document.getElementById(divid);
	//DIVtoRemove.parentNode.removeChild(DIVtoRemove);
}

function CheckFields(CtrlName,idc,oper,table,divout){
      var reqcell="";                  //string for required field's corresponding notification cell
      var msg="";                        //debug message
	  var altro="";
      var reqlist=document.getElementById(CtrlName).value.split(",");
	  //document.write(reqlist);
      for (var x in reqlist){
		  //document.write(reqlist[x]);
           // reqcell="Required_"+reqlist[x];
            msg+="&"+reqlist[x];
            msg+="="+document.getElementById(reqlist[x]).value;
      }
     //reqcell="CreationDate";
	 if (idc!='') {
	 	altro="&id="+idc;
	 }
	 msg=msg.replace(/\+/g,'#43#');
	 msg=unescape(msg);
	 if (divout==null) divout='form';
	 sndReqpost2('table='+table+altro,divout,oper,msg)
}

