
/**
 * This script goes along with the Configuratin Snapshot of
 * the Vehicle Configurator.
 */

var NONE_LABEL = "";
var INCENTIVES_LABEL = 'incentives';
var PACKAGES_LABEL = 'packages';
var OPTIONS_LABEL = 'options';
var MODEL_LABEL = 'model';
var MSRP_LABEL = 'msrp';
var ZIP_LABEL = 'zip';
var TRIM_LABEL = 'trim';
var TRANSMISSION_LABEL = 'trans';
var ENGINE_LABEL = 'engine';
var DRIVE_LABEL = 'drive';
var EXTERIOR_LABEL = 'exterior';
var INTERIOR_LABEL = 'interior';
var BODY_LABEL = 'body';
var MSRP_BASE_LABEL = 'msrp_base';
var MSRP_OPTIONS_LABEL = 'msrp_options';
var MSRP_TRANSMISSION_LABEL = 'msrp_trans';
var MSRP_DESTINATION_LABEL = 'msrp_dest'; 
var FINANCE_LABEL = 'finance';
var LEASE_LABEL = 'lease';
var LEASEPRESENT_LABEL = 'leasepresent';
var MODELCODE_LABEL= 'modelCode';
var UNFORMATTEDMSRP_LABEL = 'unformattedmsrp';


var pEstimates = new Array();

//scr 3057 added 2 more attributes leaseTermId, financeTermId
function payEstimate(modelYear, vehicleCode, moneyFactor, modelCode, cashDownPay, estTradeIn,
financeInterestRate,financeTerm, leaseMilesPerYear, leaseTerm, calculatedLeaseAmt, calculatedFinanceAmt,
nationalIncentivePresent, regionalIncentivePresent, leasePresent, negotiatedPrice, origMsrp, mazdaCash,
residual, leaseTermId, financeTermId)
{

	this.peModelYear = modelYear;
	this.peVehicleCode = vehicleCode;
	this.peMoneyFactor = moneyFactor;
	this.peModelCode = modelCode;
	this.peCashDownPay = cashDownPay;
	this.peEstTradeIn = estTradeIn;
	this.peFinanceInterestRate = financeInterestRate;
	this.peFinanceTerm = financeTerm;
	this.peLeaseMilesPerYear = leaseMilesPerYear;
	this.peLeaseTerm = leaseTerm;
	this.peCalculatedLeaseAmt = calculatedLeaseAmt;
	this.peCalculatedFinanceAmt = calculatedFinanceAmt;
	this.peNationalIncentivePresent = nationalIncentivePresent;
	this.peRegionalIncentivePresent = regionalIncentivePresent;
	this.peLeasePresent = leasePresent;
	this.peNegotiatedPrice = negotiatedPrice;
	this.peOrigMsrp = origMsrp;
	this.peMazdaCash = mazdaCash;
	this.peResidual = residual;
	//scr 3057 added 2 more attributes leaseTermId, financeTermId 
	this.peLeaseTermId = leaseTermId; 
	this.peFinanceTermId = financeTermId; 

}




/**
 * Constructor for DisplayItem object.
 */
function DisplayItem( fieldName, value ) {

	this.fieldName = fieldName;

	if ( value == null || value.length == 0 ) {

		this.value = NONE_LABEL;
	} else {

		this.value = value;
	}


}

var incentives=new Array();
var options=new Array();
var packages=new Array();
var displayItems= new Array();
var oSnapshot = null;



displayItems[0] = new DisplayItem( MODEL_LABEL, NONE_LABEL );
displayItems[1] = new DisplayItem( MSRP_LABEL, NONE_LABEL );
displayItems[2] = new DisplayItem( MSRP_BASE_LABEL, NONE_LABEL );
displayItems[3] = new DisplayItem( MSRP_OPTIONS_LABEL, NONE_LABEL );
displayItems[4] = new DisplayItem( MSRP_TRANSMISSION_LABEL, NONE_LABEL );
displayItems[5] = new DisplayItem( MSRP_DESTINATION_LABEL, NONE_LABEL );

/**
 * Keeps the original status
 */
function OriginalSnapshot( key ) {
	this.key = key;
	this.items = new Array();

	this.items[0] = new DisplayItem( MODEL_LABEL, NONE_LABEL );
	this.items[1] = new DisplayItem( MSRP_LABEL, NONE_LABEL );
	this.items[2] = new DisplayItem( MSRP_BASE_LABEL, NONE_LABEL );
	this.items[3] = new DisplayItem( MSRP_OPTIONS_LABEL, NONE_LABEL );
	this.items[4] = new DisplayItem( MSRP_TRANSMISSION_LABEL, NONE_LABEL );
	this.items[5] = new DisplayItem( MSRP_DESTINATION_LABEL, NONE_LABEL );
	
	

	for ( var nI = 0; nI < this.items.length; nI++ ) {
		var fieldtxt = new makeLayer( this.items[nI].fieldName );
		if ( fieldtxt != null && fieldtxt.el != null ) {
			this.items[nI].value = fieldtxt.el.innerHTML;
		}
	}
}

