//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Debt Investment Calculator w/Amortization Schedule
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 04/16/2001
//Last Modified: 06/27/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-43-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);
   if(prin1 == "") {
   prin1 = 0;
   } else {
   prin1 = prin1;
   }

   var prin2 = stripNum(form.principal.value);
   if(prin2 == "") {
   prin2 = 0;
   } else {
   prin2 = prin2;
   }

   var prin3 = stripNum(form.principal.value);
   if(prin3 == "") {
   prin3 = 0;
   } else {
   prin3 = prin3;
   }

   var pmt1 = stripNum(form.origPmt.value);
   if(pmt1 == "") {
   pmt1 = 0;
   } else {
   pmt1 = pmt1;
   }

   var VpmtAdd = stripNum(form.pmtAdd.value);
   if(VpmtAdd == "") {
  VpmtAdd = 0;
   } else {
   VpmtAdd = VpmtAdd;
   }

  var pmt2 = eval(pmt1) + eval(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 > 0) {
                    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;}
                    
                    }

   while(prin2 > 0) {
                    intPort2 = eval(i * prin2);
                    accumInt2 = eval(accumInt2) + eval(intPort2);
                    prinPort2 = eval(pmt2 - intPort2);
                    prin2 = eval(prin2 - prinPort2);
                    count2 = count2 + 1
                    if(count2 > 600) { break; } else { continue;}
                    }

     var VoldNPR = count1;
     form.oldNPR.value = parseInt(VoldNPR,10);

     var VnewNPR = count2;
     form.newNPR.value = parseInt(VnewNPR,10);

     var timSave = (count1 - count2);
     form.timeSave.value = parseInt(timSave,10);

      var VoldIntCost = eval(accumInt1);
      form.oldIntCost.value = "$" + formatNumber(VoldIntCost,10);

      var VnewIntCost = eval(accumInt2);
      form.newIntCost.value = "$" + formatNumber(VnewIntCost,10);


      var VintSave = eval(accumInt1) - eval(accumInt2);
      form.intSave.value = "$" + formatNumber(VintSave,10);


      var Vroi = 0;
      Vroi = (VintSave / (count2 / 12)) / (VpmtAdd * 12);
      form.roi.value = parseInt(Vroi * 100,10) + "%";



     var yearSave = 0;

     if(timSave / 12 < 1) {
        yearSave = 0;
       } else {
        yearSave = timSave / 12;
      }

      form.results.value = ("If you add $" + formatNumber(VpmtAdd) + " to your monthly payment, you will pay off this debt in " + count2 + " payments instead of " + count1 + ", and you will save $" + formatNumber(VintSave) + " in interest charges.  This savings translates into a guaranteed, tax-free, average annual return of " + parseInt(Vroi * 100,10) + "%.  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!");

document.debt.Hprin.value = prin3;
document.debt.HintRate.value = i;
document.debt.Hpmt2.value = pmt2;

}

}

