/* window.Request : Object-Oriented AJAX-based HTTP Request Method. */

window.Request = {
	"create"	: function(url,postdata,oncomplete,onerror,autoDelete){ return new this._obj(this,url,postdata,oncomplete,onerror,autoDelete); },
	"stop"		: function(request){ if(request&&request.type=="request") request.abort(); },
	"init"		: function(request,url){ if(request&&request.type=="request"){ request.get(url); } },
	"close"		: function(request){
		if(!request||request.type!="request")
			return false;
		try{ request.abort(); }catch(err){}
		for(var x=0; x<this._requests.length; x++)
			if(this._requests[x]==request)
				this._requests = this._requests.splice(x,1);
		try{ delete request; } catch(err){ request = null; }
	},
	"_obj"		: function(parent,url,postdata,oncomplete,onerror,autoDelete) {
		this.type = "request";
		this.parent = parent;
		this.url = url;
		this.postdata = (postdata && postdata.constructor && (postdata.constructor==Object || postdata.constructor==Function)) ? postdata : {};
		this.poststring = (postdata && typeof(postdata)==String) ? postdata : "";
		this.autoDelete = autoDelete!=false;
		this.data = null;
		this.async = true;
		this.http_code = null;
		this.status = null;
		this.responseText = null;
		this.responseXML = null;
		this.complete = false;
		this.oncomplete = oncomplete?oncomplete:function(){};
		this.onerror = onerror?onerror:function(){};
		this.request = this.parent.XMLHttpRequestObj();
		this.request.parent = this;
		this.request.onreadystatechange = function() {
			this.parent.readyState = this.readyState;
			if(this.readyState==4) {
				this.parent.http_code = this.status;
				this.parent.status = this.status;
				this.parent.responseText = this.responseText;
				this.parent.responseXML = this.responseXML;
				this.parent.data = this.responseText;
				this.parent.complete = true;
				this.parent.error = this.status!=200;
				if(this.status==200 && this.parent.oncomplete)
					try{ this.parent.oncomplete(); }catch(err){ window.setTimeout("throw(new Error(\"request.oncomplete > "+err.toString()+"\"));",1); }
				else if(this.status!=200 && this.parent.onerror)
					try{ this.parent.onerror(); }catch(err){ window.setTimeout("throw(new Error(\"request.oncomplete > "+err.toString()+"\"));",1); }
				if(this.parent.autoDelete==true)
					this.parent.parent.close(this);
			}
		};
		this.get = function(url){
			if(url!=null && url==url+"" && url.length>0)
				this.url = url;
			this._poststring = this.poststring?this.poststring:"";
			for(var x in this.postdata)
				this._poststring += "&" + escape(x) + "=" + escape(this.postdata[x]);
			if(this._poststring.indexOf("&")==0)
				this._poststring = this._poststring.substring(1,this._poststring.length);
			this.request.open(this._poststring.length>2?"POST":"GET", this.url, this.async);
			this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			try {
				this.request.setRequestHeader("Content-length", this._poststring.length);
				this.request.setRequestHeader("Connection", "close");
			}catch(e){}
			this.request.send(this._poststring.length>2?this._poststring:null);
			return this;
		};
		this.send = this.get;
		this.Send = this.get;
		this.Get = this.get;
		this.abort = function(){
			if(this.readyState!=4)
				return this;
			this._oldOnError = this.onerror;
			this.onerror = function(){};
			this.request.abort();
			this.onerror = this._oldOnError;
			try{ delete this._oldOnError; }catch(e){ this._oldOnError=null; }
			return this;
		};
		this.Abort = this.abort;
		this.cancel = this.abort;
		this.Cancel = this.abort;
		this.stop = this.abort;
		this.Stop = this.abort;
		this.parent._requests.push(this);
	},
	"XMLHttpRequestObj" : function() {
		var xmlHttp=null;
		try { xmlHttp=new XMLHttpRequest(); }
		catch (e) {
			try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		return xmlHttp;
	},
	"_requests" : new Array()
};
