/**************************************************************************************/
chiamate_url = new Array();
chiamate_params = new Array();
chiamate_div = new Array();
chiamate_div_pos = new Array();
chiamate_div_cond = new Array();
chiamate_div_html = new Array();
n_chiamate = -1;
var respFunc='';
var procedi = true;

/**********************************************************************************************/

function inserisciRichiesta(url,params,idDiv){
	n_chiamate++;
	chiamate_url[n_chiamate] = url;
	chiamate_params[n_chiamate] = params;
	chiamate_div[n_chiamate] = idDiv;
	chiamate_div_pos[n_chiamate] = '';
	chiamate_div_cond[n_chiamate] = '';
	chiamate_div_html[n_chiamate] = '';
	
	
	if (idDiv.indexOf('opener.') == 0){
		chiamate_div_pos[n_chiamate] = 'opener';
		chiamate_div[n_chiamate] = chiamate_div[n_chiamate].replace('opener.','')
	}
	
	if (idDiv.indexOf('parent.') == 0){
		chiamate_div_pos[n_chiamate] = 'parent';
		chiamate_div[n_chiamate] = chiamate_div[n_chiamate].replace('parent.','')
	}
	
	if (idDiv.indexOf('.append') >= 0){
		chiamate_div_cond[n_chiamate] = 'append';
		chiamate_div[n_chiamate] = chiamate_div[n_chiamate].replace('.append','')
	}
	
	if (procedi){
		makeRequest();
	} else {
		avanti = window.setInterval("vediProcedi();",100);
	}
}

function vediProcedi(){
if (procedi==true && n_chiamate>=0) {
	makeRequest();
	window.clearInterval(avanti);
	}
}


function makeRequest(){
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
  		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Impossibile creare una istanza XMLHTTP');
		return false;
	}

	window.parent.status="Elaborazione...";
	if (chiamate_div[0] != ''){
			
		if (chiamate_div[0].indexOf('()') >= 0){
			http_request.onreadystatechange = gestRespFunc;
		}else{
			
			var tipo = '';
			if (chiamate_div_pos[0]=='opener'){
				tipo = window.opener.document.getElementById(chiamate_div[0]).type;
			} else if (chiamate_div_pos[0]=='parent'){
				tipo = window.parent.document.getElementById(chiamate_div[0]).type;
			} else {
				tipo = document.getElementById(chiamate_div[0]).type;
			}
			
			if (tipo=='submit' || tipo=='button' || tipo=='checkbox' || tipo=='hidden' || tipo=='text'){
				chiamate_div_html[0] = document.getElementById(chiamate_div[0]).value;
				http_request.onreadystatechange = gestRespValue;
			} else {
				chiamate_div_html[0] = document.getElementById(chiamate_div[0]).innerHTML;					
				if (chiamate_div_pos[0]=='opener'){
					window.opener.document.getElementById(chiamate_div[0]).innerHTML='<img src="/dm_admin/img/loading.gif" border="0" width="19px" height="19px">';
				} else if (chiamate_div_pos[0]=='parent'){
					window.parent.document.getElementById(chiamate_div[0]).innerHTML='<img src="/dm_admin/img/loading.gif" border="0" width="19px" height="19px">';
				} else {
					document.getElementById(chiamate_div[0]).innerHTML='<img src="/dm_admin/img/loading.gif" border="0" width="19px" height="19px">';
				}
				//document.getElementById(chiamate_div[0]).innerHTML='<img src="/admin/img/loading.gif" border="0" width="19px" height="19px">';
				http_request.onreadystatechange = gestRespDiv;
			}
		}
	}else{
		http_request.onreadystatechange = gestResp;				
	}
			
	http_request.open('POST', chiamate_url[0], true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", chiamate_params[0].length);
	http_request.send(chiamate_params[0]);
}

function gestRespFunc(){
	procedi=false
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			
			respFunc = http_request.responseText;
			eval(chiamate_div[0].replace('()','(respFunc)'));

			window.status='Eseguito...';
			n_chiamate--;
			chiamate_url.shift();
			chiamate_params.shift();
			chiamate_div.shift();
			chiamate_div_pos.shift();
			chiamate_div_cond.shift();
			chiamate_div_html.shift();
			respFunc='';
			
			if (n_chiamate>0) {
				makeRequest(/*chiamate_url[0],chiamate_params[0],chiamate_div[0]*/);
			} else{
				procedi=true;
			}
		} else {
			window.parent.status="Problemi con la richiesta al server"
		}
	}
}

