
//Constructor for Comm object.
HttpComm = function(paramData, wantText, onload, onerror)
{
	this.request	= null;
	this.paramData	= paramData;	
    this.wantText   = wantText;     //raw text as opposed to a DOM document.
	this.onError	= (onerror) ? onerror : this.defaultError;
	this.onLoad		= onload;
}

HttpComm.READY_STATE_UNINITIALIZED	= 0;
HttpComm.READY_STATE_LOADING		= 1;
HttpComm.READY_STATE_LOADED			= 2;
HttpComm.READY_STATE_INTERACTIVE	= 3;
HttpComm.READY_STATE_COMPLETE		= 4;

HttpComm.prototype=
{
	loadXMLDoc:function(url)
	{
		if(window.XMLHttpRequest)
			this.request = new XMLHttpRequest();
		else if(window.ActiveXObject)
			this.request = new ActiveXObject("Microsoft.XMLHTTP");

		if(this.request)
		{
			try
			{
				//Creates variable on the lexically scoped call stack...
				//will hang around as long as the function pointer.
				var loader = this;
				this.request.onreadystatechange = function()
				{
					loader.onReadyState.call(loader);					
				}
				
				this.request.open('POST', url, true);// + "?" + this.paramData, true);
				if(this.paramData != null)
				{
					this.request.setRequestHeader("Content-Type", "text/xml");
				}

				this.request.send(this.paramData);
			}
			catch(err)
			{
				this.onError.call(this);
			}
		}
	},
	
	loadPostDoc:function(url)
	{
		if(window.XMLHttpRequest)
			this.request = new XMLHttpRequest();
		else if(window.ActiveXObject)
			this.request = new ActiveXObject("Microsoft.XMLHTTP");

		if(this.request)
		{
			try
			{
				//Creates variable on the lexically scoped call stack...
				//will hang around as long as the function pointer.
				var loader = this;
				this.request.onreadystatechange = function()
				{
					loader.onReadyState.call(loader);					
				}
				
				this.request.open('POST', url, true);// + "?" + this.paramData, true);
				if(this.paramData != null)
				{
					this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    this.request.setRequestHeader("Content-length", this.paramData.length);
                    //this.request.setRequestHeader("Connection", "close");
				}

				this.request.send(this.paramData);
			}
			catch(err)
			{
				this.onError.call(this);
			}
		}
	},
	
	onReadyState:function()
	{
		var request = this.request;
		var ready	= request.readyState;

		if(ready == HttpComm.READY_STATE_COMPLETE)
		{
			var httpStatus = request.status;
			if(httpStatus == 200 || httpStatus == 0)
			{				
			    if(this.wantText)
			    {
			        this.returnData = request.responseText;			       
			    }
			    else
				    this.returnData = request.responseXML;	
									    
				this.onLoad.call(this);
			}
			else
				this.onError.call(this);
		}
	},
	
	defaultError:function()
	{
		var out = "Error fetching data!";
		out += "\n\nreadyState: " + this.request.readyState;
		out += "\nstatus: " + this.request.status;
		out += "\nheaders: " + this.request.getAllResponseHeaders();
		out += "\nresponse: " + this.request.responseText;
		alert(out);
	}
}