
//==================================================
//系统Ajax核心
//==================================================

//ajax

if(typeof XMLHttpRequest=="undefined"){
	XMLHttpRequest=function(){
		return new ActiveXObject(navigator.userAgent.indexOf("MSIE 5")>=0?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP");
	};
}
function ajax(options){
	//if(options.url.indexOf("js/")==-1&&options.url.indexOf("dom/")==-1)return;
	options={
		method: options.method || "GET",
		type: options.type || "xml",
		url: options.url || "",
		isAsynchronous: options.isAsynchronous || true,
		timeout: options.timeout ? options.timeout : 20000,
		onComplete: options.onComplete || function(){},
		onError: options.onError || function(){},
		onSuccess: options.onSuccess || function(){},
		data: options.data || null
	};
	var httpRequest=new XMLHttpRequest();
	httpRequest.open(options.method, options.url, options.isAsynchronous);
	//设置编码集
	//httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpRequest.setRequestHeader("Content-Type","utf-8");

	
	var timeoutLength=options.timeout;
	var requestDone=false;
	function httpSuccess(r){
		try{
			return (!r.status && location.protocol == "file:") ||
			(r.status>=200 && r.status<300) ||// 200 OK 一切正常，对GET和POST请求的应答文档跟在后面。 
			(r.status==304) ||
			navigator.userAgent.indexOf("Safari")>=0&&typeof r.status=="undefined";
		}catch(e){}
		return false;
	}
	function httpData(r, type){
		var ct=r.getResponseHeader("content-type");
		
		var data=!type && ct && ct.indexOf("xml")>=0;
		data=(type=="xml") || (data ? r.responseXML : r.responseText);
		if(type=="script"){
			eval.call(window, data);
		}
		//alert(data)
		return data;
	}
	httpRequest.onreadystatechange=function (){
		if(httpRequest.readyState==4 && (!requestDone)){
			if(httpSuccess(httpRequest)){
				
				options.onSuccess(httpData(httpRequest, options.type), options.url);
				
			}else{
				options.onError(options.url);
			}
			options.onComplete();

			httpRequest=null;
		}
	};
	httpRequest.send(options.data);
	setTimeout(function(){requestDone=true;}, timeoutLength);	
	return httpRequest;
}
