/*
	little AJAX class
*/

/*
0	The request is not initialized
1	The request has been set up
2	The request has been sent
3	The request is in process
4	The request is complete
*/

var totalAjaxMechs = 0;

AjaxMech.prototype.AJAXRS_NULL = 0;
AjaxMech.prototype.AJAXRS_SETUP = 1;
AjaxMech.prototype.AJAXRS_SENT = 2;
AjaxMech.prototype.AJAXRS_INPROC = 3;
AjaxMech.prototype.AJAXRS_DONE = 4;




AjaxMech.prototype.stateChangedHandler;
AjaxMech.prototype._xmlHttp;
AjaxMech.prototype._changeHandlers;
AjaxMech.prototype.setStateChangeHandler;
AjaxMech.prototype.setHandler;



function AjaxMech() {
	totalAjaxMechs++;
	this._use_eval = false;
	this._handler = new Array();
	this._component = 0;
	/*
		this does NOT necessarily work as indicated.
		IE7 seems to work properly with the first one here.
	*/
	try	{
		// Firefox, Opera 8.0+, Safari
		this._xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer >= 6
		try {
			this._xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Fatal error: ajaxFactory() failed.");
			}
		}
	}
	this._xmlHttp.owner = this;
	
	this._xmlHttp._counter = totalAjaxMechs;
	
	var no_op = function() { }
	this._changeHandlers = new Array(no_op,no_op,no_op,no_op);
	
	/*
		THIS WORKS FOR IE7
	*/
	
	try {
		if(BROWSER == FIREFOX) {
			throw(1);
		}
		/*stateChangedHandler = function() {
			if(this.owner._changeHandlers[this.owner._xmlHttp.readyState]) {
				if(!this.owner._use_eval) {
					this.owner._changeHandlers[this.owner._xmlHttp.readyState]();
				} else {
					this.owner.fireHandler();
				}
			}
		}*/
		this.fireHandler.owner = this;
		stateChangedHandler.owner = this;
		this._xmlHttp.onreadystatechange = stateChangedHandler;
	}
	
	catch(e) {
		/*
			THIS WORKS FOR FIREFOX
			Note that, in this instance, stateChangedHandler becomes a member function... sortof.
			This way makes more sense, I think.
		*/
		this.fireHandler.owner = this;
		this.stateChangedHandler.owner = this;
		this._xmlHttp.onreadystatechange = this.stateChangedHandler;
		//this._xmlHttp.onreadystatechange = this.fireHandler;
	}
	
	
	//this._xmlHttp.onreadystatechange.owner = this;
	

}









AjaxMech.prototype.setStateChangeHandler = function(state,handler) {
	this._changeHandlers[state] = handler;
}



AjaxMech.prototype.connect = function(method,url,asynch) {
	this._xmlHttp.open(method,url + "&_clusterbuster_="+Math.random(),asynch);
}


AjaxMech.prototype.send = function() {
	this._xmlHttp.send(null);
}

AjaxMech.prototype.getResponse = function() {
	return this._xmlHttp.responseText;
}



AjaxMech.prototype.stateChangedHandler = function() {
	var state = this.owner._xmlHttp.readyState;
	
	if(typeof(this.owner._changeHandlers[this.owner._xmlHttp.readyState]) == "function") {
		this.owner._changeHandlers[this.owner._xmlHttp.readyState]();
	}
	
	this.owner.fireHandler(this.owner);
	return;
	
	if(this.owner._changeHandlers[this.owner._xmlHttp.readyState]) {
		this.owner._changeHandlers[this.owner._xmlHttp.readyState]();
	}
}




AjaxMech.prototype.setHandler = function(state,obj,funref) {
	this._handler[state] = new Array(obj,funref);
}

AjaxMech.prototype.fireHandler = function(mech) {
	if(mech._handler[mech._xmlHttp.readyState]) {
		var obj = mech._handler[mech._xmlHttp.readyState][0];
		var fun = mech._handler[mech._xmlHttp.readyState][1];
		fun.call(obj,mech);
	}
}


























function IFrameRequest() 
{
  var reqCount = 0;
  this.readyState = 0;
  this.status = 0;
  this.responseText = "";    
  reqCount++;
  this.req_id = reqCount;
}

IFrameRequest.prototype = {
  open: function(protocol, url, async) {
    this.protocol = protocol;
    this.url = url;  
  },
  
  onreadystatechange: function() { },
  
  send: function(postBody) {
    var self = this;    
    if(this.protocol.toUpperCase()=='POST')
    {
      this.url = this.url + "&" + postBody;
    }
    var IFrameDoc = document.createElement('iframe');
    IFrameDoc.setAttribute('id', 'req'+this.req_id);
    IFrameDoc.setAttribute('name', 'req'+this.req_id);
    IFrameDoc.style.width = "0";IFrameDoc.style.height = "0";IFrameDoc.style.border = "0";          
    document.body.appendChild(IFrameDoc);    
    
    try {      
      IFrameDoc.src = this.url;   
    }
    catch(e)
    {
      return false;
    }
 
    this.readyState = 1; this.onreadystatechange();
 
    setTimeout(function(){self.IFht(4);}, 4);     
  },
  
  overrideMimeType: function() { },
  
  getResponseHeader: function (name) { return ''; },
  
  setRequestHeader: function (name, data) { },
  
  IFht: function (d) { var self=this;var el=document.getElementById('req'+self.req_id); if(el.readyState=='complete') { self.responseText = document.frames['req'+self.req_id].document.body.innerHTML.replace(/[\n\r]+/ig, ""); el.parentNode.removeChild(el); self.status = 200; self.readyState = 4; self.onreadystatechange(); }else{ d*=1.5; setTimeout(function(){self.IFht(d);},d); } }
};