/* 封装的AJAX类
 * @param act		   string   提交方式 POST/GET
 * @param requesturl   string   提交的asp文件
 * @param param        string   提交地址后跟的参数
 * @param returnformat string   以何种方式返回 TEXT/XML
 * @param event        function 回调的用户函数
 * @param bindobj	   string   以前获取的数据后绑定的对象是固定，如果是动态的就需要把变量用这个传进来了
 *
 * var request.readyState HTTP就绪状态
 *	   0:请求没有发出(在调用open()之前)
 *	   1:请求已经建立但还没有发出(调用send()之前)
 *	   2:请求已经发出正在处理之中(这里通常可以从响应得到内容头部)
 *	   3:请求已经处理，响应中通常有部分数据可用， 但是服务器还没有完成响应
 *	   4:响应已完成，可以访问服务器响应并使用它
 *
 * 使用方法：
 *	   html:
 *			<script type="text/javascript" src="ajax.js"></script> //引入js文件
 *			<div id='divid'></div>
 *			<script>
 *				var ajax = new MyAjax("POST", "http://localhost/mycode/js/lb.asp", "uid=20&title="+你好", "text", myfunction, bindobj);
 *				function myfunction(response, bindobj){
 *					document.getElementById(bindobj).innerHTML = response;
 *				}
 *			</script>
 *
/*---------------------------------------------------------*/

function Ajax(async){
	this.async = async ? true : false;

	this.init = function(){
		try{
			this.handler = new XMLHttpRequest();
			return true;
		}catch(e){
			try{
				this.handler = new ActiveXObject('Microsoft.XMLHTTP');
				return true;
			}catch(e){
				try{
					this.handler = new ActiveXObject('Msxml2.XMLHTTP');
					return true;
				}catch(e){
					return false;
				}
			}
		}
	}

	this.not_ready = function(){
		return (this.handler.readyState && (this.handler.readyState < 4));
	}
	
	this.onreadystatechange = function(event){
		if (!this.handler)
			this.init();
		
		if (typeof event == 'function') {
			this.handler.onreadystatechange = event;
		}else{
			alert('XML Sender OnReadyState event Must function');
		}
	}
	
	this.post = function(desturl, datastream){
		if (!this.handler) {
			this.init();
		}
		if (!this.not_ready()) {
			this.handler.open('POST', desturl, this.async);
			if (typeof(this.handler.setRequestHeader) != 'undefined') {
				this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			}
			this.handler.send(datastream);
			if (!this.async && this.handler.readyState == 4 && this.handler.status == 200) {
				return true;
			}
		}
		return false;
	}
	
	this.get = function(desturl, datastream) {
		if (!this.handler) {
			this.init();
		}
		if (!this.not_ready()) {
			this.handler.open('GET', desturl + "?" + datastream, this.async);
			this.handler.send(null);
			if (!this.async && this.handler.readyState == 4 && this.handler.status == 200) {
				return true;
			}
		}
		return false;
	}
}

function MyAJAX(act, requesturl, param, returnformat, event, bindobj){
	this.act = act;
	this.requesturl = requesturl;
	this.param = param;
	this.returnformat = returnformat
	this.callback = event;
	this.xml = null;

	this.user = function(){
		this.xml = new Ajax(true);
		if (this.act.toLowerCase() == 'post'){
			this.xml.post(this.requesturl, this.param);
			this.xml.onreadystatechange(this.callBackResponse);
		} else if(this.act.toLowerCase() == 'get'){
			this.xml.get(this.requesturl, this.param);
			this.xml.onreadystatechange(this.callBackResponse);
		}
	}
	
	this.urlencode = function(text){
		return escape(text).replace(/\+/g, "%2B");
	}
	
	var me = this;
	
	this.callBackResponse = function(){		
		if (me.returnformat.toLowerCase() == 'xml'){
			if (me.xml.handler.readyState == 4 && me.xml.handler.status == 200 && me.xml.handler.responseXML){
				me.callback(me.xml.handler.responseXML, bindobj);
				//alert(me.xml.handler.responseXML.documentElement);
			}
		} else {
			if (me.xml.handler.readyState == 4 && me.xml.handler.status == 200 && me.xml.handler.responseText){
				me.callback(me.xml.handler.responseText, bindobj);
			}
		}
	}
	
	this.user();
}

