// eStylez_Comet_1.04.js
// By Lauren Smith, Information Systems Management
//
// Provides functionality for performing background XML requests, returning and handling the resulting data.
// Flexible for n-many simultaneous requests.  You may request Text (string) or XML object return types.
//
// To use this functionality:
// 1. Define a handler function which will recieve just one argument (the response XML or Text - your choice).
//		For example: function myHandler(strText) { alert('strText') }
// 2. Call the comet_GetResponse function via one of the wrapper functions, like this:
//		comet_GetXML('mypage.aspx?item=123XYZ', 'myHandlerFunctionName');		// sends an XML object to the handler function
//		or
//		comet_GetText('mypage.aspx?item=123XYZ', 'myHandlerFunctionName');	// sends a string to the handler function
//
// Notes:
// 1. Handler function will recieve a "null" if there is a problem with the request.
// 2. Required: Javascript 1.2
// 3. Platform compatibility: Windows - IE 5+, Firefox (all), Netscape 6+
//
// Todo: if you get something besides a 200 back from the server, call the handler function with null


//============================================================================= GLOBAL VARS

comet_RequestList = new Array();
comet_RequestHandlerList = new Array();


//============================================================================= FUNCTIONS

function comet_GetXML(strURL, strHandlerFunction)
// Passes the response XML to the designated handler function.
{
	comet_GetResponse(strURL, strHandlerFunction, "XML")
}


function comet_GetText(strURL, strHandlerFunction)
// Passes the response Text to the designated handler function.
{
	comet_GetResponse(strURL, strHandlerFunction, "Text")
}


function comet_GetResponse(strURL, strHandlerFunction, strResponseType)
// Function sets up our HttpRequest object (differently for IE than everyone else).
// We store the response objects and their associated handler functions in global arrays.
{
	// 1. Instantiate request object and append it to the array
	if (window.XMLHttpRequest)		// non-IE
	{
		comet_RequestList.push( new XMLHttpRequest() );
	}
	else if (window.ActiveXObject)		// IE
	{
		var oRequest = new ActiveXObject("Microsoft.XMLHTTP")
		
		if (oRequest)			// Did the object actually get created (possible permissions denial)
		{
			comet_RequestList.push(oRequest);
		}
	}

	// 2. Configure the request object
	comet_RequestHandlerList.push(strHandlerFunction);
	var i = comet_RequestList.length - 1;
	eval('f = function() { comet_RunHandler(' + i + ', "' + strResponseType + '"); }');

	comet_RequestList[i].onreadystatechange = f;
	comet_RequestList[i].open('GET', strURL, true);
	comet_RequestList[i].send(null);
}


function comet_RunHandler(i, strResponseType)
// Function is called from comet_GetResponse.  Handles the repeated checking of the
// response state and status.  Once it gets a response back, it gives it to the
// user-designated handler function.
{
	if (window.comet_RequestList[i])		// Is this a valid index in the request array?
	{
		if (comet_RequestList[i].readyState == 4)		// Is the request ready?
		{
			if (comet_RequestList[i].status == 200)		// Did the request return with success?
			{
				if (window.comet_RequestHandlerList[i])		// Do we have a handler defined for this request?
				{
					switch(strResponseType.toUpperCase())		// Which response type do we need to pass to the handler function?
					{
						case 'XML':		self[comet_RequestHandlerList[i]](comet_RequestList[i].responseXML); break
						case 'TEXT':	self[comet_RequestHandlerList[i]](comet_RequestList[i].responseText); break
						default:			self[comet_RequestHandlerList[i]](null);		// Return type not specified.
					}
				}
				else
				{
					self[comet_RequestHandlerList[i]](null);
				}
			}
		}
	}
}