document.getElementById("calc6").innerHTML = '<table width="450" border="0" cellspacing="0"><tr align="center">'+
'<td colspan="2" bgcolor="#FCF5DA"><span class="style2"><strong>Prepayment vs. Investment </strong></span></td></tr>'+
'<tr><td colspan="2" bgcolor="#FCF5DA"><span class="style2">Use this calculator to find out whether it is wiser to prepay your mortgage or invest that money with extra money you have each month.  Taxes on investments, deductibility of mortgage interest, and rate of inflation are not taken into account here.</span></td></tr>'+
'<tr class="style2"><td>Current Mortgage Balance:</td><td>$<INPUT class="textbox" TYPE="text" ID="amount666" SIZE=10></td></tr>'+
'<TR class="style2"><TD>Current monthly payment (principal + interest):</TD><TD>$<INPUT class="textbox" TYPE="text" ID="payment666" SIZE=10></TD></TR>'+
'<TR class="style2"><TD>Mortgage interest rate (APR):</TD><TD><INPUT class="textbox" TYPE="text" ID="apr666" SIZE=4>%</TD></TR>'+
'<TR class="style2"><TD>Potential investment return rate (APY):<br/><span class="style1">(1 - 4% for savings acct., 4 - 8% stocks, etc.)</span></TD><TD><INPUT class="textbox" TYPE="text" ID="apy666" SIZE=4>%</TD></TR>'+
'<TR class="style2"><TD>Amount of monthly money to either invest or prepay:</TD><TD>$<INPUT class="textbox" TYPE="text" ID="extra666" SIZE=10></TD></TR>'+
'<TR class="style2"><TD>Length of loan:</TD><TD><INPUT class="textbox" TYPE="text" ID="term666" SIZE=4> years</TD></TR>'+
'<TR><TD align="center" COLSPAN=2><button onClick="calculate666()">Calculate</button></TD></TR>'+
'<TR class="style2"><TD>Total interest paid if you choose prepayment:</TD><TD>$<span ID="intprepay666"></span></TD></TR>'+
'<TR class="style2"><TD>Total interest paid if you choose investment:</TD><TD>$<span ID="intinvest666"></span></TD></TR>'+
'<TR class="style2"><TD>Balance of investment at loan maturity:</TD><TD>$<span ID="investbalance666"></span></TD></TR>'+
'<TR><TD class="style2" COLSPAN=2><span id="summary666"></span></TD></TR>'+
'<tr><td align="center" bgcolor="#FCF5DA" colspan="2" class="style2"><a href="http://www.mortgagemagic.info/">Mortgage Marketing</a> and <a href="http://www.freemortgagenewsletters.com">Free Mortgage Newsletters available now</a>.</td></tr><tr><td bgcolor="#FCF5DA" colspan="2" class="style1">This information and&nbsp;calculator&nbsp;is a self-help tool for your independent use and&nbsp;is not intended to provide investment advice. We can not and do not guarantee their applicability or accuracy in regards to your&nbsp;personal situation.&nbsp;Please seek the help of a qualified mortgage specialist or&nbsp;other qualified professional&nbsp;regarding all personal finance issues.</td></tr></table>';


function calculate666(){

var balance = parseFloat(document.getElementById("amount666").value);
var balance2 = parseFloat(document.getElementById("amount666").value);
var p = parseFloat(document.getElementById("apr666").value);
var extra = parseFloat(document.getElementById("extra666").value);
var apy = parseFloat(document.getElementById("apy666").value);
var payment = parseFloat(document.getElementById("payment666").value);
//var tax = parseFloat(document.getElementById("tax").value);
var term = parseFloat(document.getElementById("term666").value);

term *= 12;
//apy = apy - (apy*(tax/100));

var n=term;
var interest = 0.0;
var principal=0.0;
var totalinterest=0.0;
var interest2 = 0.0;
var principal2=0.0;
var totalinterest2=0.0;
var investbal = extra*-1;;
var i=1;
var skip1 = false;
var skip2 = false;
var mos1 = "ERROR: Balance, payment, APR, term mismatch";
var mos2 = "ERROR: Balance, payment, APR, term mismatch";

  for (i=0;i<n;i++) {

 if (skip1 != true && skip2 != true) {
  		  investbal = investbal + (investbal*(apy/1200));
		  investbal += extra;
}

  if (skip1 != true) {
     interest = balance*p/1200;
     totalinterest += interest;
     principal = payment-interest;
     balance -= principal;
       if (0>balance) {
                skip1 = true;
                mos1 = i;
	            var balance2froze = balance2+principal;
                }
   }
  if (skip2 != true) {
     interest2 = balance2*p/1200;
     totalinterest2 += interest2;
     principal2 = payment-interest2;
     balance2 -= principal2;
     balance2 -= extra; //added
        if (0>balance2) {
                skip2 = true;
				mos2 = i;
                var balancefroze = balance+principal2;
				if (balancefroze < 0 ) balancefroze = 0;
                }
   }
  }

document.getElementById("intprepay666").innerHTML = addCommas666(Math.round(parseFloat(totalinterest2),2));
document.getElementById("intinvest666").innerHTML = addCommas666(Math.round(parseFloat(totalinterest),2));
document.getElementById("investbalance666").innerHTML = addCommas666(Math.round(parseFloat(investbal),2));
//document.getElementById("timeprepay").innerHTML = mos2;
//document.getElementById("timeinvest").innerHTML = mos1;

var diff = Math.round(parseFloat(investbal-balancefroze),2);
//document.getElementById("diff").innerHTML = addCommas(diff);

var endsum = "<strong>This comparison ends after " + Math.round((mos2/12)*100)/100 + " years, which is the time it takes to pay off the loan by prepaying the extra $" + addCommas666(extra) + ". At the end of that time, the investment balance of $" + addCommas666(Math.round(parseFloat(investbal),2)) + " is $" + addCommas666(Math.abs(diff));

if (diff > 0 ) {
  document.getElementById("summary666").innerHTML = endsum + " more than the loan balance at that point.  In your case it is wiser to invest the extra money you have each month rather than prepaying the loan.</strong>";
  }
else if (diff < 0 ){
 document.getElementById("summary666").innerHTML = endsum + "  less than the loan balance at that point.  In your case it is wiser to prepay the loan with the extra money you have each month rather than investing it.</strong>";
}
else {
document.getElementById("summary666").innerHTML = endsum + "  the same as the loan balance at that point.  It doesn't matter whether you invest or prepay because you won't gain or lose any money either way.</strong>";
}

}

function addCommas666(nStr)
{
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
}

