
function HTTPRequest ()
{
	this.httpRequest = false;
	this.onRequestStart = function () {};
	this.onRequestFinish = function () {};
	this.onRequestAbort = function () {};
	
	if (window.XMLHttpRequest)
	{
		this.httpRequest = new XMLHttpRequest ();
		if (this.httpRequest.overrideMimeType)
		{
			//this.httpRequest.overrideMimeType ('text/xml');
		}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			this.httpRequest = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				this.httpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	
	var obj = this;
		
	this.finish = function ()
	{
		if (obj.httpRequest.readyState == 4)
		{
			if (obj.httpRequest.status == 200)
			{
				obj.returnValue = obj.onRequestFinish (obj.httpRequest.responseText);
			}
			else
			{
				obj.returnValue = false;
				obj.onRequestAbort ();
			}
		}
	};
	
	this.error = function ()
	{
		obj.onRequestAbort ();
	}
}

HTTPRequest.prototype.hasRequestObject = function ()
{
	return this.httpRequest != false;
}

HTTPRequest.prototype.execute = function (requestURI)
{
	this.onRequestStart ();
	
	// Abort when there is no valid request object:
	if (!this.hasRequestObject ())
	{
		this.onRequestAbort ();
		return;
	}
	
	// Set the callback:
	this.httpRequest.onreadystatechange = this.finish;
	this.httpRequest.onerror = this.error;
	
	// Modify the URL to include the current cookie (for security):
	// (http://getahead.ltd.uk/blog/joe/2007/01/01/csrf_attacks_or_how_to_avoid_exposing_your_gmail_contacts.html)
	if (requestURI.indexOf ("?") >= 0)
		requestURI += '&';
	else
		requestURI += '?';
	requestURI += 'cookie=';
	requestURI += escape (document.cookie);
	
	// Perform the query:
	this.httpRequest.open ('GET', requestURI, true);
	this.httpRequest.send (null);
}

HTTPRequest.prototype.executeBlocking = function (requestURI)
{
	this.onRequestStart ();

	// Abort when there is no valid request object:
	if (!this.hasRequestObject ())
	{
		this.onRequestAbort ();
		return false;
	}

	// Set the callback:
	this.httpRequest.onreadystatechange = this.finish;
	this.httpRequest.onerror = this.error;

	if (requestURI.indexOf ("?") >= 0)
		requestURI += '&';
	else
		requestURI += '?';
	requestURI += 'cookie=';
	requestURI += escape (document.cookie);

	this.httpRequest.open ('GET', requestURI, false);
	this.httpRequest.send (null);

	if (this.httpRequest.status == 200)
		this.finish ();
	else
		this.error ();

	return this.returnValue;
}

HTTPRequest.prototype.abort = function ()
{
	if (!this.hasRequestObject ())
		return;
		
	this.httpRequest.abort ();
};
