/**
 * AJAX对象封装
 * @author: trs 应用产品开发部 ly
 * @date: 2005-11-11
 */
function registerNameSpace(nameSpace)
{
	var s="window";
	var arrayNs=nameSpace.split(".");
	for(var i=0;i<arrayNs.length;i++)
	{
		s+="."+arrayNs[i];
		if(typeof eval(s)=="undefined")
		{
			eval(s+"=new Object();");
		}
	}
}
registerNameSpace("com.trs.cds.js.ajax");
//请求状态
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') 
			{
				try {
					this.req = new XMLHttpRequest();
					} catch(e) {
					this.req = false;
				}
			}
			else if(window.ActiveXObject) 
			{
				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;
			}
			else
			{
				return null;
			}
		};
		//返回Text对象
		this.getResponseText=function()
		{
			if(this.req)return this.req.responseText;
			return null;
		};
		//设置URL
    this.setUrl=function(url)
    {
    	this.url=(url)?url:"";
    };
		//设置Method:取值范围有GET,POST,HEAD等
    this.setMethod=function(method)
    {
    	this.method=(method)?method:'GET';
    };
    //设置内容加载完成的执行体
    this.setOnLoad=function()
    {
    	if(arguments.length>=1)
    	{
    		var onload=arguments[0];
				if((typeof onload=="String")&&onload.indexOf('(')==-1)
				{
					this.onload=onload+"()";
				}
			  else
			  {
			  	var args=new Array();
			  	for(var i=1;i<arguments.length;i++)
			  	{
			  		args[i-1]=arguments[i];
			  	}
			  	this.onload=function()
  				{
  					onload.apply(this,args);
  			  }
			  }
			}
    };
    //设置同步或者异步: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()
    {
    	var args=arguments;
    	if(args.length>=1)
    	{
    		var beforeload=args[0];
				if((typeof beforeload=="String")&&beforeload.indexOf('(')==-1)
				{
					this.beforeload=beforeload+"()";
				}
			  else
			  {
			  	var args=new Array();
			  	for(var i=1;i<arguments.length;i++)
			  	{
			  		args[i-1]=arguments[i];
			  	}
			  	this.beforeload=function()
  				{
  					beforeload.apply(this,args);
  			  }
			  }
			}
    };
    //设置在内容加载时出错的执行体
    //@see setOnLoad
    this.setOnError=function(error)
    {
    	var args=arguments;
    	if(args.length>=1)
    	{
    		var onerror=args[0];
				if((typeof onerror=="String")&&onerror.indexOf('(')==-1)
				{
					this.onerror=onerror+"()";
				}
			  else
			  {
			  	var args=new Array();
			  	for(var i=1;i<arguments.length;i++)
			  	{
			  		args[i-1]=arguments[i];
			  	}
			  	this.onerror=function()
  				{
  					onerror.apply(this,args);
  			  }
			  }
			}
    };
    //设置请求的头信息
    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)//????
					{
						if(typeof this.onload=="function")
						{
							(this.onload)();
						}
						else
						{
							eval(this.onload);
						}
					}
					else{
						if(typeof this.error=="function")
						{
							(this.error)();
						}
						else
						{
							eval(this.error);
						}
					}
				}
				else//加载未完成
				{
						if(typeof this.beforeload=="function")
						{
							(this.beforeload)();
						}
						else
						{
							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();
};
