<!--
//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Auto Lease Vs. Buy Calculator
//2002 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 05/01/2002
//Last Modified: 05/01/2002
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:3525-863-7-839
//Commercial Licence Date:2006-04-28
//*******************************************



function stripNum(num) {

var iPercent
var iDollar
var iSpace
var iComma
var numLength = num.length

//lalalla Line #114

if(numLength > 0) {

   num=num.toString();

   iPercent = num.indexOf("%");
   if(iPercent >= 0) {
      num=num.substring(0,iPercent) + "" + num.substring(iPercent + 1,numLength);
      numLength=num.length;
      }
   iDollar = num.indexOf("$");
   if(iDollar >= 0) {
      num=num.substring(0,iDollar) + "" + num.substring(iDollar + 1,numLength);
      numLength=num.length;
      }
   iSpace = num.indexOf(" ");
   if(iSpace >= 0) {
      num=num.substring(0,iSpace) + "" + num.substring(iSpace + 1,numLength);
      numLength=num.length;
      }
   iComma = num.indexOf(",");
   if(iComma >= 0) {
      while(iComma >=1) {
         num=num.substring(0,iComma) + "" + num.substring(iComma + 1,numLength);
         numLength=num.length;
         iComma = num.indexOf(",");
      }
      }

      num = eval(num);


} else {

num = 0;

}

return num;

}




function formatCurrency(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
		
	finNum="$"+tmpinteger+"."+decimal;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}




function computeMonthlyPayment(prin, numPmts, intRate) {

var pmtAmt = 0;

if(intRate == 0) {
   pmtAmt = prin / numPmts;
} else {
   
   if (intRate >= 1.0) {
     intRate = intRate / 100.0;
   }
   intRate /= 12;

   var pow = 1;
   for (var j = 0; j < numPmts; j++)
      pow = pow * (1 + intRate);

   pmtAmt = (prin * pow * intRate) / (pow - 1);

}

return pmtAmt;

}




function FVsingleDep(prin, intRate, numMonths, numCompPerYr) {

var i = 0;
var intEarn = 0;
var singleFV = prin;

if(intRate >= 1) {
   intRate /= 100;
}

if(numCompPerYr == "" || numCompPerYr == 0) {
   numCompPerYr = 12;
}
intRate /= numCompPerYr;

var numPeriods = numMonths / 12 * numCompPerYr;

singleFV = Math.pow((eval(1) + eval(intRate)), numPeriods) * singleFV;

return singleFV;

}

