function Ajax()
{

	this.http	= null;
	this.url	= null;
	this.method	= 'GET';
	this.async	= true;
	this.status	= null;
	this.statusText	= '';
	this.postData	= null;
	this.readyState	= null;
	this.responseText = null;
	this.responseXML  = null;
	this.handleResp	= null;
	this.responseFormat = 'text'; // text | xml | object
	this.mimeType	= null;
	
	this.init = function()
	{
		if (!this.http)
		{
			try { this.http = new XMLHttpRequest(); }
			catch (e)
			{
				try { this.http = new ActiveXObject('MSXML2.XMLHTTP'); }
				catch (e)
				{
					try { this.http = new ActiveXObject('Microsoft.XMLHTTP'); }
					catch (e) { return false; }
				}
			}
		}
		return this.http;
	};
	
	this.doRequest = function()
	{

		if (!this.init()) { alert('Could not creat XMLHttpRequest object.'); return; }
		this.http.open(this.method, this.url, this.async);
		
		if (this.mimeType)
		{
			try { this.http.overrideMimeType(this.mimeType); }
			catch (e) { }
		}

		var self = this;
		
		this.http.onreadystatechange = function()
		{
			if (self.http.readyState == 4)
			{
				switch(self.responseFormat)
				{
					case 'text':
						resp = self.http.responseText;
						break;
					case 'xml':
						resp = self.http.responseXML;
						break;
					case 'object':
						resp = http;
						break;
				}
				if (self.http.status >= 200 && self.http.status <= 299)
					self.handleResp(resp);
				else
					self.handleErr(resp);
			}
		};

		// After onreadystatechange for IE
		if (this.method == 'POST')
		{
			this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.http.setRequestHeader("Content-length", this.postData);
			this.http.setRequestHeader("Connection", "close");
		}

		this.http.send(this.postData);
	};

	this.setMimeType = function(mimeType)
	{
		this.mimeType = mimeType;
	};
	
	this.handleErr = function()
	{
		var errorWin;
		
		try
		{
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = this.responseText;
		}
		catch (e)
		{
			alert('An error occured, but the error message cannot be displayed. This is probably because of your browser\'s pop-up blocker.\nPlease allow pop-ups from this web site if you want to see the full error messages.\n\nStatus Code: ' + this.http.status + '\nStatus Description: ' + this.http.statusText);
		}
	};
	
	this.abort = function()
	{
		if (this.http)
		{
			this.http.onreadystatechange = function() { };
			this.http.abort();
			this.http = null;
		}
	};
	
	this.get = function(url, hand, format)
	{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doRequest();
	};
	
	this.setMethod = function(method, postData)
	{
		this.method = method;
		this.postData = postData || null;
	};
}

var ajax = new Ajax();