// JavaScript Document

//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Debt Consolidation Calculator V1
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/17/2001
//Last Modified: 04/28/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 #:4306-1004-15-970
//Commercial Licence Date:2007-01-18
//*******************************************



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 compute_line(ln) {

   var v_name_cell = document.getElementById("D" + ln + "");
   var v_prin_cell = document.getElementById("prin" + ln + "");
   var v_rate_cell = document.getElementById("intRate" + ln + "");
   var v_pmt_cell = document.getElementById("pmt" + ln + "");

   var v_cost_cell = document.getElementById("cost" + ln + "");
   var v_npr_cell = document.getElementById("npr" + ln + "");

   var v_D = v_name_cell.value;
   var v_prin = sn(v_prin_cell.value);
   var v_rate = sn(v_rate_cell.value);
   var v_pmt = sn(v_pmt_cell.value);

   var v_tot_cost = 0;
   var v_npr = 0;

   if(v_prin > 0 && v_rate > 0 && v_pmt > 0) {
      v_tot_cost = computeCost(v_D,v_prin,v_rate,v_pmt);
      v_cost_cell.value = "$" + fn(v_tot_cost,2,1);
      v_npr = computeTerm(v_D,v_prin,v_rate,v_pmt);
      v_npr_cell.value = fn(v_npr,2,1);
      computeForm(document.debts);
      clear_results(document.debts);
   }

}



function computeForm(form) {

   var accumPrincipal = 0;
   var accumCost = 0;
   var accumPmtAmt = 0;
   var accumPmts = 0;
   var accumDebts = 0;
   var report_row = "";
   var reportRows = "";

   for(var i = 1; i<16; i++) {

      var v_name_cell = document.getElementById("D" + i + "");
      var v_prin_cell = document.getElementById("prin" + i + "");
      var v_rate_cell = document.getElementById("intRate" + i + "");
      var v_pmt_cell = document.getElementById("pmt" + i + "");


      var v_cost_cell = document.getElementById("cost" + i + "");
      var v_npr_cell = document.getElementById("npr" + i + "");

      var v_D = v_name_cell.value;
      var v_prin = sn(v_prin_cell.value);
      var v_rate = sn(v_rate_cell.value);
      var v_pmt = sn(v_pmt_cell.value);

      var v_tot_cost = 0;
      var v_npr = 0;

      if(v_prin > 0 && v_rate > 0 && v_pmt > 0) {
         v_tot_cost = computeCost(v_D,v_prin,v_rate,v_pmt);
         v_cost_cell.value = "$" + fn(v_tot_cost,2,1);
         v_npr = computeTerm(v_D,v_prin,v_rate,v_pmt);
         v_npr_cell.value = fn(v_npr,2,1);
         if(v_npr > accumPmts) {
            accumPmts = v_npr;
         }
         accumCost = Number(accumCost) + Number(v_tot_cost);
         accumPrincipal = Number(accumPrincipal) + Number(v_prin);
         accumPmtAmt = Number(accumPmtAmt) + Number(v_pmt);
         accumDebts++;
         report_row = createRow(v_D,v_prin,v_rate,v_pmt,v_cost_cell.value,v_npr_cell.value);
         reportRows = "" + reportRows + "" + report_row + "";
      }

   }


   if(accumDebts == 0) {
      //alert("Please enter at least one debt before computing current debt costs.");
   } else {

      document.debts.totalCost.value = "$" + fn(accumCost,2,1);
      document.debts.totalPrincipal.value = "$" + fn(accumPrincipal,2,1);
      document.debts.totalTerm.value = fn(accumPmts,2,0);
      document.debts.totalPmtAmt.value = "$" + fn(accumPmtAmt,2,1);

      document.debts.HtotalCost.value = accumCost;
      document.debts.HtotalPrincipal.value = accumPrincipal;
      document.debts.HtotalTerm.value = accumPmts;
      document.debts.HtotalPmtAmt.value = accumPmtAmt;
      document.debts.HtotalDebts.value = accumDebts;

      reportRows += "<tr><td colspan='6'><hr></td></tr>";
      reportRows += "<tr><td><font face='arial'><small><strong>Current Terms</strong></small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>$" + fn(sn(document.debts.totalPrincipal.value),2,1) + "</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>N/A</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>$" + fn(sn(document.debts.totalPmtAmt.value),2,1) + "</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>$" + fn(sn(document.debts.totalCost.value),2,1) +  "</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>" + fn(accumPmts,2,0) + "</small></font></td></tr>";


      document.debts.HreportRows.value = reportRows;
   } //end of if/else statement that checks to see if any debts were entered
}

