mtg_calculate = function() {
	var income = document.getElementById("mtg_income").value;
	if ( income.length == 0 )
	{
		alert("Please enter a yearly income");
		return;
	}
	income = fixNumber(income);
	income = parseFloat(income);
	if ( isNaN(income) )
	{
		alert("Please enter the yearly income as a number");
		return;
	}
	if ( income < 0 )
	{
		alert("Please enter a yearly income >= 0");
		return;
	}
	
	var rate = document.getElementById("mtg_rate").value;
	if ( rate.length == 0 )
	{
		alert("Please enter an interest rate");
		return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		alert("Please enter the interest rate as a number");
		return;
	}
	if ( rate <= 0 || rate >= 20 )
	{
		alert("Please enter a interest rate greater than 0 and less than 20");
		return;
	}

	var years = parseInt( document.getElementById("mtg_duration").value);

	var pforh = document.getElementById("mtg_pforh").value;
	if ( pforh.length == 0 )
	{
		alert("Please enter the percentage of your pre-tax income that you are able to spend on housing");
		return;
	}
	pforh = fixNumber(pforh);
	pforh = parseFloat(pforh);
	if ( isNaN(pforh) )
	{
		alert("Please enter the percentage of income for housing as a number");
		return;
	}
	if ( pforh <= 0 || pforh >= 100 )
	{
		alert("Please enter the percentage of income for housing as a number greater than 0 and less than 100");
		return;
	}

	var downpayment = document.getElementById("mtg_down").value;
	if ( downpayment.length == 0 )
	{
		alert("Please enter a down payment");
		return;
	}
	downpayment = fixNumber(downpayment);
	downpayment = parseFloat(downpayment);
	if ( isNaN(downpayment) )
	{
		alert("Please enter the down payment as a number");
		return;
	}
	if ( downpayment < 0 )
	{
		alert("Please enter a down payment >= 0");
		return;
	}

	var mrate = rate / 1200;
	var maxpayment = ( income / 12 ) * ( pforh / 100 );

	var loanamount;
	if ( years == -1 )
	{
		loanamount = maxpayment / mrate;
	}
	else
	{
		var factor = Math.pow(1 + mrate, years * 12 );
		loanamount = maxpayment / ( mrate / (1 - 1/factor) );
	}

	var houseprice = loanamount + downpayment;

	var x = document.getElementById("mtg_dt_results");

	var results = "With the parameters you selected, the most expensive house you can afford costs <b>" + formatDollars(houseprice) + "</b>.";

	results += "<br><br>Your monthly payment would be <b>" + formatDollars(maxpayment) + "</b>.";

	if ( pforh > 28 )
		results += "<br><br><i>* 28% is the standard banking figure for income devoted to housing.  Depending on your debt, income, and other factors, it may not be prudent to exceed this percentage.</i>";
	
	if ( years == -1 )
		results += "<br><br><i>* With an interest only mortgage, you will never own your house outright.</i>";

	x.innerHTML = results;
	x.style.display = "block";
}// modified css: http://www.dollartimes.com/calculators/on-your-site/calc-css.php
addJavascript = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',fileName);
	th.appendChild(s);
}
addStylesheet = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('link');
	s.setAttribute('type','text/css');
	s.setAttribute('title', 'dt-calc-style');
	s.setAttribute('rel','stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}


addJavascript('http://www.dollartimes.com/script/calcutil.js');
addStylesheet('http://www.dollartimes.com/calculators/on-your-site/calc-css.php');

var out = '\
<div id="mtg_dt_calculator" class="dt_calculator">\n\
	<h2>Mortgage Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">This page will calculate the most expensive house you can afford based on your income and other factors.</p>\n\
	<p class="instructions"><strong>*</strong> The percentage of your pre-tax income you are able to spend on housing. 28% is the standard amount.</p>\n\
	\n\
	<div class="a">Yearly Income (before tax)</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="mtg_income" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Down Payment</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="mtg_down" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Interest Rate</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="mtg_rate" type="text" value="6.25" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Term of Loan</div>\n\
	<div class="b"></div>\n\
	<div class="c">\n\
	<select id="mtg_duration">          <option value="5">5 years          <option value="10">10 years          <option value="15">15 years          <option value="20">20 years          <option value="25">25 years          <option selected value="30">30 years          <option value="40">40 years          <option value="-1">Interest Only</option></select>\n\
	</div>\n\
	\n\
	<div class="a" title="The percentage of your pre-tax income you are able to spend on housing.">Percent of Income for Housing <strong>*</strong></div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="mtg_pforh" type="text" title="The percentage of your pre-tax income you are able to spend on housing." value="28"/></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="clear"></div>\n\
	<div class="d"><button onclick="mtg_calculate()">Calculate</button></div>\n\
	<div id="mtg_dt_results" class="dt_results">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/mortgage.js";

var scriptEls = document.getElementsByTagName('script');
var scriptEl=false;


for(i=0; i<scriptEls.length; i++)
{
	var t = scriptEls[i];
	var src = (t.getAttribute('src'));
	if(src == calcUrl) 
	{
		scriptEl = t;
		break;
	}
}
if(!scriptEl)
{
	document.write('\n<p>Failed loading calculator. The code might have been updated. Please get the correct code at <a href="http://www.dollartimes.com/calculators/on-your-site/'+calcUrl+'">http://dollartimes.com/calculators/on-your-site/'+calcUrl+'</a></p>');
}
else
{
	var par = scriptEl.parentNode;  
	var link = par.getElementsByTagName('a')[0];
	var el = document.createElement('div');
	el.innerHTML = out;
	par.appendChild(el);

	if (link.toString().match("/calculators/")) {

		link.style.fontSize="80%";
		var calcDiv = document.getElementById('mtg_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
