// JavaScript Document

var http2 = createRequestObject();

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 handleResponse3() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();
		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			document.getElementById(update[0]).style.border="1px solid #7F9DB9;";
			document.getElementById(update[0]).style.padding="2px 2px 3px 3px";
			if (update[1]==1) {
				document.getElementById(update[0]).style.background = '#bbffbb';
			} else {
				document.getElementById(update[0]).style.background = '#ffaaaa';
			}
			if (document.getElementById(update[0]+'minus')) document.getElementById(update[0]+'minus').style.display='block';
		} 
	} else {
		isBusy = false;
	}
}

function handleResponse4() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();
		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
		}
	} else {
		isBusy = false;
	}
}

function handleResponse5() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();

		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			outputupdate=execJS(update[1]);
		}
	} else {
		isBusy = false;
	}
}

var isBusy ='';
function sndReq2(action,idoutput,operation) {
	if (isBusy == true)
	{
		http2.onreadystatechange = null;
		http2.abort();
	}
	if (operation!="sendrich") document.getElementById(idoutput).innerHTML='<img style="padding:2px; padding-top:4px;" src="http://www.bloo.it/img/n_loader.gif" />';
	http2.open('get', '/include/ajax_php.inc.php?'+action+'&idoutput='+idoutput+'&operation='+operation);
	isBusy = true;
	if (operation=="checkform") {
		http2.onreadystatechange = handleResponse3;
	} else if (operation=="sendrich") {
		http2.onreadystatechange = handleResponse4;
	} else {
		http2.onreadystatechange = handleResponse2;
	}
	http2.send(null);
}

function sndReq2b(action,idoutput,operation) {
	if (isBusy == true)
	{
		http2.onreadystatechange = null;
		http2.abort();
	}
	http2.open('get', '/include/ajax_php.inc.php?'+action+'&idoutput='+idoutput+'&operation='+operation);
	isBusy = true;
	if (operation=="checkform") {
		http2.onreadystatechange = handleResponse3;
	} else {
		http2.onreadystatechange = handleResponse2;
	}
	http2.send(null);
}

function sndReq3(action,idoutput,operation) {
	if (isBusy == true)
	{
		http2.onreadystatechange = null;
		http2.abort();
	}
	http2.open('get', '/include/ajax_php.inc.php?'+action+'&idoutput='+idoutput+'&operation='+operation);
	isBusy = true;
	http2.onreadystatechange = handleResponse5;
	http2.send(null);
}

function sndReqpost2(action,idoutput,operation,vars) {
	document.getElementById(idoutput).innerHTML='<img style="padding:2px; padding-top:4px;" src="http://www.bloo.it/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 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)
}
