/*****************************  XmlHttpRequest  *****************************

Author:	        Luke Breuer, labreuer+xmlhttprequest@gmail.com
Created:        4/19/05
Documentation:  http://luke.breuer.com/xmlhttprequest.aspx

If you use this code without significantly modifying it, I require that you
maintain a link to http://luke.breuer.com/xmlhttprequest.aspx in this
file and that you include my name and contact information in this file.
*/

function XmlHttpRequest(successCallback, failureCallback)
{
	this.SuccessCallback = successCallback;
	this.FailureCallback = failureCallback;	
	this.Request = XmlHttpRequest.GetXmlHttpObject();
	
	if (this.Request == null)
	{
		if (XmlHttpRequest.prototype.ErrorReported != true)
		{
			alert("XmlHttpRequest doesn't seem to work!");
			XmlHttpRequest.prototype.ErrorReported = true;
		}
		
		return;
	}
	
	this.Active = false;
}

XmlHttpRequest.prototype.OnHttpRequest = function(_this)	
{
	if (_this.Request.readyState == 4 && _this.SuccessCallback != null)
	{
		_this.Active = false;
		
		if (200 <= _this.Request.status && _this.Request.status < 300)
			_this.SuccessCallback(_this.Request.responseText);   
		else if (_this.FailureCallback != null)
			_this.FailureCallback(_this.Request.responseText);
	}
};	

XmlHttpRequest.prototype.PerformGet = function(url, asynchronous)
{
	// required for the closure below
	var _this = this;

	if (asynchronous == null)
		asynchronous = false;
	
	if (this.Active)
		this.Request.abort();
		
	this.Request.onreadystatechange = function() { XmlHttpRequest.prototype.OnHttpRequest(_this); };		
	this.Request.open('GET', (url.indexOf('?') < 0 ? '?' : '') + url, asynchronous);	
	this.Request.send(null);
	
	if (!asynchronous)
		this.SuccessCallback(this.Request.responseText);
	else
		this.Active = true;
};	

// asynchronous by design
XmlHttpRequest.prototype.PerformPost = function(url, formData)
{
	// required for the closure below
	var _this = this;

	// Upon deletion of the session cookie, and then the *second* PerformPost,
	//   Firefox will call OnHttpRequest with data for the *first* PerformPost
	//   when .abort() is called; since .abort() is only needed all the time
	//   in IE, this bug can be avoided.
	if (this.Active || document.all)
		this.Request.abort();

	this.Request.onreadystatechange = function() { XmlHttpRequest.prototype.OnHttpRequest(_this); };		
		
	this.Request.open("POST", url, true); 
	this.Request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.Request.send(formData);
	
	this.Active = true;
}


XmlHttpRequest.GetXmlHttpObject = function GetXmlHttpObject() 
{
	var xmlhttp;

	/*@cc_on
	@if (@_jscript_version >= 5)
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlhttp = null;
		}
	}
	@else
	xmlhttp = null;
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = null;
		}
	}
	
	return xmlhttp;
};