
var ajax = null;
var response = null;

function httpRequest(reqType,url,asynch,respHandle) {
	if (window.XMLHttpRequest) {
		ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
		if (!ajax) {
			ajax = new ActiveXObject("Msxml12.XMLHTTP");
		}
	}
	
	if (ajax) {
		if (reqType.toLowerCase()!="post") {
			initReq(reqType,url,asynch,respHandle);
		} else {
			var args = arguments[4];
			if (args!=null && args.length>0) {
				initReq(reqType,url,asynch,respHandle,args);
			}
		}
	} else {
		alert("Przegladarka nie pozwala na uzycie ajax!");
	}
}

function initReq(reqType,url,bool,respHandle) {
	try{
		ajax.onreadystatechange=function() { respHandler(respHandle); };
		ajax.open(reqType,url,bool);
		if (reqType.toLowerCase()=="post") {
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
			ajax.send(arguments[4]);
		} else {
			ajax.send(null);
		}
	} catch(err) {
		alert(
			"W tym momencie aplikacja "+
			"nie moze polaczyc sie z serwerem. "+
			"Prosze sprobowac ponownie w ciagu kilku sekund. \n"+
			"Szczegolowe informacje o bledzie: "+err.message);
	}
}

function respHandler(respHandle) {
	if (ajax.readyState==4) {
		if (ajax.status==200) {
			response = ajax.responseText;
			try {
				respHandle();
			} catch(err) {
				alert('Wystapil blad: '+err.message);
			}
		}
	}
}
