// variabile global per l'istanza dell'oggetto http request
var httpReq = null;

// funzione che processa la risposta http e richiama una funzione JS pre-definita
// il nome della funzione da richiamare deve essere passato dal template CFM come prima occorrenza nella lista dei valori restituiti
// la lista deve essere separata da barra verticale '|'
// es: NomeFunzione|valore1|valore2|ecc...
function ProcessHttpRequest() {
	if (httpReq.readyState==4){
		if (httpReq.status==200){
			returnFunction = httpReq.responseText.split('|')[0]+'()';
			eval(returnFunction);
		}else{
			alert('Si e\' verificato un errore durante l\'invio o il caricamento dei dati via http. Riprovare.');
		}
	}
}

// funzione che istanzia l'oggetto http request
function InstanceHttpReq(){
	if (window.XMLHttpRequest){
		try{return new XMLHttpRequest();}
		catch(e){
			return null;
		}
	} else {
		if (window.ActiveXObject){
			try{return new ActiveXObject('Msxml2.XMLHTTP');}
			catch(e){
				try{return new ActiveXObject('Microsoft.XMLHTTP');}
				catch(e){return null;}
			}
		}
	}
}

// funzione che dati i parametri url, metodo e dati, invia la richiesta http
function SendHttpRequest(url,metodo,dati) {
	if (!metodo){var metodo = 'GET'}
	if ((metodo!='GET')&&(metodo!='POST')){
		alert('Metodo HTTP non valido.');
	} else {
		if (!dati){var dati = '';}
		httpReq = InstanceHttpReq();
		if (httpReq){
			httpReq.onreadystatechange = ProcessHttpRequest;
			if (metodo=='GET'){
				if (url.indexOf('?')>=0) {
					url = url + '&' + dati;
				} else {
					url = url + '?' + dati;
				}
				httpReq.open(metodo, url, true);
				httpReq.send('');
			} else if (metodo=='POST'){
				httpReq.open(metodo, url, true);
				httpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
				httpReq.send(dati);
			}
		} else {
			alert('Http request non disponibile su questo browser.');
		}
	}
}

function getInformazioni (azione, div, param1, param2, param3) {
	var dati = 'azione='+azione;
	dati+='&divId='+div;
	dati+='&param1='+param1;
	if(param2)dati+='&param2='+param2;
	if(param3)dati+='&param3='+param3;
	indirizzo = '/Riservata/ajax.cfm';
	var divModificandus=document.getElementById(div);
	divModificandus.innerHTML='Attendere prego...';
	//alert(indirizzo + '\n' + dati);
	SendHttpRequest(indirizzo,'GET',dati);
}
function AggiornaInformazioni() {
	var divModificandus=document.getElementById(httpReq.responseText.split('|')[1]);
	divModificandus.innerHTML=httpReq.responseText.split('|')[2];
	callbackFunc=httpReq.responseText.split('|')[3];
	eval(callbackFunc);
}
