// XMLHttpRequest封装器HttpClient
//zwli 2008-3
/*	使用方法：
	var client = new HttpClient();
	client.isAsync = true; //（可选）设为异步请求，默认为true(异步请求)
	client.isResponseXML = false; //（可选）设定callback是否返回responseXML，默认为false返回responseText
	client.callback = function(result) {
		//当isResponseXML为true时result为xml dom对象，否则为文本
		document.getElementById("txtInfo").value = result;
	}; //(可选)请求已完成返回
	
	//（必选）发送请求
	client.makeGetRequest("ajax_gettime.asp?t="+ (new Date()).getTime());
	//client.makePostRequest("ajax_gettime.asp?t="+ (new Date()).getTime(), 'name=peter&pwd=1234');
*/
function HttpClient() {
}
HttpClient.prototype = {
	//当设置为true时，将发出异步调用
	isAsync:true,
	//XMLHttpRequest实例
	xmlhttp:false,
	
	//标识callback里返回的是否responseXML,默认为responseText
	isResponseXML:false,
	//发送异步调用后将被调用的内容
	callback:false,
	
	//当调用XMLHttpRequest的send方法时触发
	onSend:function() {
	},
	
	//当readyState的值为4时触发
	onLoad:function() {
	},
	
	//当出现http错误时触发
	onError:function(error) {
		
	},
	
	//调用XMLHttpRequest的open后出发，一般用于设置setRequestHeader
	onXMLHttpOpen:function(xmlhttp) {
	},
	
	//实例化XMLHttpRequest
	init:function() {
		try {
			//Mozilla /Safari
			this.xmlhttp = new XMLHttpRequest();
		}catch(e) {
			//IE
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
			var success = false;
			for(var i = 0; i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				}catch(e) {}
			}
			if(!success) {
				this.onError('无法创建必须的 XMLHttpRequest 对象.');
			}
		}
	},
	
	makeGetRequest:function(url) {
		this.makeRequest('GET', url, null);
	},
	
	makePostRequest:function(url, content) {
		this.makeRequest('POST', url, content);
	},
	//发出页面请求的方法
	//@method, 请求方法：GET或POST
	//@url, 请求的页面
	//@content, 如果method是POST时才需要
	makeRequest:function(method, url, content) {
		if(!this.xmlhttp) {
			this.init();
		}
		this.xmlhttp.open(method, url, this.isAsync);
		//this.xmlhttp.setRequestHeader("Content-Length", content.length);
		this.xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
		this.onXMLHttpOpen(this.xmlhttp);
		
		//重新设置onreadystatechange，因为Mozilla中完成一个调用后需要重新设置
		var self = this;
		this.xmlhttp.onreadystatechange = function() {
			self._readyStateChangeCallback();
		}
		
		this.xmlhttp.send(content);
	},
	
	//处理readystatechange
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				this.onSend();
				break;
			case 4:
				this.onLoad();
				if(this.xmlhttp.status == 200) {
					if(this.isResponseXML) {
						this.callback(this.xmlhttp.responseXML);
					}else {
						this.callback(this.xmlhttp.responseText);
					}
					
				}else{
					this.onError('HTTP Error Making Request: ' + 
								 '[' + this.xmlhttp.status + ']' +
								 this.xmlhttp.statusText);
				}
				break;
		}
	}
}
	