var	g_ajax = new c_ajax;

function c_ajax() {};

c_ajax.prototype.GetNewRequestObject = function() {
	/*@cc_on @*/
	
	/*@if (@_jscript_version >= 5)
		try {
			this.dreqobj = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e) {
			try {
				this.dreqobj = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e2) {
				this.dreqobj = false;
				}
			}
	@end @*/
	
	if ( !this.dreqobj && (typeof XMLHttpRequest != 'undefined') )
		this.dreqobj = new XMLHttpRequest();
};

c_ajax.prototype.dreqobj=null;
c_ajax.prototype.dcbfunc=null;
c_ajax.prototype.dparm=null;
c_ajax.prototype.dpcb_func=null;
c_ajax.prototype.dparm2=null;
c_ajax.prototype.dphase=0;
c_ajax.prototype.dq=new Array();
c_ajax.prototype.dq_state=true;
c_ajax.prototype.dcommitted=false;
c_ajax.prototype.dpre_q_in_url=null;
c_ajax.prototype.dpre_q_in_cb=null;
c_ajax.prototype.dpre_q_in_method=null;
c_ajax.prototype.dpre_q_in_p=null;
c_ajax.prototype.dpre_q_in_pcb=null;
c_ajax.prototype.dpre_q_in_p2=null;
c_ajax.prototype.dpre_queue_in_c2_phase=0;

c_ajax.prototype.QueueFlush = function() {
	this.dq = new Array();
	this.QueueResume();
}

c_ajax.prototype.QueuePause = function() {
	this.dq_state = false;
}

c_ajax.prototype.QueueResume = function() {
	this.dq_state = true;
	this.Dequeue();
}

c_ajax.prototype.QueueInterrupt = function(in_url, in_cb, in_method, in_p, in_pcb, in_p2, in_phase) {
	var url = in_url;
	this.dcbfunc = in_cb;
	var method = in_method;
	this.dparm = in_p;
	this.dpcb_func = in_pcb;
	this.dparm2 = in_p2;
	this.dphase = in_phase;
	if (url && method)
		ret = this._CallXMLHTTPObject (url, method);
	return ret;
}

c_ajax.prototype.QueueInject = function (in_url, in_cb, in_method, in_p, in_pcb, in_p2, in_phase) {
	this.dq_state = false;
	for (var cnt = this.dq.length; cnt > 0; cnt--)
		this.dq[cnt] = this.dq[cnt - 1];
	this.dq[0] = new Array(in_url, in_cb, in_method, in_p, in_pcb, in_p2, in_phase);
	this.dq_state = true;
}

c_ajax.prototype.CallXMLHTTPObjectGET = function(in_url, in_cb) {
	return this.CallXMLHTTPObject(in_url, in_cb, "GET", null, null, 0);
}

c_ajax.prototype.CallXMLHTTPObjectGETParam = function(in_url, in_cb, in_p) {
	return this.CallXMLHTTPObject(in_url, in_cb, "GET", in_p, null, 0);
}

c_ajax.prototype.CallXMLHTTPObjectGETParamPartial = function(in_url, in_cb, in_p, in_pcb, in_p2) {
	return this.CallXMLHTTPObject(in_url, in_cb, "GET", in_p, in_pcb, in_p2, 0);
}

c_ajax.prototype.CallXMLHTTPObjectGETParamPartialPhase = function(in_url, in_cb, in_p, in_pcb, in_p2, in_phase) {
	return this.CallXMLHTTPObject(in_url, in_cb, "GET", in_p, in_pcb, in_p2, in_phase);
}

c_ajax.prototype.CallXMLHTTPObjectPOST = function(in_url, in_cb) {
	return this.CallXMLHTTPObject(in_url, in_cb, "POST", null, null, 0);
}

c_ajax.prototype.CallXMLHTTPObjectPOSTParam = function(in_url, in_cb, in_p) {
	return this.CallXMLHTTPObject(in_url, in_cb, "POST", in_p, null, 0);
}

c_ajax.prototype.CallXMLHTTPObjectPOSTParamPartial = function(in_url, in_cb, in_p, in_pcb, in_p2) {
	return this.CallXMLHTTPObject(in_url, in_cb, "POST", in_p, in_pcb, in_p2, 0);
}

c_ajax.prototype.CallXMLHTTPObjectPOSTParamPartialPhase = function(in_url, in_cb, in_p, in_pcb, in_p2, in_phase) {
	return this.CallXMLHTTPObject(in_url, in_cb, "POST", in_p, in_pcb, in_p2, in_phase);
}

c_ajax.prototype.CallXMLHTTPObject = function(in_url, in_cb, in_method, in_p, in_pcb, in_p2, in_phase) {
	this.dpre_q_in_url=in_url;
	this.dpre_q_in_cb=in_cb;
	this.dpre_q_in_method=in_method;
	this.dpre_q_in_p=in_p;
	this.dpre_q_in_pcb=in_pcb;
	this.dpre_q_in_p2=in_p2;
	this.dpre_queue_in_c2_phase=in_phase;
	if ((this.dpre_queue_in_c2_phase < 1) || (this.dpre_queue_in_c2_phase > 3))
		this.dpre_queue_in_c2_phase = 3;
	this.Enqueue();
	return true;
};

