/*****************************************************************************************************************
©2006. Platinum Synergy Group Inc.
ajax.js

Builds and handles an XMLHttpRequest Object.

Created on: Friday, November 17th, 2006
Created By: Derek Santos

XMLHTTPRequest READY STATES
-----------------------
    0, Uninitialized, open() has not yet been called
    1, Loading, send() has not yet been called
    2, Loaded, headers have returned and the status is available
    3, Interactive, responseText is being loaded with data
    4, Complete, all operations have finished 
	
Revision History:

********************************************************************************************************************/

/* -------------------
	Constructor
   ------------------- */
function AjaxController()
{
	//this.sCode = "&"+jx+"="+document.getElementById(jx).value;
	this.httpRequest = this.CreateHttpRequest();
}

//Attribute which contains the xmlHttpRequest Object.
AjaxController.prototype.httpRequest;
AjaxController.prototype.response;
AjaxController.prototype.sCode;


/* --------------------------------------------------------------------
	Creates and returns a  new instance of an xmlHttpRequest Object.
	If it fails to create the object
   -------------------------------------------------------------------- */
AjaxController.prototype.CreateHttpRequest = function() 
{
	//Will store the reference to the XMLHttpRequest OBject.
	var xmlHttp = null;
	try
	{
		//This is for all browser which are non-IE
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		//Assume IE6 or older
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		//Try all versions until one works.
		for(var i=0; i < xmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	//Return the created XMLHttpRequest Object or return an erorr.
	if(!xmlHttp)
		alert("Error creating the XMLHttpRequest Object.");
	else
		return xmlHttp;	
}


/* -----------------------------------------------------------------------
	Executes a POST request and returns the response text.
	Params: url   - The URL in which to send a request too.
			data  - The POST data.
			async - Whether or not to perform the request asyncronously.
   ----------------------------------------------------------------------- */
AjaxController.prototype.InitPOST = function(url,data,async)
{
	var respone = null;
	var http = this.httpRequest;
	try
	{				
		if(http.readyState == 4 || http.readyState == 0)
		{			
			//Open Request
			http.open("POST", url, async);

			if(async){http.onreadystatechange = this.HandleResponse;}
			
			//Set request header to allow POST data to be sent.
			http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			
			http.send(data);//Send the request.				
			
			if(!async){this.HandleResponse();}					
			
		}			
	}
	catch(e)
	{
		alert(e.toString());
	}
}

/* -----------------------------------------------------------------------
	Executes a GET request and returns the response text.
	Params: url   - The URL in which to send the request too, 
	                includes GET variables
			async - Whether or not to perform the request asyncronously.
   ----------------------------------------------------------------------- */
AjaxController.prototype.InitGET = function(url,async)
{
	var respone = null;
	var http = this.httpRequest;
	try
	{				
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
		{	
			xmlHttp.open("GET", url, async);
			
			if(async){xmlHttp.onreadystatechange = this.HandleResponse;}
			
			xmlHttp.send(null);	
			
			if(!async){this.HandleResponse();}			
					
		}
	}
	catch(e)
	{
		alert(e.toString());
	}
}

/* ----------------------------------------------------------------------------------------
	Handles an asyncronous AJAX request. Sets response atrribute to request response text.
   ---------------------------------------------------------------------------------------- */
AjaxController.prototype.HandleResponse = function()
{
	var http = this.httpRequest;
		
	//When ready state is 4, proceed to parse server response.
	if(http.readyState == 4)
	{
		//continue HTTP status is "ok".
		if(http.status == 200)
		{	
			if(!http.responseText || http.responseText.length == 0)
			{
				alert("Server Error");
			}
			else
			{
			
				this.response = http.responseText;//Retrieve the response text.							
			//alert(this.response);
			}	
		}
	}
	
}