/*Simple AJAX Code-Kit (SACK) v1.6.1 ©2005 Gregory Wild-Smith www.twilightuniverse.com
Software licenced under a modified X11 licence,see documentation or authors website for more details */
/*Be aware this is a modified and perhaps not fit for your needs version ### блоги */
function http_req(file){
	this.xmlhttp=null;
	this.ProgID=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
	this.resetData=function(){
		this.charset='UTF-8';//ISO-8859-1
		this.method="POST";
  		this.queryStringSeparator="?";
		this.argSeparator="&";
		this.URLString="";
		this.encodeURIString=true;
  		this.execute=false;
  		this.target=null;
		this.targetObj=null;
		this.requestFile=file;
		this.vars=new Object();
		this.responseStatus=new Array(2);
  	};
	this.resetFunctions=function(){
  		this.onLoading=function(){};
  		this.onLoaded=function(){};
  		this.onInteract=function(){};
  		this.onDone=function(){};
  		this.onError=function(){};
		this.onFail=function(){};
		this.onBadRequest=function(){};
	};
	this.reset=function(){
		this.resetFunctions();
		this.resetData();
	};
	this.createHTTP=function(){
		for(var i=0;i<this.ProgID.length;i++){
			try{
				this.xmlhttp=new ActiveXObject(this.ProgID[i]);
				if(this.xmlhttp!=null){
					break;
				}
			} 
			catch(e){}
		}
		if(!this.xmlhttp){
			if(typeof XMLHttpRequest!="undefined"){
				this.xmlhttp=new XMLHttpRequest();
			}
			else{
				this.failed=true;
			}
		}
	};
	this.setVar=function(name,value){
		this.vars[name]=Array(value,false);
	};
	this.encVar=function(name,value,returnvars){
		if(true==returnvars){
			return Array(encodeURIComponent(name),encodeURIComponent(value));
		} else{
			this.vars[encodeURIComponent(name)]=Array(encodeURIComponent(value),true);
		}
	}
	this.processURLString=function(string,encode){
		encoded=encodeURIComponent(this.argSeparator);
		regexp=new RegExp(this.argSeparator+"|"+encoded);
		varArray=string.split(regexp);
		for (i=0;i<varArray.length;i++){
			urlVars=varArray[i].split("=");
			if(true==encode){
				this.encVar(urlVars[0],urlVars[1]);
			}
			else{
				this.setVar(urlVars[0],urlVars[1]);
			}
		}
	}
	this.createURLString=function(urlstring){
		if(this.encodeURIString&&this.URLString.length){
			this.processURLString(this.URLString,true);
		}
		if(urlstring){
			if(this.URLString.length){
				this.URLString+=this.argSeparator+urlstring;
			}
			else{
				this.URLString=urlstring;
			}
		}
		if(this.nocache){
			this.setVar("rndval",new Date().getTime());
		}
		urlstringtemp=new Array();
		for (key in this.vars){
			if(false==this.vars[key][1]&&true==this.encodeURIString){
				encoded=this.encVar(key,this.vars[key][0],true);
				delete this.vars[key];
				this.vars[encoded[0]]=Array(encoded[1],true);
				key=encoded[0];
			}
			urlstringtemp[urlstringtemp.length]=key+"="+this.vars[key][0];
		}
		if(urlstring){
			this.URLString+=this.argSeparator+urlstringtemp.join(this.argSeparator);
		}
		else{
			this.URLString+=urlstringtemp.join(this.argSeparator);
		}
	}
	this.runResponse=function(){
		eval(this.response);
	}
	this.runHTTP=function(urlstring){
		if(this.failed){
			this.onFail();
		}
		else{
			this.createURLString(urlstring);
			if(this.target){
				this.targetObj=document.getElementById(this.target);
			}
			if(this.xmlhttp){
				var self=this;
				if(this.method=="GET"){
					totalurlstring=this.requestFile+this.queryStringSeparator+this.URLString;
					this.xmlhttp.open(this.method,totalurlstring,true);
				}
				else{
					RequestHeader="application/x-www-form-urlencoded; charset="+this.charset;
					this.xmlhttp.open(this.method,this.requestFile,true);
					try{
						this.xmlhttp.setRequestHeader("Content-Type",RequestHeader);
					}
					catch(e){}
				}
				this.xmlhttp.onreadystatechange=function(){
					switch(self.xmlhttp.readyState){
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteract();
							break;
						case 4:
							self.response=self.xmlhttp.responseText;
							self.responseXML=self.xmlhttp.responseXML;
							self.responseStatus[0]=self.xmlhttp.status;
							self.responseStatus[1]=self.xmlhttp.statusText;
							if(self.execute){
								self.runResponse();
							}
							if(self.targetObj){
								targetNodeName=self.targetObj.nodeName;
								targetNodeName.toLowerCase();
								if(targetNodeName=="input"||targetNodeName=="select"||targetNodeName=="option"||targetNodeName=="textarea"){
									self.targetObj.value=self.response;
								}
								else{
									self.targetObj.innerHTML=self.response;
								}
							}
							if(self.responseStatus[0]=="200"||self.responseStatus[0]=="304"){
								self.onDone();
							}
							else if(self.responseStatus[0]=="400"){
								self.onBadRequest();
							}
							else{
								self.onError();
							}
							self.URLString="";
							break;
					}
				};
				this.xmlhttp.send(this.URLString);
			}
		}
	};
	this.reset();
	this.createHTTP();
}