c_ajax.prototype.Enqueue = function() {
	this.dq[this.dq.length] = new Array(this.dpre_q_in_url, this.dpre_q_in_cb,
		this.dpre_q_in_method, this.dpre_q_in_p, this.dpre_q_in_pcb,
		this.dpre_q_in_p2, this.dpre_queue_in_c2_phase);
	this.dpre_q_in_url=null;
	this.dpre_q_in_cb=null;
	this.dpre_q_in_method=null;
	this.dpre_q_in_p=null;
	this.dpre_q_in_pcb=null;
	this.dpre_q_in_p2=null;
	this.dpre_queue_in_c2_phase=0;
	if (!this.dreqobj)
		this.Dequeue();
};

c_ajax.prototype.Dequeue = function() {
	var cmd = null;
	var ret=false;
	if (this.dq.length && this.dq_state) {
		cmd = this.dq[0];
		var url = cmd[0];
		this.dcbfunc = cmd[1];
		var method = cmd[2];
		this.dparm = cmd[3];
		this.dpcb_func = cmd[4];
		this.dparm2 = cmd[5];
		this.dphase = cmd[6];
		for (var cnt = 1; cnt < this.dq.length; cnt++)
			this.dq[cnt - 1] = this.dq[cnt];
		this.dq.length = cnt - 1;
	}
	if (url && method)
		ret = this._CallXMLHTTPObject(url, method);
	return ret;
};

c_ajax.prototype._CallXMLHTTPObject = function(in_url, in_method) {
	try {
		var sVars = null;
		if (in_method == "POST") {
			var rmatch = /^([^\?]*)\?(.*)$/.exec(in_url);
			in_url = rmatch[1];
			sVars = unescape(rmatch[2]);
		}
		this.dcommitted = false;
		this.GetNewRequestObject();
		this.dreqobj.open(in_method, in_url, true);
		if (in_method == "POST") {
            this.dreqobj.setRequestHeader("Method", "POST "+in_url+" HTTP/1.1");
            this.dreqobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		this.dreqobj.onreadystatechange = hndlFunc;
		this.dreqobj.send(sVars);
		return true;
	}
	catch(z) {}
	return false;
};

function hndlFunc() {
	var ie = navigator.appName=='Microsoft Internet Explorer';
	if (g_ajax && g_ajax.dreqobj) {
		if (g_ajax.dreqobj.readyState == 0) {}
		else {
			if (g_ajax.dreqobj.readyState == 1) {
				if (g_ajax.dphase == g_ajax.dreqobj.readyState) {
					if (g_ajax.dpcb_func) {
						var resp;
						if(!ie && g_ajax.dreqobj.responseText)
							resp=g_ajax.dreqobj.responseText;
						g_ajax.dpcb_func(resp, g_ajax.dparm2 ? g_ajax.dparm2 : g_ajax.dparm);
					}
				}
			}
			else {
				if (g_ajax.dreqobj.readyState == 2) {
					if (g_ajax.dphase == g_ajax.dreqobj.readyState) {
						if (g_ajax.dpcb_func) {
							var resp;
							if(!ie && g_ajax.dreqobj.responseText)
								resp=g_ajax.dreqobj.responseText;
							g_ajax.dpcb_func(resp, g_ajax.dparm2 ? g_ajax.dparm2 : g_ajax.dparm);
						}
					}
				}
				else {
					if (g_ajax.dreqobj.readyState == 3) {
						g_ajax.dcommitted = true;
						if (g_ajax.dphase == g_ajax.dreqobj.readyState) {
							if (g_ajax.dpcb_func) {
								var resp;
								if(!ie && g_ajax.dreqobj.responseText)
									resp=g_ajax.dreqobj.responseText;
								g_ajax.dpcb_func(resp, g_ajax.dparm2 ? g_ajax.dparm2 : g_ajax.dparm);
							}
						}
					}
					else {
						if (g_ajax.dreqobj.readyState == 4) {
							g_ajax.dcbfunc(g_ajax.dreqobj.responseText, g_ajax.dparm, g_ajax.dparm2);
							g_ajax.dreqobj = null;
							g_ajax.dcommitted = false;
							g_ajax.dphase = 0;
							g_ajax.Dequeue();
						}
					}
				}
			}
		}
	}
    return true;
};

if (typeof SupportsAjax == 'undefined'){
	function SupportsAjax() {
		var test_obj = new c_ajax;
		test_obj.GetNewRequestObject();
		if ( test_obj.dreqobj ) {
			test_obj.dreqobj = null;
			test_obj = null;
			return true;
		}
		test_obj = null;
		return false;
	};
}

function SimpleAJAXCall(in_uri, in_cb, in_method, in_p ) {
	if ((typeof in_method == 'undefined') || ((in_method != 'GET')&&(in_method != 'POST')))
		in_method = 'GET';
	in_method = in_method.toUpperCase();
	if (SupportsAjax() && (typeof in_uri != 'undefined') && in_uri && (typeof in_cb == 'function')) {
		if ( in_method == 'POST' )
			return g_ajax.CallXMLHTTPObjectPOSTParam(in_uri, in_cb, in_p);
		else
			return g_ajax.CallXMLHTTPObjectGETParam(in_uri, in_cb, in_p);
	} 
    else
        return false;
};

