// AJAX CLASS
//

//AJAX = function(url,query)
function AJAX(url, query)
{
	this.url 			= url;
	this.query 			= query;
	this.uri			= url + "?q=" + query;
	this.init 			= false;
	this.http 			= false;
	this.mCnt			= 0;
	
	d = new Date();
	this.uri = this.uri + "^uid="+d.getTime();
	this.setup();
}
AJAX.prototype.debug = false;
AJAX.prototype.setup = function()
{
	if (typeof XMLHttpRequest!='undefined')
	{
		this.http = new XMLHttpRequest();
	}
	if (!this.http && window.ActiveXObject)
	{
		try
		{
			this.http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				this.http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { this.http = false; }
		}
			
	}
	if (this.http) this.init = true;
	if (this.debug) alert(this.http);
}

AJAX.handler = function(o)
{
	var obj = o;
	if (obj.debug) alert("readyState: " + obj.http.readyState);
	
	if (obj.http.readyState == 4)
	{
		// properties
		obj.txt				= obj.http.responseText;
		obj.xml 			= obj.http.responseXML;
		obj.readyState 		= obj.http.readyState;
		obj.status	 		= obj.http.status;
		obj.statusText		= obj.http.statusText;
		obj.headers 		= obj.http.getAllResponseHeaders();
		if (obj.debug) alert("rtn: " + obj.txt);
		// methods
		obj.getHeader		= function(aHeader) { return obj.http.getResponseHeader(aHeader); }
		
		//if (obj.status != 200) obj.init = false;
		
		if (obj.debug) alert("status: " + obj.status + "\ncalling onload()");
		var success = (obj.init && obj.status == "200");	
		obj.onload(obj, success);
	}
}

//---------------------------------------------------
AJAX.prototype.GetRtnStr = function()
	{
	return this.txt;
	}
//---------------------------------------------------
AJAX.prototype.PutStrInInnerHTML = function(elemID)
	{
	document.getElementById('elemID').innerHTML = this.txt;
	}
//---------------------------------------------------
AJAX.prototype.PutStrInValue = function(elemID)
	{
	document.getElementById(elemID).value = this.txt;
	}
	
//---------------------------------------------------
AJAX.prototype.go = function(v)
	{
	if (!this.init) return false;
	var me = this;
		
		if (v == "GET")
		{
			++this.mCnt;
			if (this.debug) alert("getting: " + this.uri);
			this.http.open("GET", this.uri+this.mCnt, true);
			this.http.onreadystatechange = function() { AJAX.handler(me); }
			this.http.send(null);
		}
		else if (v == "POST")
		{
			if (this.debug) alert("posting: " + this.url + "\npost data: " + this.query);
			this.http.open("POST", this.url, true);
			// todo: set this as an option so we can actually do a web-service type thing
		    this.http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//			this.xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

			this.http.onreadystatechange = function() { AJAX.handler(me);}
			this.http.send(this.query);
		}
	}

AJAX.prototype.get = function() { this.go("GET"); }
AJAX.prototype.post = function() { this.go("POST"); }
AJAX.prototype.onload = function() { };