/**
 * Creates and stores original snapshot.
 */
function createOriginalSnapshot( key ) {
	oSnapshot = new OriginalSnapshot( key );
}

/**
 * Restores the original snapshot if the key matches.
 */
function restoreOriginalSnapshot( key ) {
	if ( oSnapshot != null && key == oSnapshot.key ) {
		for ( var nI = 0; nI < oSnapshot.items.length; nI++ ) {
			onDisplayItemChange( oSnapshot.items[ nI ].fieldName,
				oSnapshot.items[ nI ].value );
		}
	}
}

/**
 * This function updates individual field in the Snapshot.
 * This function must be called if any one of the fields is changed.
 */
function onDisplayItemChange( fieldName, description ) {


	//
	// Find the display Item, from the displayItems that this 'dispItem' corresponds to.
	// Update the value field
	//
	var nI = 0;
	var found = false;
	if ( fieldName != null && displayItems != null ) {
		for ( var nI = 0; nI < displayItems.length; nI++ ) {
			if ( fieldName == displayItems[ nI ].fieldName ) {
				if ( description == null || description.length == 0 ) {
					displayItems[ nI ].value = NONE_LABEL;
				} else {
					displayItems[ nI ].value = description;
				}
				found = true;
				break;
			}
		}
	}

	if ( found ) {
		if ( description == null || typeof( description ) == "string" ) {
			var fieldtxt = new makeLayer(displayItems[nI].fieldName);
			if ( fieldtxt != null && fieldtxt.el != null ) {
				fieldtxt.el.innerHTML = displayItems[nI].value;
			}
		} else {
			var dispString = "";
			for ( var nJ = 0; nJ < description.length; nJ++ ) {
				if ( dispString.length > 0 ) {
					dispString = dispString + "<br>" + description[ nJ ];
				} else {
					dispString = description[ nJ ];
				}
			}
			var fieldtxt = new makeLayer(displayItems[nI].fieldName);
			if ( fieldtxt != null && fieldtxt.el != null ) {
				fieldtxt.el.innerHTML = dispString;
			}
		}
	}

	if ( fieldName == UNFORMATTEDMSRP_LABEL)
	{
		//alert("MSRP HAS CHANGED : value : "+ description + "& fieldName ="+fieldName) ;
		//logic to recalculate the New Values for the Payment Estimator
		calculateFinanceLeasePayment(description);

	}
}

function getPaymentEstimate() {

	var modelCode = document.getElementById("hiddenModelCode").value;
	//alert("in getPaymentEstimate : *"+document.getElementById("hiddenModelCode").value+"*");
	var pEstimate = null;

	if ( pEstimates != null ) {
		for ( var nI = 0; nI < pEstimates.length; nI++ ) {
			if ( modelCode == pEstimates[ nI ].peModelCode ) {
				pEstimate = pEstimates[ nI ];
				//alert("<FOUND MATCH" + pEstimate.peModelCode + "*");
				break;
			}
		}
	}

	return pEstimate;
}






function calculateFinanceLeasePayment(newMsrp)
{
	//alert("<calculateFinanceLeasePayment newMsrp> "+newMsrp);
	calculateFinancePayment(newMsrp);
	calculateLeasePayment(newMsrp);
}

