/**
 * 作者： 许德建
 */

function Ajax() {
	
	this.ajaxUrl = "../../ajax";
	
	//xml结果方式
	this.xml = false;
	
	//返回结果
	this.result = null;
	
	//返回代码
	this.code = 0;
	
	//返回消息信息
	this.msg = null;
	
	//页面状态
	this.status = 0;
	
	//请求对象
	this.xmlhttp = null;
	
	//是否异步
	this.async = true; 
	
	//数据对象
	this.dataDiv = null;
	
	//是否显示loading
	this.showLoading = true;
	
	this.loadingImg = null;
	
	//操作按钮
	this.oInput = null;
	
	this.init = function() {
		this.code = 0;
		this.msg = null;
		this.status = 0;
		this.result = null;
	};
	
	this.parseResult = function() {
		this.init();
		if(this.xmlhttp == null) {
			return;
		}
		this.status = this.xmlhttp.status;
		if(this.xml) {
			this.result = this.xmlhttp.responseXML;
			if(this.result != null && this.result.documentElement != null) {
				var root = this.result.documentElement;
				this.code = root.getAttribute("code");
				if(this.code == null || this.code == "" || isNaN(this.code)) this.code = 0;
				else this.code = parseInt(this.code);
				this.msg = root.getAttribute("msg");
			}
		}
		else {
			this.result = this.xmlhttp.responseText.replace(/^\s+|\s+$/g, "");
			var index = -1;
			if(this.result.indexOf("<RETURN>") == 0 && (index = this.result.indexOf("</RETURN>")) != -1) {
				var codemsg = this.result.substr(8, index - 8);
				this.result = this.result.substr(index + 10);
				var codemsgs = codemsg.split(":");
				if(codemsgs[0] == null || codemsgs[0] == "" || isNaN(codemsgs[0])) this.code = 0;
				else this.code = parseInt(codemsgs[0]);
				if(codemsgs[1] != null) this.msg = codemsgs[1];
			}
		}
		
	};
	
	this.post = function(url, postContent, method) {
		if(this.xmlhttp) this.xmlhttp.abort();
		
		// code for Mozilla, etc.
		if (window.XMLHttpRequest) this.xmlhttp = new XMLHttpRequest();
		// code for IE
		else if (window.ActiveXObject) this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	
		if(!this.xmlhttp) {
			alert("无法实例XMLHttp对象，可能您的浏览器不支持");
			return false;
		}
		if(this.async) {
			if(this.showLoading && this.dataDiv != null && !this.xml) {
				if(this.loadingImg != null && this.loadingImg.length > 0) this.dataDiv.innerHTML = '<div align=center ><img src="/images/cmn/'+this.loadingImg+'" /></div>';
				else this.dataDiv.innerHTML = '<div align=center style="padding-top:100px;"><img src="/images/cmn/loader-bar.gif" /></div>';
			}
			if(this.oInput != null) {
				this.oInput.disabled = true;
			}
			var _this = this;
			this.xmlhttp.onreadystatechange = function() {
				// if xmlhttp shows "loaded"
				if (_this.xmlhttp.readyState == 4) {
					if(_this.oInput != null) {
						_this.oInput.disabled = false;
					}
					_this.parseResult();
					if(_this.dataDiv != null && !_this.xml) {
						_this.dataDiv.innerHTML = _this.result;
					}
					_this.callback();
				}
			};
		}
		
		if(method != null && method.toLowerCase() == "get") {
			if(url.indexOf("?") != -1) {
				url += "&" + postContent
			}
			else {
				url += "?" + postContent
			}
			this.xmlhttp.open("GET", url, this.async);
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			this.xmlhttp.setRequestHeader("User-Agent", "MyCustomUser");
			this.xmlhttp.setRequestHeader("If-Modified-Since", "0");
			this.xmlhttp.setRequestHeader("pragma", "no-cache");
			this.xmlhttp.setRequestHeader("Cache-Control", "no-cache,   must-revalidate");
			this.xmlhttp.setRequestHeader("expires", "-1");
			this.xmlhttp.send(null);
		}
		else {
			this.xmlhttp.open("POST", url, this.async);
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.xmlhttp.setRequestHeader("User-Agent", "MyCustomUser");
			this.xmlhttp.setRequestHeader("If-Modified-Since", "0");
			this.xmlhttp.setRequestHeader("pragma", "no-cache");
			this.xmlhttp.setRequestHeader("Cache-Control", "no-cache,   must-revalidate");
			this.xmlhttp.setRequestHeader("expires", "-1");
			this.xmlhttp.send(postContent);
		}
		if(!this.async) {
			this.parseResult();
		}
	
	};
	
	this.submit = function(oForm) {
	  this.post(oForm.action, parseForm(oForm), oForm.method);
	};
	
	this.execute = function(id, doc) {
		this.post(this.ajaxUrl + "?id=" + id, doc);
	};

	this.select = function(oSelect, url, postContent, header, initValue) {
		this.xml = false;
		this.async = false;
		this.post(url, postContent, "post");
		var optionHTML = this.result
		if(header != null) optionHTML = header + optionHTML;
		if(initValue) {
			var v = oSelect.value;
			if(v != null && v.length > 0) {
				var reg = new RegExp("\\svalue=(\"|'|)"+v+"(\"|'|)\\s*>");
				optionHTML = optionHTML.replace(reg, " value=\"" + v + "\" selected >");
			}
		}
		oSelect.innerHTML = "";
		oSelect.outerHTML = oSelect.outerHTML.replace(/<\/select>$/i, optionHTML + "</select>");
	};
	
	this.fetchSeq = function(key) {
		var ajax = new Ajax();
		ajax.async = false;
		ajax.execute("com.xu.sys.service.SeqService&code="+key);
		if(ajax.code > 0) return ajax.msg;
		else if(ajax.msg != null) alert(ajax.msg);
		else alert("系统出错");
		return null;
	};
	
}

Ajax.prototype.callback = function() {
};

function parseForm(oForm) {
	if(oForm == null) return "";
	var elements = oForm.elements;// Enumeration the oForm elements
	var element;
	var postContent = "";// oForm contents need to submit
	for(var i = 0; i < elements.length; i++) {
		element = elements[i];
		if(element.name == null || element.name == "") {
			continue;
		}
		if(element.type == "text" || element.type == "textarea" || element.type == "hidden") {
			if(postContent.length > 0) postContent += "&";
			postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value); 
		}
	    else if(element.type=="select-one"||element.type=="select-multiple") {
	    	var options = element.options, item;
			for(var j = 0; j < options.length; j++) {
	    		item=options[j];
	    		if(item.selected) {
	    			if(postContent.length > 0) postContent += "&";
    				postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(item.value);
				}
			}
		} 
		else if(element.type == "checkbox" || element.type == "radio") {
			if(element.checked) {
    			if(postContent.length > 0) postContent += "&";
    			var val = element.y != null ? "Y" : element.value;
				postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(val);
			}
			else if(element.y != null){
    			if(postContent.length > 0) postContent += "&";
				postContent += encodeURIComponent(element.name) + "=N";
			}
		}
		else if(element.type == "file") {
			if(element.value != "") {
    			if(postContent.length > 0) postContent += "&";
				postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value);
			}
		}
		else {
			if(postContent.length > 0) postContent += "&";
			postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value);
		}
	}
	return postContent;
}