// creates and returns an XMLHttpRequest instance
function XmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;

	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP');

		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {} // ignore potential error
		}
	}

	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

/*********************
Fonctions de base 
**********************/

function ajaxGetText(url, bAsync, callbackFunc, waitingFunc)
{
	var xmlHttp = XmlHttpRequestObject();
	xmlHttp.open("GET", url, bAsync);
	xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
	if(bAsync == false) 
	{
		if(waitingFunc != null) waitingFunc();
		xmlHttp.send(null);
		callbackFunc(xmlHttp.responseText);
	}
	else
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
			{	
				callbackFunc(xmlHttp.responseText);
				delete xmlHttp;
				xmlHttp = null;
			}
			else
			{
				// Waiting stuff here
				if(waitingFunc != null)
				{
					waitingFunc();
				}
			}
		}
		xmlHttp.send(null);
	}
}
function ajaxGetXML(url, bAsync, callbackFunc, waitingFunc)
{
	var xmlHttp = XmlHttpRequestObject();
	xmlHttp.open("GET", url, bAsync);
	xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
	if(bAsync == false) 
	{
		xmlHttp.send(null);
		callbackFunc(xmlHttp.responseXML);
	}
	else
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
			{	
				callbackFunc(xmlHttp.responseXML);
				delete xmlHttp;
				xmlHttp = null;
			}
			else
			{
				if(waitingFunc != null)
				{
					waitingFunc();
				}
			}
		}
		xmlHttp.send(null);
	}
}
function ajaxPostText(url, bAsync, params, callbackFunc, waitingFunc)
{
	var xmlHttp = XmlHttpRequestObject();
	xmlHttp.open("POST", url, bAsync);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
	if(bAsync == false) 
	{
		if(waitingFunc != null) waitingFunc();
		xmlHttp.send(params);
		callbackFunc(xmlHttp.responseText);
	}
	else
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
			{
				callbackFunc(xmlHttp.responseText);
				delete xmlHttp;
				xmlHttp = null;
			}
			else
			{
				if(waitingFunc != null)
				{
					waitingFunc();
				}
			}
		}
		xmlHttp.send(params);
	}
}
function ajaxPostXML(url, bAsync, params, callbackFunc, waitingFunc)
{
	var xmlHttp = XmlHttpRequestObject();
	xmlHttp.open("POST", url, bAsync);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
	if(bAsync == false) 
	{
		xmlHttp.send(params);
		callbackFunc(xmlHttp.responseXML);
	}
	else
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
			{
				callbackFunc(xmlHttp.responseXML);
				delete xmlHttp;
				xmlHttp = null;
			}
			else
			{
				if(waitingFunc != null)
				{
					waitingFunc();
				}
			}
		}
		xmlHttp.send(params);
	}
}
function ajaxPostFormGetText(url, bAsync, form_id, callbackFunc, waitingFunc)
{
	var form = document.getElementById(form_id);
	var params = '';
	for(i=0;i<form.elements.length;i++)
	{
		if(form.elements[i].name != '' && form.elements[i].value != '')
			params += '&' + form.elements[i].name + "=" + encodeURIComponent(form.elements[i].value);
	}
	
	var xmlHttp = XmlHttpRequestObject();
	xmlHttp.open("POST", url, bAsync);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
	if(bAsync == false) 
	{
		if(waitingFunc != null) waitingFunc();
		xmlHttp.send(params);
		callbackFunc(xmlHttp.responseText);
	}
	else
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
			{
				callbackFunc(xmlHttp.responseText);
				delete xmlHttp;
				xmlHttp = null;
			}
			else
			{
				if(waitingFunc != null)
				{
					waitingFunc();
				}
			}
		}
		xmlHttp.send(params);
	}
}
