var xmlHttp=createRequestObject();
var i=0;

function createRequestObject(){
	var http;
	if (typeof window.ActiveXObject!='undefined'){
		try{
			http=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			http=false;
		}
	} else {
		try {
			http=new XMLHttpRequest();
		} catch (e) {
			http=false;
		}
	}

	if (!http)
		alert("Error xml http objet");
	else
		return http;
	return null;
}


var myAjax = {
	/* get data from server using GET method
	* params - asociative array of params like param_name=param_value
	* responseFunc - function to process received data
	* 		this function acept data as array on text strings
	* errorFunc - func that will be exekucted in case server side processing
	* 		fails - it's parameter is array of text strings from server
	* connectionErrorFunc - func that will be exekucuted where server connection
	*		fails - it takes no params
	*
	*/
	get: function(params,script,responseFunc,errorFunc,connectionErrorFunc){
		if (xmlHttp.readyState==4 || xmlHttp.readyState==0){
			str=myAjax.getParamsStr(params);
			xmlHttp.open("GET",script+(str.length<1?"":"?")+str);
			xmlHttp.onreadystatechange=function(){
				myAjax.getResponse(responseFunc,errorFunc);
			}
			window.status="Nahrávam údaje....";
			xmlHttp.send(null);
		} else {
			setTimeout(function(){myAjax.get(params,script,
				responseFunc);},100+Math.ceil(Math.random()*100));
		}
	},

	getResponse: function(func,errorFunc,connectionErrorFunc){
		if (xmlHttp.readyState==4){
			if (xmlHttp.status==200 || xmlHttp.status==304){
				pole=xmlHttp.responseText.split('\n');
				if (pole.shift()=='OK'){
					window.status='Hotovo';
					func(pole);
				} else {
					if (errorFunc != null){
						window.status='Hotovo';
						errorFunc(pole);
					}else{
						window.status='Hotovo';
						alert(pole.shift());
					}
				}
			} else {
				if (connectionErrorFunc != null){
					window.status='Hotovo';
					connectionErrorFunc();
				}else{
					window.status='Hotovo';
					alert("Chyba spojenia!");
				}
			}
		}
	},

	getParamsStr:function(params){
		var str='';
		if (typeof params!='undefined')
			for (var param in params){
				str+=(str.length<1?'':'&')+param+"="
					+encodeURIComponent(params[param]);
			}
		return str;
	}
}


