
//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Variable Compounding & Deposit Savings Calculator
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/17/2001
//Last Modified: 05/04/2009
//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 #:5754-1409-20-1195
//Commercial Licence Date:2010-05-03
//*******************************************



function fn(num, places, comma) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    var myDecFact = 1;
    var myPlaces = 0;
    var myZeros = "";
    while(myPlaces < places) {
       myDecFact = myDecFact * 10;
       myPlaces = Number(myPlaces) + Number(1);
       myZeros = myZeros + "0";
    }
    
	onum=Math.round(num*myDecFact)/myDecFact;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal=myZeros;
	} else{
		decimal=Math.round((onum-integer)* myDecFact)
	}
	decimal=decimal.toString();
	if (decimal.length<places) {
        fillZeroes = places - decimal.length;
	   for (z=0;z<fillZeroes;z++) {
        decimal="0"+decimal;
        }
     }

   if(places > 0) {
      decimal = "." + decimal;
   }

   if(comma == 1) {
	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;
   } else {
      finNum=integer+""+decimal;
   }

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}




function sn(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 computeForm(form)  {

   if(document.calc.interest.value == "") {
      alert("Please enter the Interest Rate.");
   } else 
   if(document.calc.moAdd.value == "") {
      alert("Please enter the Monthly Addition.");
   } else
   if(document.calc.payments.value == "") {
      alert("Please enter the Number of Years.");
   } else {
  
      var i = sn(document.calc.interest.value);
      i /= 100

      var VcompDays = 0;
      var maxCompsPerYr = 0;

      if(document.calc.compInt.selectedIndex == 0) {
         i /= 365;
         VcompDays = 1;
         maxCompsPerYr = 365;
      } else
      if(document.calc.compInt.selectedIndex == 1) {
         i /= 52;
         VcompDays = 7;
         maxCompsPerYr = 52;
      } else
      if(document.calc.compInt.selectedIndex == 2) {
         i /= 12;
         VcompDays = 30;
         maxCompsPerYr = 12;
      } else
      if(document.calc.compInt.selectedIndex == 3) {
         i /= 4;
         VcompDays = 91;
         maxCompsPerYr = 4;
      } else
      if(document.calc.compInt.selectedIndex == 4) {
         i /= 2;
         VcompDays = 182;
         maxCompsPerYr = 2;
      } else
      if(document.calc.compInt.selectedIndex == 5) {
         i /= 1;
         VcompDays = 365;
         maxCompsPerYr = 1;
      }

      var ma = sn(document.calc.moAdd.value);

      var VperiodDays = 0;
      var maxAddsPerYr = 0;

      if(document.calc.period.selectedIndex == 0) {
         VperiodDays = 1;
         maxAddsPerYr = 365;
      } else
      if(document.calc.period.selectedIndex == 1) {
         VperiodDays = 7;
         maxAddsPerYr = 52;
      } else
      if(document.calc.period.selectedIndex == 2) {
         VperiodDays = 14;
         maxAddsPerYr = 26;
      } else
      if(document.calc.period.selectedIndex == 3) {
         VperiodDays = 15;
         maxAddsPerYr = 24;
      } else
      if(document.calc.period.selectedIndex == 4) {
         VperiodDays = 30;
         maxAddsPerYr = 12;
      } else
      if(document.calc.period.selectedIndex == 5) {
         VperiodDays = 61;
         maxAddsPerYr = 6;
      } else
      if(document.calc.period.selectedIndex == 6) {
         VperiodDays = 91;
         maxAddsPerYr = 4;
      } else
      if(document.calc.period.selectedIndex == 7) {
         VperiodDays = 182;
         maxAddsPerYr = 2;
      } else
      if(document.calc.period.selectedIndex == 8) {
         VperiodDays = 365;
         maxAddsPerYr = 1;
      }

      //IF DEPOSIT FREQUENCY EQUALS COMPOUNDING FREQUENCY
      if(VperiodDays == VcompDays) {

         var ma = sn(document.calc.moAdd.value);
         var prin = sn(document.calc.principal.value);
         var origPrin = prin;
         var pmts = sn(document.calc.payments.value);
         pmts = Number(pmts * maxCompsPerYr);
         var count = 0;
    
         while(count < pmts) {

            newprin = prin + ma;
            prin = (newprin * i) + Number(prin + ma);
            count = count + 1;

         }

         var Vfv = prin;
         document.calc.fv.value = "$" + fn(prin,2,1);

         var totinv = Number(count * ma) + Number(origPrin);
         document.calc.totDeposits.value = "$" + fn(totinv,2,1);

         var Vtotalint = Number(prin - totinv);
         document.calc.totalint.value = "$" + fn(Vtotalint,2,1);


      //IF DEPOSITS ARE LESS FREQUENT THAN COMPOUNDING FREQUENCY
      } else
      if(VperiodDays > VcompDays) {
 
         var prin = sn(document.calc.principal.value);
         var origPrin = prin;

         var pmts = sn(document.calc.payments.value);
         pmts = pmts * 365;

         var maxComps = Number(pmts * maxCompsPerYr);
         var maxAdds = Number(pmts * maxAddsPerYr);

         var count = 0;
         var accumAdd = Number(ma);
         var numAdds = 1;
         var countAddDays = 0;
         var countCompDays = 0;
         var numComps = 0;
         var accumComp = 0;
         var currentInt = 0;
         prin =  Number(prin) + Number(ma);
    
         while(count < pmts) {

            //Add Addition if interval is met
            if(countAddDays == VperiodDays && numAdds < maxAdds) {
               prin = Number(prin) + Number(ma);
               accumAdd = Number(accumAdd) + Number(ma);
               accumPrin = Number(accumPrin) + Number(prin);
               numAdds = Number(numAdds) + Number(1);
               countAddDays = 1;
            } else {
               countAddDays = Number(countAddDays) + Number(1);
            }

            //Compound interest if interval is met
            if(countCompDays == VcompDays && numComps < maxComps) {
               accumComp = Number(accumComp) + Number(prin * i)
               prin = Number(prin * i) + Number(prin);
               currentInt = Number(prin * i);
               numComps = Number(numComps) + Number(1);
               countCompDays = 1;
            } else {
               countCompDays = Number(countCompDays) + Number(1);
            }

            count = Number(count) + Number(1);

         }

         var Vfv = prin;
         document.calc.fv.value = "$" + fn(prin,2,1);

         var totinv = Number(accumAdd) + Number(origPrin);
         document.calc.totDeposits.value = "$" + fn(totinv,2,1);

         var Vtotalint = Number(prin - totinv);
         document.calc.totalint.value = "$" + fn(Vtotalint,2,1);

      //IF DEPOSITS ARE MORE FREQUENT THAN COMPOUNDING FREQUENCY
      } else {

         if(document.calc.period.selectedIndex == 5) {
            VperiodDays = 60;
         }
 
         var prin = sn(document.calc.principal.value);
         var origPrin = prin;

         var pmts = sn(document.calc.payments.value);
         pmts = pmts * 365;

         var maxComps = Number(pmts * maxCompsPerYr);
         var maxAdds = Number(pmts * maxAddsPerYr);

         var count = 0;
         var accumAdd = 0;
         var numAdds = 0;
         var countAddDays = 0;
         var countCompDays = 0;
         var numComps = 0;
         var accumComp = 0;
         var depositIntervalDays = 0;
         var periodsPast = 0;
         var accumPrin = 0;
         var prinAvg = 0;

         var periodsPerComp = parseInt(VcompDays / VperiodDays,0);
    
         while(count < pmts) {
            //while(count < document.calc.testNum.value) {

            depositIntervalDays = Number(depositIntervalDays) + Number(1);

            //Accumulate period balances to figure average balance
            if(depositIntervalDays == VperiodDays || countCompDays == VcompDays) {
               periodsPast = Number(periodsPast) + Number(1);
               depositIntervalDays = 0;
            }

            //Add Addition if interval is met
            if(countAddDays == VperiodDays && numAdds < maxAdds) {
               prin = Number(prin) + Number(ma);
               accumAdd = Number(accumAdd) + Number(ma);
               accumPrin = Number(accumPrin) + Number(prin);
               prinAvg = accumPrin / periodsPast;
               numAdds = Number(numAdds) + Number(1);
               countAddDays = 1;
            } else {
               countAddDays = Number(countAddDays) + Number(1);
            }

            //Compound interest if interval is met
            if(countCompDays == (VcompDays - 1) && numComps < maxComps) {
               periodsPast = 0;
               prin = Number(prinAvg * i) + Number(prin);
               accumPrin = 0;
               accumComp = Number(accumComp) + Number(prinAvg * i)
               numComps = Number(numComps) + Number(1);
               countCompDays = 1;
            } else {
               countCompDays = Number(countCompDays) + Number(1);
            }

            count = Number(count) + Number(1);

         }

         var Vfv = prin;
         document.calc.fv.value = "$" + fn(prin,2,1);

         var totinv = Number(accumAdd) + Number(origPrin);
         document.calc.totDeposits.value = "$" + fn(totinv,2,1);

         var Vtotalint = Number(prin - totinv);
         document.calc.totalint.value = "$" + fn(Vtotalint,2,1);


      //END IF ELSE STATEMENT THAT DETERMINES WHICH ROUTINE TO RUN
      }

   //END NESTED IF TO CHECK FOR NON-NUMERIC ENTRIES
   }

}

function clear_results(form) {

   document.calc.fv.value = "";
   document.calc.totDeposits.value = "";
   document.calc.totalint.value = "";

}

