/**
 * 作者：夏孟兵
引用说明: var ajax=new AJAX();
		//ajax.abort_time=1000;//超时时间(默认超时)
		ajax.start(url, method, callback, data, async);
*/
function AJAX() {
	this.abort_time = null;//默认不超时
	var abort_index = null;
	function creatXMLHttp() {
		if (window.ActiveXObject) {
			var msXMLHttp = new Array("Microsoft.XMLHTTP", "MSXML2.XMLHTTP", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.5.0");
			for (var i = 0; i < msXMLHttp.length; i++) {
				try {
					return new ActiveXObject(msXMLHttp[i]);
				}
				catch (e) {
				}
			}
		} else {
			if (window.XMLHttpRequest) {
				try {
					return new XMLHttpRequest();
				}
				catch (e) {
				}
			}
		}
		return null;
	}
	this.start = function (url, method, data,callback, async) {
		var xmlHttp = creatXMLHttp();
		if (xmlHttp == null) {
			return;
		}
		try {
			if(async==null||async!=false)
				xmlHttp.open(method, url, true);
			else
				xmlHttp.open(method, url, false);
		}
		catch (e) {
   		//跨域提示无权限
			alert(e);
		}
		if (this.abort_time != null) {
			abort_index = setTimeout(function () {
				ajax_abort(xmlHttp);
			}, this.abort_time);
		}
		//有回调方法时回调
		if (callback != null && typeof (callback) == "function") {
			onStateChange(xmlHttp, callback);
		}
		if (method == null || method.toLowerCase() != "get") {
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		xmlHttp.send(data);
		try {
			data = null;
			url = null;
			callback=null;
			method = null;
			async = null;
			this.abort_time = null;
   			//CollectGarbage();
		}
		catch (e) {
		}
	}
	function onStateChange(xmlHttp, callback) {
		if (window.ActiveXObject) {
			xmlHttp.onreadystatechange = function () {
				if (xmlHttp.readyState == 4) {
					try {
						callback(xmlHttp);
					}
					catch (e) {
					}
				}
			};
		} else {
			xmlHttp.onload = function () {
				try {
					callback(xmlHttp);
				}
				catch (e) {
				}
			};
		}
	}
	function ajax_abort(xmlHttp) {
		try {
			window.clearTimeout(abort_index);
		}
		catch (e) {
		}
		try {
			if (xmlHttp != null) {
				xmlHttp.abort();
			}
		}
		catch (e) {
		}
	}
}
/**
 * 默认的ajax操作
 */
function startAjax(url,data,callback)
{
	var ajax=new AJAX();
	ajax.start(url,"post",data,callback,true);
}
