/* bom检查 */
/**
 * GBMadAJAX V1.0 2006-12-20 01:25:09 By 罕疯(http://www.gbmad.net/)
 * 本代码采用“知识共享署名 2.5 中国大陆许可协议(http://creativecommons.org/licenses/by/2.5/cn/)”进行许可。
 * 简单说下吧，就是：你可以任何方式使用这些代码。但是必须保留这段署名及注释。尊重他人就是尊重自己。
 * 
 * 永久地址：http://www.gbmad.net/blog/article.asp?id=1
 */

function GBMadAJAX() {
	var xml;
	var rs;
	var body = new Array();
	var self = this;
	var sending = false;
	var sto;
	//开始初始化XMLHttpRequest 对象
	if (window.XMLHttpRequest) {
		//Mozilla 浏览器
		xml = new XMLHttpRequest();
	} else {
		try {
			//设置IE浏览器
			xml = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
		}
	}
	this.method = "GET";
	this.url = "?";
	this.async = true;
	this.to = 0;
	this.rsid = "";
	this.rbt = 0;
	this.rbody = "";
	this.Uninitialized = function() {};
	this.Loading = function() {};
	this.Loaded = function() {};
	this.Interactive = function() {};
	this.Completed = function() {};
	this.Error = function() {};
	this.Busy = function() {};
	this.TimeOut = function() {};
	this.Add = function(key, val) {
		body[body.length] = encodeURIComponent(key) + "=" + encodeURIComponent(val);
	}
	this.Open = function() {
		if (this.rsid) {
			rs = document.getElementById(this.rsid);
			this.Uninitialized = function() {
				rs.innerHTML = "Uninitialized...";
				rs.innerHTML += "Your browser don't support for XmlHttpRequest.";
			}
			this.Loading = function() {
				rs.innerHTML = "Loading...";
			}
			this.Loaded = function() {
				rs.innerHTML = "Loaded...";
			}
			this.Interactive = function() {
				rs.innerHTML = "Interactive...";
			}
			this.Completed = function() {
				rs.innerHTML = "Completed...";
				rs.innerHTML = xml.responseText;
			}
			this.Error = function() {
				rs.innerHTML = "Completed...";
				rs.innerHTML += "Status: " + xml.status + " " + xml.statusText;
			}
			this.Busy = function() {
				rs.innerHTML = "Server too busy...Please wait or click the Refresh button.";
			}
			this.TimeOut = function() {
				rs.innerHTML = "Server time out...Please click the Refresh button.";
			}
		}
		if (xml) {
			if (sending) {
				this.Busy();
			} else {
				sending = true;
				this.method = this.method.toUpperCase();
				if (this.url.indexOf("?") > -1) {
					this.url += "&" + Math.random() + "=";
				} else {
					this.url += "?" + Math.random() + "=";
				}
				if (this.method == "GET") {
					this.url += "&" + body.join("&")
				}
				xml.open(this.method, this.url, this.async);
				if (this.async) {
					xml.onreadystatechange = StateChange;
				}
				if (this.to) {
					sto = window.setTimeout(function() {Abort();}, this.to);
				}
				if (this.method == "GET") {
					xml.send("");
				} else {
					xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					xml.send(body.join("&"));
				}
				if (!this.async) {
					Received();
				}
			}
		} else {
			this.Uninitialized();
		}
	}
	function StateChange() {
		switch (xml.readyState) {
			case 0:
				self.Uninitialized();
				break;
			case 1:
				self.Loading();
				break;
			case 2:
				self.Loaded();
				break;
			case 3:
				self.Interactive();
				break;
			case 4:
				Received();
				break;
		}
	}
	function Received() {
		if (sto) {
			window.clearTimeout(sto);
		}
		body = new Array();
		sending = false;
		if (xml.status == 200) {
			switch (self.rbt) {
				case 1:
					self.rbody = xml.responseBody;
					break;
				case 2:
					self.rbody = xml.responseXML;
					break;
				case 3:
					self.rbody = xml.responseStream;
					break;
				default:
					self.rbody = xml.responseText;
					break;
			}
			self.Completed();
		} else {
			self.Error();
		}
	}
	function Abort() {
		if (sending) {
			xml.abort();
			body = new Array();
			sending = false;
			self.TimeOut();
		}
	}
	/*@cc_on @*/
	/*@if (@_jscript_version < 5.5)
	function encodeURIComponent(str) {
		var s = new Array('%00', '%01', '%02', '%03', '%04', '%05', '%06', '%07', '%08', '%09', '%0A', '%0B', '%0C', '%0D', '%0E', '%0F', '%10', '%11', '%12', '%13', '%14', '%15', '%16', '%17', '%18', '%19', '%1A', '%1B', '%1C', '%1D', '%1E', '%1F', '%20', '!', '%22', '%23', '%24', '%25', '%26', "'", '(', ')', '*', '%2B', '%2C', '-', '.', '%2F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%3A', '%3B', '%3C', '%3D', '%3E', '%3F', '%40', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '%5B', '%5C', '%5D', '%5E', '_', '%60', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '%7B', '%7C', '%7D', '~', '%7F');
		var r = new Array();
		var l = str.length;
		var i;
		var j = 0;
		var c;
		for (i = 0; i < l; i++) {
			c = str.charCodeAt(i);
			if (c < 128) {
				r[j++] = s[c];
			} else if (c < 2048) {
				r[j++] = "%" + ((c >> 6) + 192).toString(16).toUpperCase();
				r[j++] = "%" + ((c & 63) + 128).toString(16).toUpperCase();
			} else if (c < 65536) {
				r[j++] = "%" + ((c >> 12) + 224).toString(16).toUpperCase();
				r[j++] = "%" + (((c >> 6) & 63) + 128).toString(16).toUpperCase();
				r[j++] = "%" + ((c & 63) + 128).toString(16).toUpperCase();
			} else if (c < 2097152) {
				r[j++] = "%" + ((c >> 18) + 240).toString(16).toUpperCase();
				r[j++] = "%" + (((c >> 12) & 63) + 128).toString(16).toUpperCase();
				r[j++] = "%" + (((c >> 6) & 63) + 128).toString(16).toUpperCase();
				r[j++] = "%" + ((c & 63) + 128).toString(16).toUpperCase();
			}
		}
		return r.join("");
	}
	@end @*/
}