function calculate_payment() {
	var total  = 0;
	var rate   = 0;
	var months = 0;
	var down   = 0;
	var price  = document.getElementById('home_price').value;
	var down1  = document.getElementById('down_payment').value;
	var down2  = document.getElementById('down_percent').value;
	var apr    = document.getElementById('loan_apr').value;
	var term   = document.getElementById('loan_term').value;
	price  = parseFloat(price.replace(/[^0-9\.]/g,''));
	price  = parseFloat(price.toFixed(2));
	down1  = parseFloat(down1.replace(/[^0-9\.]/g,''));
	down1  = parseFloat(down1.toFixed(2));
	down2  = parseFloat(down2.replace(/[^0-9\.]/g,''));
	down2  = parseFloat(down2.toFixed(2));
	apr    = parseFloat(apr.replace(/[^0-9\.]/g,''));
	apr    = parseFloat(apr.toFixed(2));
	term   = parseInt(term);
	months = term * 12;
	rate   = apr / 1200;
	if (isNaN(price)) {
		alert('Please enter the home price.');
		return 0;
	}
	else if ((isNaN(down1) && isNaN(down2)) || (down1 && down2)) {
		alert('Please enter either a dollar amount or percentage of down payment.');
		return 0;
	}
	else if (isNaN(apr)) {
		alert('Please enter the interest rate.');
		return 0;
	}
	down2 = price * (down2 / 100);
	down  = down1;
	if (!down) {
		down = down2;
	}
	total = (rate + (rate / (Math.pow(rate + 1, months) - 1))) * (price - down);
	document.getElementById('result').innerHTML = format_money(total);
	document.getElementById('result_label').style.visibility = "visible";
	document.getElementById('start_quote').style.visibility = "visible";
}

function calculate_payment2() {
	var total  = 0;
	var rate   = 0;
	var months = 0;
	var amount = document.getElementById('loan_amount').value;
	var apr    = document.getElementById('loan_apr').value;
	var term   = document.getElementById('loan_term').value;
	amount = parseFloat(amount.replace(/[^0-9\.]/g,''));
	amount = parseFloat(amount.toFixed(2));
	apr    = parseFloat(apr.replace(/[^0-9\.]/g,''));
	apr    = parseFloat(apr.toFixed(2));
	term   = parseInt(term);
	months = term * 12;
	rate   = apr / 1200;
	if (isNaN(amount)) {
		alert('Please enter the loan amount.');
		return 0;
	}
	else if (isNaN(apr)) {
		alert('Please enter the interest rate.');
		return 0;
	}
	total = (rate + (rate / (Math.pow(rate + 1, months) - 1))) * amount;
	document.getElementById('result').innerHTML = format_money(total);
	document.getElementById('result_label').style.visibility = "visible";
	document.getElementById('start_quote').style.visibility = "visible";
}

function calculate_payment3(form) {
	var loan_life, periods, periodic_income, periodic_debt, intrate, down, taxins, min_total_pmt, max_total_pmt, min_pmt, max_pmt, temp, payment, min_loan, max_loan, min_price, max_price, min_total, max_total;
	periodic_income = form.income.value;
	periodic_income = parseFloat(periodic_income.replace(/[^0-9\.]/g,''));
	periodic_income = parseFloat(periodic_income.toFixed(2));
	down = form.down.value;
	down = parseFloat(down.replace(/[^0-9\.]/g,''));
	down = parseFloat(down.toFixed(2));
	periodic_debt = form.debt.value;
	periodic_debt = parseFloat(periodic_debt.replace(/[^0-9\.]/g,''));
	periodic_debt = parseFloat(periodic_debt.toFixed(2));
	intrate = form.intrate.value;
	intrate = parseFloat(intrate.replace(/[^0-9\.]/g,''));
	intrate = parseFloat(intrate.toFixed(2));
	loan_life = form.term.value;
	if (isNaN(periodic_income)) {
		alert("Please enter the income.");
		form.income.focus();
		return 0;
	}
	else if (isNaN(intrate)) {
		alert("Please enter the interest rate.");
		form.intrate.focus();
		return 0;
	}
	if (isNaN(down)) {
		down = 0;
	}
	periodic_income = periodic_income / 12;
	min_total_pmt = periodic_income * 0.28;
	temp = (periodic_income * 0.36) - periodic_debt;
	if (temp < min_total_pmt) {
		min_total_pmt = temp;
	}
	max_total_pmt = periodic_income * 0.33;
	temp = (periodic_income * 0.38) - periodic_debt;
	if (temp < max_total_pmt) {
		max_total_pmt = temp;
	}
	periods = loan_life * 12;
	taxins = get_taxes_insurance(form);
	min_pmt = min_total_pmt - taxins;
	min_loan = get_mortgage_loan(intrate, min_pmt, periods);
	min_price = min_loan + down;
	max_pmt = max_total_pmt - taxins;
	max_loan = get_mortgage_loan(intrate, max_pmt, periods);
	max_price = max_loan + down;
	document.getElementById('price_min').innerHTML       = add_commas_decimal(min_price);
	document.getElementById('loan_amt_min').innerHTML    = add_commas_decimal(min_loan);
	document.getElementById('pmt_min').innerHTML         = add_commas_decimal(min_pmt);
	document.getElementById('taxins_pmt_min').innerHTML  = add_commas_decimal(taxins);
	document.getElementById('total_pmt_min').innerHTML   = add_commas_decimal(min_total_pmt);
	document.getElementById('price_max').innerHTML       = add_commas_decimal(max_price);
	document.getElementById('loan_amt_max').innerHTML    = add_commas_decimal(max_loan);
	document.getElementById('pmt_max').innerHTML         = add_commas_decimal(max_pmt);
	document.getElementById('taxins_pmt_max').innerHTML  = add_commas_decimal(taxins);
	document.getElementById('total_pmt_max').innerHTML   = add_commas_decimal(max_total_pmt);
	document.getElementById('result').style.display      = "inline";
	document.getElementById('start_quote').style.display = "inline";
}

