/**************************************************
*                                                 *
* Author  : Windy_sk                              *
* Create  : 2003-05-03                            *
* Modified: 2007-12-9                             *
* Email   : windy_sk@126.com                      *
* HomePage: None (Maybe Soon)                     *
* Notice  : U Can Use & Modify it freely,         *
*           BUT PLEASE HOLD THIS ITEM.            *
*                                                 *
**************************************************/

function Ajax() {
	ajax_requester = false;
	ajax_returnType = "TEXT";
	ajax_curState = "";
	ajax_onData = null;
	ajax_onStart = null
	ajax_onOver = null;
	ajax_header = "";
	ajax_result = "";
	ajax_charset = "";

	this.init = function(the_type, ajax_onData, ajax_onStart, ajax_onOver, ajax_charset) {
		if(typeof(the_type)=="undefined") {
			self.ajax_returnType = "TEXT";
		} else {
			self.ajax_returnType = the_type;
		}
		if(typeof(ajax_charset)=="undefined") {
			self.ajax_charset = "GBK";
		} else {
			self.ajax_charset = ajax_charset;
		}
		if(typeof(ajax_onData)!="function") {
			self.ajax_onData = this.getValue;
		} else {
			self.ajax_onData = ajax_onData;
		}
		if(typeof(ajax_onStart)!="function") {
			self.ajax_onStart = this.Dummy;
		} else {
			self.ajax_onStart = ajax_onStart;
		}
		if(typeof(ajax_onOver)!="function") {
			self.ajax_onOver = this.Dummy;
		} else {
			self.ajax_onOver = ajax_onOver;
		}
		self.ajax_requester = this.getRequester();
		self.ajax_requester.onreadystatechange = self.handleResult;
		return;
	}

	this.getRequester = function() {
		var xmlhttp_request = false;
		try {
			if(window.ActiveXObject) {
				var versions = ['Microsoft.XMLHTTP', 'MSXML6.XMLHTTP', 'MSXML5.XMLHTTP', 'MSXML4.XMLHTTP', 'MSXML3.XMLHTTP', 'MSXML2.XMLHTTP', 'MSXML.XMLHTTP'];
				for(var i=0; i<versions.length; i++) {
					try{
						xmlhttp_request = new ActiveXObject(versions[i]);
						try{
							xmlhttp_request.setRequestHeader("Content-Type", "text/xml; " + self.ajax_charset);
							//xmlhttp_request.setRequestHeader("Content-Type",  self.ajax_charset);
						} catch(e) {}
						break;
					} catch(e) {
						xmlhttp_request = false;
					}
				}
			} else if(window.XMLHttpRequest) {
				xmlhttp_request = new XMLHttpRequest();
				try{
					xmlhttp_request.overrideMimeType('text/xml');
				} catch(e) {}
			}
		}catch(e){
			xmlhttp_request = false;
		}
		return xmlhttp_request;
	}

	handleResult = function() {
		var State_lst = ["尝试连接", "正在装载", "装载完毕", "文档处理", "处理完成"];
		self.ajax_curState = State_lst[self.ajax_requester.readyState];
		if(self.ajax_requester.readyState==4) {
			if(self.ajax_requester.status==0 || self.ajax_requester.status==200) {
				try {
					if(self.ajax_returnType.toUpperCase()=="XML") {
						self.ajax_result = self.ajax_onData(self.ajax_requester.responseXML.documentElement);
					} else {
						self.ajax_result = self.ajax_onData(self.ajax_requester.responseText);
					}
					try {
						self.ajax_header = self.ajax_requester.getAllResponseHeaders();
					} catch(e) {}
				} catch(e) {
					self.ajax_curState = "Error - " + (typeof(e.description)=="undefined" ? e.message : e.description);
				}
			} else {
				self.ajax_curState = "Error - " + self.ajax_requester.status + " - 连接远程数据失败!";
			}
			self.ajax_onOver();
		}
		return;
	}

	send = function(the_method, the_url, the_para) {
		self.ajax_onStart();
		if(typeof(self.ajax_requester)!="object") return;
		try{
			if(typeof(the_para)=="undefined") the_para = "";
			if(the_method.toUpperCase()=="POST") {
				self.ajax_requester.open("POST", the_url, true);
				self.ajax_requester.setRequestHeader("Content-Length", the_para.length);
				self.ajax_requester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + self.ajax_charset);
				self.ajax_requester.send(the_para);
			} else {
				self.ajax_requester.open("GET", the_url, true);
				self.ajax_requester.send(null);
			}
		}catch(e){
			self.ajax_curState = "Error - 在获取数据时出错：" + (typeof(e.description)=="undefined" ? e.message : e.description);
			self.ajax_onOver();
		}
		return;
	}

	this.post_form = function(the_url, the_form) {
		var para_lst = new Array();
		var the_para = "";
		for(var i=0; i<the_form.elements.length; i++) {
			if(the_form.elements[i].name.length==0) continue;
			if(the_form.elements[i].type=="radio" || the_form.elements[i].type=="checkbox") {
				if(the_form.elements[i].checked) {
					the_para = encodeURIComponent(the_form.elements[i].name) + "=" + encodeURIComponent(the_form.elements[i].value);
				} else {
					continue;
				}
			} else if(the_form.elements[i].type=="select-multiple") {
					for(n=0; n<the_form.elements[i].options.length; n++) {
						if(the_form.elements[i].options[n].selected) {
							the_para += "&" + encodeURIComponent(the_form.elements[i].name) + "=" + encodeURIComponent(the_form.elements[i].options[n].value);
						}
					}
					if(the_para.length>1) the_para = the_para.substr(1);
			} else {
				the_para = encodeURIComponent(the_form.elements[i].name) + "=" + encodeURIComponent(the_form.elements[i].value);
			}
			para_lst.push(the_para);
		}
		self.send("POST", the_url, para_lst.join("&"));
		return;
	}

	this.post = function(the_url, the_para) {
		if(the_para==null) the_para = ""
		self.send("POST", the_url, the_para);
		return;
	}

	this.get = function(the_url, the_para) {
		if(the_para==null) the_para = "";
		if(the_url.indexOf("?")>-1) {
			the_url += "&" + the_para;
		} else {
			the_url += "?" + the_para;
		}
		self.send("GET", the_url, "");
		return;
	}

	this.GetState = function() {
		return self.ajax_curState;
	}

	this.GetHeader = function() {
		return self.ajax_header;
	}

	this.GetResult = function() {
		return self.ajax_result;
	}

	this.removeTextNode = function(xml_obj) {
		if(typeof(xml_obj.childNodes)!="undefined") {
			for(var i=0; i<xml_obj.childNodes.length; i++) {
				if(xml_obj.childNodes[i].nodeType==3) {
					xml_obj.removeChild(xml_obj.childNodes[i]);
					i--;
				}
			}
		}
		return xml_obj;
	}

	this.getValue = function(the_value) {
		return the_value;
	}

	this.Dummy = function() {
		return;
	}
}

