//NAME: xHTTP.js
//AUTHOR: Philip Siedow-Thompson
//LAST-REVISION: 2-12-08
//DESCRIPTION: Used for AJAX scripting.
//					->ashttp: used for asynchronus HTTP request.


//Create Namespace
	var xHTTP = {};

	
	//NAME: convertForPost
	//DESCRIPTION: Converts an object into a string suitable to send in a post msg.
	//USAGE: Pass the function an object and returns a string.
	//ARGUMENTS: inOBJ (the object to be converted).
	xHTTP.convertForPost = function(inOBJ)
		{
			var retARRAY = [];
			var spageREGX = /%20/g;	//Replace %20 with ...;
			
			if(inOBJ)
				{
					for(var a in inOBJ)
						{
							var retARG = encodeURIComponent(a);
							var retVAL = encodeURIComponent(inOBJ[a]);
							var retSTR = retARG + "=" + retVAL;
							retARRAY.push(retSTR.replace(spageREGX, "+"));
						}
				}
			
			var retPOST = retARRAY.join('&');
			
			return retPOST;
		}
	
	
	
	
	//NAME: convertFromPost
	//DESCRIPTION: Converts a URL encoded string to an object.
	//USAGE: Pass the function a string and it returns and objects
	//ARGUMENTS: inSTR (the string to be converted).
	xHTTP.convertFromPost = function(inSTR)
		{
			var retOBJ = new Object();
			var pairARRAY = inSTR.split("&");
			
			for(var i = 0; i<pairARRAY.length; i++)
				{
					var argARRAY = pairARRAY[i].split("=");
					retOBJ[argARRAY[0]] = argARRAY[1];
				}
			return retOBJ;
		}
	
	
	
	
	
	//NAME: ashttp
	//DESCRIPTION: Allows for async http transfer
	//USAGE: Call new xHTTP.ashttp(arguments) to create this "class"
	//ARGUMENTS: inURL (the target url), inOBJ (the object that will specify what is sent)
	//				if no inOBJ is passed, then the requests defaults to GET. othewise the 
	//				method is POST.
	xHTTP.ashttp = function(inURL, inOBJ, inFUNC, inThis)
		{	
			//Returns an XMLHTTPREQUEST object.
			this.init = function()
				{
						//Try and create the XMLHttpRequest object
					var retHttp = null;
					
					try
						{retHttp = new ActiveXObject("Msxml2.XMLHTTP");}
					catch (e)
						{try
							{retHttp = new XMLHttpRequest();}
						 catch (e)
							{
								retHttp=new ActiveXObject("Microsoft.XMLHTTP");
							}
						}
						
					return retHttp;
				}

			
			
			//Create the XMLHTTPREQUEST OBJECT
			var curREQ = this.init();
			
			
			
			if(curREQ)	//Only Continue if we were able to create the request object
				{
				
				//Handles the Data and passes it out to a function which MUST
				//TAKE ONE ARG (string)
				curREQ.onreadystatechange = function()
					{
						if(curREQ.readyState==4)	//COMPLETE
							{	
								if(curREQ.statusText=="OK")		//OK
									{
										if(inFUNC)
											{
												if(!inThis)inFUNC(curREQ.responseText);
												else inFUNC.call(inThis, curREQ.responseText);
											}
									}
							}
					}
				
				//Send the MSG
				if(inOBJ)	//If the inOBJ is passed in then... POST
					{
						curREQ.open("POST", inURL, true);
						curREQ.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
						curREQ.send(xHTTP.convertForPost(inOBJ));
					}
				else		//If the inOBJ is not passed in then assume that this is a GET operation
					{
						curREQ.open("GET", inURL, true);
						curREQ.send(null);
					}
				}
		}
