/**
*Ajax
*/
function Ajax()
{
this.getClassName=function(){return "Ajax";}
this._XMLHttpReq=this._createXMLHttpRequest();
}

  
Ajax.prototype._ExecuseResponseFunc=null;
Ajax.prototype.url=null;
Ajax.prototype.parameters=null;
Ajax.prototype._XMLHttpReq=null;
Ajax.prototype.isWorking=false;


/**
*返回response，如有异常，则返回null
*/
Ajax.prototype.getResponse=function(){

if(this._XMLHttpReq!=null&&this._XMLHttpReq.readyState == 4&&this._XMLHttpReq.status == 200)
   {
     return this._XMLHttpReq.responseText;
   }
else{
   return null;
  }
}
   



/**
* 判断浏览器，创建XMLHttpRequest对象
*/
Ajax.prototype._createXMLHttpRequest=function()
{

  if(this._XMLHttpReq==null)
  {
   try{
      if(window.XMLHttpRequest)
       {
      // 直接使用XMLHttpRequest函数来创建XMLHttpRequest对象
      this._XMLHttpReq = new XMLHttpRequest();
        }
         // 对于IE浏览器
     else if (window.ActiveXObject)
     {
      try
      {
         // 使用AcitveXObject函数创建浏览器
         this._XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
         // 如果出现异常，再次尝试以如下方式创建_XMLHttpRequest对象
         try
         {
            this._XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e)
         {
         }
      }
   }
   }
   catch(e){_throwErr("_createXMLHttpRequest():","不能创建XMLHttpRequest对象"); }
   }
   
   return this._XMLHttpReq;
}


/**
*@parameters:{parameterName:parameterValue,parameterName:parameterValue},
               或者为参数字符串，形如：key1=***&key2=***
*Get请求时，如果已经拼接在url中，则给null
*/
Ajax.prototype._sendRequest=function(method, url, parameters, execuseResponseFunction)
{
if(!this.isWorking){
try{
   this.isWorking=true;
   this.url=url;
   this._ExecuseResponseFunc=execuseResponseFunction;
   var _ajaxObj=this;
   var paramString="";
   if(parameters!=null&&typeof(parameters)!="string")
   {   
    for(var param in parameters)
    {
     paramString=paramString+(paramString=="" ? '' : '&')+param+'='+(parameters[param]==null ? "":parameters[param]);
    }
   }
   else
   {
   paramString=(parameters==null?"":parameters);
   }
   paramString=paramString+(paramString=="" ? '' : '&')+'mrtype=1';//参数添加mrtype=1
   this.parameters=paramString;
   if(method.toLowerCase() == "post")
   {
      this._XMLHttpReq.open(method, url, true);
      this._XMLHttpReq.onreadystatechange = function(){_ajaxObj._execuseResponse();}
      this._XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      this._XMLHttpReq.send(paramString);
     
   }
   else
   {
     paramString!="" ? (url += (url.match(/\?/) ? '&' : '?') + paramString):"";
     this._XMLHttpReq.open(method, url, true);
     this._XMLHttpReq.onreadystatechange = function(){_ajaxObj._execuseResponse();}
     this._XMLHttpReq.send(paramString);
      }
    }catch(e)
    {
     alert(e.message);
    }
    finally{ }
   }
}


/**
*调用回调函数，回调函数无参数
*/
Ajax.prototype._execuseResponse=function()
{
if (this._XMLHttpReq.readyState == 4)
{

       this.isWorking=false;  
   // 判断对象状态
   if (this._XMLHttpReq.status == 200&&this._ExecuseResponseFunc!=null)
   {
      // 信息已经成功返回，开始处理信息
     this._ExecuseResponseFunc(this._XMLHttpReq.responseText);
   }
   else
   {
   _throwErr("_execuseResponse()","服务器返回结果异常!请检查url和服务器端");
   }
}
}

/**
*发送get请求,回调函数可以访问getResponse()
*@param:url 发送请求的地址
*@param:parameters 发送请求的参数map:{parameterName,parameterValue,parameterName,parameterValue,...}
                      或者为参数字符串，形如：key1=***&key2=***
*@param:execuseResponseFunc 回调函数
*/
Ajax.prototype.sendGetRequest=function(url,paramters,execuseResponseFunc)
{
this._sendRequest("get", url, paramters, execuseResponseFunc);
}


/**
*发送post请求,回调函数可以访问getResponse()
*@param:url 发送请求的地址
*@param:parameters 发送请求的参数map:{parameterName,parameterValue,parameterName,parameterValue,...}
                      或者为参数字符串，形如：key1=***&key2=***
*@param:execuseResponseFunc 回调函数
*/
Ajax.prototype.sendPostRequest=function(url, parameters, execuseResponseFunc)
{
this._sendRequest("post", url, parameters, execuseResponseFunc);
}

Ajax.prototype.toString=function()
{
 return "[parameters of Ajax:"+this.parameters+",url of Ajax:"+this.url+",xmlHttpRequest of Ajax:"
        +this._XMLHttpReq+",response:"+this.getResponse()+"]";
}

