// generic Request object
function Request(cReff, url)
{
	var selfobj=this;  //reference to itself  
    //we create the XMLHttpRequest object.
	this.xmlRequestObject = null;
    this.urlString=url;	
	this.callerReff = cReff;	
	this.reqMethod="GET";
	this.managerIndex=null;	
	this.rqStatus=Request.STATUS_IDLE;	
	this.postData = "";
	
    //set the method used for the request (GET/POST)
    this.setRequestMethod=function(meth)
	{
        this.reqMethod=meth;
    }
    
    //provide request method currently used
    this.getRequestMethod=function()
    {
        return this.reqMethod;
    }
    
    //set URL for the HTTP request
    this.setURL=function(urlS)
    {
        this.urlString=urlS;    
    }
 	//get url for HTTP request   
    this.getURL=function()
    {
        return this.urlString;
    }
    
    this.setPostData=function(data)
    {
        this.postData=data;    
    }
    
    this.getPostData=function()
    {
        return this.postData;
    }
    
    //stop the current request
	this.abort = function ()
	{
		if(this.getReqStatus()==Request.STATUS_BUSY)
		{
            selfobj.xmlRequestObject.abort();
            this.setReqStatus(Request.STATUS_IDLE);
        }
	}	
	
	/**
	*@desc Starts the request, this is where the request is actually made to the server
	*@return boolean
	*/
	this.start = function ()
	{
		if(this.getReqStatus() == Request.STATUS_IDLE)
		{
            this.setReqStatus(Request.STATUS_BUSY)
            this.getHTTPObject();
            if (this.reqMethod=="POST")
            {
            	selfobj.xmlRequestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            	selfobj.xmlRequestObject.send(this.postData);
            }
            else
            {
            	selfobj.xmlRequestObject.send(null);
            }
            RequestManager.getInstance().activeRequests++;
            return true;
        }
        return false;
	}
	
	
	/**
	*@param number     See the constants of this class
    *@desc set the status of this request
    */
	this.setReqStatus=function (status)
	{
        this.rqStatus=status;
    }
    this.getReqStatus=function()
	{
        return this.rqStatus;
    }
    
    /**
	*@param number     The index from the que of the RequestManager
    *@desc set the index from the que of the RequestManager
    */
    this.setManagerIndex=function(index)
    {
        this.managerIndex=index;
    }
    this.getManagerIndex=function()
    {
        return this.managerIndex;
    }
	
	/**
	* @desc this function will return an instance to a new created XMLHttpRequest object
	* @return object
	*/
	this.getHTTPObject = function () 
	{ 
		if(selfobj.xmlRequestObject==null)
		{
            var xmlhttp; 
    		// IE7 supports native XMLHttpRequest, try to use that first
    		if (typeof XMLHttpRequest != 'undefined') 
    		{ 
    			try 
    			{ 
    				xmlhttp = new XMLHttpRequest(); 
    			} 
    			catch (e) 
    			{ 
    				xmlhttp = false; 
    			} 
    		} 
    		else
    		{
    			try 
    			{
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
				} 
				catch (e)
				{
					try
					{
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
					}
					catch (e){}
				}
    		}
    		if (!xmlhttp)
    		{
    			alert("No XmlHttpRequest object could be found for your browser\nPlease use a fully supported browser (Firefox 1.0 or newer, IE6 or newer or Safari)");
    		}
    		selfobj.xmlRequestObject=xmlhttp;
		}
		
		selfobj.xmlRequestObject.open(selfobj.reqMethod, selfobj.urlString, true);				
		selfobj.xmlRequestObject.onreadystatechange = function()
        {          
		  if(selfobj.xmlRequestObject.readyState==Request.STATE_COMPLETED)
		  {            
            selfobj.setReqStatus(Request.STATUS_IDLE);
            selfobj.callerReff.handleResponse(selfobj.getManagerIndex());            
        	RequestManager.getInstance().activeRequests--;
        	if(RequestManager.getInstance().activeRequests==0)
        	{
        		if(typeof RequestManager.getInstance().onAllRequestsEnd =="function")
        		{
        			RequestManager.getInstance().onAllRequestsEnd();
        		}
        	}
          }
	   }
		return xmlhttp; 
	} 
	
	/**	
	* Get the error code (specific to the application) from the response 
	* @return	error code
	* @type number 
	*/
	this.getResponseErrorCode=function()
	{		
		if(selfobj.xmlRequestObject.responseText)
		{

			var rsp_arr=eval('('+selfobj.xmlRequestObject.responseText+')');
			return rsp_arr['errorCode'];
		}
		return null;
	}
	
	/**	
	*@return	string|object
	*@desc get the content from the response
	*/
	this.getResponseContent=function()
	{
		if(selfobj.xmlRequestObject.responseText)
		{
			return (selfobj.xmlRequestObject.responseText);
			//return eval('('+selfobj.xmlRequestObject.responseText+')');
			//return eval(selfobj.xmlRequestObject.responseText);
			//return eval('('+selfobj.xmlRequestObject.responseText+')');
			//return eval(selfobj.xmlRequestObject.responseText);
			//var rsp_arr=eval('('+selfobj.xmlRequestObject.responseText+')');
			//return rsp_arr['content'];
		}
		return "";
	}	
}

Request.STATE_COMPLETED=4;

Request.STATUS_BUSY=1;
Request.STATUS_IDLE=2;
