req = null;
xAjaxServerReply = null; // (code, data)
xHideAjaxInfo = null; // ()
xShowAjaxInfo = null; // ()
xUpdateAjaxInfo = null; // (info)
xAjaxProgress = null; // (e)
xAjaxError = null; // (e)
xAjaxLoad = null; // (e)
xAjaxReadyStateChange

function xAjaxExecuteAction(aname, url)
{
	xShowAjaxInfo();
	xUpdateAjaxInfo('Pobieranie danych, proszę czekać...');
	url = url+'?a='+aname+'&unused='+Math.random();
	if (window.XMLHttpRequest)
	{	
		req = new XMLHttpRequest();
		req.onprogress = xAjaxProgress;
		req.open('GET', url, true);
		req.onload = xAjaxLoad;
		req.onerror = xAjaxError;
		req.onreadystatechange = xAjaxReadyStateChange;
		req.send(null);	
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = xAjaxReadyStateChange;
			//req.onload = xAjaxLoad;
			//req.onerror = xAjaxError;
			req.open('GET', url, true);
			req.send();
		}
	}
	else
	{
		alert('Nie można zainicjować XMLHttpRequest!');
	};
};

function xAjaxExecuteActionPost(aname, parr, url)
{
	xShowAjaxInfo();
	xUpdateAjaxInfo('Pobieranie danych, proszę czekać...');
	params = 'a='+aname;
	cn = parr.length;
	i = 0;
	while (i < cn)
	{
		params = params + '&'+parr[i][0]+'=' + encodeURIComponent(parr[i][1]);
		i++;
	};
	params = params + '&unused='+Math.random();
	
	if (window.XMLHttpRequest)
	{	
		req = new XMLHttpRequest();
		req.onprogress = xAjaxProgress;
		req.open('POST', url, true);
		req.onload = xAjaxLoad;
		req.onerror = xAjaxError;
		req.onreadystatechange = xAjaxReadyStateChange;

		req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		req.setRequestHeader('Content-length', params.length);
		req.setRequestHeader('Connection', 'close');
		req.send(params);	
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = xAjaxReadyStateChange;
			//req.onload = xAjaxLoad;
			//req.onerror = xAjaxError;
			req.open('POST', url, true);
			req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			req.setRequestHeader('Content-length', params.length);
			req.setRequestHeader('Connection', 'close');
			req.send(params);	
		}
	}
	else
	{
		alert('Nie można zainicjować XMLHttpRequest!');
	};
};

function xAjaxReadyStateChange() {
	if (req.readyState == 4) {
		if(req.status == 200)
		{
			p = req.responseText.indexOf(',');
			if (p > 0)
			{
				replycode = req.responseText.substr(0, p);
				replydata = req.responseText.substr(p+1);
			}
			else
			{
				replycode = -1;
				replydata = 'Błędna odpowiedź serwera';
			}
			if (xAjaxServerReply != null)
				xAjaxServerReply(replycode, replydata);
		}
		else
		{
			replycode = -2;
			replydata = 'Błąd w czasie pobierania danych';
			if (xAjaxServerReply != null)
				xAjaxServerReply(replycode, replydata);
		};
	}
}; 

XAJAX_LOADED = 1;

