
// Prevents anything but numbers from being typed into fields when attached to onkeypress event
function numberOnly(e) {
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 44 && charCode != 36 && charCode != 46)
		return false;
	return true
}

// Formats the specified Number with embedded commas
function formatDollar (n) {
	var p = n.toString();
	if (p) {
		var r = 3; var d = p.substr(p.length-3,3); var j = p.length - 3;
		while (j > 0) {
			j = j - 3;
			if (j < 0) { r = 3 + j; j = 0; }
			d = p.substr(j,r) + ',' + d;
		}
		return d;
	}
	else
		return p;
}

// Converts the specified formatted value back into a Number.
function unFormatDollar (f) {
	if (f) {
		var n = '';
		for (var i=0; i<f.length; i++) {
			var c = f.substring(i,i+1);
			if (c >= '0' && c <= '9')
				n += c;
		}
		return Number(n);
	} else
		return f;
}

// "Fixes" the specified number at 2 decimal digits
function flr(n) {
	return Math.floor(n*Math.pow(10,2))/Math.pow(10,2);
}

/*function calcPayment(source) {
	var fv = document.getElementById('fv');
	var pv = document.getElementById('pv');
	var P = unFormatDollar(fv.innerHTML) - unFormatDollar(pv.value);
	document.getElementById('p').innerHTML = formatDollar(Math.floor(P));
	if (Number(document.getElementById('n').value) > 0) { // conventional, fixed-rate
		var r = document.getElementById('r').value / 1200;
		var N = Number(document.getElementById('n').value);
		var pmt = P * r / (1.0 - Math.pow((1 + r), -N));
		document.getElementById('pmt').innerHTML = formatDollar(Math.floor(pmt));
	}
	else { // interest-only loan
		var pmt = (P * document.getElementById('r').value/12)/100;
		document.getElementById('pmt').innerHTML = formatDollar(Math.floor(pmt));
	}
	if(document.getElementById('tax').innerHTML == 'N/A')
		var mtax = 0;
	else
		var mtax = unFormatDollar(document.getElementById('tax').innerHTML) / 12;
	
	if ((unFormatDollar(pv.value) / unFormatDollar(fv.innerHTML)) < 0.2000)
		document.getElementById('totalpmt').innerHTML = formatDollar(Math.floor(pmt + (mtax))) + ' <a href="#" style="font-weight:bold; text-decoration:none; color: #CC0000;" title="Private Mortgage Insurance Required.">*</a>';
	else
		document.getElementById('totalpmt').innerHTML = formatDollar(Math.floor(pmt + (mtax)));
	if(document.getElementById('tax').innerHTML == 'N/A')
		document.getElementById('totalpmt').innerHTML += ' * taxes not included';
	document.getElementById('pmtlabel').innerHTML = (Number(document.getElementById('n').value) > 0) ? 'Principal and Interest:' : 'Interest:';
}*/

function calcPayment(source) {
    var listPrice = parseInt(unFormatDollar(document.getElementById('fv').innerHTML));
    var downPayment = parseInt(unFormatDollar(document.getElementById('pv').value));
    if (document.getElementById('tax').innerHTML == 'N/A')
        var yearlyTaxes = 0;
    else
        var yearlyTaxes = unFormatDollar(document.getElementById('tax').innerHTML);
    var monthlyTaxes = yearlyTaxes / 12;
    var loanAmount = listPrice - downPayment;

    var yearlyRate = parseFloat(document.getElementById('r').value);
    var numMonthlyPayments = parseInt(document.getElementById('n').value);

    var baseMonthlyPayment = calculateMortgagePayment(yearlyRate, loanAmount, numMonthlyPayments);
    var monthlyPayment = baseMonthlyPayment + monthlyTaxes;
    var principalAndInterest = baseMonthlyPayment * numMonthlyPayments;

    document.getElementById('totalpmt').innerHTML = formatDollar(Math.floor(monthlyPayment));
    document.getElementById('pmt').innerHTML = formatDollar(Math.floor(principalAndInterest));
    document.getElementById('pmtlabel').innerHTML = 'Principal + Interest:';
    document.getElementById('p').innerHTML = formatDollar(Math.floor(loanAmount));
}

function calculateMortgagePayment(apr, principal, numPayments) {
    // Based on formula from http://en.wikipedia.org/wiki/Mortgage_calculator
    // c = rP / ( 1 - ( 1 + r )^-N )

    var r = apr / 100 / 12; // Monthly interest rate as a decimal, not percentage
    var N = numPayments; // Loan term
    var P = principal; // Amount borrowed
    var c; // Monthly payment

    c = r * P / (1 - Math.pow(1 + r, -1 * N));

    return c;
}

function saveMortgagePrefs() {
	// Get the current mortgage settings on this form
	var n = unFormatDollar(document.getElementById('n').value);
	var r = document.getElementById('r').value;
	var pv = unFormatDollar(document.getElementById('pv').value);
	document.cookie = 'NOTHMORTGAGEPREFS=n='+escape(n)+'&r='+escape(r)+'&pv='+escape(pv) + '; path=/;';
	document.getElementById('SaveLink').innerHTML = 'Saving';
	var i = 0
	setTimeout('writeSaving(\'' + i + '\')',500);
}
function writeSaving(i) {
	i++;
	if(i == 4)
		document.getElementById('SaveLink').innerHTML = '<a href="javascript:saveMortgagePrefs();">Save these preferences</a>';
	else {
		document.getElementById('SaveLink').innerHTML += '...'
		setTimeout('writeSaving(\'' + i + '\')',500);
	}
}