function calculateFinancePayment(newMsrp)
{
	//alert("<calculateFinancePayment>");
	var payEstimate=getPaymentEstimate();
	
	var newNegotiatedPrice = 0;
	var estFinancePayment= 0.0 ;
	var estlongFinancePayment = 0.0 ;
	var amtFianced= 0 ;
	var tempCalc=0;
	var tempFinanceTerm=0;
	var tempFinanceAmt=0;	
	

	
	/*alert("payEstimate.peCashDownPay : " + payEstimate.peCashDownPay) ;
	alert("payEstimate.peEstTradeIn : " + payEstimate.peEstTradeIn) ;
	alert("payEstimate.peFinanceInterestRate : " + payEstimate.peFinanceInterestRate ) ;
	alert("payEstimate.peFinanceTerm : " + payEstimate.peFinanceTerm ) ;
	alert("payEstimate.peLeaseMilesPerYear : " + payEstimate.peLeaseMilesPerYear ) ;
	alert("payEstimate.peLeaseTerm : " + payEstimate.peLeaseTerm ) ;
	*/	


	
	if(payEstimate.peNegotiatedPrice == 0 || payEstimate.peNegotiatedPrice==payEstimate.peOrigMsrp)
	{
		newNegotiatedPrice=newMsrp;
	}
	else
	{
		newNegotiatedPrice=payEstimate.peOrigMsrp;
		//alert("MSRP "+payEstimate.peOrigMsrp); 
	}
	/*alert("payEstimate.peNegotiatedPrice : " + payEstimate.peNegotiatedPrice ) ;
	alert("payEstimate.peOrigMsrp : " + payEstimate.peOrigMsrp ) ;
	*/	

/*	alert("negotiated Price : "+newNegotiatedPrice + " newMsrp ; "+newMsrp);
	alert("payEstimate.peCashDownPay  : "+payEstimate.peCashDownPay  );
	alert("payEstimate.peEstTradeIn  : "+payEstimate.peEstTradeIn  );
	alert("payEstimate.peCashDownPay + payEstimate.peEstTradeIn ="+(parseInt(payEstimate.peCashDownPay) + parseInt(payEstimate.peEstTradeIn)));
*/

	amtFianced = 0 + parseInt(newNegotiatedPrice) - (parseInt(payEstimate.peCashDownPay) + parseInt(payEstimate.peEstTradeIn));
	/*alert("amount Financed :"+amtFianced) ;
	alert("finance InterestRate : "+parseFloat(payEstimate.peFinanceInterestRate));
	alert("finance Term : "+parseInt(payEstimate.peFinanceTerm));
	alert("test : "+ payEstimate.peFinanceInterestRate/100/12); 
	*/



	
	if(parseFloat(payEstimate.peFinanceInterestRate)==0 || parseFloat(payEstimate.peFinanceInterestRate)==0.0)
	{
		estlongFinancePayment =amtFianced/parseInt(payEstimate.peFinanceTerm);
		payEstimate.peCalculatedFinanceAmt = estlongFinancePayment;
		tempFinanceAmt = Math.round(payEstimate.peCalculatedFinanceAmt);
		if ( document.getElementById(FINANCE_LABEL) != null ) {
			document.getElementById(FINANCE_LABEL).innerHTML = (tempFinanceAmt > 0 && isFinite(tempFinanceAmt))?"$"+commaNum(tempFinanceAmt)+"/mo":"Calculate";
		}
	}else if(parseFloat(payEstimate.peFinanceInterestRate)> 0)
	{


		estlongFinancePayment = Math.round((amtFianced*Math.pow((1+(payEstimate.peFinanceInterestRate/100/12)),payEstimate.peFinanceTerm)*(payEstimate.peFinanceInterestRate/100/12))/(Math.pow((1+(payEstimate.peFinanceInterestRate/100/12)),payEstimate.peFinanceTerm)-1));
		//alert("rounded estlongFinancePayment " + estlongFinancePayment) ;
		estlongFinancePayment =(amtFianced*Math.pow((1+(payEstimate.peFinanceInterestRate/100/12)),parseInt(payEstimate.peFinanceTerm))*(payEstimate.peFinanceInterestRate/100/12))/(Math.pow((1+(payEstimate.peFinanceInterestRate/100/12)),parseInt(payEstimate.peFinanceTerm))-1);
		payEstimate.peCalculatedFinanceAmt = estlongFinancePayment;
		//alert("payEstimate.peCalculatedFinanceAmt" + payEstimate.peCalculatedFinanceAmt) ; 
		tempFinanceAmt = Math.round(payEstimate.peCalculatedFinanceAmt);
		if ( document.getElementById(FINANCE_LABEL) != null ) {
			document.getElementById(FINANCE_LABEL).innerHTML = (tempFinanceAmt > 0 && isFinite(tempFinanceAmt))?"$"+commaNum(tempFinanceAmt)+"/mo":"Calculate";
		}
	} 
}

