function AjaxRequest()
{
	this.init();
}

AjaxRequest.prototype=
{
	requestObject:null,
	
	async:true,
	
	isAsync:function()
	{
		return this.async;
	},
	
	setAsync:function(yes)
	{
		this.async=yes;
	},
	
	callback:null,
	
	getCallback:function()
	{
		return this.callback;
	},
	
	setCallback:function(func)
	{
		this.callback=func;
	},
	
	requestType:"GET",
	
	getRequestType:function()
	{
		return this.requestType;
	},
	
	setRequestType:function(type)
	{
		this.requestType=((type=="GET" || type=="POST") ? type : this.getRequestType());
	},
	
	onError:function(message)
	{
		throw new Error(message);
	},
	
	getOnError:function()
	{
		return this.onError;
	},
	
	setOnError:function(func)
	{
		this.onError=func;
	},
	
	onLoad:function() { },
	
	getOnLoad:function()
	{
		return this.onLoad;
	},
	
	setOnLoad:function(func)
	{
		this.onLoad=func;
	},
	
	onSend:function() {	},
	
	getOnSend:function()
	{
		return this.onSend;
	},
	
	setOnSend:function(func)
	{
		this.onSend=func;
	},
	
	init:function()
	{
		try
		{
			this.requestObject=new XMLHttpRequest();
		}
		catch (e)
		{
			var maybeThisOne=new Array("MSXML2.XMLHTTP.5.0",
									   "MSXML2.XMLHTTP.4.0",
									   "MSXML2.XMLHTTP.3.0",
									   "MSXML2.XMLHTTP",
									   "Microsoft.XMLHTTP");
			var done=false;
			for ( i=0 ; i<maybeThisOne.length && !done ; ++i )
			{
				try
				{
					this.requestObject=new ActiveXObject(maybeThisOne[i]);
					done=true;
				}
				catch (e) { }
			}
			if (!done)
				this.onError("Unable to initialize requestObject");
		}
	},
	
	send:function(url, content)
	{
		if (content)
			this.setRequestType("POST");
		var host=location.host;
		if (location.href.indexOf('https')!=-1)
			host="https://" + host;
		else
			host="http://" + host;
		this.requestObject.open(this.getRequestType(), host + '/' + url, this.isAsync());
		if (content)
		{
			var arr=new Array();
			for ( c in content )
				arr.push(c + "=" + encodeURI(content[c]));
			content=arr.join('&');
			this.requestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.requestObject.setRequestHeader("Content-length", content.length);
		}
		var self=this;
		this.requestObject.onreadystatechange=function()
		{
			self._onReadyStateChange();
		}
		this.requestObject.send(content);
		if (!this.isAsync())
			return this.requestObject.responseText;
	},
	
	_onReadyStateChange:function()
	{
		switch (this.requestObject.readyState)
		{
			case 2:
				this.onSend();
				break;
			case 4:
				this.onLoad();
				if (this.requestObject.status==200)
				{
					if (this.callback!=null)
						this.callback(this.requestObject.responseText);
				}
				else
					this.onError("HTTP error while occured: (" + this.requestObject.status + ") " + this.requestObject.statusText);
				break;
		}
	}
}

