var XHRresponse = new function(){
	this.status = null;
	this.response = null;
	this.init = function() {
		XHRresponse.status = this.AJAXresponse;
		return this;
	};
	this.clear = function() {
		this.status=this.response=null;
	};
};
function AJAX(url, args, keyHash, method, timeout, bCached, callbackFunc, callbackPhase, callbackArgs) {
	this.url = url;
	this.args = args;
	this.keyHash = keyHash;
	this.method = method.toLowerCase();
	this.timeout = timeout;
	this.timeoutID = null;
	this.bCached = bCached;
	this.callback = callbackFunc;
	this.callbackPhase = callbackPhase.toLowerCase();
	this.callbackArgs = (callbackArgs)?callbackArgs:[];
	this.request = new this.XHR();
}
AJAX.prototype = {
	XHR : function() {
		var req = false;
		if (window.XMLHttpRequest) {
			try {req = new XMLHttpRequest();}
			catch (e){}
		} else if (window.ActiveXObject) {
			try {req = new ActiveXObject('Msxml2.XMLHTTP');}
			catch (e) {
				try {req = new ActiveXObject('Microsoft.XMLHTTP');}
				catch (e){alert("Ваш браузер не поддерживает AJAX!");}
			}
		}
		return req;
	},
	send : function() {
		if (!this.request) return false;
		var correctURL=this.url, correctArgs = "";
		if (this.args && this.args.length > 0) {
			correctArgs += ((this.method=="get")?"?":"")+this.args;
		}
		if (this.keyHash) {
			correctArgs += ((correctArgs.length>0)?"&":"?")+"key="+this.keyHash;
		}
		if (this.method == "get") {
			correctURL += correctArgs;
			correctArgs = null;
		}
		var t = this;
		if (t.timeout) {
			t.timeoutID = setTimeout(function(){
				t.callback.apply({response:null,request:null,err:{reason:"timeout"}},t.callbackArgs);
				t.request.abort();
			},t.timeout);
		}
		this.request.onreadystatechange = function(){(function(){
			var complete = false;
			try {
				if (this.request.readyState == 4) {
					complete = true;
				}
				switch (this.callbackPhase) {
				case "complete":
					if (this.request.readyState == 4) {
						if (this.request.status == 200) {
							this.callback.apply({response:this.request.responseText,request:null,err:null},this.callbackArgs);
						} else {
							this.callback.apply({response:null,request:null,err:{reason:"!200",status:this.request.status}},this.callbackArgs);
						}
					}
				break;
				case "process":
					this.callback.apply({response:null,request:this.request,err:null},this.callbackArgs);
				break;
				}
			}
			catch (e) {complete = true;}
			if (complete) {
				this.timeout?clearTimeout(this.timeoutID):null;
			}
		}).call(t);};
		this.request.open(this.method, correctURL, true);
		(this.method == "post")?this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"):null;
		(!this.bCached)?this.request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT"):null;
		this.request.send(correctArgs);
		return true;
	}
}

/**
 * (c)2008 DriverX
 */