function calculateLeasePayment(newMsrp)
{

	//alert("<calculateLeasePayment>");
	var capCost = 0 ;
	var residualAmt = 0;
	var newNegotiatedPrice = 0;
	var depreciation = 0  ;
	var moneyFactorAmt = 0 ;
	var estimatedPayment = 0 ;
	var amtFinanced = 0;
	var tempLeaseAmt=0;
	var payEstimate = getPaymentEstimate();

if (payEstimate.peLeasePresent!="true" && document.getElementById(LEASE_LABEL)) document.getElementById(LEASE_LABEL).innerHTML = "n/a";
if(payEstimate.peLeasePresent=="true")
{
	if(payEstimate.peNegotiatedPrice == 0 || payEstimate.peNegotiatedPrice==payEstimate.peOrigMsrp)
	{
		//alert("IN here negPrice = Original MSRP") ;
		newNegotiatedPrice=newMsrp;
	}
	else
	{
		newNegotiatedPrice=payEstimate.peOrigMsrp;
	}
	//alert("negotiated Price : "+newNegotiatedPrice + " newMsrp ; "+newMsrp);
	//alert("payEstimate.peResidual  : "+payEstimate.peResidual  );
	//alert("payEstimate.peMazdaCash  : "+payEstimate.peMazdaCash  );
	//alert("payEstimate.peMoneyFactor  :"+payEstimate.peMoneyFactor);

	amtFinanced = 0 + newNegotiatedPrice - (parseInt(payEstimate.peCashDownPay) + parseInt(payEstimate.peEstTradeIn));
	//alert("<amtFinanced> " + amtFinanced);
	//alert("<peCashDownPay> " + payEstimate.peCashDownPay);
	capCost = 0 + newNegotiatedPrice - (parseInt(payEstimate.peCashDownPay) + parseInt(payEstimate.peEstTradeIn) + parseInt(payEstimate.peMazdaCash));
	//alert("<capCost> " + capCost);
	residualAmt = newMsrp * parseFloat(payEstimate.peResidual)/100 ;
	//alert("<residualAmt> " + residualAmt);
	//Depreciation = (Capitalized Cost - Residual Amount)/Term;
	depreciation = (capCost - residualAmt)/parseInt(payEstimate.peLeaseTerm);
	//alert("<depreciation> " + depreciation);
	//Money Factor Amount = (Capitalized Cost + Residual Amount) * Money Factor
	//alert("<money factor> " + parseFloat(payEstimate.peMoneyFactor));
	moneyFactorAmt = (capCost + residualAmt) * parseFloat(payEstimate.peMoneyFactor) ;
	//alert("moneyFactorAmt : "+moneyFactorAmt);
	estimatedPayment = depreciation + moneyFactorAmt;
	//alert("estimated LEASE Payment" + estimatedPayment);
	payEstimate.peCalculatedLeaseAmt = estimatedPayment;

	//scr04519 - hard coded special $299 lease
	//scr04646 - hard coded special $289 lease
	//scr04765 - REMOVED hard coded special $289 lease 
	//if(payEstimate.peModelCode =='CX7  SP 2A  ' && 
	//(newNegotiatedPrice == 24310 || newNegotiatedPrice == 24345) &&
	//(amtFinanced == 24310 || amtFinanced == 24345)) 
	//{
	//		//alert("model code  is "+payEstimate.peModelCode ); 
	//		payEstimate.peCalculatedLeaseAmt = 289;
	//		if (document.getElementById("cx7_disclaimer")) document.getElementById("cx7_disclaimer").style.display = "block";
	//} else 
	if (document.getElementById("cx7_disclaimer"))
		document.getElementById("cx7_disclaimer").style.display = "none";

	//alert("payEstimate.peCalculatedLeaseAmt :"+payEstimate.peCalculatedLeaseAmt) ;
	tempLeaseAmt = Math.round(payEstimate.peCalculatedLeaseAmt);
	if(document.getElementById(LEASE_LABEL)) document.getElementById(LEASE_LABEL).innerHTML = (tempLeaseAmt > 0 && isFinite(tempLeaseAmt))?"$"+commaNum(tempLeaseAmt)+"/mo":"Calculate";
	}

}


function refreshSummarySnapShot()
{
	//alert("in refreshSS");
	//alert("MODEL CODE " ); 
	//alert("modelcode" + document.getElementById("hiddenModelCode").value); 
	//alert("msrp " + document.getElementById("hiddenUnformattedmsrp").value);
	
	updateSummaryEstimatedLease(document.getElementById("hiddenModelCode").value, document.getElementById("hiddenUnformattedmsrp").value); 
}

function updateSummaryEstimatedLease( modelCode, msrp ) {
	//alert("CALLING ON TRIM SELECTED from COLORS PAGE");
	
	//scr04519 - hard coded special $299 lease
	//scr04646 - hard coded special $289 lease
	//scr04765 - REMOVED hard coded special $289 lease 
	//if(modelCode =='CX7  SP 2A  ' && (msrp == 24310 || msrp == 24345))
	//{
	//	tempLeaseAmt = 289;
	//	if(document.getElementById(LEASE_LABEL)) 
	//	{
	//	   document.getElementById(LEASE_LABEL).innerHTML = 
	//	   (tempLeaseAmt > 0 && isFinite(tempLeaseAmt))?"$"+commaNum(tempLeaseAmt)+"/mo":"Calculate";
//
	//	}
	//   if (document.getElementById("cx7_disclaimer")) document.getElementById("cx7_disclaimer").style.display = "block";

	//} else 
	if (document.getElementById("cx7_disclaimer"))
		document.getElementById("cx7_disclaimer").style.display = "none";

}
