
_file_XQ = true;

if (typeof _file_prototype == "undefined")
  {
  	alert ("xq.js: you must include the file 'prototype.js')");
  }
  
	
  
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */

	function XQ (workerURL)
	/*
		Initialize the JobQueue interface
		INPUTS:
			baseURL : the URL of the folder where the worker page resides
	*/
	{
		this._workerURL = workerURL;
	}
	
/* --------------------------------------------------------------------------------------------------- */

	XQ.prototype.Load = function (worker, parameters, callback, userData)
	/*
		new-style data loader
		INPUTS:
			worker     : the name of the worker to invoke (e.g. "security")
			parameters : an associative array containing the data to send to the worker
			callback   : a pointer to the function which will receive the results of the call
					     the function should have the following API:
					   
						 onRequestCompleted (success, xw)
							where "success" is a boolean value (set to TRUE if the call succeeded)
								  "xw" is a pointer to the XWrapper object which made the call
			userData   : a user-defined value which will be attached to the xwrapper when it completes
	*/
	{
		if (typeof userData == "undefined") {userData = "";}
		parameters['request'] = worker;		
		var xw = new XWrapper(this._workerURL, parameters, callback, userData);
	}

/* --------------------------------------------------------------------------------------------------- */

	XQ.prototype.QueueRequest = function (request, requestType, callback, userData)
	/*
		old-style data loader
		INPUTS:
			request : a dictionary containing the keyword/value pairs which will be sent to the worker
			requestType : the name of the worker to invoke (e.g. "security")
			parameters : a string containing the path to the function which will be called when the request completes
			callback : a pointer to the function which will receive the results of the call
					   the function should have the following API:
					   
						onRequestCompleted (success, data, worker, userData)
							where "success" is a boolean value (set to TRUE if the call succeeded)
								  "data" is the text of the response, or an error code if the call failed
								  "worker" is the name of the worker who processed the request (deprecated)
								  "userData" is the user-supplied data which was submitted when the call was made
			userData : a user-defined value which will be passed back to the callback when the call completes
	*/
	{
	
		// Turn the request dictionary object into an associative array
		var pList = {};
		for (var q = 1; q <= request.Length(); q++)
		  {
			var fName = request.NthKey(q);
			var fValue = request.Lookup(fName);
			eval ("pList['" + fName + "'] = fValue");
		  }
		
		// Add the request & UserData elements to the parameter list
		pList["request"] = requestType;
		pList["callback"] = callback;
				
		// Create the asynchronous request object
		 new XWrapper(this._workerURL, pList, jqGlue, userData);

	}


	function jqGlue (success, xw)
	{
		var cb = xw.getParamValue("callback");
		if (eval(cb))
		  {
		  	cb = eval(cb);
		  	cb(success, (success) ? xw.getResponseText() : (xw.getStatusCode() + " " + xw.getStatusText() + " [" + xw.getURL() + "]"), "xq", xw.getUserData());
		  }
	}

/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
	
	function XCache (xSocket)
	/* Use this object to save the results of an XMLHTTP request
		INPUTS: 
			xSocket : the XMLHTTP object which made the request
	 */
	{
		this.status = xSocket.statusCodeIn;
		this.statusText = xSocket.statusTextIn;
		this.responseText = xSocket.responseTextIn;
		this.responseXML = xSocket.responseXMLIn;
	}
	XCache.prototype.getStatusCode = function () {return this.status;}
	XCache.prototype.getStatusText = function () {return this.statusText;}
	XCache.prototype.getResponseText = function () {return this.responseText;}
	XCache.prototype.getResponseXML = function () {return this.responseXML;}

/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
	
	function XWrapper (url, pList, callback, userData)
	/* Use this object to make an asynchronous request for data
		INPUTS:
			url : the URL to load
			pList : a hash containing the parameters to send to the URL via a form post
					(e.g. {foo : "bar", spam : "nuts";})
			callback : a pointer to the function which will receive the results of the call
					   the function should have the following API:
					   
						onRequestCompleted (success, xw)
							where "success" is a boolean value (set to TRUE if the call succeeded)
								  "xw" is a pointer to the XWrapper object which made the call
	
	 */
	{
		this._uid = this.NextID();
		this._url = url;
		this._xSocket = null;
		this._callback = callback || null;
		this._userData = userData || "";
		this._parameters = pList || {};
		this._socket= new Ajax.Request (url,
										  {
											method:'post',
											parameters: pList,
											onComplete: function(transport)
													   {
													   	  this._xSocket = transport;
														  this._callback((transport.status == 200), this);
													   }.bind(this)
										  }
										);
	}
	
	XWrapper.prototype.uid = 1;
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getCallback = function () {
		return this._callback;
	}

/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.NextID = function() {
		return XWrapper.prototype.uid++;
	}
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getUID = function()
	/* Return the ID of the request object */ 
	{return this._uid;}
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getURL = function() 
	/* Return the URL which was loaded */
	{return this._url;}
	
/* --------------------------------------------------------------------------------------------------- */
	
	XWrapper.prototype.getParameterList = function ()
	/* Return the associative array containing the parameter which were passed when the page was loaded */
	{return this._parameters;}

/* --------------------------------------------------------------------------------------------------- */
	
	XWrapper.prototype.getUserData = function ()
	/* Return the user-supplied key which can be used to identify the request */
	{return this._userData;}

/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getParamValue = function (pName) 
	/* Return the value of the given parameter, or null if no such parameter exists */
	{
		var result = null;
		try {result = this._parameters[pName];} catch (exception) {/* no such parameter*/}
		return result;
	}
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getStatusCode = function ()
	/* Return the status code which was returned when the call was made */ 
	{return this._xSocket.status;}
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getStatusText = function () 
	/* Return the status text which was returned when the call was made */ 
	{return this._xSocket.statusText;}
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getResponseText = function () 
	/* Return the content of the page which was loaded */ 
	{return this._xSocket.responseText;}
	
/* --------------------------------------------------------------------------------------------------- */

	XWrapper.prototype.getResponseXML = function () 
	/* return the content of the page as an XML DOM document */
	{return this._xSocket.responseXML;}

/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */

