/*
    ---- 超轻量级Ajax功能封装,不需要其他的js库支持 ---
*/
function ajaxInfo(_url)
{
	var T = this;
	this.method = "GET";  	//GET | POST | HEAD
	this.url = (_url?_url:null);  //请求地址
	this.asynch = true;  	//异步
	this.container= null; 	//容器,接收返回的数据
	this.loadFunc = null; 	//正在载入调用
	this.okFunc   = null; 	//载入成功调用
	this.errFunc  = null; 	//载入失败调用
	this.sendContent = null;  		//send发送的内容
	this.requestType = "TEXT"; 		//"TEXT" | "XML" | "ALLHEADER" | "HEADER"
	this.contentType = null  		//设置请求的: Content-Type
	this.headerName  = null;   	//请求的头部名称
	this.xmlHttpRequest = null;  	//保存XMLHttpRequest请求对象
	
	//赋予T.xmlHttpRequest一个可用的httpXMLRequest对象
	this.prepareXMLHttpRequest = function()
	{	
		if(T.xmlHttpRequest)//T.xmlHttpRequest不为空，则直接重置后返回.
		{   
			T.xmlHttpRequest.abort();
			return ;
		}
		if(ajaxInfo.prototype.XmlRequestArray.length>0) //有空闲的xmlRequest对象，取一个赋给T.xmlHttpRequest
		{
			T.xmlHttpRequest = ajaxInfo.prototype.XmlRequestArray.pop();
		}
		else //没有空闲的xmlRequest对象，建立一个赋给T.xmlHttpRequest
		{
			var objXMLHttp;
			if (window.XMLHttpRequest)	{	objXMLHttp = new XMLHttpRequest();	}
			else
			{	var MSXML=['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
				for(var n = 0; n < MSXML.length; n ++)
				{	
					try	{	objXMLHttp = new ActiveXObject(MSXML[n]);	break;	}
					catch(e){}
				}
			}
			T.xmlHttpRequest = objXMLHttp;
		}
		T.xmlHttpRequest.abort();//重置
	}

	//根据请求类别，填充数据
	this.fill = function()
	{
		if(T.requestType=="XML")
		{	
			T.container= T.xmlHttpRequest.responseXML;
		}
		else if(T.requestType=="TEXT")
		{	
			T.container = T.xmlHttpRequest.responseText;
		}
		else if(T.requestType=="ALLHEADER")
		{	
			T.container = T.xmlHttpRequest.getAllResponseHeaders();
		}
		else if(T.requestType=="HEADER")
		{	
			T.container = T.xmlHttpRequest.getResponseHeader(T.headerName);
		}
		else
		{
			T.container = "不支持的请求类型.";
		}
	}
	
	//发送请求，填充T.container，可以带一个url参数.
	this.get = function (_url)
	{
		if(T.xmlHttpRequest && ajaxInfo.prototype.useTime<ajaxInfo.prototype.timeOut)
		{
			setTimeout(function(){T.get(_url)},100);
			ajaxInfo.prototype.useTime+=100;
			return;
		}
		ajaxInfo.prototype.useTime = 0;
		T.url = _url || T.url;
		if(T.url=="" || T.url==null)
		{	//确保有请求地址,可根据需要修改，弹出错误提示或抛出异常？
			alert(ajaxInfo.prototype.NoUrl); 
			return;
		}
		T.prepareXMLHttpRequest();
		if(!T.xmlHttpRequest)
		{	//确保建立请求对象，根据需要修改，弹出错误提示或抛出异常？
			alert(ajaxInfo.prototype.NoXMLHttpRequest); 
			return;
		}

		if(T.asynch) 
		{	//设置异步操作
			T.xmlHttpRequest.onreadystatechange = function()
			{	switch (T.xmlHttpRequest.readyState)
				{	
					/*	0 － （未初始化）还没有调用send()方法
						1 － （载入）已调用send()方法，正在发送请求
						2 － （载入完成）send()方法执行完成，已经接收到全部响应内容
						3 － （交互）正在解析响应内容
						4 － （完成）响应内容解析完成，可以在客户端调用了
					*/
					case 0:
					case 1:
					case 2:
					case 3:
						if(T.loadFunc)
						{ 
							T.loadFunc(T.xmlHttpRequest.readyState); 
						}
						break;
					case 4:	
						if(T.xmlHttpRequest.status==200)
						{	
							T.fill();
							if(T.okFunc)
							{
								T.okFunc(T.xmlHttpRequest.status);   
							}
							ajaxInfo.prototype.XmlRequestArray.push(T.xmlHttpRequest);
							T.xmlHttpRequest = null;
						}
						else
						{	
							if(T.errFunc)
							{
								T.errFunc(T.xmlHttpRequest.status); 
							}
						}
						break;
					default:
						alert("出界拉! 杂个会这样呢...");
						break;
				}
			}
		}
		
		T.xmlHttpRequest.open(T.method,T.url,T.asynch);//发送请求
		if(T.contentType)	
		{
			T.xmlHttpRequest.setRequestHeader("Content-Type", T.contentType);
		}
		T.xmlHttpRequest.send(T.sendContent);
		if(!T.asynch)//同步请求操作(兼容firefox,firefox在同步的情况下无法正常调用onreadystatechange设定的函数)
		{
			T.fill();
			ajaxInfo.prototype.XmlRequestArray.push(T.xmlHttpRequest);
			T.xmlHttpRequest = null;
		}
	}
}
ajaxInfo.prototype.NoXMLHttpRequest = "您的浏览器不支持XMLHttpRequest，请升级浏览器以便正常访问";
ajaxInfo.prototype.NoUrl = "请求地址为空，操作失败.";
ajaxInfo.prototype.ContenTypeForm = "application/x-www-form-urlencoded";
ajaxInfo.prototype.timeOut = 5000;//超时时间
ajaxInfo.prototype.useTime = 0;	  //耗时
ajaxInfo.prototype.XmlRequestArray = new Array(); //保存空闲的HttpXMLRequest对象,以便复用