/*********************************************************************************************************************************/
/* Name: Ajax Handler                                                                                                            */   
/* Purpose: Utility class to handle all Ajax Requests. The class executes a page and returns the reponse to a call back function */
/* Author: Teckfusion Systems <info@teckfusionsystems.com>                                                                       */
/* Copyright: Bobby                                                                                                              */
/* Creation Date: Dec 21, 2006                                                                                                   */
/* Revision History:                                                                                                             */
/**/
/********************************************************************************************************************************/

/***********************************************************************************************************/
/* Name: Constructor                                                                                       */
/* Purpose: Constructor for the AjaxHandler Class                                                          */
/* Parameters: 																							   */
/**********************************************************************************************************/

AjaxHandler = function()
{
	this.objRequestHandler = this.createXMLHttpRequest();  //For  Creating XMLHTTPRequest Oblect
	if(!this.objRequestHandler)
	{
		this.raiseError('Unable to create Ajax object');  //For  Error Handling
	}
}
/****************************************************************************************************************************/
/* Name: setTarget                                                                                       					*/
/* Purpose: Set the target of response			                                                           					*/
/* Parameters: 	
				Name: Call Response Function Type: String Purpose: Instance variable to store the response of HttpResponse  */
/****************************************************************************************************************************/

AjaxHandler.prototype.setTarget = function(strCallBackFunction)
{
	this.strCallBackFunction = strCallBackFunction;
}
/****************************************************************************************************************************/
/* Name: createXMLHttpRequest                                                                                       					*/
/* Purpose: Set the target of HttpRequest			                                                           					*/
/* Parameters: 	
				Name: Call createXMLHttpRequest Function Type: String Purpose: Instance variable to store the response of HttpResponse  */
/****************************************************************************************************************************/

AjaxHandler.prototype.createXMLHttpRequest = function() 
{
	var objRequestObject;
	if(window.XMLHttpRequest) 
	{
		try 
		{
		  objRequestObject = new XMLHttpRequest();
		} 
		catch(e) 
		{
		  objRequestObject = false;
		}
	 } 
	 else if(window.ActiveXObject) 
	 {
		try 
		{
		  objRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(e) 
		{
		  objRequestObject = false;
		}
	  }
	  
	  return objRequestObject;
}


AjaxHandler.prototype.handleResponse = function() 
{	
	try
	{
		if(this.objRequestHandler.readyState == 4)
		{
			var strResponse = this.objRequestHandler.responseText; 		
			var strFunctionToCall = this.strCallBackFunction + "('" + escape(strResponse) + "')";
			eval(strFunctionToCall);
		}
	}
	catch(Exception)
	{
		this.raiseError('Error:' + Exception.description); 
	}
}

AjaxHandler.prototype.sendRequest = function(url) 
{ 
	try
	{
		/*var inhtml='<div id="showindicator" align="center"  style="font:Arial; font-size:12px"><br><img name="picLoad" id="picLoad" src="images/indicator.gif"></div>';     
		var strFunctionToCall = this.strCallBackFunction + "('" + escape(inhtml) + "')";
		eval(strFunctionToCall);*/
	
		var objCurrentObject = this;	
		this.objRequestHandler.open('get', url);
		this.objRequestHandler.onreadystatechange = function() {objCurrentObject.handleResponse();};
		this.objRequestHandler.send(null);
	}
	catch(Exception)
	{
		this.raiseError('Error:' + Exception.description); 
	}
}

AjaxHandler.prototype.raiseError = function(strMessage)
{
	var objError = new ErrorHandler(strMessage);	
	objError.DisplayAlert();
}
