﻿/*-----Ajax_V2.04版本[2008/5/14最后修改]-----
* 作者：袁琳(monkey-lin@163.com)
*****************************************/
var Ajax=function(){
	
	//--------内部配制参数--------
   this.localhost	="http://www.whccb.com/";					//设置URL参数总地址（默认为网站绝对地址）
//	this.localhost	="/";					//设置URL参数总地址（默认为网站绝对地址）
	this.setType	=true;					//设置传输方式（true：异步 false：同步）
	this.pageCode	="utf-8";				//字符编码
	this.method		="GET";					//传送方式（GET/POST）
	this.returnType	="XML";					//信息返回类型（XML：XML格式/Text：文本格式）
	this.isEncode	=true;					//是否使用自动encodeURI()后发送代码
	
	//----SOAP及POST参数----
	this.webMethod	=null;					//Soap参数：Web方法（不为空时开启SOAP发送方式）
	this.argObj		=null;					//参数对象（Soap对象及Post）
	
	//----调试参数----
	this.isTishi	=false;					//设置是否开启[流程提示]
	this.isDebug	=true;					//设置是否开启[错误调试]
	this.showState	=false;					//设置是否开启[动态数据获取]提示
	
	//----外部调用通用参数----
	this.rtnFunObj	=null;					//设置返回处理XML的函数对象
	this.rtnFunName	=null;					//设置返回处理XML的函数名
	this.url		=null;					//相对地址及参数
	this.showObj	=null;					//显示提示的对象
	
	if(this.isTishi){alert("[AJAX提示] 已完成参数初始设置过程！");}
}

	Ajax.prototype.send=function(){
		var postUrl="";
		if(this.isEncode){
			postUrl=encodeURI(this.localhost+this.url);
		}else{
			postUrl=this.localhost+this.url;
		}
		var XMLRequest 	=this.createXMLRequest();
		var funObj		=this.funObj;
		var showState	=this.showState;
		var isTishi		=this.isTishi;
		var isDebug		=this.isDebug;
		var url			=postUrl;
		var showObj		=this.showObj;
		var rtnFunObj	=this.rtnFunObj;
		var rtnFunName	=this.rtnFunName;
		var returnType	=this.returnType;
		XMLRequest.onreadystatechange = function(){
			
			//判断信息返回状态
			if(XMLRequest.readyState<4){
				if(showState){checkState(showObj,"数据读取中...");}
				
			//------信息返回成功------	
			}else if(XMLRequest.readyState==4){			
				if(XMLRequest.status == 200){
					
					var returnInfo;
					switch(returnType){
						
						//返回XML格式
						case "XML":
							returnInfo=XMLRequest.responseXML;
							break;
							
						//返回Text格式
						case "Text":
							returnInfo=XMLRequest.responseText;
							break;

						//默认返回XML格式
						default:
							returnInfo=XMLRequest.responseXML;
							break;
					}
					if(rtnFunObj!=null&&rtnFunName!=null){
						if(isTishi){alert("[AJAX提示] 返回信息成功，根据设置，系统选择第一种函数返回方式，返回的信息如下：");alert(returnInfo);}					
						rtnFunObj.returnFunction=rtnFunName;
						rtnFunObj.returnFunction(returnInfo);
					}else if(rtnFunName!=null){
						if(isTishi){alert("[AJAX提示] 返回信息成功，根据设置，系统选择第二种函数返回方式，返回的信息如下：");alert(returnInfo);}
						rtnFunName(returnInfo);
					}
				//------页面不正常------
				}else{
					if(showObj){checkState(showObj,"<font color=red>服务端程序异常！</font>");}
					if(isDebug){prompt("[错误警告] 下面是出错的地址信息",url);}
				}
			}
		}
		XMLRequest.open(this.method,postUrl,this.setType);
		XMLRequest.setRequestHeader("If-Modified-Since","0");
		XMLRequest.setRequestHeader("Content-Type", "text/xml;charset="+this.pageCode);
		if(this.webMethod!=null){XMLRequest.setRequestHeader("SOAPAction", this.webMethod);}
		if(this.isTishi){prompt("[AJAX提示] 请求发送成功(以下地址可用于拷贝)：",postUrl);}
		XMLRequest.send(this.argObj);
	}
	
	Ajax.prototype.createXMLRequest=function(){
	
		//初始化、指定处理函数、发送请求的函数
		var http_request = false;
		
		//开始初始化XMLHttpRequest 对象
		if(window.XMLHttpRequest){
			
			//Mozilla 浏览器
			http_request = new XMLHttpRequest();
			if(http_request.overrideMimeType){
				
				//设置MiME 类别
				http_request.overrideMimeType("text/xml");
			}
			
		// IE 浏览器选择XML版本
		}else if(window.ActiveXObject){ 
			try{//------ 创建XML3.0版本
				http_request = new ActiveXObject("Msxml2.XMLHTTP.3.0");
			}catch(e){
				try{//------创建XML2.0版本
					http_request = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{//------创建XML1.0版本
						http_request = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e){}
				}
			}
		}
		if(!http_request){ 
		
			//----异常，创建对象实例失败
			if(this.isTishi||this.isDebug){alert("[错误警告]不能创建XMLHttpRequest对象实例！");}
			return false;
		}else{
			if(this.isTishi){alert("[AJAX提示] 建立XMLHttpRequest对象完成！");}
			return http_request;
		}
	}

function checkState(docContent,str){
	docContent.innerHTML="<table width='100%' cellpadding='0' cellspacing='0'>";
	docContent.innerHTML+="<tr><td align='center' valign='middle'>"+str+"</td></tr></table>";
}

function loadXMLDoc(dname){
	var xmlDoc;
	
	// 针对 IE 的代码
	if (window.ActiveXObject){
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	
	// 针对 Mozilla、Firefox、Opera 的代码
	}else if (document.implementation && document.implementation.createDocument){
		xmlDoc=document.implementation.createDocument("","",null);
	}else{
		alert('您的浏览器无法处理此脚本');
	}
	
	xmlDoc.async=false;
	xmlDoc.load(dname);
	return xmlDoc.xml;
}

if(navigator.userAgent.indexOf('MSIE') ==-1){
	XMLDocument.prototype.__proto__.__defineGetter__("text",function(){
		try{
			return this.firstChild.textContent;
		}catch(e){
			return "";
		}
	});
	
	Element.prototype.__proto__.__defineGetter__("text",function(){
		return this.textContent;
	});
}
