//*******************************************
//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 #:3313-751-44-731
//Commercial Licence Date:2006-01-12
//*******************************************



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 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 + "");

}

}
