function floor(number)
{
	//return Math.floor(number * Math.pow(10, 2)) / Math.pow(10, 2);
	return number;
}

function removeFiller(txt)
{
	return txt.replace(/[^0-9\.]/gi, "");
}

function dosum()
{
	// Remove commas and dollar signs
	var price = removeFiller(document.getElementById('LA').value); // Price
	var downPayment = removeFiller(document.getElementById('DP').value); // Down Payment
	var intrestRate = removeFiller(document.getElementById('IR').value); // Intrest Rate
	var years = removeFiller(document.getElementById('YR').value); // Years
	var annualTax = removeFiller(document.getElementById('AT').value); // Annual Tax
	var insurance = removeFiller(document.getElementById('AI').value); // Insurance
	var payment = removeFiller(document.getElementById('MP').value); // Payment
	
	// Get annual tax rate
	annualTax = floor(price * .005);
	
	// Calculate insurance
	insurance = price * .002;
	
	var monthlyIntrest = intrestRate / 1200;
	var base = 1;
	var mbase = 1 + monthlyIntrest;

	for (i = 0; i < years * 12; i++)
		base = base * mbase;

	if(downPayment)
		var xprice = price - downPayment;
	else
		var xprice = price;

	monthlyPrincipalIntrest = floor(xprice * monthlyIntrest / ( 1 - (1 / base)));
	monthlyTax = floor(annualTax / 12);
	monthlyIntrest = floor(insurance / 12);
	var payment =
		xprice * monthlyIntrest / ( 1 - (1 / base)) +
		annualTax / 12 + 
		insurance / 12;
	payment = floor(payment);

	// Write to form
	document.getElementById('LA').value = commify(price);
	document.getElementById('DP').value = commify(downPayment);
	document.getElementById('IR').value = intrestRate + '%';
	document.getElementById('AT').value = commify(annualTax);
	document.getElementById('AI').value = commify(insurance);
	document.getElementById('MP').value = commify(floor(monthlyPrincipalIntrest));
}

function commify(num)
{
	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);
	if(num == 0)
		return (((sign) ? '' : '-') + '$');
	else
		return (((sign) ? '' : '-') + '$' + num);
}
