
function ajax(jsonObject, callbackFunction)
{
	var that=this;
	this.ajaxRequest;
	this.responseJsonObject;
	this.jsonString = JSON.stringify(jsonObject);
	this.returnJson = true;

	try
	{
		// Opera 8.0+, Firefox, Konqueror, Safari
		this.ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			this.ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try
			{
				this.ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Upgrade your browser!");
				return false;
			}
		}
	}

	this.ajaxRequest.onreadystatechange = function()
	{
		if(that.ajaxRequest.readyState == 4 && that.returnJson)
		{
			var ajaxErrorString = "{\"error\": \"true\", \"type\": \"connection\"}";
			try
			{
				if(that.ajaxRequest.status == 200)
				{
					that.responseJsonObject = JSON.parse(that.ajaxRequest.responseText);
				}
				else
				{
					that.responseJsonObject = JSON.parse(ajaxErrorString);
				}
			}
			catch (e)
			{
				that.responseJsonObject = JSON.parse(ajaxErrorString);
			}
			that.callback(that.responseJsonObject);
		}
		else if(that.ajaxRequest.readyState == 4)
		{
			that.callback(that.ajaxRequest.responseText);
		}
	}

	this.sendAjax = function(serverFile, type)
	{
		var params = "json="+encodeURIComponent(that.jsonString);
		if(type == "POST")
		{
			that.ajaxRequest.open("POST", serverFile, true);

			//Send the proper header information along with the request
			that.ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			that.ajaxRequest.setRequestHeader("Content-length", params.length);
			//that.ajaxRequest.setRequestHeader("Connection", "close");
			that.ajaxRequest.send(params);
		}
		else if(type == "GET")
		{
			that.ajaxRequest.open("GET", serverFile+"?"+params, true);
			that.ajaxRequest.send(null);
		}
	}
	
	this.callback = callbackFunction || function() {};
}
