// ############## PAYMENT ESTIMATOR DISPLAY UTILITY FUNCTIONS ##################
var estUtils = {
	
	// ###	Sets the selected index for the option.value that matchs the given setValue
	setSelectValue: function (elmId, setValue) {
		if ( $(elmId).type == 'select-one' ) {
			for (var n=0; n < $(elmId).options.length; n++) {
				if ($(elmId).options[n].value == setValue) {
					$(elmId).selectedIndex = n;
					break;
				}
			}
		}
	},

	// ###	Replaces options for select with the given elmId with data from valueArray. Value
	//		array format application is <option value="[key]">[ valueArray[key] ]</option>
	setSelectOptions: function (elmId, valueArray) {
		var options = $(elmId).options;
		for (var idx in options)
			$(elmId).remove(idx);
		for (var key in valueArray) {
			var newOption = new Option( key, valueArray[key] );
			options.add( newOption );
		}
	},
	
	// ###	Returns an array of terms for a given milage code in a given array of 
	//		LeaseDataClass objects where both the key and value are equal a
	//		single/common term value
	getTermsForMileageCode: function ( mileageCodeValue, leaseDataList ) {
		var termsArray=[];
		for (var idx in leaseDataList) {
			listItem = leaseDataList[idx];
			if (listItem.mileageCde == mileageCodeValue) {
				termsArray[listItem.term] = listItem.term;
			}
		}
		return termsArray;
	},
	
	// ###	Class to handle the complex needs of crmEvent2
	ReportingMgtClass: function () {
		var tYear;
		var tMake;
		var tModel;
		var tValue;
		var purchasePay;
		var leasePay;
		var creditRating;
		var doConsolidatedReport;
		var fireReportCount;
		reset();
		
		function reset() {
			tYear='';
			tMake='';
			tModel='';
			tValue='';
			purchasePay='';
			leasePay='';
			creditRating='';
			doConsolidatedReport=false;
			fireReportCount=0;
		}
		
		this.doReset = function () {reset();}
		this.setTradeInInfo = function (year, make, model, value) {
			tYear=year;
			tMake=make;
			tModel=model;
			tValue=value;
		}
		this.setPurchasePay = function (value) {purchasePay = value;}
		this.setLeasePay = function (value) {leasePay = value;}
		this.setCreditRating = function (value) {creditRating = value;}
		this.setDoConsolidatedReport = function (value) {doConsolidatedReport = value;}
		this.fireReport = function () {
			
			var temp="";
			if(creditRating == "LS0")
				temp="Excellent";
			else if(creditRating == "LS1")
				temp="Great";
			else if(creditRating == "LS2")
				temp="Good";
			else if(creditRating == "LS3")
				temp="Fair";
			else if(creditRating == "LS4")
				temp="Poor";
		
		
			
			if (doConsolidatedReport) {
				if (fireReportCount == 1) {
					//crmDebug('Consolidated Report\ntYear: '+tYear+'\ntMake: '+tMake+'\ntModel: '+tModel+'tValue: '+tValue+'\npurchasePay: '+purchasePay+'\nleasePay: '+leasePay);
					// Changed for SR 16925
                                  var zip = document.paymentEstimatorForm.txtZipcode.value;
              			   
                                  document.cookie="ZipCode="+zip;
		                // Changes end here
				    //crmEvent2( tModel, tYear, tMake, purchasePay, leasePay, tValue );
					crmEvent2( purchasePay, leasePay, temp );
				    
                            reset();
				} else {
					fireReportCount++;
				}
			} else {
				//crmDebug('Single Report\ntYear: '+tYear+'\ntMake: '+tMake+'\ntModel: '+tModel+'\ntValue: '+tValue+'\npurchasePay: '+purchasePay+'\nleasePay: '+leasePay);
				// Changed for SR 16925
                                 var zip = document.paymentEstimatorForm.txtZipcode.value;
              			 
                                  document.cookie="ZipCode="+zip;
		          				//crmEvent2( tModel, tYear, tMake, purchasePay, leasePay, tValue );
								crmEvent2( purchasePay, leasePay , temp);
				//changes for SR 16925
				reset();
			}
		}
	}
}