YAHOO.namespace("LP.ajaxtemplate.BaseAjaxObject")

/**
 * @author kunal.gandhi
 * This is the base object for all kinds of AJAX request that provides the basic structure for making a 
 * successpful AJAX call. The child nodes need to implement the call back which will be called from this base
 * object.
 */

YAHOO.LP.ajaxtemplate.BaseAjaxObject = function (params){
    /*
     *The event that should trigger an Ajax Request. 
     */
    this.event = params.event;
    
    /*
     * The element Id that will trigger the ajax call. a listener is registered to this one with
     * the event
     */
	this.srcElementId = params.srcElementId;
	/*
	 * Part of query string that doesnt change. (eg: naturalId=auto_makes)
	 */
	this.queryStringBase = params.queryStringBase;
	
	/*
	 * The entire query string that goes to the server.
	 */
	this.queryStringComplete = "";
	
	/*
	 * This holds the part of query string that gets the value dynamically from the form after
	 * the event is triggered. The query string will be in format 
	 * "<param_name1>=<elementId1>&<param_name2>=<elementId2>....<param_namen>=<elementIdn>"
	 * where param_namen is the parameter name the needs to be sent with the request
	 * 		 elementIdn is the id of the element that will be holding the value.
	 */
	this.stringOfParamNamesAndElementIds = params.stringOfParamNamesAndElementIds;
   
	
	/*
	 * The url used to make the AJAX request
	 */
	this.url = params.url;
	
	/*
	 * The type of request 
	 */
	this.method = params.method;
	
	/*
	 * call back events and their respective functions which will be implemented by the child classes
	 */
	this.callback =
	{
		success:this.onSuccess,
		failure:this.onFailure,
		scope: this,
		argument: params.args
	}; 
	/*
	* Adding listener to the source element that triggers this request. the call back event will
	* be handled by the method implemented in the child class
	*/
	if(this.srcElementId != null && this.event != null){
		YAHOO.util.Event.addListener(this.srcElementId,this.event,this.eventTriggeredAjaxRequest,this,true); 
	}
	/*
	 * If there is not source element and event just trigger the call back manually and make the
	 * request 
	 */
	else{
		this.noEventTriggeredAjaxRequest();
	}
};

YAHOO.LP.ajaxtemplate.BaseAjaxObject.prototype = {
	/**
	 * The function makes the aynshronous AJAX request. Once, the client gets a response, the #callback function
	 * is called depending on the status code of the response. Currently, we only support success and failure
	 * status code.
	 * @author kunal.gandhi
	 */
	makeRequest: function(){
		YAHOO.util.Connect.asyncRequest(this.method, this.url, this.callback, this.queryStringComplete);
	}
	
}
