var main_url="http://www.mortgagesum.com/mortgagewidget/";

var body = "";

function chh(){
	var yy=document.cont1.firstyear.value;
	var aa=document.cont1.amt.value;
	var bb=document.cont1.interest.value;
	var cc=document.cont1.yrs.value;
	if(aa == "") { aa="0"; }
	if(bb == "") { bb="0"; }
	if(cc == "") { cc="0"; }
	if(yy == "") { yy="0"; }
	if(aa == "0") {  var show =  "<br>Please insert the values for the Principal field"; document.getElementById("priceid1").innerHTML=show; }
	if(bb == "0") {  var show =  "<br>Please insert the values for the Interst field"; document.getElementById("priceid1").innerHTML=show;   }
	if(cc == "0") {  var show =  "<br>Please select the value in Number of years field "; document.getElementById("priceid1").innerHTML=show;  }
	if(yy == "0") {  var show =  "<br>Please select the value in First Payment Yr field "; document.getElementById("priceid1").innerHTML=show;  }

	if(aa != "0" && bb != "0" && cc != "0" && yy != "0"){
		
		
		GenerateValue(aa,bb,cc,yy);
		
		
	}
}

function generatePage(){
	var tmp1, tmp2;
	tmp1 = document.cont1.amt.value;
	tmp2 = document.cont1.interest.value;
	tmp3 = document.cont1.yrs.value;
	   

    if (confirm("An Amortization Table calculates the periodic payment breakdown for each specific category listed.")) {
		body = ("<HEAD><TITLE>Amortization Table</TITLE></HEAD>");	  
		body = (body + "<link href='http://www.mortgagesum.com/style.css' rel='stylesheet' type='text/css'>");
		body = (body +"<BODY style='background-color: #ffffff; background-image:none; margin:10px'>");
		body = (body +"<h2>Amortization Table</h2>");
		body = (body +"<span class='text_content'>The following table is based on the information entered in the calculator form.<b<br><br>");
		body = (body +"Mortgage Amount: " +formatCurrency(tmp1));
		body = (body +"<BR>Interest Rate: " + tmp2 + " %");
		body = (body +"<BR>Mortgage Length: " + tmp3 + " Years ");
		body = (body +"<BR><br><table border='0' cellpadding='0' cellspacing='1' width='80%' class='calcShadeBase'>");
		body = (body +"<TR><TD ALIGN=CENTER class='text_content'><B>Year</B></TD><TD ALIGN=RIGHT class='text_content'><B>Interest&nbsp;</B></TD><TD ALIGN=RIGHT class='text_content'><B>Principal&nbsp;</B></TD><TD ALIGN=RIGHT class='text_content'><B>Balance&nbsp;</B></TD></TR>\n");
		createtable();
		body = (body +"</TABLE></CENTER><br/><a href='http://www.mortgagesum.com'>Amortization Calculator</a>");      
		msgWindow=window.open("","displayWindow","toolbar=no,class=text_content,width=500,height=400,directories=no,status=no,scrollbars=yes,resize=no,menubar=no")
		msgWindow.document.write(body);
		msgWindow.document.close();
		return(true);
	}
	
	return(false);
}

function createtable()
{
	var tmp3
	tmp3 = 12;
	
	tmp1 = document.cont1.amt.value;
	tmp1 = tmp1.replace(/,/, "");
	tmp1 = parseFloat(tmp1);
	var tmp2A = document.cont1.interest.value;
	tmp2 = tmp2A.replace(/,/, "");
	tmp2 = parseFloat(tmp2);
	tmp4 = document.cont1.yrs.value;
	tmp4 = parseFloat(tmp4);
	tmp5 = document.cont1.firstyear.value;
	tmp5 = parseFloat(tmp5);
 
	var currInterest = 0
	var currPrin = 0
	prevBalance = tmp1;
	InterestRate = ( tmp2 / 100) / tmp3;
	Payment = (tmp1*((tmp2/(tmp3*100))/(1-(Math.pow(1+(tmp2/(tmp3*100)),((tmp4*tmp3)*-1))))));
	MonthlyPayment = Payment;   
	currStart = tmp5;
  
   for(i=1;i<=tmp4;i++) {
      for(j=1;j<=tmp3;j++) {
         periodInterest = prevBalance * InterestRate;
         periodPrin = MonthlyPayment - periodInterest;
         currBalance = prevBalance - periodPrin;
         currInterest += periodInterest;
         currPrin += periodPrin;
         prevBalance = currBalance;
      }
      if( currBalance <= 0 )   currBalance = 0;
      body = (body +"<TR><TD ALIGN=CENTER class='text_content'>"+ currStart +"</TD><TD ALIGN=RIGHT class='text_content'>"+ formatCurrency(currInterest) +"&nbsp;</TD><TD ALIGN=RIGHT class='text_content'>"+ formatCurrency(currPrin) +"&nbsp;</TD><TD ALIGN=RIGHT class='text_content'>"+ formatCurrency(currBalance)+"&nbsp;</TD></TR>");
      currInterest = 0
      currPrin = 0
      currStart = parseInt(currStart)
      currStart += 1
      if(currBalance<=0) {
         return(true)
      }  
   }
   return (true)
}


