//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Savings Goal Calculator
//1998 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 08/21/1998
//Last Modified: 07/02/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-46-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.downPay.value == "" || form.downPay.value == 0) {
   alert("Please enter an amount in Line #1.");
   form.downPay.focus();
   } else
      if(form.intRate.value == "" || form.intRate.value == 0) {
      alert("Please enter an amount in Line #3.");
      form.intRate.focus();
   } else
      if(form.numYears.value == "" || form.numYears.value == 0) {
      alert("Please enter an amount in Line #4.");
      form.numYears.focus();
   } else {

var VdownPay = stripNum(form.downPay.value);

var VsaveBal = stripNum(form.saveBal.value);

var VnumYears = stripNum(form.numYears.value);

var intRate = stripNum(form.intRate.value);

if (intRate >= 1.0) {
   intRate = intRate / 100.0;
} 

//FIGURE FUTURE VALUE OF PRESENT SAVINGS

    var factor1 = eval(intRate) + eval(1);

    var denom1 = 1;

    var count1 = 0;

     while(count1 < VnumYears) {
        denom1 = denom1 * factor1;
        count1 = eval(count1) + eval(1);
      }

    var VsaveFV = VsaveBal * denom1;

    form.saveFV.value = "$" + formatNumber(VsaveFV);

    var VsaveGap = eval(VdownPay) - eval(VsaveFV);

    form.saveGap.value = "$" + formatNumber(VsaveGap);

//FIGURE PRESENT VALUE OF ADJUSTED SAVINGS GAP
   
   var count2 = 0;

   var intRate2 = intRate / 12;

   var numMonths = VnumYears * 12;

   var factor2 = eval(1) + eval(intRate2);

   var denom2 = 1;
    
    while(count2 < numMonths) {
       denom2 = denom2 * factor2;
        count2 = eval(count2) + eval(1);
        }

    var Vpv = eval(denom2) - eval(1);

    Vpv = intRate2 / Vpv;

    Vpv = Vpv * VsaveGap;

    form.moSave.value = "$" + formatNumber(Vpv);


//END VARIFICATION IF STATEMENT
   }
    
}

function help0(form) {
form.saveBal.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 help1(form) {
form.help.value = "";
form.help.value = ("Line #1: ENTER: Your the amount of your future savings goal.");
}

function help2(form) {
form.help.value = "";
form.help.value = ("Line #2: ENTER: The amount of money you currently have set aside (in an interest earning account) for applying to toward your future savings goal.");
}

function help3(form) {
form.help.value = "";
form.help.value = ("Line #3: ENTER: The annual percentage rate (APR) at which you expect your savings to grow.");
}

function help4(form) {
form.help.value = "";
form.help.value = ("Line #4: ENTER: The number of years between now and when you want to accomplish your savings goal. Then click on the \"Compute\" button.");
}

function help5(form) {
form.help.value = "";
form.help.value = ("Line #5: RESULT: The amount your current savings will grow to at the end of the specified number of years.");
}

function help6(form) {
form.help.value = "";
form.help.value = ("Line #6: RESULT: The total additonal amount you will need to meet your savings goal.");
}

function help7(form) {
form.help.value = "";
form.help.value = ("Line #7: RESULT: How much money you'll have to save every month to reach your future savings goal.");
}
