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 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 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 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 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><b>Monthly payment</b></td><td align=right><small>" + form.moPmt_10.value + "</small></font></td><td align=right><small>" + form.moPmt_15.value + "</small></font></td><td align=right><small>" + form.moPmt_20.value + "</small></font></td><td align=right><small>" + form.moPmt_25.value + "</small></font></td><td align=right><small>" + form.moPmt_30.value + "</small></font></td></tr>");
termRows = ("" + termRows + "<tr><td><b>Principal</b></td><td align=right><small>" + form.totPrin_10.value + "</small></font></td><td align=right><small>" + form.totPrin_15.value + "</small></font></td><td align=right><small>" + form.totPrin_20.value + "</small></font></td><td align=right><small>" + form.totPrin_25.value + "</small></font></td><td align=right><small>" + form.totPrin_30.value + "</small></font></td></tr>");
termRows = ("" + termRows + "<tr><td><b>Interest</b></td><td align=right><small>" + form.totInt_10.value + "</small></font></td><td align=right><small>" + form.totInt_15.value + "</small></font></td><td align=right><small>" + form.totInt_20.value + "</small></font></td><td align=right><small>" + form.totInt_25.value + "</small></font></td><td align=right><small>" + form.totInt_30.value + "</small></font></td></tr>");
termRows = ("" + termRows + "<tr><td><b>Total Payments</b></td><td align=right><small>" + form.totPmts_10.value + "</small></font></td><td align=right><small>" + form.totPmts_15.value + "</small></font></td><td align=right><small>" + form.totPmts_20.value + "</small></font></td><td align=right><small>" + form.totPmts_25.value + "</small></font></td><td align=right><small>" + form.totPmts_30.value + "</small></font></td></tr>");
var part1 = ("<head><title>Mortgage Term Comparison</title></head>" + "<body>");
var part2 = ("<center><table border=0 cellpadding=2 cellspacing=0 width='100%'><tr><td colspan=6><center><font color='#C42E4B'><b>Mortgage Term Comparison</b></font></center><hr></td></tr><tr><td colspan=6><b>Principal:</b> $" + formatNumberDec(Vprin,0,1) + "<br><b>Interest Rate:</b> " + formatNumberDec(VintRate,3,1) + "%<br><hr></td></tr><tr><td align='center'><b>Description</b></td><td align='center'><b>10 Yrs</b></td><td align='center'><b>15 Yrs</b></td><td align='center'><b>20 Yrs</b></td><td align='center'><b>25 Yrs</b></td><td align='center'><b>30 Yrs</b></td></tr>");
var part3 = ("" + termRows + "");
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=200,toolbar=yes,menubar=yes,scrollbars=yes");
  reportWin.document.write(schedule);
  reportWin.document.close();
   }
}