function stripNum(num) {
var iPercent
var iDollar
var iSpace
var iComma
var numLength = num.length
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 computeMonthlyPayment(prin, numPmts, intRate) {
var pmtAmt = 0;
if(intRate == 0) {
   pmtAmt = prin / numPmts;
} else {
   if (intRate >= 1.0) {
     intRate = intRate / 100.0;
   }
   intRate /= 12;
   var pow = 1;
   for (var j = 0; j < numPmts; j++)
      pow = pow * (1 + intRate);
   pmtAmt = (prin * pow * intRate) / (pow - 1);
}
return pmtAmt;
}
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;
}

function computeFixedInterestCost(principal, intRate, pmtAmt) { 
   var i = eval(intRate);
   if(i >= 1) {
   i /= 100;
   }
   i /= 12;
   var prin = eval(principal);
   var intPort = 0;
   var accumInt = 0;
   var prinPort = 0;
   var pmtCount = 0;
   var testForLast = 0;

   //CYCLES THROUGH EACH PAYMENT OF GIVEN DEBT
   while(prin > 0) {
      testForLast = (prin * (1 + i));
      if(pmtAmt < testForLast) {
         intPort = prin * i;
         accumInt = eval(accumInt) + eval(intPort);
         prinPort = eval(pmtAmt) - eval(intPort);
         prin = eval(prin) - eval(prinPort);
      } else {
      //DETERMINE FINAL PAYMENT AMOUNT
      intPort = prin * i;
      accumInt = eval(accumInt) + eval(intPort);
      prinPort = prin;
      prin = 0;
      }
      pmtCount = eval(pmtCount) + eval(1);
      if(pmtCount > 1000 || accumInt > 1000000000) {
         prin = 0;
      }
   }
return accumInt;
}

function computeForm(form) {

if(form.principal.value.length == 0) {
   alert("Please enter the amount of the mortgage.");
   form.principal.focus();
} else
if(form.intRate.value == "") {
  alert("Please enter mortgage's annual interest rate.");
  form.intRate.focus();
} else {

var Vprin = stripNum(form.principal.value);
var VintRate = stripNum(form.intRate.value);

var VmoPmt_10 = computeMonthlyPayment(Vprin, 120, VintRate);
form.moPmt_10.value = "$" + formatNumberDec(VmoPmt_10,0,1);

var VmoPmt_15 = computeMonthlyPayment(Vprin, 180, VintRate);
form.moPmt_15.value = "$" + formatNumberDec(VmoPmt_15,0,1);

var VmoPmt_20 = computeMonthlyPayment(Vprin, 240, VintRate);
form.moPmt_20.value = "$" + formatNumberDec(VmoPmt_20,0,1);

var VmoPmt_25 = computeMonthlyPayment(Vprin, 300, VintRate);
form.moPmt_25.value = "$" + formatNumberDec(VmoPmt_25,0,1);

var VmoPmt_30 = computeMonthlyPayment(Vprin, 360, VintRate);
form.moPmt_30.value = "$" + formatNumberDec(VmoPmt_30,0,1);


var VtotPrin_10 = Vprin;
form.totPrin_10.value = "$" + formatNumberDec(VtotPrin_10,0,1);

var VtotPrin_15 = Vprin;
form.totPrin_15.value = "$" + formatNumberDec(VtotPrin_15,0,1);

var VtotPrin_20 = Vprin;
form.totPrin_20.value = "$" + formatNumberDec(VtotPrin_20,0,1);

var VtotPrin_25 = Vprin;
form.totPrin_25.value = "$" + formatNumberDec(VtotPrin_25,0,1);

var VtotPrin_30 = Vprin;
form.totPrin_30.value = "$" + formatNumberDec(VtotPrin_30,0,1);


var VtotInt_10 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_10);
VtotInt_10 = Math.round(VtotInt_10);
form.totInt_10.value = "$" + formatNumberDec(VtotInt_10,0,1);

var VtotInt_15 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_15);
VtotInt_15 = Math.round(VtotInt_15);
form.totInt_15.value = "$" + formatNumberDec(VtotInt_15,0,1);

var VtotInt_20 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_20);
VtotInt_20 = Math.round(VtotInt_20);
form.totInt_20.value = "$" + formatNumberDec(VtotInt_20,0,1);

