/* ****************************************************************************
**	Class: net
**
**	Summary: Class encapsulation of the Ajax httprequest code.  Holds all 
**					the standard code needed to facilitate a simple Ajax request 
**					to the server.
**
** ************************************************************************* */
var net = new Object();


/* *****************************************************************************
**	Name: READY_STATE
**
**	Type:
**			READY_STATE_UNINIT	
**			READY_STATE_LOADING
**			READY_STATE_LOADED
**			READY_STATE_INTERACTIVE
**			READY_STATE_COMPLETE
**
**	Summary:  These are the variables associated with the different states  
**				that our current request to the server are in.  Not every state  
**				will be seen when doing the onReadyState callback loop.  But basically 
**				the request proceeds when we reach the complete state.
**
** ************************************************************************** */
net.READY_STATE_UNINIT 			= 0;
net.READY_STATE_LOADING 		= 1;
net.READY_STATE_LOADED 			= 2;
net.READY_STATE_INTERACTIVE 	= 3;
net.READY_STATE_COMPLETE 		= 4;


/* *****************************************************************************
**	Name: net.ContentLoader
**
**	Inputs:	
**						url	=	The page we are using to process the httprequest
**
**						onLoad = The function that we created to process our ajax request
**									null gives us the default action of just return true;
**
**						onError = This may be null allowing for a default error alert 
**										to show, but custom error functions maybe passed in to 
**										make it customized.
**
**						method = The method used for the http request ex. POST, GET...
**
**						params = The string used to pass data through the connection 
**										via post.
**						
**						contentType = allowed to be null since it is set to a default 
**													later.  It tells the request what exactly is going 
**													on with this particular httprequest.
**
**						objectID = The id of the tag where we will finally insert the 
**										newly returned data from the processing page.  It is 
**										used within the onLoad function.
**
**						passObject = This is a multi use variable.  Pass in a Object to make
**										the overall functionality in the end flexable
**	
**	Return: None
**
**	Summary:  This is the inilizer for the Ajax Request.  It takes in several 
**					parameters that are needed to furnish the entire httprequest.  It 
**					stores the parameters as globals  allowing for use with all 
**					functions within the class.  Once all is initalized it initiates 
**					the call that starts the hand shaking and httprequest.
**
** ************************************************************************** */
net.ContentLoader = function ( 	url , onLoad , onError , method , 
										params , contentType , objectID ,
																			passObject){	
	
	this.url = url;
	this.req = null;
	// short hand if/else (<condition>)?<true>:<false>;
	this.onLoad = ( onLoad ) ? onLoad : this.defaultonLoad;

	this.passObject = passObject;
	// short hand if/else (<condition>)?<true>:<false>;
	this.onError = ( onError ) ? onError : this.defaultError;
	
	this.objectID = objectID;	
	this.loadXMLDocs( url , method , params, contentType );
}


/* *****************************************************************************
**	Name: net.ContentLoader.prototype
**
**	Summary:  This is where all the different functions within the net class
**				are located.
**
** ************************************************************************** */
net.ContentLoader.prototype = {
	
	
	/* ***************************************************************************
	**	Name: loadXMLDocs
	**
	**	Inputs:
	**						url	=	The page we are using to process the httprequest
	**
	**						method = The method used for the http request ex. POST, GET...
	**
	**						params = The string used to pass data through the 
	**										connection via post.
	**						
	**						contentType = allowed to be null since it is set to a default 
	**													later.  It tells the request what exactly is 
	**													going on with this particular httprequest.
	**	
	**	Return: None
	**
	**	Summary:  This is used to complete the setting up of our httprequest.  
	**					It picks the correct type of request to make.  XMLHTTPRequest 
	**					for non MS browsers.  and ActiveXObject("Microsoft.XMLHTTP") 
	**					for MS browsers.  It also goes about setting the default method,
	**					 and contentTypes up if they are null at this point.
	**
	** ************************************************************************ */
	loadXMLDocs : function( url , method , params , contentType ) {			
	
		if( !( method ) ){ 
			method = "POST";
		}
	
		if( contentType == null  && method == "POST"){
			contentType = "application/x-www-form-urlencoded";
		} 
	
		if( window.XMLHttpRequest ) {
			this.req = new XMLHttpRequest();			
		} else if ( typeof ActiveXObject != "undefined" ){
			this.req = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
		
		/* /////////////////////////////////////////////////////////////////////////
		//
		//	Beyond this point we assure ourselves that the request (this.req) is 
		//	setup.  If for some reason it isn't we have ourselves a browser that 
		//	doesn't support this functionality.  Due to this we must skip the next 
		//	block of code.  Otherwise we continue on intoactually facilitating the
		//	request to the server.
		// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		//
		// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		//	Inside the code block it uses a try/catch inorder to allow us to  
		//	see if any problems occur with the connection setup.
		//
		//  ///////////////////////////////////////////////////////////////////// */
	
		if( this.req ){
			
			try{
				var loader = this;										
				
				this.req.onreadystatechange = function(){loader.onReadyState.call( loader );}
				this.req.open(method , url , true);					
			
				if( contentType ){
					this.req.setRequestHeader( "Content-Type" , contentType );
				}
			
				this.req.send( params )			
			
			} catch ( err ){
				this.onError.call( this );
			}
			
		}
	},		


	/* ***************************************************************************
	**	Name: onReadyState
	**
	**	Inputs:	None
	**	
	**	Return: None
	**
	**	Summary: This is where the HTTPRequest waits for the server to respond 
	**					to the request.  It looks for the readystate of the request and 
	**					compares it to our states.  Once we hit the complete state we can 
	**					continue.  The next check is on the status of the request.  If it 
	**					returns with a everything is OK signal we continue onto our  
	**					loader function.  If sometype of error happened.  we must go about  
	**					doing our error handling function to notify our user of the 
	**					bad news.
	**
	**	Note: THIS IS A CALLBACK LOOP.  IT CALLS ITSELF OVER AND OVER TILL
	**				COMPLETION.
	**
	** ************************************************************************ */
	onReadyState : function(){
		var req = this.req;
		var ready = req.readyState;
		if(ready == net.READY_STATE_COMPLETE){
			var httpStatus = req.status;
			if(httpStatus == 200 || httpStatus == 0){				
				this.onLoad.call(this);
			} else {
				this.onError.call(this);
				
			}
		}
	},


	/* ***************************************************************************
	**	Name: defaultError
	**
	**	Inputs:	None
	**	
	**	Return: None
	**
	**	Summary: This is the default error function.  If no particular error 
	** 					procedure is defined on initialization, we use this one.  If 
	**					we do pass one in, this function is ignored.
	**
	** ************************************************************************ */
	defaultError : function(){
		alert( "ERROR fetching data!" + "\n\n readyState: " + this.req.readyState +
		"\nheaders: " + this.req.getAllResponseHeaders() );
	}, 
	
	
	/* *****************************************************************************
	**	Name: defaultonLoad
	**
	**	Inputs:	None
	**
	**	Return: None
	**
	**	Summary: onload event that does nothing.
	**
	** ************************************************************************** */
	defaultonLoad : function(){return true;}
	
} // End of Prototype section
