registerNameSpace("com.trs.util.ajax");

$import("ajax/ajax.js","../log/log4js.js")

com.trs.util.ajax.READY_STATE_UNINITIALIZED=0;
com.trs.util.ajax.READY_STATE_LOADING=1;
com.trs.util.ajax.READY_STATE_LOADED=2;
com.trs.util.ajax.READY_STATE_INTERACTIVE=3;
com.trs.util.ajax.READY_STATE_COMPLETE=4;
com.trs.util.ajax.AjaxRequest=function()
{
	var logger=getLogger();
	this.close=function()
	{
		logger.info("close");
		delete this.xmlHttprequest;
	}
	this.initRequest=function()
	{
		logger.info("initRequest");
		if(logger.isDebugEnabled())
		{
			logger.debug("window.XMLHttpRequest="+typeof window.XMLHttpRequest);
			logger.debug("window.ActiveXObject="+typeof window.ActiveXObject);
		}
		if(typeof window.XMLHttpRequest!='undefined') 
		{
			try {
				this.xmlHttprequest = new XMLHttpRequest();
				} catch(e) {
				logger.error(e.message);
				this.xmlHttprequest = false;
			}
		}
		else if(window.ActiveXObject) 
		{
			try 
			{
				this.xmlHttprequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) 
			{
				logger.error(e.message);
				try 
				{
						this.xmlHttprequest = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e) 
				{
					logger.error(e.message);
					this.xmlHttprequest = false;
				}
			}
		}
	};
	this.getResponseXML=function()
	{
		if(this.xmlHttprequest)
		{
			return this.xmlHttprequest.responseXML;
		}
		else
		{
			return null;
		}
	};
	this.getResponseText=function()
	{
		if(this.xmlHttprequest)return this.xmlHttprequest.responseText;
		return null;
	};
    this.setUrl=function(url)
    {
    	this.url=(url)?url:"";
    };
    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()
  				{
					logger.info("this.onload");
  					onload.apply(this,args);
	  			}
			  }
			}
    };
    this.setAasynchronous=function(asy)
    {
		this.asynchronous=asy;
    };
    this.setParams=function(params)
    {
    	this.params=(params)?params:null;
    };
    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()
  				{
					logger.info("this.beforeload");
  					beforeload.apply(this,args);
	  		    }
			  }
			}
    };
    this.setOnError=function()
    {
    	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()
  				{
					logger.info("this.onerror");
  					onerror.apply(this,args);
  			  }
			  }
			}
    };
    this.setRequestHeader=function(key,value)
    {
    	if(this.xmlHttprequest)
    	{
    		this.xmlHttprequest.setRequestHeader(key,value);
    	}
    };
	this.load=function()
	{
		logger.info("load");
		if(logger.isDebugEnabled())
		{
			logger.debug("this.xmlHttprequest="+this.xmlHttprequest);
			logger.debug("this.asynchronous="+this.asynchronous);
			logger.debug("this.method="+this.method);
			logger.debug("this.url="+this.url);
			logger.debug("this.params="+this.params);
		}
		if (this.xmlHttprequest)
		{
			try
			{
				var loader=this;
				if(this.asynchronous)
				{
					this.xmlHttprequest.onreadystatechange=function()
					{
						loader.onReadyState.call(loader);
					}
					this.xmlHttprequest.open(this.method,this.url,true);
					this.xmlHttprequest.send(this.params);
				}
				else
				{
					this.xmlHttprequest.open(this.method,this.url,false);
					loader.onReadyState().call(loader);
					this.xmlHttprequest.send(this.params);
				}
			}catch (err)
			{
				logger.error(err.message);
				eval(this.error);
				throw err;
			}
		}
	};
	this.onReadyState=function()
	{
		logger.info("onReadyState");
		if(logger.isDebugEnabled())
		{
			logger.debug("status="+this.xmlHttprequest.readyState);
		}
		try
		{
			var status=this.xmlHttprequest.readyState;
			if (status==com.trs.util.ajax.READY_STATE_COMPLETE)
			{
				if(logger.isDebugEnabled())
				{
					logger.debug("httpStatus="+this.xmlHttprequest.status);
				}
				var httpStatus=this.xmlHttprequest.status;
				if (httpStatus==200 || httpStatus==0)
				{
					if(typeof this.onload=="function")
					{
						(this.onload)();
					}
					else
					{
						eval(this.onload);
					}
					this.close();
				}
				else{
					if(typeof this.error=="function")
					{
						(this.error)();
					}
					else
					{
						eval(this.error);
					}
					this.close();
				}
			}
			else
			{
				if(typeof this.beforeload=="function")
				{
					(this.beforeload)();
				}
				else
				{
					eval(this.beforeload);
				}
			}
		}catch(err)
		{
			logger.error(err.message);
			throw err;
		}
	};
	this.defaultError=function()
	{
		logger.error("error fetching data!"
		+"\n\nreadyState:"+this.xmlHttprequest.readyState
		+"\nstatus: "+this.xmlHttprequest.status);
	};
	this.url=null;
	this.xmlHttprequest=false;
	this.method='GET';
	this.asynchronous=true;
	this.params=null;
	this.onload=null;
	this.beforeload=false;
	this.onerror=this.defaultError;
	this.initRequest();
};

function $ajax()
{
	return new com.trs.util.ajax.AjaxRequest();
}