
var STR_IDLE = "Click submit to add message ...";
var STR_PROC = "Processing message ...";
var STR_BUSY = " waiting for server ...";
var STR_CHEK = "Checking server for data ...";

var READY_STATE_uninitialized 	= 0;
var READY_STATE_loading 		= 1;
var READY_STATE_loaded 			= 2; 
var READY_STATE_interactive 	= 3;
var READY_STATE_complete 		= 4;

var RESPONSE_OK					= 200;
var RESPONSE_NOT_FOUND			= 404;

function AjaxMgr(strMethod, bAsync)
{
	this.m_FnProcessXmlDocument = null;
	this.m_FnStatusStringCallback = null;
	this.m_FnHandleServerResponse = null;
	this.m_FnOnComplete = null;
	this.m_FnOnBusy = null;
	this.m_FnProcessXmlString = function(xmlStr) {return false;} // default - need to explicitly set
	
	this.m_GetString = new String();
	this.m_bAsync = bAsync;
	this.m_strMethod = strMethod;

	this.createXmlHttpRequestObject = function()
	{  
		// will store the reference to the XMLHttpRequest object
		var xmlHttp;
		
		if(window.ActiveXObject) // if running Internet Explorer
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) 
			{
	  			xmlHttp = false;
			}
		}
		else // if running Mozilla or other browsers
		{
			try 
			{
	  			xmlHttp = new XMLHttpRequest();
			}
			catch (e) 
			{
				xmlHttp = false;
			}
		}
		
		// return the created object or display an error message
		if (!xmlHttp)
			alert("Error creating the XMLHttpRequest object.");
		else 
			return xmlHttp;
	}
	
	this.xmlHttpReq = this.createXmlHttpRequestObject();

	this.SetCallbacks = function(fnHandleServerResponse, fnProcessXmlCallback, fnStatusStringCallback, fnOnComplete, fnOnBusy)
	{
		this.m_FnProcessXmlDocument = fnProcessXmlCallback;
		this.m_FnStatusStringCallback = fnStatusStringCallback;
		this.m_FnHandleServerResponse = fnHandleServerResponse;
		this.m_FnOnComplete = fnOnComplete;
		this.m_FnOnBusy = fnOnBusy;
	}

	this.loadXMLnonIE = function(xmlDomDoc, strXML) 
	{	
		//create a DOMParser
		var objDOMParser = new DOMParser();
		//create new document from string
		var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
		//make sure to remove all nodes from the document
		while (xmlDomDoc.hasChildNodes())
		{
			xmlDomDoc.removeChild(xmlDomDoc.lastChild);
		}
		//add the nodes from the new document
		for (var i=0; i < objDoc.childNodes.length; i++) 
		{            
			//import the node
			var objImportedNode = xmlDomDoc.importNode(objDoc.childNodes[i], true);
			//append the child to the current document
			xmlDomDoc.appendChild(objImportedNode);
		}
	}
		

	this.CreateXmlDoc = function(strXmlString)
	{
		// strip down to correct xml
		if (window.ActiveXObject)
		{
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc.loadXML(strXmlString);
			return xmlDoc;
		}
		// code for Mozilla, etc.
		else if (document.implementation &&	document.implementation.createDocument)
		{
			var xmlDoc = document.implementation.createDocument("", "root", null);
			this.loadXMLnonIE(xmlDoc, strXmlString);
			return xmlDoc;
		}
		else
		{
			alert('No XML DOM available. Please update or switch browser.');
		}
	}		
	

	// make asynchronous HTTP request using the XMLHttpRequest object 
	this.SendRequest = function()
	{
		var bRequestSent = false;
		// proceed only if the xmlHttp object isn't busy
		if (this.xmlHttpReq.readyState == READY_STATE_complete || 
			this.xmlHttpReq.readyState == READY_STATE_uninitialized)
		{
			this.xmlHttpReq.open(this.m_strMethod, this.m_GetString, this.m_bAsync);  
			// define the method to handle server responses
			this.xmlHttpReq.onreadystatechange = this.m_FnHandleServerResponse;
			// make the server request
			this.xmlHttpReq.send(null);
			
			bRequestSent = true;
		}
		else
		{
			this.OnBusy();
		}
		
		return bRequestSent;
	}
	
	this.OnComplete = function()
	{				
		if (this.m_FnOnComplete != null)
		{
			this.m_FnOnComplete();
		}
	}

	this.OnBusy = function()
	{				
		if (this.m_FnOnBusy != null)
		{
			this.m_FnOnBusy();
		}
	}

	// executed automatically when a message is received from the server
	this.handleServerResponse = function() 
	{
		// move forward only if the transaction has completed
		if (this.xmlHttpReq.readyState == READY_STATE_complete) 
		{
			// status of 200 indicates the transaction completed successfully
			if (this.xmlHttpReq.status == RESPONSE_OK) 
			{
				// if the client doesn't want the string, convert to a document
				if ( !this.m_FnProcessXmlString(this.xmlHttpReq.responseText) )
				{
					// extract the XML retrieved from the server
					var xmlDocumentElement = this.CreateXmlDoc(this.xmlHttpReq.responseText);
					
					if (this.m_FnProcessXmlDocument != null)
					{
						this.m_FnProcessXmlDocument(xmlDocumentElement);
					}
				}
				
				this.OnComplete();				
			} 
			// a HTTP status different than 200 signals an error
			else 
			{
				alert("There was a problem accessing the server: " + this.xmlHttpReq.statusText);
			}
		}
	}
}

