var iWidth = 250;
var iHeight = 250;

var startWidget='<div id="myCalculator" style="border:1px solid #000088;font-family:verdana;line-height:100%;font-size:13px;background-color:#d9dfed;width:' + iWidth + 'px;" ><div id="content" style="padding: 5px;">';

var stopWidget='<div id="calculatorOutput"></div><span style="font-size:80%"><a href="http://www.myamortizationchart.com/calculator/">Put an amortization calculator on your site!</a></div></div>';

document.write(startWidget + buildCalc(100000,8,30) + stopWidget);

function calculateMe()
{
  var principle = document.getElementById('frmPrinciple').value;
  var rate = document.getElementById('frmRate').value;
  var term = document.getElementById('frmTerm').value;
  var output = document.getElementById('calculatorOutput');
  
  if (isNaN(principle) || isNaN(rate) || isNaN(term))
  {
  	alert('Please enter only numbers for the principle, rate, and term');
    return;
  }
  
  if(principle.length<=0 || rate.length<=0 || term.length <= 0)
  {
  	alert('Please enter information for the principle, rate, and term.');
  	return;
  }
  
  if (principle <=0 || rate <= 0 || term <= 0)
  {
  	alert('Please enter only positive numbers for the principle, rate, and term.');
  	return;
  } 
  
  var PF = 12; //payment frequenecy;
  var I = rate/100;
  var i; //to be calculated
  var n = PF * term; //payments
  i = Math.pow((1 + I/PF),(12/PF)) - 1;
  var afSub = Math.pow( (1/(1+i)) , n );
  var AF = (1 - afSub ) / i;
  var payment = principle/AF;  
  
  //assemble the final monthly payment
  var roundedPayment = parseInt(payment);
  var decimal = Math.abs(Math.round(payment*100) - roundedPayment*100);
  var monthlyPayment;
  
  if (decimal < 10)
  {
  	monthlyPayment = "$" + roundedPayment + ".0" + decimal;
  }
  else if (decimal>=10)
  {
  	monthlyPayment = "$" + roundedPayment + "." + decimal;
  }
  else
  {
  	monthlyPayment = "$" + roundedPayment;
  } 
 
  var outputString = '<span style="display:block;text-align:left;">Your monthly payment is <span style="font-weight:bold;background-color: #FCFE6C; border-bottom: 1px solid #B8BF4A;">' + monthlyPayment + '</span>.\
  <br /><br /><a href="http://www.myamortizationchart.com/?p=' + principle + '&r=' + rate + '&t=' + term + '" style="font-size:80%;" target="_BLANK" >Click here to view a complete amortization schedule on myAmortizationChart.com</a><br /><br /></span>'; 

  output.innerHTML = outputString;
}

function resetCalc(p,r,t)
{
  var widget = document.getElementById('myCalculator');
  widget.innerHTML = buildCalc(p,r,t);
}

function buildCalc(p,r,t)
{
var middleWidget='<span style="margin-top: 5px;font-family:trebuchet ms,trebuchet;display:block;font-size:120%;text-align:left;font-weight:bold;color: #000088;">Amortization Calculator</span>\
<span style="display:block;padding:3px 0 7px 0;color: #666;font-size:80%;">Provided by <a style="color:#666;text-decoration:underline;" href="http://www.myamortizationchart.com/"\
target="_BLANK" >myAmortizationChart.com</a></span>\
<span style="font-size:80%;font-style:italic;color:#777;">Determine the monthly payment of a loan.</span>\
<div style="display:block;margin-top: 5px;">\
<table style="font-size:85%;"><tr><td>Principle</td><td align="right">$</td><td><input id="frmPrinciple"  style="width:75px;" type="text" value="' + p + '" /></td></tr>\
<td>Interest Rate</td><td>&nbsp;</td><td><input id="frmRate" type="text" style="width:50px;" value="' + r + '" />%</td></tr>\
<td>Loan Term</td><td>&nbsp;</td><td><input id="frmTerm" type="text" style="width: 50px;" value="' + t + '" />years</td></tr></table>\
<input id="frmSubmit" onClick="calculateMe()" type="submit" style="display:block;margin: 10px auto;padding:2px;cursor:pointer;\
border-left:2px solid #9DB2EA; border-top:2px solid #9DB2EA; border-right:2px solid #162A61; border-bottom:2px solid #162A61;\
background-color: #2647A0; color: white;" value="Calculate" /></div>';

return middleWidget;
}

