<!--
// ajax.js
// (c) 2007 ISoftData Systems, Inc.
// Brian Roy
// Last modified: 5/30/07

// This library contains a base Javascript library for Ajax
//  for the AU3 project.

// AU3_createXMLHttpRequestObject()
// Input: None
// Return: an XMLHttpRequestObject
// This function creates an XMLHttpRequestObject appropriately for the
//	client browser.  It both returns a handle to the object and assigns
//  the object to the container's XMLHttpRO parameter
function AU3_createXMLHttpRequestObject()
{
	var XHRO = false;

	// Test if the browser supports XMLHttpRequest (non-IE)
	if (window.XMLHttpRequest)
	{
		XHRO = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// create an object for IE browsers
		try
		{
			// attempt to use a Javascript 5 object
			XHRO = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch (e)
		{
			// the client is an older version of IE, use the old object
			XHRO = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
	}
	// clear the variables attached to this object
	this.busy = false;
	this.url = false;

	return XHRO;
}

// AU3_openXMLHttpRequestObject( URL[, method[, username[, password]]] )
// Input: A URL to open, optionally, the method used to pass data
//			If no method is defined, POST is used by default
//			Also, the username and password to use to connect to the
//			server are optionally provided.  By default these parameters remain
//			undefined.
// Output: None
// Return: true on success, false on failure
// This function performs OPEN on the local XMLHttpRequestObject.  It
//	must therefore support most of the parameters accepted for OPEN, however,
//	it provides logical defaults and assumes that the connection is 
//	asynchronous.
function AU3_openXMLHttpRequestObject( URL, method, username, password )
{
	var retVal = false;
	// fail out if no request object exists or if it is busy
	if (this.XMLHttpRO && !this.busy)
	{
		// check method parameter and define default if necessary
		var method = (method === undefined) ? "POST" : method;

		// open the XMLHttpRequest Object with the specified params
		this.XMLHttpRO.open( method, URL, true, username, password );

		// If we're using POST, we should set a few more parameters
		if (method == "POST")
		{
			this.XMLHttpRO.setRequestHeader( "Content-type", 
					"application/x-www-form-urlencoded; charset=UTF-8" );
		}

		retVal = true;
	}
	return retVal;
}

// AU3_sendXMLHttpRequestObject( data )
// Input: data to send through the XMLHttpRequestObject
// Return: the value returned by the send method of the XMLHttpRequestObject
//			or FALSE if the object is busy or does not exist
// This function wraps the send method for the internal XMLHttpRequestObject
function AU3_sendXMLHttpRequestObject( data )
{
	var retVal = false;
	// only process the request if the object exists and is not busy
	if (this.XMLHttpRO && !this.busy)
	{
		this.busy = true;
		// additional POST request header
		if (data != null)
		{
			this.XMLHttpRO.setRequestHeader( "Content-Length", data.length );
		}
		// send the data using the built in method
		retVal = this.XMLHttpRO.send( data );
	}
	return retVal;
}

// AU3_abortXMLHttpRequestObject()
// Input: None
// Return: none
// This wraps the abort method for the internal XMLHttpRequestObject
function AU3_abortXMLHttpRequestObject()
{
	this.busy = false;
	this.XMLHttpRO.abort();
	this.XMLHttpRO = null;
}

//////////////////////////////////////////////////////
// Create the AU3Ajax container object
//////////////////////////////////////////////////////
function AU3Ajax( )
{
	// Do we provide additional debugging data
	this.debug = true;

	// variables for the XMLHttpRequestObject
	//	create the XMLHttpRequestObject on class creation
	this.XMLHttpRO = this.create();
	this.busy = false;

	// workaround for Javascript idiosyncracies
	var that = this;

	// user-defined direct onreadystatechange handling
	//	By and large, only onDataReceieved will be used.  onReadyStateChange
	//	is provided largely for debugging purposes
	this.onDataReceived = function(responseText){};
	this.onReadyStateChange = function(status, responseText){};

	// Add some basic onReadyStateChange handling
	this.XMLHttpRO.onreadystatechange = function()
	{
		var ajaxStatus;
		try
		{
			ajaxStatus = that.XMLHttpRO.status;
		}
		catch (e)
		{
			return;
		}
		// check the HTTP status response
		//	If it is not 200 (Successful transfer), and debug mode is on,
		//	display an alert.
		// 	A readyState of 4 means data download complete
		if ( that.XMLHttpRO.readyState == 4 )
		{
			that.busy = false;
			if (that.debug && that.XMLHttpRO.status != 200)
			{
				alert( 'HTTP Status Received: ' + that.XMLHttpRO.status );
			}

			// check if the data was received successfully
			// A readyState of 4 means data download complete
			// An HTTP Status of 200 means there were no problems
			if (that.XMLHttpRO.status == 200)
			{
				// call user defined onDataReceived function
				that.onDataReceived( that.XMLHttpRO.responseText );
			}
		}
		// call the user-defined onReadyStateChange handling
		that.onReadyStateChange( ajaxStatus, 
				that.XMLHttpRO.responseText );
	}
}
//////////////////////////////////////////////////
// static methods for the AU3Ajax object which are based on
//	XMLHttpRequestObjects
//////////////////////////////////////////////////
AU3Ajax.prototype.open = AU3_openXMLHttpRequestObject;
AU3Ajax.prototype.send = AU3_sendXMLHttpRequestObject;
AU3Ajax.prototype.abort = AU3_abortXMLHttpRequestObject;
AU3Ajax.prototype.create = AU3_createXMLHttpRequestObject;


//-->
