/* ---------------------------------------
 * author	: eric thul
 * date		: 2005.08.02
 * title	: Request.js
 * version	: 0.2
 *
 * this script handles the retrieval of 
 * xml through the php proxy script, which
 * contacts delphi to run a perlscript that
 * sends the xml.
 *
 * i know this seems a little odd, but the
 * way the xmlhttp request object works, is
 * it does not allow remote scripts to be
 * opened. in other words, the script send-
 * ing the xml must be on the same server
 * as this javascript.
 *
 * for another option, check out:
 * http://www.mozilla.org/projects/security/
 *   components/signed-scripts.html
 *
 * note that the following code has been
 * adapted from:
 * http://www.sitepoint.com/blog-post-view.php?id=171725
 * many thanks! also check out for more info:
 * http://jpspan.sourceforge.net/wiki/doku.php?
 *   id=javascript:xmlhttprequest:snippets:callback
 * --------------------------------------- */

/* constructor */
function Request() {};

/* create a closure, which is a hash
 *   of keys that either contain a value 
 *   or a function pointer 
 */
Request.prototype = {
	/* url for the proxy to allow remote server access */
	proxy: null,
	/* xml http request object */
	req: null,
	/* query to hold the user req string */
	query: null,
	/* method to initialize xml http req object */
	init: function(proxy)
	{
		try {
			if (window.XMLHttpRequest)
				this.req = new XMLHttpRequest();
			else if (window.ActiveXObject)
				this.req = this.ieversionxml();
		}
		catch (e) {
			this.req = false;
			alert("!!!error: "+e);
		}
		this.proxy = proxy;
	},
	/* goes through msxml versions to find correct one */
	ieversionxml: function()
	{
		/* this wonderful code is from:
		 *   http://jpspan.sourceforge.net/wiki/doku.php?
		 *   id=javascript:xmlhttprequest:snippets:callback
		 * big Thank You!
		 */
		var msxml = new Array (
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP'
		);
		for (i = 0; i < msxml.length; i++)
		{
			try {
				return new ActiveXObject(msxml[i]);
			}
			catch (e) {}
		}
	},
	/* makes calls to the server and sets handlers */
	loadXMLDoc: function(handler,query)
	{
		this.handler = handler;
		this.query = query;
		this.req.open("GET",this.proxy+this.query,true);
		this.req.send(null);
		var self = this;
		this.req.onreadystatechange = function() {
			self._stateCallback(self);
		};
	},
	/* internal method to call user's state handles */
	_stateCallback: function(self)
	{
		/* 0 = uninitialized (onUninit)
		 * 1 = page is loading (onLoading)
		 * 2 = page has loaded (onLoaded)
		 * 3 = page is interactive to the user (onInteractive)
		 * 4 = page is complete  (onComplete)
		 */
		if (self.req.readyState == 0)
		{
			if (self.handler.onUninit)
				self.handler.onUninit(self.req.reponseXML,self.query);
		}
		else if (self.req.readyState == 1)
		{
			if (self.handler.onLoading)
				self.handler.onLoading(self.req.responseXML,self.query);
		}
		else if (self.req.readyState == 2)
		{
			if (self.handler.onLoaded)
				self.handler.onLoaded(self.req.responseXML,self.query);
		}
		else if (self.req.readyState == 3)
		{
			if (self.handler.onInteractive)
				self.handler.onInteractive(self.req.responseXML,self.query);
		}
		else if (self.req.readyState == 4)
		{
			/* 200 = all is groovy */
			if (self.req.status && self.req.status == 200 && self.handler.onComplete)
				self.handler.onComplete(self.req.responseXML,self.query);
		}
		else
		{ }
	}
};
