/*
Simple Ajax Script
Author: Abdullah Ibne Alam. Shohag
Reference: http://www.sitepoint.com
E-mail: abdullah.alam@soltius.com.bd
*/

function SimpleAjax()
{
	this.XmlHttp = 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',
	this.mimeType = null;

	this.init = function()
	{
		if (!this.XmlHttp)
		{
			try
			{
				this.XmlHttp = new XMLHttpRequest();
			}
			catch (e)
			{
				try
				{
					this.XmlHttp = new ActiveXObject('MSXML2.XMLHTTP');
				}
				catch (e)
				{
					try
					{
						this.XmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch (e)
					{
						return false;
					}
				}
			}
		}
		return this.XmlHttp;
	};

	this.doReq = function()
	{
		if (!this.init())
		{
			alert('Could not create XMLHttpRequest object. \nBrowser does not support HTTP Request.');
			return;
		}
		this.XmlHttp.open(this.method, this.url, this.async);
		var self = this;
		this.XmlHttp.onreadystatechange = function()
		{
			if (self.XmlHttp.readyState == 4)
			{
				switch (self.responseFormat)
				{
					case 'text':
						resp = self.XmlHttp.responseText;
						break;
					case 'xml':
						resp = self.XmlHttp.responseXML;
						break;
					case 'object':
						resp = XmlHttp;
						break;
				}				
				if (self.XmlHttp.status >= 200 && self.XmlHttp.status <= 299)
				{
					self.handleResp(resp);
				}
			}
		};
		if (this.XmlHttp) this.XmlHttp.send(this.postData);
	};

	this.doGet = function(url, hand, format)
	{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
	};
}