// ============= simple ajax class ============== //
// use:
/*

httpObj = new http_class();
httpObj.setAsync(true); // defaults to true, no need to set this unless you want synchronous communication
httpObj.setURL("http://www.domain.com");
httpObj.httpMessage({"param1":1, "param2":2});

if you want the page to reload instead
httpObj.setAsync(false); // forces a page reload and does not do AJAX
httpObj.setURL("http://www.domain.com");
httpObj.httpMessage({"param1":1, "param2":2});


*/

var HTTP_SIMULATE = false;
var HTTP_SIMULATE_RESPONSE = "<status>3</status>one\ntwo\nthree\n";

var HTTP_DEBUG = false;

function http_class()
{
	this.getXmlHttpRequestObject = function()
	{
		if (window.XMLHttpRequest) 
		{
			return new XMLHttpRequest();
		} 
		else if(window.ActiveXObject) 
		{
			return new ActiveXObject("Microsoft.XMLHTTP");
		} 
		else 
		{
			alert('Status: Cound not create XmlHttpRequest Object.  Please consider upgrading your browser.');
		}
	}	
	
	this.httpObj = this.getXmlHttpRequestObject();
	this.url;
	this.async = true;
	this.response = "";
	this.callback = null;
	this.busy = false;
	this.method = "GET";
	
	this.setURL = function(url)
	{
		this.url = url;
	}

	this.setAsync = function(async)
	{
		this.async = async;
	}

	this.setPost = function()
	{
		this.method = "POST";
	}
	
	this.setGet = function()
	{
		this.method = "GET";
	}
	
	this.httpMessage = function(params)
	{
		this.url = this.getURL(params);
		this.send();
	}
	
	this.send = function()
	{
		if (HTTP_SIMULATE)
		{
			alert("Simulating call to: " + this.url);
			this.response = HTTP_SIMULATE_RESPONSE;
			if (HTTP_DEBUG)
					alert("callback:" + this.callback);
				eval(this.callback+'()');
			return;
		}
	
		if (this.busy)
		{
			alert("Still processing previous request. Please try again in a moment");
			return;
		}
		
		if (this.async)
		{
			if (HTTP_DEBUG)
				alert(this.url);
			this.busy = true;
			this.httpObj.open(this.method, this.url, true);		
			this.httpObj.onreadystatechange = createObjectCallback(this, this.handler);
			this.httpObj.send(null);
		}
		else
		{
			window.location.href = this.url;
		}
	}
	
	this.getURL = function(params)
	{
		str = this.url + "?";

		for (n in params)
		{
			str += n + "=" + params[n] + "&";
		}
		
		str = str.substr(0,str.length-1);
		return str;
	}	

	this.handler = function() 
	{
		if (this.httpObj.readyState == 4) 
		{
			this.response = this.httpObj.responseText;
			if (HTTP_DEBUG)
				alert("Response:" + this.response);
			this.busy = false;

			if (this.callback != null)
			{
				this.callback(this.response);
			}
			else
			{
				if (HTTP_DEBUG)
					alert("No callback defined");
			}
		}
	}	
	
	this.getResponse = function()
	{
		return this.response;
	}
	
	this.setCallback = function(cback)
	{
		this.callback = cback;
	}
}

function createObjectCallback(obj, fn)
{
	return function() { fn.apply(obj, arguments); };
} 