function computeLoan(form) {

   computeForm(document.debts);

   if(document.debts.consolAPR.value == 0 || document.debts.consolAPR.value == "") {
      alert("Please enter an annual interest rate for your consolidation loan.");
      document.debts.consolAPR.focus();
   } else
   if(document.debts.consolTerm.value == 0 || document.debts.consolTerm.value == "") {
      alert("Please enter the length of the term (number of years) for your consolidation loan.");
      document.debts.consolTerm.focus();
   } else {

      var VconsolName = "Consolidation Loan";
      var VconsolPrin = document.debts.HtotalPrincipal.value;
      var VconsolAPR = sn(document.debts.consolAPR.value);
      var VconsolTerm = sn(document.debts.consolTerm.value);

      var i = VconsolAPR;
      if(i >= 1.0) {
         i = i / 100.0;
      }
      i /= 12;

      var noMonths = VconsolTerm * 12;
      var pow = 1;

      for(var j = 0; j < noMonths; j++) {
         pow = pow * (1 + i);
      }

      var VconsolPmt = (VconsolPrin * pow * i) / (pow - 1);

      var VtotConCost = computeCost(VconsolName,VconsolPrin,VconsolAPR,VconsolPmt);

      var pmtSign = "-";
      var pmtColor = "green";
      var pmtReport = "";
      var VHtotalPmt = document.debts.HtotalPmtAmt.value;
      document.debts.totalNowPmt.value = fn(VHtotalPmt,2,1);
      document.debts.totalConPmt.value = fn(VconsolPmt,2,1);
      var VpmtDiff = Number(VHtotalPmt) - Number(VconsolPmt);
      if(VpmtDiff < 0) {
         VpmtDiff *= -1;
         pmtSign = "+";
         pmtColor = "red";
      }
      pmtReport = pmtSign + "$" + fn(VpmtDiff,2,1);
      document.debts.pmtDiff.value = pmtSign + "" + fn(VpmtDiff,2,1);


      var termSign = "-";
      var termColor = "green";
      var termReport = "";
      var VHtotalTerm = document.debts.HtotalTerm.value;
      document.debts.totalNowTerm.value = fn(VHtotalTerm,2,1);
      document.debts.totalConTerm.value = fn(noMonths,2,0);
      var VtermDiff = Number(VHtotalTerm) - Number(noMonths);
      if(VtermDiff < 0) {
         VtermDiff *= -1;
         termSign = "+";
         termColor = "red";
      }
      termReport = termSign + "" + fn(VtermDiff,2,1);
      document.debts.termDiff.value = termSign + "" + fn(VtermDiff,2,1);

      var VconsolFees = sn(document.debts.consolFees.value);
      if(VconsolFees == "") {
         VconsolFees = 0;
      }
      var intSign = "-";
      var intColor = "green";
      var intReport = "";
      var VHtotalInt = document.debts.HtotalCost.value;
      document.debts.totalNowInt.value = fn(VHtotalInt,2,1);
      VtotConCost = Number(VtotConCost) + Number(VconsolFees);
      document.debts.totalConInt.value = fn(VtotConCost,2,1);
      var VintDiff = Number(VHtotalInt) - Number(VtotConCost);
      if(VintDiff < 0) {
         VintDiff *= -1;
         intSign = "+";
         intColor = "red";
      }
      intReport = intSign + "$" + fn(VintDiff,2,1)
      document.debts.intDiff.value = intSign + "" + fn(VintDiff,2,1);

      var VHtotalDebts = document.debts.HtotalDebts.value;

      var v_summary = "";

      if(pmtSign == "-" && termSign == "+" && intSign == "+") {
         v_summary += "If you consolidate your " + VHtotalDebts + " debts into a ";
         v_summary += "single $" + fn(VconsolPrin,2,1) + " loan, ";
         v_summary += "for " + noMonths + " months, at " + sn(document.debts.consolAPR.value) + "% APR, ";
         v_summary += "your monthly debt payments will be reduced ";
         v_summary += "by $" + fn(VpmtDiff,2,1) + ", but it will take you an ";
         v_summary += "additional " + fn(VtermDiff,2,1) + " months to pay off your debts ";
         v_summary += "and you will end up paying an additional $" + fn(VintDiff,2,1) + " in ";
         v_summary += "interest charges and/or loan fees.";
      } else
      if(pmtSign == "-" && termSign == "-" && intSign == "-") {
         v_summary += "If you consolidate your " + VHtotalDebts + " debts into a ";
         v_summary += "single $" + fn(VconsolPrin,2,1) + " loan, ";
         v_summary += "for " + noMonths + " months, at " + sn(document.debts.consolAPR.value) + "% APR, ";
         v_summary += "your monthly debt payments will be reduced by $" + fn(VpmtDiff,2,1) + ", you ";
         v_summary += "will pay off your debts " + fn(VtermDiff,2,1) + " months earlier ";
         v_summary += "and you will end up saving $" + fn(VintDiff,2,1) + " in interest ";
         v_summary += "charges and/or loan fees. Just make sure you don't create any new debt paying ";
         v_summary += "off your consolidation loan.";
      } else
      if(pmtSign == "-" && termSign == "+" && intSign == "-") {
         v_summary += "If you consolidate your " + VHtotalDebts + " debts into a ";
         v_summary += "single $" + fn(VconsolPrin,2,1) + " loan, ";
         v_summary += "for " + noMonths + " months, at " + sn(document.debts.consolAPR.value) + "% APR, ";
         v_summary += "your monthly debt payments will be reduced by $" + fn(VpmtDiff,2,1) + ", it ";
         v_summary += "will take you an additional " + fn(VtermDiff,2,1) + " months to pay ";
         v_summary += "off your debts and you will end up saving $" + fn(VintDiff,2,1) + " in ";
         v_summary += "interest charges and/or loan fees. Just make sure you don't do what most people ";
         v_summary += "who consolidate do -- they create new debt while they're paying off their consolidation loan.";
      } else
      if(pmtSign == "-" && termSign == "-" && intSign == "+") {
         v_summary += "If you consolidate your " + VHtotalDebts + " debts into a ";
         v_summary += "single $" + fn(VconsolPrin,2,1) + " loan, ";
         v_summary += "for " + noMonths + " months, at " + sn(document.debts.consolAPR.value) + "% APR, ";
         v_summary += "your monthly debt payments will be reduced by $" + fn(VpmtDiff,2,1) + ", you ";
         v_summary += "will pay off your debts " + fn(VtermDiff,2,1) + " months earlier but you ";
         v_summary += "will end up paying an additional $" + fn(VintDiff,2,1) + " in interest ";
         v_summary += "charges and/or loan fees.";
      } else
      if(pmtSign == "+" && termSign == "-" && intSign == "+") {
         v_summary += "If you consolidate your " + VHtotalDebts + " debts into a ";
         v_summary += "single $" + fn(VconsolPrin,2,1) + " loan, for " + noMonths + " months, ";
         v_summary += "at " + sn(document.debts.consolAPR.value) + "% APR, your monthly debt payments will ";
         v_summary += "increase by $" + fn(VpmtDiff,2,1) + ", you will pay off your ";
         v_summary += "debts " + fn(VtermDiff,2,1) + " months earlier but you will end up ";
         v_summary += "paying an additional $" + fn(VintDiff,2,1) + " in interest charges ";
         v_summary += "and/or loan fees.";
      } else {
         v_summary += "No Summary Available.";
      }

      var v_sum_cell = document.getElementById("summary");
      v_sum_cell.innerHTML = "<font face='arial'><small><strong>Summary: </strong>" + v_summary + "</small></font>";

      var reportRows = document.debts.HreportRows.value;

      reportRows += "<tr><td colspan='6'><hr></td></tr><tr>";
      reportRows += "<td><font face='arial'><small><strong>Consolidation Terms</strong></small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>$" + document.debts.totalPrincipal.value + "</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>" + fn(sn(document.debts.consolAPR.value),2,0) + "%</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>$" + document.debts.totalConPmt.value + "</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>$" + document.debts.totalConInt.value +  "</small></font></td>";
      reportRows += "<td align='right'><font face='arial'><small>" + document.debts.totalConTerm.value + "</small></font></td>";
      reportRows += "</tr><tr><td colspan='6'><hr></td></tr>";
      reportRows += "<tr><td><font face='arial'><small><strong>Difference</strong></small></font></td>";
      reportRows += "<td align='right'></td><td align='right'></td>";
      reportRows += "<td align='right'><FONT COLOR=" + pmtColor + "><font face='arial'><small>" + pmtReport + "</small></font></font></td>";
      reportRows += "<td align='right'><font face='arial'><small><font color=" + intColor + ">" + intReport +  "</small></font></font></td>";
      reportRows += "<td align='right'><font face='arial'><small><font color=" + termColor + ">" + termReport + "</small></font></font></td>";
      reportRows += "</tr><tr><td colspan='6'><hr /></td></tr><tr><td colspan='6'>";
      reportRows += "" + v_sum_cell.innerHTML + "</td></tr>";

      document.debts.HreportRows.value = reportRows;

   } //end of if/esle statement that checks to see if top section computed
}

