
/* XQuery Object Constructor
 */
function XQuery( tar )
{
	// Server-Side Request Target
	this.xmlurl = tar;
	// Request parameters
	this.params = {};
	// XMLHTTP Object, to handle the Request.
	this.reqobj = null;
};

/* Set a http Request variable
 */
XQuery.prototype.setparam = function( _name, _value )
{ this.params[_name] = escape(_value); }

/* Fire Away Request.
	_method = GET || POST
 */
XQuery.prototype.send = function( _method )
{
	var e, i, j = 0, t = this;
	
	// Compose http Request String
	for( var i in this.params )
	{
		this.xmlurl += j==0 ? '?' : '&';
		this.xmlurl += i + '=' + this.params[i];
		j++;
	}
	//alert(this.xmlurl);
	
	// Construct an XMLHTTP Object, to handle the Request.
	if( window.XMLHttpRequest )
	{
		// Crreate a XMLHttpRequest Object for Mozilla, Safari,...
		this.reqobj = new XMLHttpRequest();
		if( this.reqobj.overrideMimeType ) this.reqobj.overrideMimeType ('text/xml');
	}
	else if( window.ActiveXObject )
	{
		// Create an ActiveX Object for Internet Explorer browser
		// First Attempt
		try
		{
			this.reqobj = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch( e )
		{
			// second attempt
			try
			{
				this.reqobj = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			catch( e ){}
		}
	}

	if ( !this.reqobj )
	{
		// XMLHTTP instance could not be created
		// Call Failure function
		this.oops( -1, 'XML HTTP Request Object could not be created' );
	}
	
	// Define function to fetch response:
	// An anonymous function calls back into our class,
	// and allows the response to be handled in the right scope.
	this.reqobj.onreadystatechange = function(){t.onData()};
	// Open a connection
	this.reqobj.open( _method, this.xmlurl, true );
	// Send the request
	this.reqobj.send( null );
	
};

/* Validate & catch Response(s) sent back from Server-Side Application
 */
XQuery.prototype.onData = function()
{
	// If Request has completed
	if( this.reqobj.readyState == 4 )
	{
		// and server returns valid status (200)
		// (not 404, e.g.)
		if( this.reqobj.status == 200 )
		{
			// The Request has been completed successfully.
			// get Response Node (root) from XML
			if( this.onComplete != null ) this.onComplete(this.reqobj);
		}
		else
		{
			// A problem occured with the request
			this.oops( this.reqobj.status, this.reqobj.statusText );
		}
	}
	// Request has not been completed
	else{}
};

/* Handle errors/failures in Request, and invoke call to this.onError()
 */
XQuery.prototype.oops = function( _code, _msg )
{
	// Oops!
	// alert( '(XQuery) Oops!\n---------------------\n' + _code + '\n' + _msg );
	// Invoke call to this.onError()
	if( this.onError != null ) this.onError();
};

/* UDF; User Defined Functions
 */
XQuery.prototype.onComplete = null;
XQuery.prototype.onError = null;

/* Extract HTML chunk from Request's Object
	(Request.ResponseText Request.ResponseXML)
 */
function xq_html_chunk(r)
{
	// get the <html_chunk> node
	var c = r.responseXML.getElementsByTagName('html_chunk');
	
	// if found, return the contents of that node
	if(c) return c[0].firstChild.nodeValue;
	
	// if not, return failure
	return false;
}

