//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Debt Investment Calculator - One-time Investment
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 06/16/2001
//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-44-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.principal.value == "" || form.principal.value == 0) {
   alert("Please enter the principal amount (how much you owe as of today).");
   form.principal.focus();
} else
if(form.interest.value == "" || form.interest.value == 0) {
   alert("Please enter an annual interest rate.");
   form.interest.focus();
} else
if(form.origPmt.value == "" || form.origPmt.value == 0) {
   alert("Please enter your current monthly payment amount.");
   form.origPmt.focus();
} else {


var i = stripNum(form.interest.value);
 if (i > 1.0) {
   i = i / 100.0;
 }
 i /= 12;


   var prin1 = stripNum(form.principal.value);

   var prin2 = stripNum(form.principal.value);

   var prin3 = stripNum(form.principal.value);

   var pmt1 = stripNum(form.origPmt.value);

  var VpmtAdd = stripNum(form.pmtAdd.value);

  var pmt2 = eval(pmt1) + eval(VpmtAdd);

  var prinAdd =  VpmtAdd;

   var prinPort1 = 0;

   var prinPort2 = 0;

   var intPort1 = 0;

   var intPort2 = 0;

   var count1 = 0;

   var count2 = 0;

   var accumInt1 = 0;

   var accumInt2 = 0;
    
   while((prin1 * (eval(i) + eval(1))) >= pmt1) {
                    intPort1 = i * prin1;
                    accumInt1 = eval(accumInt1) + eval(intPort1);
                    prinPort1 = eval(pmt1) - eval(intPort1);
                    prin1 = eval(prin1) - eval(prinPort1);
                    count1 = count1 + 1
                    if(count1 > 600) { break; } else { continue;}
                    }
                    intPort1 = i * prin1;
                    accumInt1 = eval(accumInt1) + eval(intPort1);
                    prinPort1 = prin1;
                    count1 = count1 + 1
                    prin1 = eval(prin1) - eval(prinPort1);

   while((prin2 * (eval(i) + eval(1))) >= pmt1) {
                    intPort2 = eval(i * prin2);
                    accumInt2 = eval(accumInt2) + eval(intPort2);
                    prinPort2 = eval(pmt1 - intPort2);
                    prin2 = eval(prin2 - prinPort2);
                    count2 = count2 + 1
                    if(count2 == 1) {
                       prin2 = eval(prin2) - eval(prinAdd);
                       }
                    if(count2 > 600) { break; } else { continue;}
                    }
                    intPort2 = eval(i * prin2);
                    accumInt2 = eval(accumInt2) + eval(intPort2);
                    prinPort2 = prin2;
                    count2 = count2 + 1
                    prin2 = eval(prin2 - prinPort2);

     var VoldNPR = count1;
     form.oldNPR.value = formatNumber(VoldNPR);

     var VnewNPR = count2;
     form.newNPR.value = formatNumber(VnewNPR);

     var timSave = (count1 - count2);
     form.timeSave.value = formatNumber(timSave);

      var VoldIntCost = eval(accumInt1);
      form.oldIntCost.value = "$" + formatNumber(VoldIntCost);

      var VnewIntCost = eval(accumInt2);
      form.newIntCost.value = "$" + formatNumber(VnewIntCost);


      var VintSave = eval(accumInt1) - eval(accumInt2);
      form.intSave.value = "$" + formatNumber(VintSave);


      var Vroi = 0;
      Vroi = (VintSave / (count2 / 12)) / prinAdd;
      form.roi.value = formatNumber(Vroi * 100) + "%";



     var yearSave = 0;

     if(timSave / 12 < 1) {
        yearSave = 0;
       } else {
        yearSave = timSave / 12;
      }

var timeSaveText = "";

if(timSave > 11) {
timeSaveText = ("And that's not even considering the emotional returns you'll get when you pay off this debt " + timSave + "-months (" + parseInt(yearSave,10) + " years, " + (timSave %12) + " months) ahead of schedule!");
} else
if(timSave > 0) {
timeSaveText = ("And that's not even considering the emotional returns you'll get when you pay off this debt " + timSave + "-months ahead of schedule!");
}

      form.results.value = ("If you add $" + formatNumber(VpmtAdd) + " to your next scheduled payment, you will save $" + formatNumber(VintSave) + " in interest charges over the life of your loan. This savings translates into a guaranteed, tax-free, average annual return of " + formatNumber(Vroi * 100) + "%. " + timeSaveText + "");

}

}
