// Copyright @ Dark Rother (Zbigniew Olejnik)

function createREQ(){

	try{
		req = new XMLHttpRequest(); // mozilla
	}catch(err1){
		
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP"); // IE
		}catch(err2){
			
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP"); // inne IE
			}catch(err3){
				req = false;	
			}
		}
	}
	return(req);
}

function requestGET(url, query, req, asyn){
	myRand = parseInt(Math.random()*999999);
	myDate = new Date().getTime();
	myAntiCache = myDate + myRand; 
	
	req.open("GET", url + '?' + query + '&rand=' + myAntiCache, asyn);
	req.send(null);
}
function requestPOST(url, query, req, asyn){
	req.open("POST", url, asyn);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(query);
}

function doCallback(callback, item1){
	eval(callback + '(item1)');	
}

function doAjax(url, query, callback, reqtype, asyn, getxml){
	
	var myreq = createREQ();
	myreq.onreadystatechange = function(){
		if(myreq.readyState == 4){
			if(myreq.status == 200){
				
				var item1 = myreq.responseText;
				if(getxml == 1){ item1 = myreq.responseXML; }
				doCallback(callback, item1);
			}else{
				alert("Wystapil blad: " + myreq.statusText);	
			}
		}else{
			try{
				document.getElementById('loader').innerHTML = '<img src="Gfx/loader.gif" width="35" height="35">';
			}catch(err){}
		}
	}
	
	if(reqtype == 'POST'){
		requestPOST(url, query, myreq, asyn);
	}else{
		requestGET(url, query, myreq, asyn);
	}
	
}