var ajax_running = false;
var ajax_queue = Array();

function loadingStart() {
	var loading = document.getElementById("bar_loading");
	//loading.style.display = "";
	if(checker.ie) {
		loading.style.top = ((document.documentElement.offsetHeight - loading.offsetHeight)/2 + document.documentElement.scrollTop) + "px";
		loading.style.left = ((document.documentElement.offsetWidth - loading.offsetWidth)/2 + document.documentElement.scrollLeft) + "px";
	} else {
		loading.style.top = ((window.innerHeight - loading.offsetHeight)/2 + document.documentElement.scrollTop) + "px";
		loading.style.left = ((window.innerWidth - loading.offsetWidth)/2 + document.documentElement.scrollLeft) + "px";
		loading.style.MozOpacity = 0.90;
	}
	ajax_running = true;
	return;
}

function loadingEnd() {
	var result = "";
	var loading = document.getElementById("bar_loading");
	if(typeof(myAjax)!="undefined") result = myAjax.GetState();
	if(result.match(/Error/i)) alert(result);
	loading.style.display = "none";
	ajax_running = false;
	try {
		var new_job = ajax_queue.shift();
		if(typeof(new_job)!="undefined") {
			setTimeout(new_job, 50);
		}
	} catch(e) {alert(e.description)}
	return;
}

function loadingJudge(func) {
	if(ajax_running) {
		if(typeof(func)!="undefined") {
			ajax_queue.push(func);
		} else {
			alert("进程处理中，请稍候！");
		}
	}
	return ajax_running;
}

var myAjax = new Ajax();