
var XHR =
{
	pool: new Array(),
	nb_chargement: 0,

	nullFunction: function() {}, // for nuking the onreadystatechange

	createXHR: function ()
	{
		if (window.XMLHttpRequest)
			return new XMLHttpRequest();
		else if (window.ActiveXObject)
			return new ActiveXObject('Microsoft.XMLHTTP')
	},

	getInstance: function ()
	{
		if (this.pool.length < 1)
			return this.createXHR();
		else
			return this.pool.pop();
	},

	release: function (xhr)
	{
		xhr.onreadystatechange = this.nullFunction;
		this.pool.push(xhr);
	},

	AfficherChargement: function ()
	{
		document.getElementById('ajax_wait').style.display = 'block';
	},

	MasquerChargement: function ()
	{
		document.getElementById('ajax_wait').style.display = 'none'
	},

	req: function (script, fonction, parametres, chargement, callback)
	{
		if (chargement)
		{
			this.nb_chargement++;
			this.AfficherChargement();
		}
		
		var xhr = this.getInstance();
		
		url = script+'.serveur.php?fonction='+fonction+'&parametres='+parametres;
		
		if (!callback)
			callback = eval;
		
		xhr.open("GET", url, true);
		xhr.send(null);
		xhr.onreadystatechange = function()
		{
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				if (chargement)
				{
					XHR.nb_chargement--;
					if (!XHR.nb_chargement)
						XHR.MasquerChargement();
				}
				callback(xhr.responseText);
				XHR.release(xhr);
			}
		};
	},

	PositionChargement: function ()
	{
			document.getElementById("ajax_wait").style.top = y_souris+20+'px';
			document.getElementById("ajax_wait").style.left = x_souris+20+'px';
	}
}
