/***********************************************************
********************** TROUBLESHOOT ************************
************************************************************
	If your ajax doesn't work, try these:

	1. Make sure method is in quotes (e.g. "POST")
	2. Make sure handling method is without quotes (e.g. HandleResponse)
	3. Make sure your XML is valid / all tags are closed
	4. Don't forget the freakin' header("Content-Type: text/xml")

/***********************************************************
********************** AJAX ENGINE *************************
***********************************************************/

function getNewInstance() {
	try {
		return new XMLHttpRequest();
	} catch (e) {
	      try{
	            //return new ActiveXObject('Microsoft.XMLHTTP');
	            return new ActiveXObject('Msxml2.XMLHTTP.3.0');
		}
		catch(ee) {
			alert("You need a browser which supports an XMLHttpRequest Object.\nMozilla build 0.9.5 has this Object and IE5 and above")
		}
	}
}

function request(XMLLoader, FileName, Method, PostVariables, Handler) {
	if (XMLLoader) {
		XMLLoader.open(Method, FileName, true);
		XMLLoader.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLLoader.onreadystatechange=Handler;
		XMLLoader.send(PostVariables);
	}
}

function abort(XMLLoader) {
	XMLLoader.onreadystatechange = function(e){};
	XMLLoader.abort();
}

function checkXML(XMLLoader) {
/*	if(isIE6) {
		var s = XMLLoader.prototype;
		for(i in XMLLoader) s += i+' => '+XMLLoader[i]+'\n';
		alert('checkXML > XMLLoader.readyState='+XMLLoader.readyState+' && XMLLoader.status='+XMLLoader.status+' && XMLLoader.responseText='+XMLLoader.responseText);
		alert('checkXML > '+s);
	}
*/
	if (XMLLoader.readyState == 4) {
		if ((XMLLoader.status) && (XMLLoader.status == 200)) {
			return XMLLoader.responseXML;
		}
		else {
			alert('Error occured in AJAX');
		}
    	}
	return null;
}

function AjaxRequest(URL, Method, Post, Func) {
	this.mode = 'STD';
	this.ID = AjaxNextID++;
	this.URL = URL ? URL : '';
	this.method = Method ? Method : 'GET';
	this.data = Post ? Post : '';
	this.func = Func ? Func : function(e) {};
	this.transport = getNewInstance();
	this.transport.parent = this;
	this.abort = function() { abort(this.transport) };
	AjaxHandlers[this.ID] = Func;
	this.handler = function() { XML = checkXML(this); if(XML != null) { try {this.parent.func(XML);} catch(e) { AjaxHandlers[AjaxNextID-1](XML); } } };
	this.send = function(){ request(this.transport, this.URL, this.method, this.data, this.handler) };
//	if(this.URL != '') this.send();
}

var isIE6 = jQuery.browser.msie && jQuery.browser.version == '6.0' && navigator.userAgent.match(/8\.0/) == null;

if(isIE6) {
	AjaxRequest = function(URL, Method, Post, Func) {
	      this.mode = 'MS';
		this.ID = AjaxNextID++;
		this.URL = URL ? URL : '';
		this.method = Method ? Method : 'GET';
		this.data = Post ? Post : '';
		this.func = Func ? Func : function(e) {};
		this.transport = getNewInstance();
		AjaxInstances[this.ID] = this.transport;
		AjaxHandlers[this.ID] = Func;
		this.handler = function() { XML = checkXML(AjaxInstances[AjaxNextID-1]); if(XML != null) { AjaxHandlers[AjaxNextID-1](XML); } };
		this.send = function(){ request(this.transport, this.URL, this.method, this.data, this.handler) };
	}
}

var AjaxHandlers = new Array();
var AjaxInstances = new Array();
var AjaxNextID = 0;

/***********************************************************
****************** BUNDLED FUNCTIONS ***********************
***********************************************************/

function getXMLvalue(XML, varname) {
	var node = XML.getElementsByTagName(varname)[0];
	if(node && node.firstChild)
		return node.firstChild.nodeValue;
	else return '';
}