function computeForm(form) {

if(form.lsCost.value == "" || form.lsCost.value == 0) {
   alert("Please enter cost of the truck you are considering leasing on Line #1A.");
   form.lsCost.focus();
} else
if(form.lnCost.value == "" || form.lnCost.value == 0) {
   alert("Please enter cost of the truck you are considering buying on Line #1B.");
   form.lnCost.focus();
} else
if(form.lsTaxRate.value == "" || form.lsTaxRate.value == 0) {
   alert("Please enter the applicable sales tax rate on Line #2A.");
   form.lsTaxRate.focus();
} else
if(form.lnTaxRate.value == "" || form.lnTaxRate.value == 0) {
   alert("Please enter the applicable sales tax rate on Line #2B.");
   form.lnTaxRate.focus();
} else
if(form.lsMonths.value == "" || form.lsMonths.value == 0) {
   alert("Please enter the term of the lease (number of months) on Line #7A.");
   form.lsMonths.focus();
} else
if(form.lnMonths.value == "" || form.lnMonths.value == 0) {
   alert("Please enter the term of the loan (number of months) on Line #7B.");
   form.lnMonths.focus();
} else
if((form.lnPmt.value == "" || form.lnPmt.value == 0) && (form.lnIntRate.value == "" || form.lnIntRate.value == "")) {
   alert("If you leave the loan's monthly payment amount (Line #9B) blank, then you must enter an interest rate on Line #8B.");
   form.lnIntRate.focus();
} else
if((form.lsPmt.value == "" || form.lsPmt.value == 0) && (form.lsIntRate.value == "" || form.lsIntRate.value == "")) {
   alert("If you leave the lease's monthly payment amount (Line #9A) blank, then you must enter an interest rate on Line #8A.");
   form.lsIntRate.focus();
} else {

//CALC BUY COSTS

var VlnCost = stripNum(form.lnCost.value);
var VlnTaxRate = stripNum(form.lnTaxRate.value);
var VlnSalesTax = 0;

if(VlnTaxRate >= 1) {
   VlnTaxRate /= 100;
}
VlnSalesTax = VlnTaxRate * VlnCost;

var VlnFees = stripNum(form.lnFees.value);

var VlnGrossPurchase = eval(VlnCost) + eval(VlnSalesTax) + eval(VlnFees);

//Initialize value to accumulate prin for forgone int calc
var VlnUpFrontFVamt = 0;

var VlnDownPay = stripNum(form.lnDownPay.value);
//Add downpayment to amount for figuring forgone interest
VlnUpFrontFVamt = eval(VlnUpFrontFVamt) + eval(VlnDownPay);

VlnFinanceAmt = eval(VlnCost) - eval(VlnDownPay);

var VlnTradeIn = stripNum(form.lnTradeIn.value);
//Subtract tradein allowance from amount to finance.
VlnFinanceAmt = eval(VlnFinanceAmt) - eval(VlnTradeIn);

var VlnRebate = stripNum(form.lnRebate.value);
//Subtract from amount to figure forgone interest
VlnUpFrontFVamt = eval(VlnUpFrontFVamt) - eval(VlnRebate);

//Add Sales tax amount to figure forgone interest
VlnUpFrontFVamt = eval(VlnUpFrontFVamt) + eval(VlnSalesTax);

//Add Fees amount to figure forgone interest
VlnUpFrontFVamt = eval(VlnUpFrontFVamt) + eval(VlnFees);


var VlnMonths = stripNum(form.lnMonths.value);
var VlnPmt = 0;
var VlnIntRate = form.lnIntRate.value;
if(VlnIntRate == "") {
   VlnPmt = stripNum(form.lnPmt.value);
} else {
   VlnIntRate = stripNum(VlnIntRate);
   VlnPmt = computeMonthlyPayment(VlnFinanceAmt, VlnMonths, VlnIntRate);
}

form.lnMoPmt.value = formatCurrency(VlnPmt);

var VlnTotPmt = VlnMonths * VlnPmt;
form.lnTotPmt.value = formatCurrency(VlnTotPmt);

var VlnIntExp = eval(VlnTotPmt) - eval(VlnFinanceAmt);
form.lnIntExp.value = formatCurrency(VlnIntExp);

var VlnUpFront = eval(VlnSalesTax) + eval(VlnFees) - eval(VlnRebate);
form.lnUpFront.value = formatCurrency(VlnUpFront);

var VlnResale = form.lnResale.value;
var VlnDeprecExp = 0;
if(VlnResale == "") {
   VlnDeprecExp = getDeprec(VlnCost,VlnMonths);
} else {
   VlnResale = stripNum(VlnResale);
   VlnDeprecExp = eval(VlnCost) - eval(VlnResale);
}
form.lnDeprecExp.value = formatCurrency(VlnDeprecExp);

//Compute forgone interest if upfront cash less rebate > 0
var VannSaveRate = stripNum(form.annSaveRate.value);
var VlnUpFrontFV = 0;
var VlnForgoneInt = 0;
if(VlnUpFrontFVamt > 0) {
   VlnUpFrontFV = FVsingleDep(VlnUpFrontFVamt, VannSaveRate, VlnMonths, 12);
   VlnForgoneInt = eval(VlnUpFrontFV) - eval(VlnUpFrontFVamt);
}
form.lnForgoneInt.value = formatCurrency(VlnForgoneInt);

var VlnTotCost = eval(VlnIntExp) + eval(VlnUpFront) + eval(VlnDeprecExp) + eval(VlnForgoneInt);
form.lnTotCost.value = formatCurrency(VlnTotCost);

var VlnAvgAnnCost = VlnTotCost / (VlnMonths / 12);
form.lnAvgAnnCost.value = formatCurrency(VlnAvgAnnCost);

//CALC LEASE COSTS

var VlsCost = stripNum(form.lsCost.value);

var VlsGrossCapCost = VlsCost;

var VlsDownPay = stripNum(form.lsDownPay.value);

var VlsTradeIn = stripNum(form.lsTradeIn.value);

var VlsRebate = stripNum(form.lsRebate.value);

var VlsTotCapCostReduct = eval(VlsDownPay) + eval(VlsTradeIn) + eval(VlsRebate);

var VlsNetCapCost = eval(VlsGrossCapCost) - eval(VlsTotCapCostReduct);

var VlsMonths = stripNum(form.lsMonths.value);

var VlsResale = form.lsResale.value;
var VlsDeprecExp = 0;
if(VlsResale == "") {
   VlsDeprecExp = getDeprec(VlsCost,VlsMonths);
} else {
   VlsResale = stripNum(VlsResale);
   VlsDeprecExp = eval(VlsCost) - eval(VlsResale);
}
//form.lsDeprecExp.value = formatCurrency(VlsDeprecExp);

var VlsTaxRate = stripNum(form.lsTaxRate.value);
if(VlsTaxRate >= 1) {
   VlsTaxRate /= 100;
}

var VlsPmt = form.lsPmt.value;
var VlsIntRate = form.lsIntRate.value;
if(VlsIntRate >= 1) {
   VlsIntRate /= 100;
}
var VlsResidual = 0;
var VlsMonthlyDeprec = 0;
var VlsMoneyFactor = 0;
var VlsLeaseRate = 0;
var VlsMoPmt = 0;
if(VlsPmt == 0 || VlsPmt == "") {
   VlsResidual = eval(VlsCost) - eval(VlsDeprecExp);
   VlsMonthlyDeprec = (eval(VlsNetCapCost) - eval(VlsResidual)) / VlsMonths;
   VlsMoneyFactor = VlsIntRate / 24;
   VlsLeaseRate = (eval(VlsNetCapCost) + eval(VlsResidual)) * VlsMoneyFactor;
  VlsMoPmt = eval(VlsMonthlyDeprec) + eval(VlsLeaseRate);
  VlsMoPmt = VlsMoPmt * (eval(1) + eval(VlsTaxRate))
} else {
  VlsMoPmt = stripNum(VlsPmt);
}
form.lsMoPmt.value = formatCurrency(VlsMoPmt);

var VlsTotPmt = VlsMoPmt * VlsMonths;
form.lsTotPmt.value = formatCurrency(VlsTotPmt);

//Calc sales tax on cap reduction
var VlsCapReductTax = VlsTotCapCostReduct * VlsTaxRate;

var VlsFees = stripNum(form.lsFees.value);

var VlsUpFront = eval(VlsCapReductTax) + eval(VlsFees);
form.lsUpFront.value = formatCurrency(VlsUpFront);

var VlsUpFrontFVamt = 0;
VlsUpFrontFVamt = eval(VlsDownPay) + eval(VlsTradeIn) + eval(VlsUpFront) - eval(VlsRebate);

//Compute forgone interest if upfront cash less rebate > 0
var VlsUpFrontFV = 0;
var VlsForgoneInt = 0;
if(VlsUpFrontFVamt > 0) {
   VlsUpFrontFV = FVsingleDep(VlsUpFrontFVamt, VannSaveRate, VlsMonths, 12);
   VlsForgoneInt = eval(VlsUpFrontFV) - eval(VlsUpFrontFVamt);
}
form.lsForgoneInt.value = formatCurrency(VlsForgoneInt);

var VlsTotCost = eval(VlsTotPmt) + eval(VlsUpFront) + eval(VlsForgoneInt);
form.lsTotCost.value = formatCurrency(VlsTotCost);

var VlsAvgAnnCost = VlsTotCost / (VlsMonths / 12);
form.lsAvgAnnCost.value = formatCurrency(VlsAvgAnnCost);


//END VARIFICATION IF STATEMENT
   }
    
}

