/**
*	Object:		OneAjax
*	Purpose:		Generic object to handle and route AJAX requests on the
*					onesite platform
*
*	@author Mike Benshoof <mbenshoof@onesite.com>
*/
var OneAjax = {
	
	//Required Request Configuration
	_ajaxPath : '/resources/ajax/_router.one',
	_method : 'POST',
	_requestType : 'service',
	_handlerName : null,
	_responseFunction : null,
	_action : null,
	
	//Response Configuration
	_responseType : 'XML',		
	
	//User Passed Params
	_params : null,	
	
	//Holds generated data to pass to string
	_varString : null,
	
	//Varible to determine if request is valid
	_isValid : true,	
	
	//Holds any extra vars to pass to handling function
	_extraVars : null,
	
	/**
	*	Function:	request
	*	Purpose:		Build the param string to pass to the handling function, 
	*					make the AJAX call, and route the response
	*
	*	@param array params ~Array of parameters to build the request/response
	*/
	request : function(params) {

		/*
		*	Check for all the required parameters:
		*		-responseFunction
		*		-action
		*	If they exist, set them in the object and remove from
		*	from the array of params - otherwise, stop processing
		*/
		if(('responseFunction' in params) && ('action' in params) && ('handlerName' in params)) {
			
			this._responseFunction = params['responseFunction'];
			this._action = params['action'];
			this._handlerName = params['handlerName'];
			delete params['responseFunction'];
			delete params['action'];
			delete params['handlerName'];
		}	
		else {

			return;
		}

		//Store the user params and update optional params passed by user
		this._params = params;		
		this._setOptionalVars();
				
		//Set up the initial parser function based on the response type (ie xml)
		switch (this._responseType) {
			
			case 'XML':
				var handlerFunction = this.parseXmlResponse;
				break;
			case 'JSON':
				var handlerFunction = this.parseJsonResponse;
				break;
			case 'text':
				var handlerFunction = this.parseTextResponse;
				break;
			default:
				var handlerFunction = this.parseXmlResponse;
				break;				
		}

		//Generate the variable string to pass for the AJAX request
		this._generateVarString();
		
		//Check to make sure that the request is valid
		if(this._isValid === false) {
			
			return;
		}
		
		//Build the argument to pass to the handler function
		var passArgument = new Array();
		passArgument['responseFunction'] = this._responseFunction;
		passArgument['extraVars'] = this._extraVars;
		
		//Make the AJAX request, passing addition post process params
		var request = YAHOO.util.Connect.asyncRequest(	this._method, 
																		this._ajaxPath, 
																		{
																			success: handlerFunction, 
																			failure: this._genericFail, 
																			argument: passArgument
																		}, 
																		this._varString);		
	},	
	
	/**
	*	Function:	_setOptionalVars	
	*	Purpose:		Check for the optional parameters
	*						-ajaxPath (custom path)
	*						-responseType (XML,JSON,Text)
	*						-requestType (service or class)
	*						-serviceName (needed if type is service)
	*						-className (needed if type is class)
	*					If an param exists, then set it in the object, overwriting 
	*					the default and remove it from the params array
	*
	*	@return void
	*/
	_setOptionalVars : function() {	
		
		//Check for a user passed path
		// */resources/ajax/_router.one by default
		if('ajaxPath' in this._params) {
			
			this._path = this._params['ajaxPath'];
			delete params['ajaxPath'];
		}
		
		//Check for a user passed response type
		// *XML by default
		if('responseType' in this._params) {
			
			this._responseType = this._params['responseType'];
			delete this._params['responseType'];
		}	
		
		//Check for a user passed request type 
		// *Service by default
		if('requestType' in this._params) {
			
			this._requestType = this._params['requestType'];
			delete this._params['requestType'];
		}
		
		//Check for extra, pass through vars
		if('extraVars' in this._params) {
			
			this._extraVars = this._params['extraVars'];
			delete this._params['extraVars'];
		}
		
		return;
	},	
	
	/**
	*	Function:	_generateVarString
	*	Purpose:		Validate the variable string based on the request type
	*					and then build the variable string to pass to the router
	*					with all arbitrary params passed in
	*
	*	@return void
	*/
	_generateVarString : function() {
		
		//Initialize the post string and create
		this._varString = 'action=' + this._action;
		this._varString += '&requestType=' + this._requestType;
		this._varString += '&handlerName=' + this._handlerName;
	
		//Loop through all the params, remove any that are functions, and
		//append to the param string to pass to the router
		for (key in this._params) {
			
			//Grab the type of the value
			var valType = typeof this._params[key];
			
			//Strip out functions from array prototype
			if(valType !== 'function') {
				
				this._varString += '&' + key + '=' + this._params[key];
			}			
		}	
		
		//Exit the function
		return;		
	},
		
	/**
	*	Function:	parseXmlReponse
	*	Purpose:		Parse the response text from the AJAX request as 
	*					an XML document and pass the xml object to the 
	*					main handling function
	*
	*	@param object o ~The YUI Ajax response object
	*	@return object ~The response text parsed as an xml object
	*/
	parseXmlResponse : function(o) {

		//Grab the parameters passed
		var params = o.argument;
		
		//Parse the response function and extra, pass through vars
		responseFunc = params['responseFunction'];
		extra = params['extraVars'];
		
		//Return the XML object from the response object
		responseFunc(o.responseXml,extra);
	},	

	/**
	*	Function:	parseJsonReponse
	*	Purpose:		Parse the response text from the AJAX request as 
	*					a JSON encoded array and pass the actual array to the 
	*					main handling function
	*
	*	@param object o ~The YUI Ajax response object
	*	@return array ~The response text parsed as an array
	*/
	parseJsonResponse : function(o) {
		
		try {
			
			//Try to parse the response into an array
			var resultArray = eval('(' + o.responseText + ')');
		}
		catch(e) {
			
			//Caught an exception - alert the message
			return;
		}
		
		//Grab the parameters passed
		var params = o.argument;
		
		//Parse the response function and extra, pass through vars
		responseFunc = params['responseFunction'];
		extra = params['extraVars'];
		
		//Pass the resulting array to the original handling function
		responseFunc(resultArray,extra);
		
	},	

	/**
	*	Function:	parseTextReponse
	*	Purpose:		Simply pass the text value of the YUI response
	*					value back to the main handling function
	*
	*	@param object o ~The YUI Ajax response object
	*	@return string ~The response text as a string
	*/
	parseTextResponse : function(o) {
		
		//Grab the parameters passed
		var params = o.argument;
		
		//Parse the response function and extra, pass through vars
		responseFunc = params['responseFunction'];
		extra = params['extraVars'];		
		
		//Simply pass the text back to the original handling function
		responseFunc(o.responseText,extra);
		
	},	
	
	/**
	*	Function:	_genericFail
	*	Purpose:		Catch all function that will stop processing after
	*					a failure without calling the main handling function
	*
	*	@param object o ~The YUI Ajax response object
	*	@return void	
	*/
	_gerericFail : function(o) {
		
		//alert('Failed Ajax Request');
		return;
	}		

};//End of OneAjax object