var VtotInt_25 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_25);
VtotInt_25 = Math.round(VtotInt_25);
form.totInt_25.value = "$" + formatNumberDec(VtotInt_25,0,1);

var VtotInt_30 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_30);
VtotInt_30 = Math.round(VtotInt_30);
form.totInt_30.value = "$" + formatNumberDec(VtotInt_30,0,1);


var VtotPmts_10 = eval(VtotPrin_10) + eval(VtotInt_10);
form.totPmts_10.value = "$" + formatNumberDec(VtotPmts_10,0,1);

var VtotPmts_15 = eval(VtotPrin_15) + eval(VtotInt_15);
form.totPmts_15.value = "$" + formatNumberDec(VtotPmts_15,0,1);

var VtotPmts_20 = eval(VtotPrin_20) + eval(VtotInt_20);
form.totPmts_20.value = "$" + formatNumberDec(VtotPmts_20,0,1);

var VtotPmts_25 = eval(VtotPrin_25) + eval(VtotInt_25);
form.totPmts_25.value = "$" + formatNumberDec(VtotPmts_25,0,1);

var VtotPmts_30 = eval(VtotPrin_30) + eval(VtotInt_30);
form.totPmts_30.value = "$" + formatNumberDec(VtotPmts_30,0,1);

}
}

function createReport(form) {

if(form.principal.value.length == 0) {
   alert("Please enter the amount of the mortgage.");
   form.principal.focus();
} else
if(form.intRate.value == "") {
  alert("Please enter mortgage's annual interest rate.");
  form.intRate.focus();
} else {

computeForm(form);

var Vprin = stripNum(form.principal.value);
var VintRate = stripNum(form.intRate.value);

var termRows = "";

termRows = ("" + termRows + "<tr><td><font face='arial'><small>Monthly payment:</small></font></td><td align=right><font face='arial'><small>" + form.moPmt_10.value + "</small></font></td><td align=right><font face='arial'><small>" + form.moPmt_15.value + "</small></font></td><td align=right><font face='arial'><small>" + form.moPmt_20.value + "</small></font></td><td align=right><font face='arial'><small>" + form.moPmt_25.value + "</small></font></td><td align=right><font face='arial'><small>" + form.moPmt_30.value + "</small></font></td></tr>");

termRows = ("" + termRows + "<tr><td><font face='arial'><small>Principal payments:</small></font></td><td align=right><font face='arial'><small>" + form.totPrin_10.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPrin_15.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPrin_20.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPrin_25.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPrin_30.value + "</small></font></td></tr>");

termRows = ("" + termRows + "<tr><td><font face='arial'><small>Interest payments:</small></font></td><td align=right><font face='arial'><small>" + form.totInt_10.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totInt_15.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totInt_20.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totInt_25.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totInt_30.value + "</small></font></td></tr>");

termRows = ("" + termRows + "<tr><td><font face='arial'><small>Total payments:</small></font></td><td align=right><font face='arial'><small>" + form.totPmts_10.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPmts_15.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPmts_20.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPmts_25.value + "</small></font></td><td align=right><font face='arial'><small>" + form.totPmts_30.value + "</small></font></td></tr>");

var part1 = ("<html><head><title>Mortgage Term Comparison</title></head>" + "<body bgcolor= '#FFFFFF'><center><font face='arial'><big><strong>Mortgage Term Comparison</strong></big></font></center>");

var part2 = ("<center><table border=1 cellpadding=2 cellspacing=0><tr><td colspan=6><font face='arial'><small><b>Principal: $" + formatNumberDec(Vprin,0,1) + "<br>Interest Rate: " + formatNumberDec(VintRate,3,1) + "%</b></small></font></td></tr><tr bgcolor='silver'><td align='center'>&nbsp;</td><td align='center'><font face='arial'><small><b>10 Years</b></small></font></td><td align='center'><font face='arial'><small><b>15 Years</b></small></font></td><td align='center'><font face='arial'><small><b>20 Years</b></small></font></td><td align='center'><font face='arial'><small><b>25 Years</b></small></font></td><td align='center'><font face='arial'><small><b>30 Years</b></small></font></td></tr>");

var part3 = ("" + termRows + "");

var part4 = ("</table><br><font face='arial'>The information contained herein is intended for general information purposes only. These calculations are only estimates and should not be used to determine actual loan costs.</font><form method='post'><input type='button' value='Close Window' onClick='window.close()'></form></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();

   }

}