function GenerateValue(tmp1,tmp2,tmp3,tmp5) 
{  

	var str=tmp1;
	var tmp1=(str.replace(/,/, ""));

	var tmp4;

	tmp1 = parseFloat(tmp1);
		
	if (isNaN(tmp1)) tmp1=0;
  
	tmp2 = parseFloat(tmp2);
		
	if (isNaN(tmp2)) tmp2=0;
  
	tmp3 = parseFloat(tmp3);
		
	if (isNaN(tmp3)) tmp3=0;
	
	 
	tmp4 = 12;

	Payment = (tmp1*((tmp2/(tmp4*100))/(1-(Math.pow(1+(tmp2/(tmp4*100)),((tmp3*tmp4)*-1))))));
	Interest = ((Payment*(tmp3*tmp4))-tmp1);
	
	Payment = formatCurrency(Payment);
	Interest = formatCurrency(Interest);
	
	var show ="Your Monthly Payment for<span class='mggreen'> "+tmp3+"</span> Years <br> at an Interest Rate of<span class='mggreen'> "+tmp2+"% </span><br>for a Loan Amount of $<span class='mggreen'>"+str+"</span> is $<span class='mggreen'>"+Payment+"</span> a Month and the total interest would be $<span class='mggreen'>"+Interest+"</span>.<br/><br/>";
		document.getElementById("priceid1").innerHTML=show;
	
	return(true);
}



function writetext(){
	document.getElementById('morgtage_calc').innerHTML = '<form method="post" name="cont1" action="#"><table width="244" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="' + main_url + 'images/header-amortization.gif" width="244" height="50"></td></tr><tr><td height="155"  align="center" valign="top" background="' + main_url + 'images/mortgage_05.gif"><table width="93%" cellpadding="2" cellspacing="1"><tr><td height="35" colspan="2" align="left" valign="top" class="mgtext"><strong>Calculate your monthly loan payments.</strong></td><tr><td width="56%" height="22" align="left" class="mgtext">First Payment Yr : </td><td width="44%" align="left"><select name="firstyear" class="mgtextbox" onSelect="javascript:chh();"><option value="2007">2007</option><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option><option value="2011">2011</option></select></td><tr><td width="56%" height="22" align="left" class="mgtext">Mortgage Amount : </td><td width="44%" align="left"><input name="amt" type="text" class="mgtextbox" onChange="javascript:chh();" value="300,000" size="10"></td><tr><td height="25" align="left" class="mgtext" >Interest : </td><td align="left" class="mgtext"><input name="interest" type="text" class="mgtextbox" size="3" onChange="javascript:chh();" value="6.00"> %</td></tr><tr><td height="25" align="left" class="mgtext" >Number of Years :</td><td align="left"  class="mgtext"><select name="yrs" class="mgtextbox" onSelect="javascript:chh();"><option value="30">30</option><option value="25" selected>25</option><option value="15">15</option><option value="10">10</option><option value="7">7</option><option value="5">5</option><option value="4">4</option><option value="3"></option></select> Yrs</td></tr><tr><td colspan="2" align="right" class="mgcalcShade2"><img src="' + main_url + 'images/button.jpg" onClick="javascript:chh();">&nbsp;&nbsp;&nbsp;<img src="' + main_url + 'images/button2.jpg" onClick="javascript:generatePage();"></td></tr></table></td></tr><tr><td ><img src="' + main_url + 'images/top.jpg" width="244" height="10"></td></tr><tr><td align="left" valign="top" background="' + main_url + 'images/middle.jpg" style="padding-left:12px; padding-right:12px;" height="70"> <span id="priceid1" class="mgtext"></span></td></tr><tr><td height="45" align="center" valign="top" background="' + main_url + 'images/mortgage-widget_06.jpg"><table width="230" border="0" cellspacing="0" cellpadding="0"><tr><td height="39" align="right" valign="bottom" background="' + main_url + 'images/image.gif"><a target="_blank" href="http://www.mortgagesum.com"><u>Amortization Calculator</u></a></td></tr></table></td></tr><tr><td><img src="' + main_url + 'images/mortgage_08.jpg" width="243" height="12"></td></tr></table></form>';
}

function loadfunctions(){
	writetext();
	chh();
}

function formatCurrency(num) 
{	
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' +  num + '.' + cents);
}


window.onLoad=loadfunctions();