var net = new Object();

net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

//ContentLoader constructor
net.ContentLoader = function(url, onload, onerror, method, params, contentType, propertyName)
{
	//set ContentLoader's property variables
	this.propertyName = propertyName;
	this.url = url;
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	
	//call ContentLoader.loadXMLDoc()
	this.loadXMLDoc(url, method, params, contentType);
}//end net.ContentLoader()

//This block of code defines the methods of the ContentLoader object
//The ".prototype" modifier specifies that all methods contained within the block of code will be inherited by all subsequent instances of the ContentLoader object
net.ContentLoader.prototype = 
{
	//This method loads the XMLHttpRequest object as the "req" memeber variable of the ContentLoader object
	loadXMLDoc:function(url, method, params, contentType)
	{
		if(!method)
		{
			method = "GET";
		}//end if
		
		if(!contentType && method == "POST")
		{
			contentType = "application/x-www-form-urlencoded";
		}//end if
		
		//Mozilla
		if(window.XMLHttpRequest)
		{
			this.req = new XMLHttpRequest();
		}//end if
		//IE
		else if(window.ActiveXObject)
		{
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}//end else if
		if(this.req)
		{
			try
			{
				var loader = this;
				
				//set the function to call (onReadyState()) when the state of the XMLHttpRequest changes (i.e when responses are sent back from the server)
				this.req.onreadystatechange = function() 
				{
					loader.onReadyState.call(loader);
				}//end this
				
				//open the XMLHttpRequest object with the specified parameters. This must happen before it sent to the server
				this.req.open(method, url, true);
				
				//if a specific content type has been specified, then set it in the XMLHttpRequest object
				if(contentType)
				{
					this.req.setRequestHeader("Content-Type", contentType);
				}//end if
				
				//send the XMLHttpRequest object to the specified URL with some parameters
				this.req.send(params);
			}//end try
			catch(err)
			{
				this.onerror.call(this);
			}//end catch
		}//end if
	},
	//This is the method that will be called when the state of the XMLHttpRequest object changes
	onReadyState:function()
	{
		var req = this.req;
		var ready = req.readyState;
		
		if(ready == net.READY_STATE_COMPLETE)
		{
			var httpStatus = req.status;
			//alert(httpStatus);
			//call the JavaScript function specified by the onload member variable if the correct HttpStatus is returned
			if(httpStatus == 200)
			{
				this.onload.call(this);
			}//end if
			//call the method specified by the onerror member variable if the HttpStatus is not 200 or 0
			else if (httpStatus == 0)
			{
				this.onerror.call(this);
			}//end else
			else
			{
				this.onerror.call(this);
			}
		}//end if
	//	if the state of the XMLHttpRequest object is not "Complete" (ie "Loading" or "Loaded"), then change the loading element's appearance to reflect that
		else
		{
			//document.getElementById(this.propertyName + "Div").className = "loading";
			
		}//end else
	},
	//This method displays an alert if there is a problem with the ContentLoader getting a response back from the server.
	defaultError:function()
	{
		alert("error fetching data!"
			+"\n\nreadyState:" + this.req.readyState
			+"\nstatus:" + this.req.status
			+"\nheaders:" + this.req.getAllResponseHeaders());
	}
}//end prototype
