﻿	// build a string in US currency format
	function money(fValue) {
		var fDollar, fCents;
		
		fDollar = Math.floor(fValue);
		fCents = Math.round(100 * (fValue - fDollar));
		if (fCents == 0)
			return '$' + fDollar + '.00';
		else if (fCents < 10)
			return '$' + fDollar + '.0' + fCents;
		return '$' + fDollar + '.' + fCents;
	}

	// update the inner contents of div / layer (netscape & ie compat)
	function displayValue(sObjectID, sHTML) {
        document.getElementById(sObjectID).innerHTML = sHTML;
	}

	// calculate the monthly payment based on financing
	function recalculate(bErrors)
	{
		var fac;		// factor for payment calculation
		
		if (isNaN(parseFloat(document.forms[0].loanrate.value))) {
			if (bErrors) {
				alert('The Interest Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}
		var fRate = parseFloat(document.forms[0].loanrate.value) / 1200.0;
		if (fRate < 0) {
			if (bErrors) {
				alert('The Interest Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}

		if (isNaN(parseInt(document.forms[0].loanterm.options[document.forms[0].loanterm.selectedIndex].value))) {
			if (bErrors) {
				alert('The Term (months) must be a positive integer');
				return false;
			}
			return false;
		}
		var nMonths = parseInt(document.forms[0].loanterm.options[document.forms[0].loanterm.selectedIndex].value);
		if (nMonths < 1) {
			if (bErrors) {
				alert('The Term (months) must be a positive integer');
				return false;
			}
			return false;
		}

		if (isNaN(parseFloat(document.forms[0].cashdown.value))) {
			if (document.forms[0].cashdown.value != '') {
				if (bErrors) {
					alert('The Cash Down must be a valid number greater than or equal to zero');
					return false;
				}
			}
			fDown = 0	
		} else {
			var fDown = parseFloat(document.forms[0].cashdown.value);
			if (fDown < 0) {
				if (bErrors) {
					alert('The Cash Down must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		}
		
		if (isNaN(parseFloat(document.forms[0].tradein.value))) {
			if (document.forms[0].tradein.value != '') {
				if (bErrors) {
					alert('The Trade-In Value must be a valid number greater than or equal to zero');
					return false;
				}
				return false;	
			} else
			 fTradeIn = 0;
		} else {
			var fTradeIn = parseFloat(document.forms[0].tradein.value);
			if (fTradeIn < 0) {
				if (bErrors) {
					alert('The Trade-In Value must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		}

		if (isNaN(parseFloat(document.forms[0].tradeinowed.value))) {
			if (document.forms[0].tradeinowed.value != '') {
				if (bErrors) {
					alert('The Trade-In Owed must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			} else
				fTradeInOwed = 0
		} else {
			var fTradeInOwed = parseFloat(document.forms[0].tradeinowed.value);
			if (fTradeInOwed < 0) {
				if (bErrors) {
					alert('The Trade-In Owed must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		}

		if (isNaN(parseFloat(document.forms[0].loanamount.value))) {
			if (bErrors) {
				alert('The Vehicle Price must be a valid number greater than or equal to zero');
				return false;
			}
			return false;	
		}
		var fAmount = parseFloat(document.forms[0].loanamount.value);
		if (fAmount < 0) {
			if (bErrors) {
				alert('The VehiclePrice must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}

		// calculate the total down payment here
		var fTotalDown =  Math.round(100 * (fDown + fTradeIn - fTradeInOwed)) / 100.0;
		displayValue('totaldown', money(fTotalDown));
			
		// calculate the loan amount here
		fAmount = fAmount - fTotalDown;
		displayValue('financed', money(Math.round(100 * fAmount) / 100.0));
			
		// calculate the payment based on loan amount & rate
		fac = 1;
		for (var i=0; i < nMonths; i++)
			fac = fac * (fRate + 1.0);
		if (fac > 1) {
			// calculate the finance payments
			var fPayment = (fAmount * fac * fRate) / (fac - 1.0);
			displayValue('monthlypayment', money(Math.round(100 * fPayment) / 100.0));
				
			// calculate the payment per $1000 financed
			var fPaymentPer1000 = (1000.0 * fac * fRate) / (fac - 1.0);
			displayValue('paymentper1000', money(Math.round(100 * fPaymentPer1000) / 100.0));
		} else {
			// financing is 0%
			var fPayment = (fAmount / nMonths);
			displayValue('monthlypayment', money(Math.round(100 * fPayment) / 100.0));

			// calculate the payment per $1000 financed
			var fPaymentPer1000 = 1000.0 / nMonths;
			displayValue('paymentper1000', money(Math.round(100 * fPaymentPer1000) / 100.0));
		}
		// calculate the total payments here
		var fTotal = fPayment * nMonths;
		displayValue('totalpayments', money(Math.round(100 * fTotal) / 100.0));
			
		return true;
	}
	
		// calculate the loan amount (purchase price) based on financing
	function recalculateprice(bErrors)
	{
		var fac;		// factor for payment calculation

		if (isNaN(parseInt(document.forms[0].loanterm.options[document.forms[0].loanterm.selectedIndex].value))) {
			if (bErrors) {
				alert('The Term (months) must be a positive integer');
				return false;
			}
			return false;
		}
		var nMonths = parseInt(document.forms[0].loanterm.options[document.forms[0].loanterm.selectedIndex].value);
		if (nMonths < 1) {
			if (bErrors) {
				alert('The Term (months) must be a positive integer');
				return false;
			}
			return false;
		}
		
		if (isNaN(parseFloat(document.forms[0].loanrate.value))) {
			if (bErrors) {
				alert('The Interest Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;	
		}
		var fRate = parseFloat(document.forms[0].loanrate.value) / 1200.0;
		if (fRate < 0) {
			if (bErrors) {
				alert('The Interest Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}

		var fDown = 0;
		if (isNaN(parseFloat(document.forms[0].cashdown.value))) {
			if (document.forms[0].tradein.value != '') {
				if (bErrors) {
					alert('The Cash Down must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		} else {
			fDown = parseFloat(document.forms[0].cashdown.value);
			if (fDown < 0) {
				if (bErrors) {
					alert('The Cash Down must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		}
		
		var fTradeIn = 0;
		if (isNaN(parseFloat(document.forms[0].tradein.value))) {
			if (document.forms[0].tradein.value != '') {
				if (bErrors) {
					alert('The Trade-In Value must be a valid number greater than or equal to zero');
					return false;
				}
				return false;	
			}
		} else {
			fTradeIn = parseFloat(document.forms[0].tradein.value);
			if (fTradeIn < 0) {
				if (bErrors) {
					alert('The Trade-In Value must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		}

		if (isNaN(parseFloat(document.forms[0].tradeinowed.value))) {
			if (document.forms[0].tradeinowed.value != '') {
				if (bErrors) {
					alert('The Trade-In Owed must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			} else
				fTradeInOwed = 0
		} else {
			var fTradeInOwed = parseFloat(document.forms[0].tradeinowed.value);
			if (fTradeInOwed < 0) {
				if (bErrors) {
					alert('The Trade-In Owed must be a valid number greater than or equal to zero');
					return false;
				}
				return false;
			}
		}

		if (isNaN(parseFloat(document.forms[0].monthlypayment.value))) {
			if (bErrors) {
				alert('The Monthly Payment must be a valid number greater than or equal to zero');
				return false;
			}
			return false;	
		}
		var fPayment = parseFloat(document.forms[0].monthlypayment.value);
		if (fPayment < 0) {
			if (bErrors) {
				alert('The Monthly Payment must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}
		
		// calculate the total down payment here
		var fTotalDown =  Math.round(100 * (fDown + fTradeIn - fTradeInOwed)) / 100.0;
		displayValue('totaldown', money(fTotalDown));
						
		// calculate the payment based on loan amount & rate
		fac = 1;
		for (var i=0; i < nMonths; i++)
			fac = fac * (fRate + 1.0);
		if (fac > 1) {
			// calculate the finance payments
			var fAmount = (fPayment * (fac - 1.0)) / (fac * fRate);
			displayValue('financed', money(Math.round(100 * fAmount) / 100.0));
		} else {
			// financing is 0%
			var fAmount = fPayment * nMonths;
			displayValue('financed',  money(Math.round(100 * fAmount) / 100.0));
		}
		// calculate the total payments here			
		var fTotal = fPayment * nMonths;
		displayValue('totalpayments', money(Math.round(100 * fTotal) / 100.0));
			
		// calculate the total vehicle price here
		displayValue('totalprice', money(fAmount + fTotalDown));
		return true;
	}

	// calculate the total payments for interest rate / rebate (form1)
	function recalculaterate(bErrors)
	{
		var fac;		// factor for payment calculation
		var fPayment;	// payment for finance
		
		// REBATE
		if (isNaN(parseFloat(document.forms[0].rebate.value))) {
			if (bErrors) {
				alert('The Rebate must be a positive number');
				return false;
			}
			return false;
		}
		var fRebate = parseFloat(document.forms[0].rebate.value);
		if (fRebate <= 0) {
			if (bErrors) {
				alert('The Rebate must be a valid number greater than zero');
				return false;
			}
			return false;
		}

		// STANDARD RATE
		if (isNaN(parseFloat(document.forms[0].stdrate.value))) {
			if (bErrors) {
				alert('The Standard Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}
		var fStdRate = parseFloat(document.forms[0].stdrate.value) / 1200.0;
		if (fStdRate < 0) {
			if (bErrors) {
				alert('The Standard Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}
		// SPECIAL RATE
		if (isNaN(parseFloat(document.forms[0].specrate.value))) {
			if (bErrors) {
				alert('The Special Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}
		var fSpecRate = parseFloat(document.forms[0].specrate.value) / 1200.0;
		if (fSpecRate < 0) {
			if (bErrors) {
				alert('The Special Rate must be a valid number greater than or equal to zero');
				return false;
			}
			return false;
		}

		// LOAN AMOUNT
		if (isNaN(parseFloat(document.forms[0].loanamount.value))) {
			if (bErrors) {
				alert('The Rebate must be a positive number');
				return false;
			}
			return false;
		}
		var fAmount = parseFloat(document.forms[0].loanamount.value);
		if (fAmount <= 0) {
			if (bErrors) {
				alert('The Loan Amount must be a valid number greater than zero');
				return false;
			}
			return false;
		}

		// STANDARD LOAN TERM
		if (isNaN(parseInt(document.forms[0].loanterm.value))) {
			if (bErrors) {
				alert('The Term (months) must be a positive integer');
				return false;
			}
			return false;
		}
		var nMonths = parseInt(document.forms[0].loanterm.value);
		if (nMonths < 1) {
			if (bErrors) {
				alert('The Term (months) must be a positive integer');
				return false;
			}
			return false;
		}

		// SPECIAL LOAN TERM
		if (isNaN(parseInt(document.forms[0].specloanterm.value))) {
			if (bErrors) {
				alert('The Special Loan Term (months) must be a positive integer');
				return false;
			}
			return false;
		}
		var nSpecMonths = parseInt(document.forms[0].specloanterm.value);
		if (nSpecMonths < 1) {
			if (bErrors) {
				alert('The Special Loan Term (months) must be a positive integer');
				return false;
			}
			return false;
		}
		
		// calculate the total payments for each option
		fac = 1;
		for (var i=0; i < nMonths; i++)
			fac = fac * (fStdRate + 1.0);

		// calculate payments for rebate + std rate
		if (fac > 1) {
			// calculate the finance payments
			fPayment = ((fAmount - fRebate) * fac * fStdRate) / (fac - 1.0);
			document.getElementById("pmtrebate").innerHTML = Math.round(100.0 * fPayment) / 100.0;
			document.getElementById("termrebate").innerHTML = nMonths;
			var fTotalRebate = Math.round(100.0 * fPayment * nMonths) / 100.0;
			document.getElementById("totalrebate").innerHTML = fTotalRebate;
		} else {
			// financing is 0%
			fPayment = ((fAmount - fRebate) / nMonths);
			document.getElementById("pmtrebate").innerHTML = Math.round(100.0 * fPayment) / 100.0;
			document.getElementById("termrebate").innerHTML = nMonths;
			var fTotalRebate = Math.round(100.0 * fPayment * nMonths) / 100.0;
			document.getElementById("totalrebate").innerHTML = fTotalRebate;
		}
		
		// calculate payments for special rate and no rebate
		fac = 1;
		for (var i=0; i < nSpecMonths; i++)
			fac = fac * (fSpecRate + 1.0);
		if (fac > 1) {
			// calculate the finance payments
			fPayment = (fAmount * fac * fSpecRate) / (fac - 1.0);
			document.getElementById("pmtrate").innerHTML = Math.round(100.0 * fPayment) / 100.0;
			document.getElementById("termrate").innerHTML = nSpecMonths;
			var fTotalRate = Math.round(100.0 * fPayment * nSpecMonths) / 100.0;
			document.getElementById("totalrate").innerHTML = fTotalRate;
		} else {
			// financing is 0%
			fPayment = (fAmount / nSpecMonths);
			document.getElementById("pmtrate").innerHTML = Math.round(100.0 * fPayment) / 100.0;
			document.getElementById("termrate").innerHTML = nSpecMonths;
			var fTotalRate = Math.round(100.0 * fPayment * nSpecMonths) / 100.0;
			ddocument.getElementById("totalrate").innerHTML= fTotalRate;
		}
		// determine the best value
		if (fTotalRate < fTotalRebate) {
			// It’s $ XXX to your advantage to consider the rate/rebate.
			document.getElementById("suggest").innerHTML = '<B CLASS="error">It\'s ' + money(fTotalRebate - fTotalRate) + ' to your advantage to consider the special loan rate.</B>';
		} else {
			document.getElementById("suggest").innerHTML = '<B CLASS="error">It\'s ' + money(fTotalRate - fTotalRebate) + ' to your advantage to consider the rebate.</B>';
		}
		
		return true;
	}


	// prohibits the user from submitting the form
	function submitForm()
	{
		return false;
	}