function computeCost(name,principal,interest,payment) {

   var i = interest;
   if (i > 1.0) {
      i = i / 100.0;
   }
   i /= 12;

   var prin = principal;
   var count = 0;
   var prinPort = 0;
   var intPort = 0;
   var pmt = payment;
   var accumInt = 0;

   while(Number(prin) > Number(pmt)) {
      intPort = prin * i;
      accumInt = accumInt + intPort;
      prinPort = pmt - intPort;
      prin = prin - prinPort;
      count = count +1;
      if(count > 600) {
         neverPayOff(name);
         break;
         return false;
      }
   }

   intPort = prin * i;
   accumInt = accumInt + intPort;
   prinPort = pmt - intPort;
   prin = prin - prinPort;

   var totalInt = accumInt;

   return totalInt;
}

function computeTerm(name,principal,interest,payment) {

   var i = interest;
   if (i > 1.0) {
      i = i / 100.0;
   }
   i /= 12;

   var prin = principal;
   var count = 0;
   var prinPort = 0;
   var intPort = 0;
   var pmt = payment;
   var accumInt = 0;
   var lastPmt = 0;

   while(Number(prin) > Number(pmt)) {
      intPort = prin * i;
      prinPort = pmt - intPort;
      prin = prin - prinPort;
      count = count +1;
      if(count > 600) {
         break;
         return false;
      }
   }

   totalPmts = prin / payment + count;

   return totalPmts;
}

