var ajaxRequests = new Array();
var activeAjaxRequest = null;
var debugAjaxTextarea;
var ajaxId = 1;

setOnload(function()
{
	addEventListener(window, 'beforeunload',
	function()
	{
		if (ajaxRequests.length > 0)
		{
			//return '<?=$text->get('last_modifications', JAVASCRIPT);//Vos dernières modifications sont en train de se mettre a jour en arrière-plan (téléchargement d\'images ou modification d\'un texte).\nSi vous quittez cette page, les modifications ou les téléchargements risquent d\'être interrompus.\nQuitter la page??> ('+ajaxRequests.length+ ' <?=$text->get('remainings_operations', JAVASCRIPT);//opération(s) restante(s)?>)';
		}
	});
});

function sendAjaxRequest(url, params, object, responseFunction, delay)
{
	if (window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}
	else
	if (window.ActiveXObject)
	{
		xhr = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	xhr.id = ajaxId;
	xhr.url = url;
	xhr.params = params;
	xhr.object = object;
	xhr.responseFunction = responseFunction;
	
	if (delay)
	{
		var time = new Date();
		xhr.timer = time.getTime() + delay;
	}
	
	xhr.clear = function ()
	{
		for (var i = 0; i < ajaxRequests.length; i++)
			if (ajaxRequests[i] == this)
			{
				ajaxRequests.remove(i);
				break;
			}
	}
	
	ajaxId++;
	
	if (!ajaxRequests)
		ajaxRequest = new Array();
		
	ajaxRequests.push(xhr);
	setTimeout('processAjaxRequests();', 1);
	
	return xhr;
}

function processAjaxRequests(next)
{
	if (next)
	{
		activeAjaxRequest = false;
	}
	
	if ((!activeAjaxRequest || next) && ajaxRequests.length)
	{
		var time = new Date();
		
		//alert('process next request: url: ' + ajaxRequests[0].url + ' params:'+ajaxRequests[0].params + '\nRemaining:'+(ajaxRequests.length - 1));
		
		for (var i = 0; i <= ajaxRequests.length; i++)
		{
			if (i < ajaxRequests.length && (!ajaxRequests[i].timer || (ajaxRequests[i].timer < time.getTime())))
				break;
		}
		
		if (i >= ajaxRequests.length)
		{
			setTimeout('processAjaxRequests(true);', 10);
			return;
		}
		
		activeAjaxRequest = ajaxRequests[i];
		
		processAjaxRequest(activeAjaxRequest);
		
		if (ajaxRequests.length > 1)
			ajaxRequests.remove(0);else
			ajaxRequests = new Array();
	}
	else if (!ajaxRequests.length)
	{
		
	}
}

function processAjaxRequest(xhr)
{
	if (xhr)
	{	
		xhr.params += '&ajax=true';
		
		xhr.open('POST', xhr.url, true);
		xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xhr.setRequestHeader('Content-length', xhr.params.length);
		xhr.setRequestHeader('Connection', 'close');
		
		xhr.onreadystatechange = function () 
		{
			if (this.readyState==4) {
				
				if (!debugAjaxTextarea)
				{
					debugAjaxLink = document.createElement('a');
					debugAjaxLink.innerHTML = 'Cliquez ici pour voir les processus ajax';
					debugAjaxLink.onclick = function ()
					{
						if (debugAjaxTextarea.style.display == 'none')
							debugAjaxTextarea.style.display = 'block';else
							debugAjaxTextarea.style.display = 'none';
					}
					
					debugAjaxTextarea = document.createElement('ul');
					debugAjaxTextarea.style.display = 'none';
					
					document.body.appendChild(debugAjaxLink);
					document.body.appendChild(debugAjaxTextarea);
				}
				
				
				try
				{
					var li = document.createElement('li');
					var link = document.createElement('a');
						link.innerHTML = 'Display request n°'+debugAjaxTextarea.childNodes.length;
						
					var pre = document.createElement('pre');
						pre.appendChild(document.createTextNode(xhr.url + xhr.params + ':\n\n'));
						pre.appendChild(document.createTextNode(xhr.responseText));
						
					var content = document.createElement('div');
						content.appendChild(pre);
						content.style.display = 'none';
					
					link.content = content;
					
					link.onclick = function ()
					{
						if (this.content.style.display == 'none')
							this.content.style.display = 'block';else
							this.content.style.display = 'none';
					}
					
					li.appendChild(link);
					li.appendChild(content);
					
					debugAjaxTextarea.appendChild(li);
				}
				catch(e)
				{
					
				}
				
				processAjaxRequests(true);
				
				if (typeof(this.object) != 'object' && this.responseFunction)
				{
					this.responseFunction(this);
				}
				else if (typeof(this.object) == 'object' && this.responseFunction)
				{
					eval('this.object.'+this.responseFunction+'(this);');
				}
				//ajaxResponse(xhr)
			}
		};
		
		xhr.send(xhr.params);
	}
	return xhr;
}