function get_taxes_insurance(form) {
	var yearly_taxes, yearly_ins, periodic_txins;
	yearly_taxes = form.taxes.value;
	yearly_taxes = parseFloat(yearly_taxes.replace(/[^0-9\.]/g,''));
	yearly_taxes = parseFloat(yearly_taxes.toFixed(2));
	yearly_ins = form.insurance.value;
	yearly_ins = parseFloat(yearly_ins.replace(/[^0-9\.]/g,''));
	yearly_ins = parseFloat(yearly_ins.toFixed(2));
	if (isNaN(yearly_taxes)) {
		yearly_taxes = 0;
	}
	if (isNaN(yearly_ins)) {
		yearly_ins = 0;
	}
	periodic_txins = (yearly_taxes + yearly_ins) / 12;
	return periodic_txins;
}

function get_mortgage_loan(intrate, payment, periods) {
	var period_intrate, disc_factor, principal;
	period_intrate = intrate / (12 * 100);
	disc_factor = (Math.pow(1 + period_intrate, periods) - 1) / (period_intrate *  Math.pow(1 + period_intrate, periods));
	principal = payment * disc_factor;
	return principal;
}

function add_commas(number) {
	var T = '', S = String(number), L = S.length-1, C, j;
	for (j = 0; j <= L; j++) {
		T += C = S.charAt(j);
		if ((j < L) && ((L - j) % 3 == 0) && (C != '-')) {
			T += ',';
		}
	}
	return T;
}

function add_commas_decimal(number) {
	var number, whole, decimal, dec_str, formatted_number;
	if (isNaN(number)) {
		return number;
	}
	whole = Math.floor(number);
	decimal = number - whole;
	if (decimal != 0) {
		decimal = Math.round(decimal * 100) / 100;
		dec_str = String(decimal);
		dec_str = dec_str.substr(dec_str.indexOf(".") + 1);
		if (dec_str.length == 1) {
			dec_str = dec_str + "0";
		}
		dec_str = "." + dec_str;
	}
	else {
		dec_str = "";
	}
	formatted_number = "$" + add_commas(whole) + dec_str;
	return formatted_number;
}

function strip_number(field) {
	var original_number = field.value;
	var stripped_number = "";
	var parsed_number;
	for (var i = 0; i < original_number.length; i++) {
		var digit = original_number.charAt(i);
		if (digit == '.' || !(digit < "0" || digit > "9")) {
			stripped_number = stripped_number + digit;
		}
	}
	if (stripped_number) {
		parsed_number = parseFloat(stripped_number);
		if (isNaN(parsed_number)) {
			field.value = "";
		}
		else {
			field.value = parsed_number;
		}
	}
	else {
		field.value = "";
	}
}

function format_money(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);
}

function format_number(num) {
	num = num.toString().replace(/[\.]+/g,'.');
	num = num.toString().replace(/[^0-9\.]/g,'');
	if (isNaN(num)) {
		num = "0";
	}
	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));
	}
	if (cents > 0) {
		num = num + '.' + cents;
	}
	return num;
}

function gotoloans() {
	window.location = "loans";
}