//1.1.0
function clsAjax(){
	var url = '';
	var panelresult = null;
	var pictureLocation = null;
	var innerhtml = 'LOADING';
	var qstr = '';
	var qarr = new Array();
	var self = null;
	var functionName = '';
	var isImportant = 0;
	var SecureCode = 'AjaxanFin';
	var temppictureLocation = new Array;
	var temppanelresult = new Array;
	var ErrMessage = '';
	var IsPicANDtext = 0;
	var isCached = 0;
	var arr_cached = new Array;
	
	var onreceived="";
	var onsend="";

	this.init = function(URL, presult, plocation, inner){
		url = URL;
		panelresult = presult;
		pictureLocation = plocation;
		innerhtml = inner;
		IsPicANDtext = presult==plocation;
	};
	
	this.important = function(state){
		isImportant = state;
		if(!state)
			SecureCode = '';
	};
	
	this.cached = function(state){
		isCached = state;
	};
	
	this.setSecureCode = function(str){
		SecureCode = str;
	};
	
	this.setErr = function(str){
		ErrMessage = str;
	};
	
	this.setQuery = function(str){
		qstr = str;	
	};

	
	this.setonreceived=function(fn){
		onreceived=fn;
	};
	
	this.setonsend=function(fn){
		onsend=fn;
	}
	
	this.returnQuery = function(){
		qarr=this.optimize(qarr);
		var x = "";
		for(var i=0;i<qarr.length;i++){
			x+= "&"+qarr[i][0]+"="+ enCode(qarr[i][1]);
		}
		if(x!="")
			x = x.substr(1,x.length);
		return x;
	};
	
	function ord(c){
		return c.charCodeAt(0);
	};

	function chr(n){
		return String.fromCharCode(n);
	};

	function updatepage(str){
		if(typeof panelresult!='object')   panelresult = document.getElementById(panelresult);
		if(typeof panelresult=='object'){
			try{panelresult.className = '';}catch(e){};
			try{pictureLocation.className = '';}catch(e){};
		}
		if(!isCached || !arr_cached[cachenumber]){
			if (isImportant){
				var bb = true;
				if(str.indexOf(SecureCode)>-1){
					var temp = str.toString().substr(str.indexOf(SecureCode), SecureCode.length);
					if (temp == SecureCode){
						str = str.substr(0,str.length-SecureCode.length);
					}else{
						bb=false;}
				}else{
					bb=false;
				}
			}else{
				//str = str.substr(0,str.length-SecureCode.length);
				bb=true;
			}
			if(!bb){
				if (ErrMessage!='')
					alert(ErrMessage);
				str = temppanelresult['innerHTML'];
				if(panelresult)
					panelresult.className = temppanelresult['className'];
				if(!IsPicANDtext){
					try{pictureLocation.innerHTML=temppictureLocation['innerHTML'];}catch(e){}
					try{pictureLocation.className=temppictureLocation['className'];}catch(e){}
				}
			}
		}
		try{
			if(isCached)
				if(!arr_cached[cachenumber])
					arr_cached[cachenumber] = str;
			panelresult.innerHTML='';
			var temp = document.createElement('div');
			temp.innerHTML = str;
			var arr = new Array;
			for(var i=0; i<temp.childNodes.length;i++){
				arr[i] = temp.childNodes[i];
			}
			for(i=0;i<arr.length;i++)
				panelresult.appendChild(arr[i]);
		}catch(e){}
		if(bb)
			if(!IsPicANDtext)
				try{pictureLocation.style.display = "none";}catch(e){};
		if(functionName!='' && functionName)
			setTimeout(functionName, 1);
		if(onreceived)
			onreceived(str);
	};
	
	
	this.send = function(option, FN) {
		if (url==''){
			alert("Error! url is null");
			return false;
		}
		if(isCached){
			if(arr_cached[cachenumber]){
				updatepage(arr_cached[cachenumber]);
				return;
			}
		}
		if(FN)
			functionName = FN;
		var xmlHttpReq = false; 
		self = this;
		if (window.XMLHttpRequest) { 
			self.xmlHttpReq = new XMLHttpRequest(); 
		} 
		else if (window.ActiveXObject) { 
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		self.xmlHttpReq.open('POST', url, true); 
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); 
		self.xmlHttpReq.onreadystatechange = dataRecived;
		
		this.addKey("option",option);
		qarr=this.optimize(qarr);
		var x = "";
		for(var i=0;i<qarr.length;i++){
			x+= "&"+qarr[i][0]+"="+ enCode(qarr[i][1]);
		}
		if(x!="")
			x = x.substr(1,x.length);
		this.loading();
		self.xmlHttpReq.send(x);
	};
	
	function dataRecived(){
		if (self.xmlHttpReq.readyState == 4){
			updatepage(self.xmlHttpReq.responseText);
		}
	};
	
	this.clear = function (){
		qstr="";
	};

	this.loading=function() {
		if(onsend)
			return onsend();
		if (typeof panelresult !='object') panelresult = document.getElementById(panelresult);
		try{
			temppanelresult['innerHTML'] = panelresult.innerHTML;
			temppanelresult['className'] = panelresult.className;
			panelresult.className = "Ajax_Panel_Refresh";
		}catch(e){
			temppanelresult['innerHTML'] = '';
			temppanelresult['className'] = '';
		}
		try{
			if (typeof pictureLocation !='object') pictureLocation = document.getElementById(pictureLocation);
		}catch(e){}
		try{
			temppictureLocation['innerHTML'] = pictureLocation.innerHTML;
			temppictureLocation['className'] = pictureLocation.className;
			pictureLocation.className = "Ajax_Panel_Refresh";
			if(typeof innerhtml=="object"){
				pictureLocation.innerHTML="";
				pictureLocation.appendChild(innerhtml);
			}else{
				pictureLocation.innerHTML = innerhtml;
			}
		}catch(e){}
	};	

	this.optimize = function (arr){
		var temp = new Array;
		var j=0;
		for(var i=0;i<arr.length;i++)
			if(arr[i][0]!="")
				temp[j++]=arr[i];
		return temp;
	};
	
	this.addKey = function (name,value){
		if (name=="") return 0;
		name = name.toLowerCase();
		this.delKey(name);
		qarr[qarr.length]=Array(name,value);
	};
	
	this.addKeys=function(){
		for(var i=0;i<arguments.length;i++)
			this.addKey(arguments[i++],arguments[i]);
	};
	
	this.setCachednumber = function(number){
		cachenumber = number;
	};

	this.delKey = function (name){
		if(name=='')
			return;
		name=name.toLowerCase();
		if(name=='null')
			return;
		for(var i=0;i<qarr.length;i++)
			if(qarr[i][0]==name){
				qarr[i][0]='';return;}
	};
	
	this.returnKey = function (name){
	};
	
	function enCode(str){
		return encodeURIComponent(str.toString());
	};

}

