////////////////////////////////////////////////////////
//                                                    //
// AJAXfn.js                                          //
//                                                    //
// Descrizione: funzioni JS relative a AJAX           //
//                                                    //
////////////////////////////////////////////////////////

// L'array che contiene i nostri oggetti XMLHTTP
var XMLHTTPObjs = new Array();

// Classe per gestire richieste AJAX concorrenti
// Presa da http://drakware.com/?e=3
function CXMLReq(freed)
	{
	this.freed = freed;
	this.xmlhttp = null;

        // I vari browser ottengono l'oggetto XMLHTTP in modo
        // diverso (che gioia!) quindi proviamo nei vari modi
        // possibili fino a che ne troviamo uno che funziona.
        // Visto che usiamo new, in caso di fallimento Javascript
	// lancia un'eccezione che andiamo a catturare con
	// try/catch.
	try
		{
		// Firefox, Opera 8.0+, Safari
		this.xmlhttp = new XMLHttpRequest();
		}
	catch (e)
		{
		// Internet Explorer 6.0+
		try
			{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e)
			{
			// Internet Explorer 5.5+
			try
				{
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e) // Il browser non supporta XMLHTTP... niente AJAX :(
				{
				return null;
				}
			}
		}
	}
	
function xmlhttpChange(pos, onReturn, onReturnParams)
	{
	if (XMLHTTPObjs[pos].freed == 0 && XMLHTTPObjs[pos].xmlhttp.readyState == 4)
		{
		if (XMLHTTPObjs[pos].xmlhttp.status == 200  // OK
			|| XMLHTTPObjs[pos].xmlhttp.status == 304) // Not modified
			{
			if (onReturn != undefined)
				{
				if (onReturnParams != undefined)
					onReturn(XMLHTTPObjs[pos].xmlhttp.responseText, onReturnParams);
				else
					onReturn(XMLHTTPObjs[pos].xmlhttp.responseText);
				}
			}
		else
			{
			AJAXMessage("Errore durante la chiamata AJAX (status = " +
				XMLHTTPObjs[pos].xmlhttp.status + ")");
			}

		XMLHTTPObjs[pos].freed = 1;
		}
	}

// Esegue una chiamata AJAX
// 1) Trova il primo oggetto XMLHTTP non in uso
// 2) Chiama l'URL che gli passiamo
// 3) Quando la pagina restituisce un risultato chiama onReturn
//    passando il responseText dell'oggetto XMLHTTP
// Nota: il parametro onReturnParams è opzionale. Serve per passare dei parametri
// a onReturn (oltre al responseText). Per passare più di un parametro usare un array
function AJAXCall(url, onReturn, onReturnParams)
	{
	var pos = -1;

	// Trova il primo oggetto libero
	for (var i=0; i<XMLHTTPObjs.length; i++)
		{
		if (XMLHTTPObjs[i].freed == 1)
			{
			pos = i;
			break;
			}
		}
	// Oppure, se non lo trova, ne aggiunge un altro
	if (pos == -1)
		{
		pos = XMLHTTPObjs.length;
		XMLHTTPObjs[pos] = new CXMLReq(1);
		}

	if (XMLHTTPObjs[pos].xmlhttp == null)
		return -1;

	XMLHTTPObjs[pos].freed = 0;
	XMLHTTPObjs[pos].xmlhttp.onreadystatechange=function()
		{
		xmlhttpChange(pos, onReturn, onReturnParams);
		}
	
	XMLHTTPObjs[pos].xmlhttp.open("GET", url, true);
	XMLHTTPObjs[pos].xmlhttp.send(null);
	}

function AJAXCallPOST(url, POSTparams, onReturn, onReturnParams)
	{
	var pos = -1;

	// Trova il primo oggetto libero
	for (var i=0; i<XMLHTTPObjs.length; i++)
		{
		if (XMLHTTPObjs[i].freed == 1)
			{
			pos = i;
			break;
			}
		}
	// Oppure, se non lo trova, ne aggiunge un altro
	if (pos == -1)
		{
		pos = XMLHTTPObjs.length;
		XMLHTTPObjs[pos] = new CXMLReq(1);
		}

	if (XMLHTTPObjs[pos].xmlhttp == null)
		return -1;

	XMLHTTPObjs[pos].freed = 0;
	XMLHTTPObjs[pos].xmlhttp.onreadystatechange=function()
		{
		xmlhttpChange(pos, onReturn, onReturnParams);
		}
	
	XMLHTTPObjs[pos].xmlhttp.open("POST", url, true);
	XMLHTTPObjs[pos].xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	XMLHTTPObjs[pos].xmlhttp.setRequestHeader("Content-length", POSTparams.length);
	XMLHTTPObjs[pos].xmlhttp.setRequestHeader("Connection", "close");

	XMLHTTPObjs[pos].xmlhttp.send(POSTparams);
	}

// Mostra "caricamento in corso".
function showLoading(show)
	{
	if (show)
		{
		d = document.createElement("div");
		d.id = "AJAXLoading";
		d.innerHTML = "Caricamento in corso";
		document.body.appendChild(d);
		}
	else
		{
		d = document.getElementById("AJAXLoading");
		if (d)
			{
			document.body.removeChild(d);
			}
		}
	}

// Mostra un messaggio.
function AJAXMessage(msg)
	{
	d = document.createElement("div");
	d.id = "AJAXMsg";
	d.innerHTML = msg;
	// Dopo tre secondi eliminiamo il div
	setTimeout("document.body.removeChild(document.getElementById('AJAXMsg'))", 5000);
	document.body.appendChild(d);
	}

// Crea un rettangolone semitrasparente che copre un certo elemento
// Parametri:
// elem: l'elemento da coprire
// divID: l'ID del nuovo div creato
function AJAXLoadingRect(elem, divID)
	{
	d = document.createElement("div");

	d.className = "AJAXLoadingRect";
	d.id = divID;
	d.style.width = elem.clientWidth+"px";
	d.style.height = elem.clientHeight+"px";
	d.style.top = elem.offsetTop+"px";
	d.style.left = elem.offsetLeft+"px";
	d.innerHTML = "Attendere prego...";
	d.style.lineHeight = elem.clientHeight+"px";
	document.body.appendChild(d);

	return d;
	}