//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Fixed Vs. Minimum Payment Calculator V2
//2004 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 07/13/2004
//Last Modified: 07/13/2004
//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-131-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 formatNumberDec(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 = eval(myPlaces) + eval(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;
}


var pmt_num_arr = new Array();
var pmt_amt_arr = new Array();
var int_port_arr = new Array();
var prin_port_arr = new Array();
var prin_arr = new Array();


function computeForm(form) {

if(form.principal.value == 0 || form.principal.value == "") {
   alert("Please enter the balance on your credit card.");
   form.principal.focus();
} else
if(form.interest.value == 0 || form.interest.value == "") {
   alert("Please enter the credit card's annual interest rate.");
   form.interest.focus();
} else
if(form.pmtMethod[1].checked && form.fixed_pmt.value.length == 0) {
   alert("Please enter the fixed payment amount you could afford to pay each month.");
   form.fixed_pmt.focus();
} else {

   var Vprincipal = stripNum(form.principal.value);

   var Vinterest = stripNum(form.interest.value);
   var i = Vinterest;
   if(Vinterest >= 1) {
      i /= 100;
   }
   i /= 12;

   var Vminpayperc = form.minpayperc.options[form.minpayperc.selectedIndex].value;
   var Vmin_pmt = Vminpayperc * Vprincipal;
   var Vfixed_pmt = stripNum(form.fixed_pmt.value);

   var Vpmt_amt = 0;
   var Vpmt_method = 0;

   if(form.pmtMethod[0].checked) {
      Vpmt_amt = Vmin_pmt;
      Vpmt_method = 0;
   } else {
      Vpmt_amt = Vfixed_pmt;
      Vpmt_method = 1;
   }

   var prin = Vprincipal;
   var count = 0;
   var int_port = 0;
   var prin_port = 0;
   var accum_int = 0;

   var Vpmt_rows = "";

   while(prin > 0) {

      count += 1;

      if(Vpmt_method == 0) {
         Vpmt_amt = Vminpayperc * prin;
         if(Vpmt_amt < 10) {
            Vpmt_amt = 10;
         }
      }


      if((prin * (eval(1) + eval(i))) > Vpmt_amt) {

         int_port = i * prin;
         prin_port = eval(Vpmt_amt) - eval(int_port);
         accum_int += int_port;
         prin = eval(prin) - eval(prin_port);

      } else {

         int_port = i * prin;
         prin_port = prin;
         Vpmt_amt = eval(prin) + eval(int_port);
         accum_int += int_port;
         prin = 0;

      }

      pmt_num_arr[count] = count;
      pmt_amt_arr[count] = formatNumberDec(Vpmt_amt,2,1);
      int_port_arr[count] = formatNumberDec(int_port,2,1);
      prin_port_arr[count] = formatNumberDec(prin_port,2,1);
      prin_arr[count] = formatNumberDec(prin,2,1);

      if(count > 1000) {
         alert("At the terms you entered your balance will never be paid off. Please increase the payment amount until this alert does not show up.");

         return;
         break;
      }


   }


   form.num_pmts.value = count;

   var Vnum_years = count / 12;
   form.num_years.value = formatNumberDec(Vnum_years,0,0);

   form.int_paid.value = "$" + formatNumberDec(accum_int,2,1);
   form.prin_paid.value = "$" + formatNumberDec(Vprincipal,2,1);

   if(Vpmt_method == 0) {
      form.summary.value = "If you make the " + formatNumberDec(Vminpayperc * 100,1,1) + "% minimum payments per month, it will take you " + count + " months to pay off your existing balance.  You will pay $" + formatNumberDec(accum_int,2,1) + " in interest while paying off this balance.";
   } else {
      form.summary.value = "If you make $" + formatNumberDec(Vfixed_pmt,2,1) + " payments per month, it will take you " + count + " months to pay off your existing balance.  You will pay $" + formatNumberDec(accum_int,2,1) + " in interest while paying off this balance.";
   }

}
}

function pmtSchedule(form) {

   var pmt_count = stripNum(form.num_pmts.value);

   if(pmt_count == 0) {
      alert("Please compute the top portion before attempting to create the payment schedule.");
      clearResults(form);

   } else {

   var row_count = 0;
   var Vpmt_rows = "";

   var today = new Date();
   var loanMM = today.getMonth() + 1;
   var loanYY = today.getYear();
   if(loanYY < 1900) {
      loanYY += 1900;
   }

   while(row_count < pmt_count) {

      row_count += 1;

      Vpmt_rows += "<tr>\n";
      Vpmt_rows += "<td align='right'><font face='arial'><small>" + loanMM + "/" + loanYY + "</small></font></td>\n";
      Vpmt_rows += "<td align='right'><font face='arial'><small>" + row_count + "</small></font></td>\n";
      Vpmt_rows += "<td align='right'><font face='arial'><small>$" + pmt_amt_arr[row_count] + "</small></font></td>\n";
      Vpmt_rows += "<td align='right'><font face='arial'><small>$" + int_port_arr[row_count] + "</small></font></td>\n";
      Vpmt_rows += "<td align='right'><font face='arial'><small>$" + prin_port_arr[row_count] + "</small></font></td>\n";
      Vpmt_rows += "<td align='right'><font face='arial'><small>$" + prin_arr[row_count] + "</small></font></td>\n";
      Vpmt_rows += "</tr>\n";

      loanMM += 1;
      if(loanMM == 13) {
         loanMM = 1;
         loanYY += 1;
      }

   }


var part1 = ("<html><head><title>Amortization Schedule</title></head>" + "<body bgcolor= '#FFFFFF'><br><br><center><font face='arial'><big><strong>Amortization Schedule</strong></big></font></center>");

   var Vminpayperc = form.minpayperc.options[form.minpayperc.selectedIndex].value;
   var Vpmt_text = "";
   if(form.pmtMethod[0].checked) {
      Vpmt_text = formatNumberDec(Vminpayperc * 100,1,0) + "% of balance";
   } else {
      Vpmt_text = "$" + formatNumberDec(form.fixed_pmt.value,2,1);
   }

var part2 = ("<center><table border=1 cellpadding=2 cellspacing=0 bordercolor='#CCCCCC'><tr><td colspan=6><font face='arial'><small>Principal: $" + formatNumberDec(form.principal.value,2,1) + "<br># of Payments: " + pmt_count + "<br>Interest Rate: " + formatNumberDec(form.interest.value,3,0) + "%<br>Payment: " + Vpmt_text + "</b></small></font></td></tr><tr><td colspan=6><center><font face='arial'><b>Schedule of Payments</b></font><br><font face='arial'><small><small>Please allow for slight rounding differences.</small></small></font></center></td></tr><tr><td align='center'><font face='arial'><small><b>Pmt Date</b></small></font></td><td align='center'><font face='arial'><small><b>Pmt #</b></small></font></td><td align='center'><font face='arial'><small><b>Pmt Amt</b></small></font></td><td align='center'><font face='arial'><small><b>Interest</b></small></font></td><td align='center'><font face='arial'><small><b>Principal</b></small></font></td><td align='center'><font face='arial'><small><b>Balance</b></small></font></td></tr>");

var part3 = ("" + Vpmt_rows + "");

var part4 = ("</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=400,toolbar=yes,menubar=yes,scrollbars=yes");

  reportWin.document.write(schedule);

  reportWin.document.close();

}

}

function clearResults(form) {

   form.num_pmts.value = "";
   form.num_years.value = "";
   form.int_paid.value = "";
   form.prin_paid.value = "";
   form.summary.value = "";
   form.pmt_rows.value = "";


}
