YAHOO.namespace("LP.form.AutoAjaxObject")

/**
 * Constructor for ajax used in the auto forms.
 * @constructor
 * @base YAHOO.LP.ajaxtemplate.BaseAjaxObject
 * 
 */
YAHOO.LP.form.AutoAjaxObject = function(params){

	YAHOO.LP.form.AutoAjaxObject.superclass.constructor.call(this,params);	
	this.cleanTarget = true;
	 if(this.srcElementId == null && this.event == null){
		this.cleanTarget = false;
	}
	
	this.targetElement = document.getElementById(params.targetElement);
	
	/**
	 * the query string that is has the make, model,year,trim,exterior and interior values selected by the user
	 * 
	 */
	this.stringOfParamNamesAndValues = params.stringOfParamNamesAndValues;
	
};

/*
 * Extending the YAHOO.LP.ajaxtemplate.BaseAjaxObject which has all the structure
 * to register listeners and making the AJAX request.
 */

YAHOO.lang.extend(YAHOO.LP.form.AutoAjaxObject,YAHOO.LP.ajaxtemplate.BaseAjaxObject);


YAHOO.LP.form.AutoAjaxObject.prototype = {
	
	/**
	 * This method is the implementation of the onSuccess call back..
	 * @author kunal.gandhi
	 * @param response object
	 * @see YAHOO.LP.ajaxtemplate.BaseAjaxObject#callback
	 * @
	 */
	onSuccess: function(aResponse){
		var result = aResponse.responseText.strip();
		var array = result.split(",");
		//sort the target element (year) when the source element is model.
		if(this.srcElementId == "model"){
			array.sort(function(a,b){return a - b});
		}
		
		element = document.getElementById(aResponse.argument.targetElement);
		
		for(i=0;i<array.length;i++){
		 if(aResponse.argument.targetElement == "PRODUCT" || aResponse.argument.targetElement =="product"){
			this.addOption(element,array[i],"PP_"+array[i]);
		 }
		else{
			this.addOption(element,array[i],array[i]);
		}
		if(this.isPresentInRequest(array[i])){
				element.options[i+1].selected = true;
			}
	  }//end of for
	},
	
	isPresentInRequest: function(value){
	 array = this.stringOfParamNamesAndValues.split("&");
	  for(j = 0;j<array.length;j++){
	   	 if(array[j].length > 0 && array[j].split("=")[1].toUpperCase().match(value.toUpperCase())){
	  		return true;  	   	
	   	 } 	
	  }
	 return false;
	},
	
	/**
	 * This method is the implementation of the onFailure call back..
	 * @author kunal.gandhi
	 * @param response object
	 * @see YAHOO.LP.ajaxtemplate.BaseAjaxObject#callback
	 * @
	 */
	onFailure: function(aResponse){
		
	},
	
	/**
	 * This method is the implementation of the call back called when an event is triggered by the
	 * source element with which a event listener is registered by the base class. The call back
	 * also CONSTRUCTS the queryString depending on the this.stringOfParamNamesAndElementIds value.
	 * @author kunal.gandhi
	 * @param event triggered
	 * @see YAHOO.LP.ajaxtemplate.BaseAjaxObject
	 */
	
	eventTriggeredAjaxRequest: function(e){
		//resets the query string for the new call.
		this.queryStringComplete="";
		this.constructQueryString();
		this.refresh(this.targetElement);
		YAHOO.LP.form.AutoAjaxObject.superclass.makeRequest.call(this);
	},
	
	/**
	 * This method is the implementation of the call back called if the user requires to trigger the call
	 * back manually. This is called in base class if not event and source element is mentioned.
	 * @author kunal.gandhi
	 * @see YAHOO.LP.ajaxtemplate.BaseAjaxObject
	 */
	noEventTriggeredAjaxRequest: function(){
		this.constructQueryString();
		YAHOO.LP.form.AutoAjaxObject.superclass.makeRequest.call(this);	

	},
	
	/**
	 * This method triggers the AJAX calls manually by invoking this method from the template. The AJAX
	 * objects using this trigger can HAVE events registered for later triggers. The invoker provides
	 * a query string for the AJAX call.
	 */
	manualTriggeredAjaxRequest: function(){
		queryString = this.constructQueryStringForManualTrigger();
		this.queryStringComplete = this.queryStringBase + "&" + queryString;
		this.refresh(this.targetElement);
		YAHOO.LP.form.AutoAjaxObject.superclass.makeRequest.call(this);
	},
	
		/**
	 * Constructing a query string for the initial Ajax request.
	 */
	constructQueryStringForManualTrigger: function(){
	  queryStr = "";
	  if(this.stringOfParamNamesAndElementIds != null && this.stringOfParamNamesAndValues != null){
	    arrayOfRequestString = this.stringOfParamNamesAndValues.split("&");
	 	arrayOfParamNamesAndElementIds = this.stringOfParamNamesAndElementIds.split("&");
	    for(i = 0;i<arrayOfParamNamesAndElementIds.length;i++){
	   	   name = arrayOfParamNamesAndElementIds[i].split("=")[0];
	   	   for(j = 0;j<arrayOfRequestString.length;j++){
	   	  	   if(arrayOfRequestString[j].split("=")[0] == name){
	  				queryStr = queryStr + name +"="+arrayOfRequestString[j].split("=")[1];  	  	   	
	   	  	   } 	
	   	   }
	   	   if(i < arrayOfParamNamesAndElementIds.length - 1){
	   	   		queryStr = queryStr + "&";
	   	   }
	   	}	
	  }
	 return queryStr;	
	},
	
	/**
	 * This method is more like a helper method that constructs the part of query string that
	 * needs values from the form.
	 * @author kunal.gandhi
	 * @see YAHOO.LP.ajaxtemplate.BaseAjaxObject
	 */
	constructQueryString: function(){
	 this.queryStringComplete = this.queryStringBase;
	 if(this.stringOfParamNamesAndElementIds != null){
	 	arrayOfParamNamesAndElementIds = this.stringOfParamNamesAndElementIds.split("&");
	   for(i = 0;i<arrayOfParamNamesAndElementIds.length;i++){
	   	    name = arrayOfParamNamesAndElementIds[i].split("=")[0];
	   	    elementId = arrayOfParamNamesAndElementIds[i].split("=")[1];
		    selectedIndex = document.getElementById(elementId).selectedIndex;
		    selectedValue = document.getElementById(elementId).options[selectedIndex].value;
		    this.queryStringComplete = this.queryStringComplete + "&" + name + "=" + selectedValue;	
	   		}	
		}
	},
	
	
	
	/**
	 * This method is a helper method which cleans the given drop-down before populating it with the newest
	 * response.
	 * @author kunal.gandhi
	 * 
	 */
	cleanUp: function(element){
	 var i;
	 if(element != null && element.options.length > 0){
	 	element.options.length =0;	
	 }
	},
	
	/**
	 * This method is a helper method which is used to clean ALL the child nodes of the given element
	 * @author kunal.gandhi
	 */
	refresh: function(targetElmt){
		this.cleanUp(targetElmt);
		
		if(this.srcElementId == "product"){
			this.cleanUp(document.getElementById("year"));
			this.cleanUp(document.getElementById("trim"));
			this.cleanUp(document.getElementById("exterior"));
			this.cleanUp(document.getElementById("interior"));
			this.addOption(document.getElementById("model"),"Select a Model","");
		}	
		
		if(this.srcElementId == "model"){
			this.cleanUp(document.getElementById("trim"));
			this.cleanUp(document.getElementById("exterior"));
			this.cleanUp(document.getElementById("interior"));
			this.addOption(document.getElementById("year"),"Select a Year","");
		}
		
		if(this.srcElementId == "year"){
			this.cleanUp(document.getElementById("exterior"));
			this.cleanUp(document.getElementById("interior"));	
			this.addOption(document.getElementById("trim"),"Select a Trim","");
		}
		
		if(this.srcElementId == "trim"){	
			if(targetElmt.id == "exterior"){
				this.addOption(document.getElementById("exterior"),"Select an Exterior","");
			}
			else{
				this.addOption(document.getElementById("interior"),"Select an Interior","");
			}
		}
		
		
	},
	/**
	 * This is again a helper method used to add the option for the given element.
	 * @author kunal.gandhi
	 */
	addOption: function(element,text,value){
		opt = document.createElement('option');
		opt.text = text;
		opt.setAttribute("value",value);
		try{
			element.add(opt,null);
		   }
		catch(ex){
			element.add(opt);
		}
	}
}
