dt_mtg_calculate = function() 
{
	var income, downpayment, rate, years, pforh;
	try
	{
		income = DT_F.getFormFloat( "dt_mtg_income", "Please enter a yearly income", "Please enter the yearly income as a number" );
		if ( income < 0 ) {
			throw("Please enter a yearly income >= 0");
		}
		rate = DT_F.getFormFloat( "dt_mtg_rate", "Please enter an interest rate", "Please enter the interest rate as a number" );
		if ( rate <= 0 || rate >= 30 ) {
			throw("Please enter a interest rate greater than 0 and less than 30");
		}
		downpayment = DT_F.getFormFloat( "dt_mtg_down", "Please enter a down payment", "Please enter the down payment as a number" );	
		if ( downpayment < 0 ) {
			throw("Please enter a down payment >= 0");
		}
		years = parseInt( document.getElementById("dt_mtg_duration").value, 10 );
		pforh = DT_F.getFormFloat( "dt_mtg_pforh", "Please enter the percentage of your pre-tax income that you are able to spend on housing", "Please enter the percentage of income for housing as a number"  );
		if ( pforh <= 0 ) {
			throw("Please enter a percentage of income for housing greater than 0.");
		}
		if ( pforh >= 100 ) {
			throw("Please enter a percentage of income for housing less than 100.");
		}
	}
	catch ( err )
	{
		alert(err);
		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("dt_mtg_results");

	var results = "The most expensive house you can afford costs <b>" + DT_F.formatDollars(houseprice) + "</b>.";

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

	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";
};
