var ga_xmlHttp = Array();
var ga_handleServerResponseWait = Array();
var ga_handleServerResponseError = Array();
var ga_handleServerResponseOK = Array();
var ga_openMethod = Array();
var ga_openHttp = Array();
var ga_openAsync = Array();
//
var ga_destination = Array(); // destination field
var ga_status = Array(); // status field


function myajax_init(i_index, i_href, i_destination, i_status)
{
	ga_openHttp[i_index] = i_href;
	ga_destination[i_index] = i_destination;
	ga_status[i_index] = i_status;
	ga_openMethod[i_index] = "GET";
	ga_openAsync[i_index] = true;
	ga_handleServerResponseWait[i_index] = function () { handleServerResponseWaitGeneric(i_index); };
	ga_handleServerResponseError[i_index] = function () { handleServerResponseErrorGeneric(i_index); };
	ga_handleServerResponseOK[i_index] = function () { handleServerResponseOKGeneric(i_index); };
}


function myajax_bis(i_index, i_method, i_http, i_async)
{
	if (!ga_xmlHttp[i_index]) {
		ga_xmlHttp[i_index] = createXMLHttpRequest();
		if (!ga_xmlHttp[i_index]) {
			alert("Cannot use XMLHttpRequest Object, which was created with errors.");
			return;
		}
	}
	ga_xmlHttp[i_index].open(i_method, i_http, i_async);
	ga_xmlHttp[i_index].onreadystatechange = function() { handleServerResponse(i_index); };
	ga_xmlHttp[i_index].send(null);
}


function myajax(i_index)
{
	var l_http = ga_openHttp[i_index];
	myajax_bis(i_index, ga_openMethod[i_index], l_http, ga_openAsync[i_index]);
}


function myajax_spec(i_index, i_destination)
{
	var l_http = i_destination;
	myajax_bis(i_index, ga_openMethod[i_index], l_http, ga_openAsync[i_index]);
}


function handleServerResponse(i_index)
{
	ga_handleServerResponseWait[i_index]();

	if (ga_xmlHttp [i_index].readyState != 4) {
		return;
	}

	if(ga_xmlHttp [i_index].status != 200) {
		ga_handleServerResponseError[i_index]();
		return;
	}

	ga_handleServerResponseOK[i_index]();
}


function handleServerResponseWaitGeneric(i_index)
{
	document.getElementById(ga_status[i_index]).innerHTML = "[Attente serveur...]";
}


function handleServerResponseErrorGeneric(i_index)
{
	alert("Problème d'accès au serveur : " + ga_xmlHttp[i_index].statusText);
	document.getElementById(ga_status[i_index]).innerHTML = "Code erreur : " + ga_xmlHttp[i_index].status;
}


function handleServerResponseOKGeneric(i_index)
{
	document.getElementById(ga_status[i_index]).innerHTML = '';
	document.getElementById(ga_destination[i_index]).innerHTML = ga_xmlHttp[i_index].responseText;
}
/*
xml example :
	l_xmlResponse = ga_xmlHttp [i_index].responseXML;
	l_xmlRoot = l_xmlResponse.documentElement;
	l_list = l_xmlRoot.getElementsByTagName ("generated");
	l_text = '';
	for (var l_i = 0 ; l_i < l_list.length ; l_i ++)
		l_text += l_list[l_i].firstChild.data + '<br />';
	document.getElementById (i_zoneID).innerHTML = l_text;
*/


function createXMLHttpRequest()
{
	var l_xmlHttpVersions = new Array(
		"MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");
	for (var l_i = 0 ; l_i < l_xmlHttpVersions.length ; l_i ++) {
		try {
			return new ActiveXObject (l_xmlHttpVersions [l_i]);
		} catch(e) {}
	}

	try {
		return new XMLHttpRequest();
	} catch(e) {}

	alert("XMLHttpRequest not supported, please update your browser.");
	return null;
}