/*************************************************************/
//Ajax模块
// this code create XMLHTTP Object
//edit by hyl 2006年7月30日
/*************************************************************/
//httpPool连接池
var httpPool=
{
	_maxPoolLength : 10,//连接池大小
	_httpPool : [],//连接池数组,用来存储xmlhttp连接实例
	//////////////////////////////////////////////////////////
	_getXmlHttp : function(){//得到一个空闲的xmlhttp
		var _xmlhttp = null;
		var _pool = this._httpPool;
		//遍历连接池
		for(var i=0;i<_pool.length;i++){
			if(_pool[i].readyState==4||_pool[i].readyState==0){//表示一个已经完成或者未初始化的xmlhttp连接
				_xmlhttp=_pool[i];
				break;
			}
		}
		//如果未在连接池中得到合适的实例
		if(_xmlhttp==null){
			_xmlhttp=this._getNewXmlHttpConnection();
		}
		return _xmlhttp;
	},
	//生产一个新的XMLHTTP对象实例
	_getNewXmlHttpConnection : function(){
		if(this._httpPool.length<this._maxPoolLength)//限制在连接池大小内,如果超过此过此范围不处理
		{
			var xmlObj=this._getActiveXObject();//得到一个新的连接对象
			if(xmlObj!=null){
				this._httpPool.push(xmlObj);//压入连接池(填充);
			}
			return xmlObj;//返回实例
		}
		else
		return null;
	},
	//得到一个合适的xml组件
	_getActiveXObject : function(){
		var ActiveObj=null;
		  try { ActiveObj = new ActiveXObject("Msxml2.XMLHTTP"); }
		  catch (e) { try { ActiveObj = new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch (e) { try { ActiveObj = new XMLHttpRequest(); }//fireFox浏览器
		  catch (e) { ActiveObj = null; }}}
		return ActiveObj;
	}
};
/********************************************************************************/
//连接应用
function httpConnection()
{
  var xmlhttp=null;
  bComplete = false;//标识当前的Connection是否已完毕
  xmlhttp=httpPool._getXmlHttp();//从连接池中得到一个合适的连接
  if (!xmlhttp) return null;
  //connect方法
  //参数:处理地址,请求方式,参数,回调函数
  this.url__ = "";
  this.debug =function(){
  	window.open(this.url__);
  };
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    this.url__ =sURL+"?"+sVars;
	//发送GET请求:地址?变量=值的方式
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars,false);
        sVars = "";
      }
      else//发送POST请求
      {
        xmlhttp.open(sMethod, sURL, true);//true打开异步模式,当状态改变时会调用onreadystatechange属性指定的回调函数
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
		xmlhttp.setRequestHeader("Content-Length",sVars.length);
	  	xmlhttp.setRequestHeader("Connection", "Keep-Alive");
		xmlhttp.setRequestHeader("Pragma", "no-cache");
		xmlhttp.setRequestHeader("Cache-Control", "no-cache");
	  //回调方法
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4)//ajax线程处理完毕
        {
			if(xmlhttp.status==200){
					if(!bComplete){
					  bComplete = true;
					  //alert(xmlhttp.getResponseHeader("Content-Type"));
					  fnDone(xmlhttp);//回调函数处理返回的XMLHTTP对象
				  }
			  }
			  else{
			  fnDone(null);
			  }
        }
		};
      xmlhttp.send(sVars);//GET请求发送为空，POST请求发送"名=值"对
    }
    catch(z) { return false; }
    return true;
  };
  //返回当前对象httpConnection
  return this;
}
