/*AJAX对象封装*/
var com=new Object();
com.trs=new Object();
com.trs.cds=new Object();
com.trs.cds.js=new Object();
com.trs.cds.js.ajax=new Object();//命名空间

//请求状态
com.trs.cds.js.ajax.READY_STATE_UNINITIALIZED=0;
com.trs.cds.js.ajax.READY_STATE_LOADING=1;
com.trs.cds.js.ajax.READY_STATE_LOADED=2;
com.trs.cds.js.ajax.READY_STATE_INTERACTIVE=3;
com.trs.cds.js.ajax.READY_STATE_COMPLETE=4;
//构造函数
com.trs.cds.js.ajax.AjaxRequest=function()
{
  //申请XMLHttpRequest
  this.initRequest=function()
  {
   if(typeof window.XMLHttpRequest!='undefined') 
   {
   // branch for native XMLHttpRequest object
    try {
     this.req = new XMLHttpRequest();
     } catch(e) {
     this.req = false;
    }
   }
   else if(window.ActiveXObject) 
   {
   // branch for IE/Windows ActiveX version
    try 
    {
     this.req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) 
    {
     try 
     {
       this.req = new ActiveXObject("Microsoft.XMLHTTP");
     }catch(e) 
     {
      this.req = false;
     }
    }
   }
  };
  //返回XML对象
  this.getResponseXML=function()
  {
   if(this.req)return this.req.responseXML;
  };
  //返回Text对象
  this.getResponseText=function()
  {
   if(this.req)return this.req.responseText;
  };
  //设置URL
    this.setUrl=function(url)
    {
     this.url=(url)?url:"";
    };
  //设置Method:取值范围有GET,POST,HEAD等
    this.setMethod=function(method)
    {
     this.method=(method)?method:'GET';
    };
    //设置内容加载完成的执行体,如setOnLoad("a('abc')"),将在请求完成时执行a('abc');这样的子句
    this.setOnLoad=function(onload)
    {
   this.onload=onload;
   if(this.onload.indexOf('(')==-1)
   {
    this.onload=this.onload+"()";
   }
    };
    //设置同步或者异步:true,异步;flase,同步
    this.setAasynchronous=function(asy)
    {
   this.asynchronous=(asy)?asy:true;
    };
    //设置POST方式下提交的参数
    this.setParams=function(params)
    {
     this.params=(params)?params:null;
    };
    //设置在内容加载完成前的执行体
    //@see setOnLoad
    this.setBeforeLoad=function(beforeload)
    {
   this.beforeload=beforeload;
   if(this.beforeload!=null&&this.beforeload.indexOf('(')==-1)
   {
    this.beforeload=this.beforeload+"()";
   }
    };
    //设置在内容加载时出错的执行体
    //@see setOnLoad
    this.setOnError=function(error)
    {
   this.onerror=(error)?error:this.onerror;
   if(onerror!=null&&this.onerror.indexOf('(')==-1)
   {
    this.onerror=this.onerror+"()";
   }
    };
    //设置请求的头信息
    this.setRequestHeader=function(key,value)
    {
     if(this.req)
     {
      this.req.setRequestHeader(key,value);
     }
    };
    //加载内容
  this.load=function()
  {
   if (this.req)
   {
    try
    {
     var loader=this;
     if(this.asynchronous)//异步
     {
      this.req.onreadystatechange=function()
      {
       loader.onReadyState.call(loader);
      }
      this.req.open(this.method,this.url,true);
      this.req.send(this.params);
     }
     else//同步
     {
      this.req.open(this.method,this.url,false);
      loader.onReadyState().call(loader);
      this.req.send(this.params);
     }
    }catch (err)
    {
     eval(this.error);;
    }
   }
  };
  //请求状态变化时的执行体
  this.onReadyState=function()
  {
    var status=this.req.readyState;
    if (status==com.trs.cds.js.ajax.READY_STATE_COMPLETE)//加载完成
    {
     var httpStatus=this.req.status;
     if (httpStatus==200 || httpStatus==0)//正确返回
     {
      eval(this.onload);
     }
     else{
      eval(this.error);
     }
    }
    else//加载未完成
    {
     if(this.beforeload)
     {
      eval(this.beforeload);;
     }
    }
  };
  //缺省出错执行体
  this.defaultError=function()
  {
   alert("error fetching data!"
   +"\n\nreadyState:"+this.req.readyState
   +"\nstatus: "+this.req.status
   +"\nheaders: "+this.req.getAllResponseHeaders());
  };
  this.url=null;
  this.req=false;
  this.method='GET';
  this.asynchronous=true;
  this.params=null;
  this.onload=null;
  this.beforeload=false;
  this.onerror="this.defaultError()";
  this.initRequest();
};