﻿////////////////////////////////////////////////////////////
// Author:		ldc
// Blog:		http://78992008.QZONE.QQ.COM
// License:		http://78992008.QZONE.QQ.COM
// Email:		ldc003@163.com
////////////////////////////////////////////////////////////
var AjaxRequest = function(url,args){
	this.url			= url || "";
	this.method			= args.method || "post";
	this.params			= args.params || "";
	this.contenttype		= args.contenttype || "application/x-www-form-urlencoded";
	this.charset			= args.charset || "utf-8";
	this.async			= args.async==null?true:args.async;
	this.onComplete			= args.onComplete || null;
	this.onFailure			= args.onFailure || this.onFailed;
	this.onState			= args.onState || null;
	this.ajaxObj			= null;
	this.initAjax();
};
AjaxRequest.prototype = {
	initAjax : function(){
		var self = this;
		if(this.url==""){
			this.onFailure();
			return false;
		}
		this.ajaxObj = this.createAjaxObj();
		if(this.ajaxObj==null){
			this.onFailure();
			return false;
		}
		
		if(this.method.toLocaleUpperCase() == "GET"){
			if(this.url.indexOf("?") != -1){
				if(this.params!=""){
					this.url += "&"+this.params+"&rnd=" + new Date().getTime();
				}else{
					this.url += "&rnd=" + new Date().getTime();
				}
			}else{
				if(this.params!=""){
					this.url += "?"+this.params+"&rnd=" + new Date().getTime();
				}else{
					this.url += "?rnd=" + new Date().getTime();
				}
			}
		}
		
		this.ajaxObj.open(this.method, this.url, this.async);
		this.ajaxObj.onreadystatechange = function()
		{
			self.onStateChange();
		}
		this.setHeaders();
		if(this.method.toLocaleUpperCase() == "GET"){
			this.ajaxObj.send(null);
		}else if(this.method.toLocaleUpperCase() == "POST"){
			this.ajaxObj.send(this.params);
		}else{
			this.onFailure();
		}
	},
	createAjaxObj : function(){
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		}else if (window.ActiveXObject){
			var msXmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
			for (var i = 0; i < msXmls.length; i++){
				try {
					return new ActiveXObject(msXmls[i]);
				}catch (e){
					continue;
				}
			}
		}
		return null;
	},
	onStateChange : function(){
		if(this.onState){
			this.onState(this.ajaxObj.readyState);
		}
		if (this.ajaxObj.readyState == 4){
			if (this.ajaxObj.status == 200){
				if(this.onComplete){
					this.onComplete(this.ajaxObj);
				}
			}else{
				this.onFailure();
			}
			this.ajaxObj = this.params = null;
		}
	},
	setHeaders : function(){
		var headers = {
			'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
		};
		if(this.method.toLocaleUpperCase() == "POST"){
			headers["Content-type"] = this.contenttype + (this.charset ? ';charset=' + this.charset : '');
		}
		for(var t in headers){
			this.ajaxObj.setRequestHeader(t,headers[t]);
		}
	},
	onFailed : function(e){
		//alert("连接服务器失败！");
	}
}