function gestRespDiv(){
	procedi=false
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
		
			if (chiamate_div_pos[0]=='opener'){
				if (chiamate_div_cond[0]=='append'){
					window.opener.document.getElementById(chiamate_div[0]).innerHTML = chiamate_div_html[0] + http_request.responseText;
				} else {
					window.opener.document.getElementById(chiamate_div[0]).innerHTML = http_request.responseText;
				}
			} else if (chiamate_div_pos[0]=='parent'){
				if (chiamate_div_cond[0]=='append'){
					window.parent.document.getElementById(chiamate_div[0]).innerHTML = chiamate_div_html[0] + http_request.responseText;
				} else {
					window.parent.document.getElementById(chiamate_div[0]).innerHTML = http_request.responseText;
				}
			} else {
				if (chiamate_div_cond[0]=='append'){
					document.getElementById(chiamate_div[0]).innerHTML = chiamate_div_html[0] + http_request.responseText;
				} else {
					document.getElementById(chiamate_div[0]).innerHTML = http_request.responseText;
				}
			}
			
			//document.getElementById(chiamate_div[0]).innerHTML= http_request.responseText
			window.status='Eseguito...';
			n_chiamate--;
			chiamate_url.shift();
			chiamate_params.shift();
			chiamate_div.shift();
			chiamate_div_pos.shift();
			chiamate_div_cond.shift();
			chiamate_div_html.shift();
			if (n_chiamate>0) {
				makeRequest(/*chiamate_url[0],chiamate_params[0],chiamate_div[0]*/);
			} else{
				procedi=true;
			}
		} else {
			window.parent.status="Problemi con la richiesta al server"
		}
	}
}

function gestRespValue(){
	procedi=false
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			
			
			if (chiamate_div_pos[0]=='opener'){
				if (chiamate_div_cond[0]=='append'){
					window.opener.document.getElementById(chiamate_div[0]).value = chiamate_div_html[0] + http_request.responseText;
				} else {
					window.opener.document.getElementById(chiamate_div[0]).value= http_request.responseText;
				}
			} else if (chiamate_div_pos[0]=='parent'){
				if (chiamate_div_cond[0]=='append'){
					window.parent.document.getElementById(chiamate_div[0]).value = chiamate_div_html[0] + http_request.responseText;
				} else {
					window.parent.document.getElementById(chiamate_div[0]).value= http_request.responseText;
				}
			} else {
				if (chiamate_div_cond[0]=='append'){
					document.getElementById(chiamate_div[0]).value = chiamate_div_html[0] + http_request.responseText;
				} else {
					document.getElementById(chiamate_div[0]).value= http_request.responseText;
				}
			}

			//document.getElementById(chiamate_div[0]).value= http_request.responseText
			window.status='Eseguito...';
			n_chiamate--;
			chiamate_url.shift();
			chiamate_params.shift();
			chiamate_div.shift();
			chiamate_div_pos.shift();
			chiamate_div_cond.shift();
			chiamate_div_html.shift();
			if (n_chiamate>0) {
				makeRequest(/*chiamate_url[0],chiamate_params[0],chiamate_div[0]*/);
			} else{
				procedi=true;
			}
		} else {
			window.parent.status="Problemi con la richiesta al server"
		}
	}
}

function gestResp() {
	procedi=false
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			window.status=http_request.responseText;
			n_chiamate--;
			chiamate_url.shift();
			chiamate_params.shift();
			chiamate_div.shift();
			chiamate_div_pos.shift();
			chiamate_div_cond.shift();
			chiamate_div_html.shift();
			if (n_chiamate>0) {
				makeRequest();
			} else{
				procedi=true;
			}
		} else {
			window.parent.status="Problemi con la richiesta al server"
		}
	}
}

function URLEncode(CODE){

	var plaintext = CODE;
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";
	
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" +
				        "(URL encoding only supports 8-bit characters.)\n" +
						"A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	// VALORE CODIFICATO
	return encoded;
};

