//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Car Cost Comparison Calculator V2
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/17/2001
//Last Modified: 07/17/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 #:5782-1237-58-1198
//Commercial Licence Date:2008-09-11
//*******************************************



function stripNum(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == "9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}



function formatNumber(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 computeForm(form) {

if(form.priceTag1.value.length == 0) {
   alert("Please enter the purchase price for Scenario #1");
   form.priceTag1.focus();
} else
if(form.salesTax1.value.length == 0) {
   alert("Please enter the sales tax percentage for Scenario #1");
   form.salesTax1.focus();
} else
if(form.license1.value.length == 0) {
   alert("Please enter the annual licensing cost for Scenario #1");
   form.license1.focus();
} else
if(form.finance1.value.length == 0) {
   alert("Please enter indicate whether or not you are financing the vehicle in Scenario #1");
   form.finance1.focus();
} else
if((form.finance1.value == "y" || form.finance1.value == "Y") && (form.intRate1.value.length == 0)) {
   alert("Please enter the annual financing rate for Scenario #1");
   form.intRate1.focus();
} else
if((form.finance1.value == "y" || form.finance1.value == "Y") && (form.nPer1.value.length == 0)) {
   alert("Please enter the number of months you are financing the car for in Scenario #1");
   form.nPer1.focus();
} else
if(form.insure1.value.length == 0) {
   alert("Please enter the annual insurance cost for Scenario #1");
   form.insure1.focus();
} else
if(form.miles1.value.length == 0) {
   alert("Please enter the number miles you expect to drive the Scenario #1 car per year.");
   form.miles1.focus();
} else
if(form.mpg1.value.length == 0) {
   alert("Please enter the Miles per Gallon (MPG) rating of the Scenario #1 car.");
   form.mpg1.focus();
} else
if(form.perGal1.value.length == 0) {
   alert("Please enter the local cost of one gallon of gas for the Scenario #1 car.");
   form.perGal1.focus();
} else
if(form.age1.value.length == 0) {
   alert("Please enter the current age of the Scenario #1 car.");
   form.age1.focus();
} else
if(form.lifeExpect1.value.length == 0) {
   alert("Please enter the number of years you expect to own the Scenario #1 car.");
   form.lifeExpect1.focus();
} else {


ageFact = new Array(28,20,16,8,6,5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
   
//Tax, License, and warranty Costs

var tax1 = 0;
var taxCost1 = 0;
var VsalesTax1 = stripNum(form.salesTax1.value);
var VpriceTag1 = stripNum(form.priceTag1.value);

if(VsalesTax1 >1) {
   tax1 = VsalesTax1 / 100;
   } else {
   tax1 = VsalesTax1;
}

if(tax1 > 0) {
   taxCost1 = VpriceTag1 * tax1;
   } else {
   taxCost1 = 0;
}

var licCost1 = 0;
var VwarCost1 = stripNum(form.warranty1.value);
var VtotPurch1 = 0;
var Vlicense1 = stripNum(form.license1.value);
var VlifeExpect1 = stripNum(form.lifeExpect1.value);

licCost1 = Vlicense1 * VlifeExpect1;

VtotPurch1 = eval(taxCost1) + eval(licCost1) + eval(VwarCost1);

var VpurchCost1 = VtotPurch1;
form.purchCost1.value = formatNumber(VpurchCost1);


//Depreciation Costs

var Vage1 = stripNum(form.age1.value);
var timePass1 = eval(Vage1);
var accumDeprec1 = ageFact[Vage1];

while(timePass1 < eval(VlifeExpect1) + eval(Vage1) - eval(1)) {
   timePass1 = eval(timePass1 + 1);
   accumDeprec1 = accumDeprec1 + ageFact[eval(timePass1 * 1)];
      if(timePass1 > 50) {
          break;
          } else {
          continue;
       }
}

var VdepreCost1 = accumDeprec1 / 100 * VpriceTag1;
form.depreCost1.value = formatNumber(VdepreCost1);
      

//Finance Costs

var Vfinance1 = form.finance1.value;
var VdownPay1 = stripNum(form.downPay1.value);
var VintRate1 = stripNum(form.intRate1.value);
var VnPer1 = stripNum(form.nPer1.value);
var VintCost1 = 0;

if(Vfinance1 == "n" || Vfinance1 == "N" || (VintRate1 == 0 && VnPer1 == 0)) {
   VdownPay1 = 0;
   VintRate1 = 0;
   VnPer1 = 0;
   VintCost1 = 0;
   form.intCost1.value = formatNumber(VintCost1);
   } else {
   var int1 = VintRate1;
      if (int1 > 1.0) {
         int1 = int1 / 100.0;
         VintRate1 = int1;
      }

      int1 /= 12;

      var prin1 = eval(VpriceTag1) + eval(taxCost1) + eval(Vlicense1) + eval(VwarCost1) - eval(VdownPay1);

      var pmt1 = 0;

      var pow1 = 1;

      for (var j1 = 0; j1 < VnPer1; j1++)

        pow1 = pow1 * (1 + int1);

    pmt1 = (prin1 * pow1 * int1) / (eval(pow1) - eval(1));

if(VnPer1 / 12 <= VlifeExpect1) {
       VintCost1 = parseInt((pmt1 * VnPer1) - prin1,10);
       form.intCost1.value = formatNumber(VintCost1);
       } else {
       var intPort1 = 0;
       var PrinPort1 = 0;
       var count1 = 0;
       var accumPrin1 = 0;
       var accumInt1 =0;

      while(count1 < VlifeExpect1 * 12) {

           intPort1 = prin1 * int1;

           prinPort1 = pmt1 - intPort1;

           prin1 = prin1 - prinPort1;

           accumPrin1 = accumPrin1 + prinPort1;

           accumInt1 = accumInt1 + intPort1;

           count1 = count1 + 1;

           if(count1 > 600) {
              break;
              } else {
              continue;
           }

       }
   VintCost1 = parseInt(accumInt1,10);
   form.intCost1.value = formatNumber(VintCost1);
   }
}

//Operating Costs

var Vmiles1 = stripNum(form.miles1.value);
var Vmpg1 = stripNum(form.mpg1.value);
var VperGal1 = stripNum(form.perGal1.value);
var Vgas1 = Vmiles1 * VlifeExpect1 / Vmpg1 * VperGal1;
form.gas1.value = formatNumber(Vgas1);

//Maintenance & Repair Costs

var Vmaint1 = stripNum(form.maint1.value);
var VmaintCost1 = Vmaint1 * 12 * VlifeExpect1;
form.maintCost1.value = formatNumber(VmaintCost1);


//Insurance Costs

var Vinsure1 = stripNum(form.insure1.value);
var VinsCost1 = Vinsure1 * VlifeExpect1;
form.insCost1.value = formatNumber(VinsCost1);

   
//Total Costs #1

var VtotCost1 = eval(VtotPurch1) + eval(VdepreCost1) + eval(VintCost1) + eval(Vgas1) + eval(VmaintCost1) + eval(VinsCost1);
form.totCost1.value = formatNumber(VtotCost1);

var VannCost1 = VtotCost1 / VlifeExpect1;
form.annCost1.value = formatNumber(VannCost1);

var VmileCost1 = VannCost1 / Vmiles1;
form.mileCost1.value = formatNumber(VmileCost1);

//Scenario #2 if Double Scenario Checked

if(form.calcMethod[1].checked == true) {

if(form.salesTax2.value.length == 0) {
   alert("Please enter the sales tax percentage for Scenario #2");
   form.salesTax2.focus();
} else
if(form.license2.value.length == 0) {
   alert("Please enter the annual licensing cost for Scenario #2");
   form.license2.focus();
} else
if(form.finance2.value.length == 0) {
   alert("Please enter indicate whether or not you are financing the vehicle in Scenario #2");
   form.finance2.focus();
} else
if((form.finance2.value == "y" || form.finance2.value == "Y") && (form.intRate2.value.length == 0)) {
   alert("Please enter the annual financing rate for Scenario #2");
   form.intRate2.focus();
} else
if((form.finance2.value == "y" || form.finance2.value == "Y") && (form.nPer2.value.length == 0)) {
   alert("Please enter the number of months you are financing the car for in Scenario #2");
   form.nPer2.focus();
} else
if(form.insure2.value.length == 0) {
   alert("Please enter the annual insurance cost for Scenario #2");
   form.insure2.focus();
} else
if(form.miles2.value.length == 0) {
   alert("Please enter the number miles you expect to drive the Scenario #2 car per year.");
   form.miles2.focus();
} else
if(form.mpg2.value.length == 0) {
   alert("Please enter the Miles per Gallon (MPG) rating of the Scenario #2 car.");
   form.mpg2.focus();
} else
if(form.perGal2.value.length == 0) {
   alert("Please enter the local cost of one gallon of gas for the Scenario #2 car.");
   form.perGal2.focus();
} else
if(form.age2.value.length == 0) {
   alert("Please enter the current age of the Scenario #2 car.");
   form.age2.focus();
} else
if(form.lifeExpect2.value.length == 0) {
   alert("Please enter the number of years you expect to own the Scenario #2 car.");
   form.lifeExpect2.focus();
} else {


//100
//Tax, License, and warranty Costs #2

var tax2 = 0;
var taxCost2 = 0;
var VsalesTax2 = stripNum(form.salesTax2.value);
var VpriceTag2 = stripNum(form.priceTag2.value);

if(VsalesTax2 >2) {
   tax2 = VsalesTax2 / 100;
   } else {
   tax2 = VsalesTax2;
}

if(tax2 > 0) {
   taxCost2 = VpriceTag2 * tax2;
   } else {
   taxCost2 = 0;
}

var licCost2 = 0;
var VwarCost2 = stripNum(form.warranty2.value);
var VtotPurch2 = 0;
var Vlicense2 = stripNum(form.license2.value);
var VlifeExpect2 = stripNum(form.lifeExpect2.value);

licCost2 = Vlicense2 * VlifeExpect2;

VtotPurch2 = eval(taxCost2) + eval(licCost2) + eval(VwarCost2);

var VpurchCost2 = VtotPurch2;
form.purchCost2.value = formatNumber(VpurchCost2);


//Depreciation Costs #2

var Vage2 = stripNum(form.age2.value);
var timePass2 = eval(Vage2);
var accumDeprec2 = ageFact[Vage2];

while(timePass2 < eval(VlifeExpect2) + eval(Vage2) - eval(1)) {
   timePass2 = eval(timePass2 + 1);
   accumDeprec2 = accumDeprec2 + ageFact[eval(timePass2 * 1)];
      if(timePass2 > 50) {
          break;
          } else {
          continue;
       }
}

var VdepreCost2 = accumDeprec2 / 100 * VpriceTag2;
form.depreCost2.value = formatNumber(VdepreCost2);
      

//Finance Costs #2

var Vfinance2 = form.finance2.value;
var VdownPay2 = stripNum(form.downPay2.value);
var VintRate2 = stripNum(form.intRate2.value);
var VnPer2 = stripNum(form.nPer2.value);
var VintCost2 = 0;

if(Vfinance2 == "n" || Vfinance2 == "N" || (VdownPay2 == 0 && VintRate2 == 0 && VnPer2 == 0)) {
   VdownPay2 = 0;
   VintRate2 = 0;
   VnPer2 = 0;
   VintCost2 = 0;
   form.intCost2.value = formatNumber(VintCost2);
   } else {
   var int2 = VintRate2;
      if (int2 > 1.0) {
         int2 = int2 / 100.0;
         VintRate2 = int2;
      }

      int2 /= 12;

      var prin2 = eval(VpriceTag2) + eval(taxCost2) + eval(Vlicense2) + eval(VwarCost2) - eval(VdownPay2);

      var pmt2 = 0;

      var pow2 = 1;

      for (var j2 = 0; j2 < VnPer2; j2++)

        pow2 = pow2 * (1 + int2);

    pmt2 = (prin2 * pow2 * int2) / (eval(pow2) - eval(1));

if(VnPer2 / 12 <= VlifeExpect2) {
       VintCost2 = parseInt((pmt2 * VnPer2) - prin2,10);
       form.intCost2.value = formatNumber(VintCost2);
       } else {
       var intPort2 = 0;
       var PrinPort2 = 0;
       var count2 = 0;
       var accumPrin2 = 0;
       var accumInt2 =0;

      while(count2 < VlifeExpect2 * 12) {

           intPort2 = prin2 * int2;

           prinPort2 = pmt2 - intPort2;

           prin2 = prin2 - prinPort2;

           accumPrin2 = accumPrin2 + prinPort2;

           accumInt2 = accumInt2 + intPort2;

           count2 = count2 + 1;

           if(count2 > 600) {
              break;
              } else {
              continue;
           }

       }
   VintCost2 = parseInt(accumInt2,10);
   form.intCost2.value = formatNumber(VintCost2);
   }
}

//Operating Costs #2

var Vmiles2 = stripNum(form.miles2.value);
var Vmpg2 = stripNum(form.mpg2.value);
var VperGal2 = stripNum(form.perGal2.value);
var Vgas2 = Vmiles2 * VlifeExpect2 / Vmpg2 * VperGal2;
form.gas2.value = formatNumber(Vgas2);

//Maintenance & Repair Costs #2

var Vmaint2 = stripNum(form.maint2.value);
var VmaintCost2 = Vmaint2 * 12 * VlifeExpect2;
form.maintCost2.value = formatNumber(VmaintCost2);


//Insurance Costs #2

var Vinsure2 = stripNum(form.insure2.value);
var VinsCost2 = Vinsure2 * VlifeExpect2;
form.insCost2.value = formatNumber(VinsCost2);

   
//Total Costs #2

var VtotCost2 = eval(VtotPurch2) + eval(VdepreCost2) + eval(VintCost2) + eval(Vgas2) + eval(VmaintCost2) + eval(VinsCost2);
form.totCost2.value = formatNumber(VtotCost2);

var VannCost2 = VtotCost2 / VlifeExpect2;
form.annCost2.value = formatNumber(VannCost2);

var VmileCost2 = VannCost2 / Vmiles2;
form.mileCost2.value = formatNumber(VmileCost2);

var fsummary = 0;
var scenario = "";


if(VannCost1 > VannCost2) {
   fsummary = eval(VannCost1) - eval(VannCost2);
   scenario = "Scenario #2";
   } else {
   fsummary = eval(VannCost2) - eval(VannCost1);
   scenario = "Scenario #1"; 
}

form.summary.value = (scenario + " will save you $" + parseInt(fsummary,10) + " per year.");

} //end of form verfication for scenario #2
}//end of if statement that runs if Double Calc Method is selected.
} //end of form verification for scenario #1
}

function copyPaste(form) {

    form.priceTag2.value = form.priceTag1.value;
    form.salesTax2.value = form.salesTax1.value;
    form.warranty2.value = form.warranty1.value;
    form.license2.value = form.license1.value;
    form.downPay2.value = form.downPay1.value;
    form.insure2.value = form.insure1.value;
    form.finance2.value = form.finance1.value;
    form.intRate2.value = form.intRate1.value;
    form.nPer2.value = form.nPer1.value;
    form.miles2.value = form.miles1.value;
    form.mpg2.value = form.mpg1.value;
    form.perGal2.value = form.perGal1.value;
    form.maint2.value = form.maint1.value;
    form.age2.value = form.age1.value;
    form.lifeExpect2.value = form.lifeExpect1.value;

}

function skip1(form) {
if(form.singleDbl.value == 1) {
form.salesTax1.focus();
}
}

function skip2(form) {
if(form.singleDbl.value == 1) {
form.license1.focus();
}
}

function skip3(form) {
if(form.singleDbl.value == 1) {
form.warranty1.focus();
}
}

function skip4(form) {
if(form.singleDbl.value == 1) {
form.finance1.focus();
}
}


function skip5(form) {
if(form.singleDbl.value == 1) {
form.downPay1.focus();
}
}

function skip6(form) {
if(form.singleDbl.value == 1) {
form.intRate1.focus();
}
}

function skip7(form) {
if(form.singleDbl.value == 1) {
form.nPer1.focus();
}
}

function skip8(form) {
if(form.singleDbl.value == 1) {
form.insure1.focus();
}
}
    
function skip9(form) {
if(form.singleDbl.value == 1) {
form.miles1.focus();
}
}

function skip10(form) {
if(form.singleDbl.value == 1) {
form.mpg1.focus();
}
}
    
function skip11(form) {
if(form.singleDbl.value == 1) {
form.perGal1.focus();
}
}

function skip12(form) {
if(form.singleDbl.value == 1) {
form.age1.focus();
}
}

function skip13(form) {
if(form.singleDbl.value == 1) {
form.lifeExpect1.focus();
}
}

function skip14(form) {
if(form.singleDbl.value == 1) {
form.maint1.focus();
}
}

function switchSingle(form) {
form.singleDbl.value = 1;
}

function switchDouble(form) {
form.singleDbl.value = 2;
}