function neverPayOff(name) {
   alert("Based on the terms you entered for " + name + ", your monthly interest charge is greater than your monthly payment -- which means you will never pay off " + name + ". Please increase the payment amount until this alert message disappears.");
}

function line_return(ln) {

   var v_next = ln;

   if(v_next < 15) {
      v_next++;
   }

   v_next_line = document.getElementById("D" + v_next + "");
   v_next_line.focus();

}


function createRow(name,principal,interest,payment,cost,term) {

   var reportRow = "<tr><td><font face='arial'><small>" + name + "</small></font></td>";
   reportRow += "<td align='right'><font face='arial'><small>$" + fn(principal,2,1) + "</small></font></td>";
   reportRow += "<td align='right'><font face='arial'><small>" + fn(interest,2,0) + "%</small></font></td>";
   reportRow += "<td align='right'><font face='arial'><small>$" + fn(payment,2,1) + "</small></font></td>";
   reportRow += "<td align='right'><font face='arial'><small>" + cost +  "</td>";
   reportRow += "<td align='right'><font face='arial'><small>" + term + "</small></font></td></tr>";

   return reportRow;
}

function createReport(form) {

   computeForm(document.debts);
   computeLoan(document.debts);

   var report = "<head><title>Loan Consolidation Report</title></head>";
   report += "<body bgcolor='#FFFFFF'><br /><br /><center>";
   report += "<font face='arial'><big><strong>Loan Consolidation Report</strong></big></font><br />";
   report += "<font face='arial'><small><small>(Please allow for slight rounding differences)</small></small></font>";
   report += "</center><p /><center><table border='0' cellpadding='4'><tr>";
   report += "<td bgcolor='silver'><font face='arial'><small><strong>Debt Name</strong></small></font></td>";
   report += "<td bgcolor='silver' align='center'><font face='arial'><small><strong>Principal<br />Balance</strong></small></font></td>";
   report += "<td bgcolor='silver' align='center'><font face='arial'><small><strong>APR</strong></small></font></td>";
   report += "<td bgcolor='silver' align='center'><font face='arial'><small><strong>Monthly<br />Payment</strong></small></font></td>";
   report += "<td bgcolor='silver' align='center'><font face='arial'><small><strong>Interest<br />Cost</strong></small></font></td>";
   report += "<td bgcolor='silver' align='center'><font face='arial'><small><strong>Number<br />of Pmts</strong></small></font></td>";
   report += "</tr> " + document.debts.HreportRows.value + " </table><p /><center><font face='arial'><small>";
   report += "This report was created with <u>The Debt Consolidation Calculator</u><br />Written by Daniel C. Peterson<br />";
   report += "Calculator can be found at http://www.webwinder.com</small></font><p>";
   report += "<form method='post'><input type='button' value='Close Window' onClick='window.close()'>";
   report += "</form></center></body></html>";


   reportWin = window.open("","","width=500,height=300,toolbar=yes,menubar=yes,scrollbars=yes");
   reportWin.document.write(report);
   reportWin.document.close();
   
}


function clear_results(form) {

   document.debts.totalNowPmt.value = "";
   document.debts.totalConPmt.value = "";
   document.debts.pmtDiff.value = "";
   document.debts.totalNowTerm.value = "";
   document.debts.totalConTerm.value = "";
   document.debts.termDiff.value = "";
   document.debts.totalNowInt.value = "";
   document.debts.totalConInt.value = "";
   document.debts.intDiff.value = "";

   var v_sum_cell = document.getElementById("summary");
   v_sum_cell.innerHTML = "<font face='arial'><small><strong>Summary: </strong></small></font>";


}


function reset_calc() {

   if(confirm("Are you sure you want to reset the calculator?")) {
      var v_sum_cell = document.getElementById("summary");
      v_sum_cell.innerHTML = "<font face='arial'><small><strong>Summary: </strong></small></font>";

      document.debts.reset();

   }

}
