// ############################ INITIALIZATION #################################

// ###	Create data object
var estData; //= new EstimatorDataClass(2006, 24150, 605, null);
var rptMgt; //= new estUtils.ReportingMgtMiniClass( 'model name', 'body type', 'trim', estData.getEstimatedSellingPrice(1), estData.getModelYear() );

// ############################ BEGIN DWR data acquisition code ################

var strModel;
var trimBox;
var zipBox;
var strYear;
var SELECT_PROMPT = '--Select a Vehicle--';
var TRIM_PROMPT = '--Select a Trim--';
var GENERAL_ERROR = 'The payment estimator is temporarily unavailable. Please try again.';

function initMiniEstimator() {
	//build an estimatordataclass
	estData = new EstimatorDataClass(selectedYear, parseInt(baseMsrp), parseInt(configuredMsrp) - parseInt(baseMsrp), null);
	rptMgt = new estUtils.ReportingMgtMiniClass( selectedModel, selectedBodyStyle, selectedTrimName, estData.getEstimatedSellingPrice(1), estData.getModelYear() );
	initFormData();
}

// ###	Callback function for KBB trade in value (Loan)
function updateLoanTradeIn( tradeinValue, year, make, model ) {
		rptMgt.setTradeInInfo(year,make,model,tradeinValue);
		$('txtPurchaseTradein').value = tradeinValue;
		estData.getLoanDataObj().setTradeIn( tradeinValue );
		updateMonthlyLoanPayment();
}

// ###	Initialize form values on page
function initFormData () {
	//Set Base MSRP and Total MSRP fields
	$('pe_subtotal').value = estData.getBaseMSRP(1);
	$('pe_total').value = estData.getEstimatedSellingPrice(1);
	
	//Assign event handlers
	assignLoanEventHandlers();
	//assignLeaseEventHandlers();
	
	//Populate form values
	displayLoanData();
	//if ( estData.getHasLeaseData() ) { //Check to validate that we have lease data
	//	$('leaseTable').style.display = "block";
	//	$('leaseLinks').style.display = "block";
	//	displayLeaseData();
	//} else {
	//	setHtml('leaseErrorMsg', 'Lease information is temporarily unavailable.' );
	//}
}

// ############################ LOAN FUNCTIONS #################################

// ###	Assigns onblur and onchange events of various Loan form elements on the
//		page to appropriate event handlers. This wiring creates automatic value
//		updates to the estimatorData object.
function assignLoanEventHandlers () {
	var loanObj = estData.getLoanDataObj();
	
	$('pe_terms').onchange = function () {
		loanObj.setTerm( $('pe_terms').value )
		updateMonthlyLoanPayment();
	}
	
	$('pe_downpay').onblur = function () {
		loanObj.setDownPayment( $('pe_downpay').value );
		updateMonthlyLoanPayment();
	}
	$('pe_tradein').onblur = function () {
		loanObj.setTradeIn( $('pe_tradein').value );
		updateMonthlyLoanPayment();
	}
	$('pe_apr').onblur = function () {
		loanObj.setApr( $('pe_apr').value );
		updateMonthlyLoanPayment();
	}
}

// ###	Populates page with loan values
function displayLoanData() {
	var loanObj = estData.getLoanDataObj();
	//Populate form fields with data
	//$('txtPurchaseEstimatedSellingPrice').value = loanObj.getEstimatedSellingPrice();
	estUtils.setSelectValue('pe_terms',loanObj.getTerm());
	$('pe_downpay').value = loanObj.getDownPayment();
	$('pe_tradein').value = loanObj.getTradeIn();
	//$('pe_apr').value = loanObj.getApr();
	// Changed by Aruna for the  Mini PE requirement
	//$('pe_apr').value = '';
	
	//Display loan payment value
	if($('pe_apr').value!='')
	{
		updateMonthlyLoanPayment();
	}
	else
	{
		rptMgt.fireReport();
	}
	// End of change by Aruna
}