function getDeprec(p,m) {

var numYrs = m / 12;

var deprec = 0;
var accumDeprec = 0;
var deprecPerc = 0;
var dYrCnt = 0;

var ageFact = new Array(0,24,16,12,8,6,5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

while(dYrCnt < numYrs) {
   dYrCnt = eval(dYrCnt) + eval(1);
   eval('deprecPerc = document.carLeaseBuyForm.yr' + dYrCnt + '.value;');
   deprecPerc = stripNum(deprecPerc);
   if(deprecPerc == 0) {
      deprecPerc = ageFact[dYrCnt];
   }
   deprec = deprecPerc /100 * p;
   accumDeprec = eval(accumDeprec) + eval(deprec);
   
}

return accumDeprec;

}

//function help0(form) {
//form.lsTaxRate.focus();
//form.help.value = "";
//form.help.value = ("Instructions: To see a more detailed instruction/explanation of //any text-entry field, simply click in the desired text field and the //instructions/explanations will appear in this text area.");
//}

function help1A(form) {
form.helpB.value = "";
form.help.value = ("Line #1A: ENTER: The negotiated purchase price of the truck you are thinking of leasing.");
}

function help1B(form) {
form.helpB.value = "";
form.help.value = ("Line #1B: ENTER: The negotiated purchase price of the truck you are thinking of buying.");
}

function help2A(form) {
form.helpB.value = "";
form.help.value = ("Line #2A: ENTER: The applicable sales tax rate, expressed as a percentage (enter \"6.5\" for 6%, i.e., leave off the percent sign).");
}

function help2B(form) {
form.helpB.value = "";
form.help.value = ("Line #2B: ENTER: The applicable sales tax rate, expressed as a percentage (enter \"6.5\" for 6%, i.e., leave off the percent sign).");
}

function help3A(form) {
form.helpB.value = "";
form.help.value = ("Line #3A: ENTER: The combined total of the lease's aquisition and title/registration fees.");
}

function help3B(form) {
form.helpB.value = "";
form.help.value = ("Line #3B: ENTER: The vehicle's title/registration fees.");
}


function help4A(form) {
form.helpB.value = "";
form.help.value = ("Line #4A: ENTER: The amount of your cash down payment, if any.");
}

function help4B(form) {
form.helpB.value = "";
form.help.value = ("Line #4B: ENTER: The amount of your cash down payment, if any.");
}

function help5A(form) {
form.helpB.value = "";
form.help.value = ("Line #5A: ENTER: The amount of your net trade-in allowance, if any.");
}

function help5B(form) {
form.helpB.value = "";
form.help.value = ("Line #5B: ENTER: The amount of your net trade-in allowance, if any.");
}

function help6A(form) {
form.helpB.value = "";
form.help.value = ("Line #6A: ENTER: The total amount of any rebates you have coming.");
}

function help6B(form) {
form.helpB.value = "";
form.help.value = ("Line #6B: ENTER: The total amount of any rebates you have coming.");
}


function help7A(form) {
form.helpB.value = "";
form.help.value = ("Line #7A: ENTER: The term of the lease expressed in number of months.");
}

function help7B(form) {
form.helpB.value = "";
form.help.value = ("Line #7B: ENTER: The term of the loan expressed in number of months.");
}

function help8A(form) {
form.helpB.value = "";
form.help.value = ("Line #8A: ENTER: If you would like the calculator to figure your monthly lease payment for you, enter the lease's stated annual interest rate, expressed as a percentage. Otherwise, if you know the monthly lease payment amount, leave this field blank.");
}

function help8B(form) {
form.helpB.value = "";
form.help.value = ("Line #8B: ENTER: If you would like the calculator to figure your monthly loan payment for you, enter the loan's annual interest rate, expressed as a percentage. Otherwise, if you know the monthly loan payment amount, leave this field blank.");
}

function help9A(form) {
form.helpB.value = "";
form.help.value = ("Line #9A: ENTER: If you know what your lease's monthly payment will be, enter the monthly payment amount. Otherwise, leave this field blank and the calculator will estimate your lease's monthly payment for you.");
}

function help9B(form) {
form.helpB.value = "";
form.help.value = ("Line #9B: ENTER: If you know what your loan's monthly payment will be, enter the monthly payment amount. Otherwise, leave this field blank and the calculator will compute your loan's monthly payment for you.");
}

function help10A(form) {
form.helpB.value = "";
form.help.value = ("Line #10A: ENTER: Enter the lease's security deposit amount.");
}

function help11A(form) {
form.helpB.value = "";
form.help.value = ("Line #11A: ENTER: The vehicle's estimated resale value (residual) at the end of the lease (varies by model, mileage and other factors). If you want the calculator to estimate the resale value for you using the annual depreciation percentages on line #13, leave this field blank.");
}

function help11B(form) {
form.helpB.value = "";
form.help.value = ("Line #11B: ENTER: The vehicle's estimated resale value (residual) at the end of the loan (varies by model, mileage and other factors). If you want the calculator to estimate the resale value for you using the annual depreciation percentages on line #13, leave this field blank.");
}

function help12(form) {
form.helpB.value = "";
form.help.value = ("Line #12: ENTER: Annual interest rate at which you expect your savings to grow, expressed as a percentage.");
}

function help13(form) {
form.helpB.value = "";
form.help.value = ("Line #13: ENTER: This line shows the typical falling depreciation percentages that occur each year following the purchase/lease of a new vehicle. If you think the default amounts are not reflective of your particular vehicle's depreciation, feel free to alter the percentages as you see fit.");
}

//function help00(form) {
//form.lsTotPmt.focus();
//form.help.value = "";
//form.help.value = ("Instructions: To see a more detailed instruction/explanation of //any text-entry field, simply click in the desired text field and the //instructions/explanations will appear in this text area.");
//}

function help14A(form) {
form.help.value = "";
form.helpB.value = ("Line #14A: RESULT: This is the amount of your monthly lease payment -- which includes depreciation expenses, finance charges and sales taxes.");
}

function help14B(form) {
form.help.value = "";
form.helpB.value = ("Line #14B: RESULT: This is the amount of your monthly loan payment.");
}

function help15A(form) {
form.help.value = "";
form.helpB.value = ("Line #15A: RESULT: This is the total amount of your lease payments over the term of the lease.");
}

function help15B(form) {
form.help.value = "";
form.helpB.value = ("Line #15B: RESULT: This is the total amount of your loan payments over the term of the loan.");
}

function help16B(form) {
form.help.value = "";
form.helpB.value = ("Line #16B: RESULT: This is the total interest you will pay by the time you pay off your loan.");
}

function help17A(form) {
form.help.value = "";
form.helpB.value = ("Line #17A: RESULT: This is the amount of your upfront lease expenses -- which includes aquisition and title/registrations fees, plus any applicable sales tax due at signing, less any applicable rebates.");
}


function help17B(form) {
form.help.value = "";
form.helpB.value = ("Line #17B: RESULT: This is the amount of your upfront loan expenses -- which includes sales tax and title/registrations fees, less any applicable rebates.");
}


function help18B(form) {
form.help.value = "";
form.helpB.value = ("Line #18B: RESULT: This is the total amount your vehicle's depreciation over the term of the loan.");
}

function help19A(form) {
form.help.value = "";
form.helpB.value = ("Line #19A: RESULT: This figure represents the interest you could have earned had you invested the lease's net up-front costs for the term of the lease.");
}

function help19B(form) {
form.help.value = "";
form.helpB.value = ("Line #19B: RESULT: This figure represents the interest you could have earned had you invested the loan's downpayment, trade-in allowance and net up-front costs for the term of the loan.");
}


function help20A(form) {
form.help.value = "";
form.helpB.value = ("Line #20A: RESULT: This is your total cost over the term of the lease.");
}

function help20B(form) {
form.help.value = "";
form.helpB.value = ("Line #20B: RESULT: This is your total cost over the term of the loan.");
}

function help21A(form) {
form.help.value = "";
form.helpB.value = ("Line #21A: RESULT: This is the average annual cost of leasing the vehicle (Total Cost divided by Lease Term years).");
}

function help21B(form) {
form.help.value = "";
form.helpB.value = ("Line #21B: RESULT: This This is the average annual cost of buying the vehicle (Total Cost divided by Loan Term years).");
}
// -->

