// Ajax Classes
// Copyright Scott Bounds 2006
// http://www.scottbounds.com

function ajaxRequest(){
	
	this.init = function(){
		var o=null;
  		try {
    		o=new ActiveXObject("Msxml2.XMLHTTP");
  		} catch(e) {
			try {
				o=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				o=null;
			}
  		}
  		if(!o && typeof XMLHttpRequest != "undefined") o=new XMLHttpRequest();
  		return o;	
	}
	
	this.get = function(url){
		if(this != arguments.callee._oScope) return arguments.callee.apply(arguments.callee._oScope, arguments); //check scope
		
		try {
			if (this.request.readyState == 4) this.request.abort();
			
			var _this = this;
			var method = (arguments[1]) ? arguments[1] : 'GET';
			var postStr = (arguments[2]) ? arguments[2] : '';
			if (method.toLowerCase() != 'post') method = 'GET';
	
			this.busy = true;
			this.request.onreadystatechange = _this.response;
	
			this.request.open(method.toUpperCase(), url, true);
			if (method.toLowerCase() == 'post') this.request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			
			this.responseText = null;
			this.responseXML = null;
			this.request.send(postStr);		
		} catch(e){}
	}
	
	this.response = function(){
		if(this != arguments.callee._oScope) return arguments.callee.apply(arguments.callee._oScope, arguments); //check scope
		if (this.request.readyState == 4) { // only if req shows "complete"
			this.busy = false;
			this.responseText = this.request.responseText;
			this.responseXML = this.request.responseXML;
			if (this.request.status == 200) { // only if "OK"
				try { this.success(); }
				catch(e){ /*FAIL*/ }
			} else {
				try { this.error(); }
				catch(e){ /*FAIL*/ }
			}
		}
	}
	
	this.success = function(){
		if(this != arguments.callee._oScope) return arguments.callee.apply(arguments.callee._oScope, arguments); //check scope
		//alert(this.responseText);
	}
	
	this.error = function(){
		if(this != arguments.callee._oScope) return arguments.callee.apply(arguments.callee._oScope, arguments); //check scope
		//alert('Error querying data.');
		alert(this.responseText);
	}
	
	//set scope references
	this.get._oScope = this;
	this.response._oScope = this;
	this.success._oScope = this;
	this.error._oScope = this;
	
	this.busy = false;
	
	var _this = this;
	this.request = this.init();
	
}
