// class
function Ajax()
{
	// declare and intialize properties
	
	this.url = "";
	this.params = "";
	this.method = "GET";
	this.onSuccess = null;
	
	this.onError = function(msg)
	{
		alert(msg);
	}
}

Ajax.prototype.doRequest = function()
{
	if( !this.url )
	{
		this.onError("There was no URL. The request will be aborted.");
		return false;
	}
	
	if( !this.method )
	{
		this.method = "GET";
	}
	else
	{
		this.method = this.method.toUpperCase();
	}
	
	// Access to class for readyStateHandler.
	var _this = this;
	
	// create XMLHttpRequest object
	var xmlHttpRequest = getXMLHttpRequest();
	if( !xmlHttpRequest )
	{
		this.onError("There was no XMLHttpRequest object can be created.");
		return false;
	}
	
	// allow access to class for readyStateHandler
	
	// distinctive case after transfer method
	switch(this.method)
	{
		case "GET":		xmlHttpRequest.open(this.method, this.url+"?"+this.params, true);
						xmlHttpRequest.onreadystatechange = readyStateHandler;
						xmlHttpRequest.send(null);
						break;
		
		case "POST":	xmlHttpRequest.open(this.method, this.url, true);
						xmlHttpRequest.onreadystatechange = readyStateHandler;
						xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						xmlHttpRequest.send(this.params);
						break;
	}
	
	// private method for processing of the received data
	function readyStateHandler()
	{
		if( xmlHttpRequest.readyState < 4 )
		{
			return false;
		}
		
		if( xmlHttpRequest.status == 200 || xmlHttpRequest.status == 304 )
		{
			if(_this.onSuccess)
			{
				_this.onSuccess(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
			}
		}
		else
		{
			if(_this.onError)
			{
				_this.onError("["+xmlHttpRequest.status+" "+xmlHttpRequest.statusText+"] There was an error in data transmission.");
			}
		}
	}
}

// returns XMLHttpRequest object
function getXMLHttpRequest()
{
	if(window.XMLHttpRequest)
	{
		// for firefox, opera, etc...
		return new XMLHttpRequest();
	}
	else
	{
		if(window.ActiveXObject)
		{
			
			try
			{
				// new for ie
				return new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					
					// old for ie
					return new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					return null;
				}
			}
		}
	}
	return null;
}