// ###	Checks to see if the monthly loan payment is valid. If any of the loan
//		form values are invalid, it displays an error. Otherwise it shows the
//		loan payment value on the page.
function updateMonthlyLoanPayment () {
	var paymentObj = estData.getLoanDataObj().getMonthlyLoanPaymentObj();
	setHtml('loanErrorMsg', '' ); //reset error message;
	if ( paymentObj.getIsError() ) {
		rptMgt.doReset(); //Reset any data collected (like tradein value) that could have caused the error
		setHtml('loanErrorMsg', paymentObj.getErrorMsg() );
	} else {
		rptMgt.setPurchasePay( paymentObj.getPaymentValue(1) );	//set the purchase price for reporting
		rptMgt.fireReport();									//fire off report
		setHtml('pe_estimatedpayment', paymentObj.getPaymentValue(1)); //set value in active popup
		returnEstValue(paymentObj.getPaymentValue(1)); //send value back to parent page
	}
}

// ############################ LEASE FUNCTIONS #################################

// ###	Assigns onblur and onchange events of various Lease form elements on the
//		page to appropriate event handlers. This wiring creates automatic value
//		updates to the estimatorData object.
function assignLeaseEventHandlers () {
	var leaseObj = estData.getLeaseDataObj();
	$('txtLeaseEstimatedSellingPrice').onblur = function () {
		leaseObj.setEstimatedSellingPrice( $('txtLeaseEstimatedSellingPrice').value );
		updateMonthlyLeasePayment();
	}
	$('drpLeaseMonths').onchange = function () {
		leaseObj.setTerm( $('drpLeaseMonths').value )
		updateMonthlyLeasePayment();
	}
	$('txtLeaseDownPayment').onblur = function () {
		leaseObj.setDownPayment( $('txtLeaseDownPayment').value );
		updateMonthlyLeasePayment();
	}
	$('txtLeaseTradein').onblur = function () {
		leaseObj.setTradeIn( $('txtLeaseTradein').value );
		updateMonthlyLeasePayment();
	}
	$('drpLeaseMileage').onchange = function () {
		leaseObj.setMileageCode( $('drpLeaseMileage').value );
		updateMonthlyLeasePayment();
	}
}

// ###	Populates page with loan values
function displayLeaseData() {
	var leaseObj = estData.getLeaseDataObj();
	//Populate form fields with data
	$('txtLeaseEstimatedSellingPrice').value = leaseObj.getEstimatedSellingPrice();
	estUtils.setSelectOptions(	'drpLeaseMonths',
								estUtils.getTermsForMileageCode( leaseObj.getMileageCode(), estData.getLeaseDataList() ) );
	estUtils.setSelectValue('drpLeaseMonths',leaseObj.getTerm());
	$('txtLeaseDownPayment').value = leaseObj.getDownPayment();
	$('txtLeaseTradein').value = leaseObj.getTradeIn();
	estUtils.setSelectValue('drpLeaseMileage',leaseObj.getMileageCode());

	//Display lease payment values
	updateMonthlyLeasePayment();
}

// ###	Checks to see if the monthly lease payments are valid. If any of the lease
//		form values are invalid, it displays an error. Otherwise it shows the
//		lease payment values on the page.
function updateMonthlyLeasePayment () {
	var paymentObj = estData.getLeaseDataObj().getMonthlyLeasePaymentObj();
	setHtml('leaseErrorMsg', '' ); //reset error message;
	if ( paymentObj.getIsError() )
		setHtml('leaseErrorMsg', paymentObj.getErrorMsg() );
	else {
		setHtml('leLoanPayment', paymentObj.getPaymentValue(1) );
		setHtml('leAdjCap', paymentObj.getAdjustedCapCost(1) );
		setHtml('leSecurityDeposit', paymentObj.getSecurityDeposite(1) );
		setHtml('leFirstPayment', paymentObj.getFirstPayment(1) );
		setHtml('leAcquisitionFee', paymentObj.getAcquisitionFee(1) );
		setHtml('leCapCostReduction', paymentObj.getCapCostReduction(1) );
		setHtml('leTotalDueSigning', paymentObj.getTotalDue(1) );
	}
}
//Fix for validation of only numeric values
function validateText(e)
{
	var charCode = e.which || e.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57) )
		  return false;	
}

function validateAPR(e)
{
	var charCode = e.which || e.keyCode;
	if ((charCode > 31 && (charCode < 48 || charCode > 57)) && charCode!=46)
		  return false;	
}
