//------------------------------- NEW AJAX POST ---------------------------------------

// sendAjaxCall(file, argument array, callback function, priority, text response (false = XML));

var posts = new Array();
var run = true;

function sendAjaxCall(f, arg, c, p, t)	{
	if(t == null) t = 'txt';	
	
	var a_param = "&r=" + new Date().getTime();
	for (var i=0; i < arg.length; i++)	{	a_param += "&" + encodeURI(arg[i][0]) + "=" + encodeURI(arg[i][1]);	}
	
	var a_http = false;	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		a_http = new XMLHttpRequest();
		if (a_http.overrideMimeType) {
			a_http.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			a_http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				a_http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
		
	if (!a_http) {
		alert('Cannot create XMLHTTP instance');
		return false;
	} else	{
		var this_post = new Array(a_http, a_param, f, c, p, t);
		posts.push(this_post);
		
		if(run)	{
			ajaxRun(posts.shift());
			if(p) run = false;
		}
	}
}

function ajaxRun(obj)	{
	//alert(obj[1]); (alert all parameters)
	
	obj[0].open('POST', obj[2], true);
	obj[0].onreadystatechange = function()	{		
		if (obj[0].readyState == 4) {
			if (obj[0].status == 200) {	
				switch(obj[5])	{
					case "txt":		obj[3](obj[0].responseText);	break;
					case "xml":		obj[3](obj[0].responseXML);		break;
					case "json":	obj[3](JSON.parse(obj[0].responseText));	break;
				}
				if(obj[4]) ajaxResume();
				ajaxClear(obj);
			}
		}
	}
	obj[0].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	obj[0].send(obj[1]);
}

function ajaxResume()	{
	if(posts.length > 0)	{
		var p = posts[0][4];
		ajaxRun(posts.shift());
		if(p) {
			run = false;
		} else	{
			ajaxResume();
		}
	} else	{
		run = true;
	}
}

function ajaxClear(obj)	{
	obj[0] = null;		// Other way ?
	obj[1] = null;
	obj[2] = null;
	obj[3] = null;
	obj[4] = null;
	obj = null;
}

function ajaxBuildArgs(o, arg, v)	{
	var ar = new Array(arg, v);
	o.push(ar);
	return o;
}
	
	
