/**
 * Ajax class.
 * 
 * @param {Object} url
 * @param {Object} method
 * @param {Object} isAsync
 */
function Ajax(url, method, isAsync)
{
	// member variables
	this.req = null;
	this.updates = '';
	this.isOpen = false;
	this.isAsync = isAsync;
	this.method = method;	// "GET" or "POST"
	this.onLoad = function() { alert(this.responseText); };
	this.url = url;
	this.xml = null;

	// methods
	this.getResponseText = function()
	{
		return this.req ? this.req.responseText : null;
	}

	this.getResponseXML = function()
	{
		return this.req ? this.req.responseXML : null;
	}

	this.getRequestObject = function()
	{
		if (this.req == null)
		{
			this.req = this.createRequestObject();
		}
		return this.req != null;
	}

	this.createRequestObject = function()
	{
		var ajaxRequest;
		try
		{
			ajaxRequest = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					alert("Your browser does not support AJAX!");
					return null;
				}
			}
		}
		return ajaxRequest;
	};

	this.stateChange = function()
	{
		if (this.req != null)
		{
			this.updates += ("state: " + this.req.readyState + "\n");
			if (this.checkReadyState())
			{
				this.onLoad();
			}
		}
	}

	this.checkReadyState = function()
	{
		if (this.req.readyState == 4)
		{
			if (this.req.status == 200)
			{
				return true;
			}
		}
	}

	this.open = function()
	{
		if (this.getRequestObject())
		{
			var ajaxOpen = this;
			this.req.onreadystatechange = function()
			{
				ajaxOpen.stateChange();
			};
			this.req.open(this.method, this.url, this.isAsync);
		}
		return this.req != null;
	};

	this.send = function()
	{
		if (this.open())
		{
			if (this.xml)
			{
				xml = 'xml=' + escape(this.xml);
				this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				this.req.setRequestHeader('Content-Length', xml.length);
				this.req.send(xml);
			}
			else
			{
	    		this.req.send(null);
			}
		}
	};

	this.getXMLObject = function(obj, notify)
	{
		this.onLoad = function()
		{
			var xml = this.getResponseXML();
			obj.parse(xml);
			if (notify)
			{
				notify(obj);
			}
		}
		this.send();
	}
}

/**
 * Get an Ajax request object. This function is deprecated. It is here for legacy usages.
 * 
 * @deprecated
 * @param {Object} url
 * @param {Object} handler
 */
function getAjax(url, handler)
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = handler;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
	return xmlHttp;
}

/**
 * Get an Ajax request object. This function is deprecated. It is here for legacy usages.
 * 
 * @deprecated
 * @param {Object} url
 * @param {Object} handler
 */
function getAjaxXML(url, handler, xml)
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = handler;
    xmlHttp.open("POST", url, true);
	//xmlHttp.setRequestHeader('Content-Type','text/xml; charset=UTF-8');
    //xmlHttp.send(xml);
	return xmlHttp;
}
