// Elements and ideas borrowed from http://www.learn-ajax-tutorial.com/Oop.cfm

// This always does a GET
function XMLDocRequest( caller, url, callback )
{
	this.request = null;
	this.url = url;
	this.caller = caller;
	this.callBack = callback;

	// Now make the request
	this.LoadAjax();
}

XMLDocRequest.prototype.LoadAjax = function()
{ 
	if ( this.request = this.CreateXHR() )
	{
		try
		{
			var loader = this;
			this.request.onreadystatechange = function() 
				{ loader.OnReadyState.call( loader ); }

			this.request.open( 'GET', this.url, true );
			this.request.send( null );
		}
		catch(exc)
		{}
	}
	else
	{}
}

XMLDocRequest.prototype.CreateXHR = function()
{
	// Native XMLHttpRequest object: FireFox, Safari, Mozilla, netscape
	var request = null;
	if (window.XMLHttpRequest)
		request = new XMLHttpRequest();

	// IE/Windows ActiveX version
	else if (window.ActiveXObject)      
		request = new ActiveXObject( "Microsoft.XMLHTTP" );

	return request;
}

XMLDocRequest.prototype.OnReadyState = function()
{
	if( (this.request.readyState == 4) || ( this.request.readyState == "complete" ))
	{
		if (this.request.status == 200) 
		{
			if (this.callBack) 
				this.callBack.call( this );
		}
	}
}

// decodes URL
URLDecode = function( content ) 
{
	// Replace + with ' '
	// Replace %xx with equivalent character
	// Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef"; 
	var encoded = content;
	var plaintext = "";
	
	var i = 0;
	while (i < encoded.length) 
	{
		var ch = encoded.charAt(i);
		
		if (ch == "+") 
		{
			plaintext += " ";
			i++;
		} 
		else if (ch == "%") 
		{
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) 
			{
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} 
			else 
			{
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} 
		else 
		{
			plaintext += ch;
			i++;
		}
	} // while
	
	return plaintext;
}

// In case you want a "Do nothing()" return
function DoNothing( theRequest ) 
{}