function createReport(form) {

var Prin3 = eval(document.debt.Hprin.value);
var i = eval(document.debt.HintRate.value);
var pmt2 = eval(document.debt.Hpmt2.value);

var intPort3 = 0;
var accumInt3 = 0;
var prinPort3 = 0;
var accumPrin3 = 0;
var count3 = 0;
var pmtRow = "";
var pmtNum = 0;
var nowPmt = 0;

var today = new Date();
var dayFactor = today.getTime();
var pmtDay = today.getDate();
var loanMM = today.getMonth() + 1;
var loanYY = today.getYear();
var loanDate = (loanMM + "/" + pmtDay + "/" + loanYY);
var monthMS = 86400000 * 30.4;
var pmtDate = 0;

while(Prin3 > 0) {
   if(Prin3 < pmt2) {
   intPort3 = Prin3 * i;
   accumInt3 = eval(accumInt3) + eval(intPort3);
   prinPort3 = eval(Prin3);
   accumPrin3 = eval(accumPrin3) + eval(prinPort3);
   Prin3 = 0;
   } else {
   intPort3 = Prin3 * i;
   accumInt3 = eval(accumInt3) + eval(intPort3);
   prinPort3 = eval(pmt2) - eval(intPort3);
   accumPrin3 = eval(accumPrin3) + eval(prinPort3);
   Prin3 = eval(Prin3) - eval(prinPort3);
   }
   pmtNow = eval(intPort3) + eval(prinPort3);
   count3 = eval(count3) + eval(1);
   pmtNum = eval(pmtNum) + eval(1);
   dayFactor = eval(dayFactor) + eval(monthMS);
   pmtDate = new Date(dayFactor);
   pmtMonth = pmtDate.getMonth();
   pmtMonth = pmtMonth + 1;
   pmtYear = pmtDate.getYear();
   pmtString = (pmtMonth + "/" + pmtDay + "/" + pmtYear);
   pmtRow = ("" + pmtRow + "<TR><TD ALIGN=RIGHT><font face='arial'><small>" + pmtNum + "</small></font></TD><TD ALIGN=RIGHT><font face='arial'><small>" + pmtString + "</small></font></TD><TD ALIGN=RIGHT><font face='arial'><small>" + formatNumber(pmtNow) + "</small></font></TD><TD ALIGN=RIGHT><font face='arial'><small>" + formatNumber(prinPort3) + "</small></font></TD><TD ALIGN=RIGHT><font face='arial'><small>" + formatNumber(intPort3) + "</small></font></TD><TD ALIGN=RIGHT><font face='arial'><small>" + formatNumber(Prin3) + "</small></font></TD></TR>");
      if(count3 > 600) {
         alert("Using your current entries you will never pay off this loan.");
         break;
         } else {
         continue;
         }
    }

var part1 = ("<HEAD><TITLE>Amortization Schedule</TITLE></HEAD>" + "<BODY BGCOLOR = '#FFFFFF'><BR><BR><CENTER><font face='arial'><big><strong>Amortization Schedule</strong></big></FONT></CENTER>");

var part2 = ("<CENTER><TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0><TR><TD COLSPAN=6><font face='arial'><small><B>Loan Date: " + loanDate + "<BR>Principal: $" + formatNumber(form.Hprin.value) + "<BR># of Payments: " + count3 + "<BR>Interest Rate: " + formatNumber(i * 12 * 100) + "%<BR>Payment: $" + formatNumber(pmt2) + "</B></small></font></TD></TR><TR><TD COLSPAN=6><CENTER><font face='arial'>Schedule of Payments</FONT><BR><font face='arial'><small><small>Please allow for slight rounding differences.</small></small></FONT></CENTER></TD></TR><TR><TD><font face='arial'><small><B>Pmt #</B></small></font></TD><TD ALIGN=CENTER><font face='arial'><small><B>Date</B></small></font></TD><TD ALIGN=CENTER><font face='arial'><small><B>Payment</B></small></font></TD><TD><font face='arial'><small><B>Principal</B></small></font></TD><TD><font face='arial'><small><B>Interest</B></small></font></TD><TD><font face='arial'><small><B>Balance</B></small></font></TD></TR>");

var part3 = ("" + pmtRow + "");

var part4 = ("<TR><TD><font face='arial'><small><B>Totals</B></small></font></TD><TD></TD><TD></TD><TD ALIGN=RIGHT><font face='arial'><small><B>" + formatNumber(accumPrin3) + "</B></small></font></TD><TD><font face='arial'><small><B>" + formatNumber(accumInt3) + "</B></small></font></TD><TD></TD></TR></TABLE><br><center><form method='post'><input type='button' value='Close Window' onClick='window.close()'></form></center></BODY></HTML>");

var schedule = (part1 + "" + part2 + "" + part3 + part4 + "");

  reportWin = window.open("","","width=500,height=300,toolbar=yes,menubar=yes,scrollbars=yes");
  reportWin.document.write(schedule);
  reportWin.document.close();

   }


function clearForm(form)

{

    form.principal.value = "";

    form.interest.value = "";

    form.origPmt.value = "";

    form.pmtAdd.value = "";

    form.oldNPR.value = "";

    form.newNPR.value = "";

    form.timeSave.value = "";

    form.oldIntCost.value = "";

    form.newIntCost.value = "";

    form.intSave.value = "";

    form.roi.value = "";

    form.results.value = "";
}