/*  ver 08.02.12, tesztelve 96%

	megadható paraméterek:
	 - url: feldolgozó egység url-je
	 - resultObj: eredmény célobjektuma, resultFunc kötelező mellette
	 - resultFunc: eredmény cél függvénye
	 - resultHTMLObj: eredmény HTML objektuma
	 - method: post/get, post az alapértelmezett
	 - waitingMode: ha 0: újra kattintásra azzonnal újra elküldődik a kérést
	 				ha 1: csak akkor küldi újra a kérést, ha timeLimit*maxReTrying ezredmásodpercig nincs eredmény
	 - loadingImgID: ha meg van adva, akkor az adott azonosítójú képet megjeleníti
	 - fadeableObjID: ha meg van adva, akkor az adott azonosítójú objektumokat elhalványítja
	 
	 Töltést jelző gifanimáció kreátor: http://www.ajaxload.info/
*/

Ajax.DEFAULT_FADEVALUE='40';	//áttetszőség %-aban

function Ajax(data){
	
	this.xmlHttp=null;
	this.loading=0;
	
	this.url=null;
	this.resultObj=null;
	this.resultFunc=null;
	this.resultHTMLObj=null;
	this.method='POST';
	this.timeLimit=3000;	//két próbálkozás közötti idő
	this.waitingMode=1;		//0: nincs várakozás azonnal újra küld, 1: amíg folyamatban van valami nem küld
	this.maxReTrying=2;		//maximum újra próbálkozások száma (0 - ...)
	this.currentParams=null;
	this.currentTryIndex=1;
	
	this.loadingImgID=null;
	this.fadeableObjIDs=new Array();
	
	this.reqTimeStart=null;
	this.reqTimeEnd=null;
	
	this.responseText=null;
	this.responseXML=null;
	
	if (typeof(data)!='undefined'){
		if (typeof(data['url'])!='undefined') this.url=data['url'];
		if (typeof(data['resultObj'])!='undefined') this.resultObj=data['resultObj'];
		if (typeof(data['resultFunc'])!='undefined') this.resultFunc=data['resultFunc'];
		if (typeof(data['resultHTMLObj'])!='undefined') this.resultHTMLObj=data['resultHTMLObj'];
		if (typeof(data['method'])!='undefined') this.method=data['method'].toUpperCase();
		if (typeof(data['loadingImgID'])!='undefined')
			this.loadingImgID=data['loadingImgID'];					//töltést jelző kép ID-je
		if (typeof(data['fadeableObjIDs'])!='undefined') 
			this.fadeableObjIDs=data['fadeableObjIDs'].split("|");	//elhalványuló objektumok ID-je "|" jellel elválasztva
	}

	this.request=function(params){	//a params tömb formátumú kell hogy legyen
		if (this.waitingMode==0 && this.isLoading()) this.abort();
		if (this.waitingMode==1 && this.isLoading()) return false;
		if (this.waitingMode==0 || (this.waitingMode==1 && this.currentTryIndex==1)){
			var d=new Date();
			this.reqTimeStart=d.getTime();
			this.reqTimeEnd==null;
		}
		this.xmlHttp=Ajax.createRequestObject();
		if (this.xmlHttp==null) return false;
		
		function timeout_wrap(obj){return function(){ obj.timeout();}}
		window.setTimeout(timeout_wrap(this),this.timeLimit);
		
		if (typeof(params)=='undefined') params='';
		this.currentParams=params;
		if (typeof(params)=='object') params=Ajax.encodeParams(params);	
		if (params.charAt(0)=='?' || params.charAt(0)=='&') params=params.substr(1);

		function wrapper(obj){return function(){ obj.stateChecker();}}
		this.xmlHttp.onreadystatechange=wrapper(this);
		
		if (this.method=='GET'){
			if (params!=''){
				if (this.url.charAt('?')>-1) params='&' + params;
				else params='?' + params;
			}
			this.xmlHttp.open("GET",this.url + params,true);
			this.xmlHttp.send(null);
		}
		else{
			this.xmlHttp.open("POST",this.url,true);
			this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");    
			this.xmlHttp.send(params);
		}
		
		this.showEffect();
		this.loading=1;
		return true;
	}
	
	this.stateChecker=function(){
		if (this.xmlHttp.readyState!=4) return;
		this.hideEffect();
		var d=new Date();
		this.reqTimeEnd=d.getTime();
		this.loading=0;
		if (this.xmlHttp.status>200){ //&& this.xmlHttp.status<300){
				this.error();
				return;
		}
		this.responseText=this.xmlHttp.responseText;
		this.responseXML=this.xmlHttp.responseXML;
		if (this.resultObj==null && this.resultFunc!=null)
			this.resultFunc(this.xmlHttp.responseText);
		else if (this.resultObj!=null && this.resultFunc!=null) 
			this.resultFunc.call(this.resultObj,this.xmlHttp.responseText);
		else if (this.resultHTMLObj!=null)
			this.resultHTMLObj.innerHTML=this.xmlHttp.responseText;
		else;
	}
	
	this.timeout = function(){
		if (!this.isLoading()) return;
		this.abort();
		if (this.currentTryIndex<=this.maxReTrying){
			this.currentTryIndex++;
			this.request(this.currentParams);
			return;
		}
		this.hideEffect();
		this.currentTryIndex=1;
		this.reqTimeStart=0;
		this.reqTimeEnd=-1;
		this.error("timeout");
	}
	
	this.abort = function(){
		this.xmlHttp.onreadystatechange = function() { }; 
		this.xmlHttp.abort();
		this.xmlHttp = null; 
		this.loading=0;
	}
	
	this.getLoadingTime=function(){
		if (this.isLoading() || this.reqTimeStart==null || this.reqTimeEnd==null) return null;
		return this.reqTimeEnd-this.reqTimeStart;
	}
	
	this.isLoading = function(){
		return this.loading;
	}
	
	this.error = function(notes){
		//notes lehet pl.: timeout
	}
	
	this.showEffect = function(){
		var obj,i;
		if (this.loadingImgID!=null){
			obj=document.getElementById(this.loadingImgID);
			obj.style.display="block";
		}
		for (i=0;i<this.fadeableObjIDs.length;i++){
			Ajax.fade(this.fadeableObjIDs[i],Ajax.DEFAULT_FADEVALUE);
		}
	}
	
	this.hideEffect = function(){
		var obj,i;
		if (this.loadingImgID!=null){
			obj=document.getElementById(this.loadingImgID);
			obj.style.display="none";
		}
		for (i=0;i<this.fadeableObjIDs.length;i++){
			Ajax.fade(this.fadeableObjIDs[i],100);
		}
	}
}

Ajax.createRequestObject=function(){
	var xmlHttp=null;
	try{		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){	// Internet Explorer
		try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
	}
	return xmlHttp;
}

Ajax.isSupported=function(){
	return Ajax.support;
}

Ajax.encodeParams = function(params){
	var key,tmp="",i,pre="";
	if (typeof(params)!='object') return '';
	if (arguments.length>1) pre=arguments[1];
	for(key in params){
		if (typeof(params[key])!='object'){
			if (pre=="")
				tmp+=key + '=' + escape(params[key]) + '&';
			else
				tmp+=pre + '[' + key + ']=' + escape(params[key]) + '&';
		}
		else{
			if (pre=="")
				tmp+=Ajax.encodeParams(params[key],key) + '&';
			else
				tmp+=Ajax.encodeParams(params[key],pre + '[' + key + ']') + '&';
		}
	}
	return tmp.substr(0,tmp.length-1);
}

Ajax.fade=function(objID,value){if (Util) Util.setOpacity(objID,value);}

var obj=Ajax.createRequestObject();
if (obj==null) Ajax.support=false
else Ajax.support=true;