var a = 0;

function disableAmort() {
				 document.forms[0].amortization.disabled = true;
			}
			
function computeLoan() {
			if (validateCalcForm()) {
			var p = parseFloat(document.forms[0].balance.value);
			var r = (parseFloat(document.forms[0].interest.value)/100)/12;
			var n = parseFloat(document.forms[0].termlength.value);	
			a = (p * (Math.pow((1 + r), n)) * r)/((Math.pow((1 + r), n)) - 1);
			var payment = a;
			payment = Math.round(payment * 100);
			payment = payment / 100;
			payment = payment.toString();
			if (payment.length > payment.indexOf(".") + 2) {
				payment = payment.substring(0, (payment.indexOf(".") + 3))
			}
			document.forms[0].payment.value = payment;		
			document.forms[0].amortization.disabled = false;
			}
		}
		
function doAmortization() {
			var balance = document.forms[0].balance.value;
			payment = a;
			var interest = (parseFloat(document.forms[0].interest.value)/100)/12;
				if (isNaN(parseInt(document.forms[0].extra.value))) {
					var extra = 0;
				} else {
					var extra = (parseFloat(document.forms[0].extra.value));
				}
			var thisInterest = 0;
			var thisPrincipal = 0;
			var newBalance = 0;
			var paymentCounter = 1;
			var totalInterest = 0;
			scheduleWindow = window.open("", "","scrollbars,resizable,width=570");
			scheduleWindow.focus();
			scheduleWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Amortization Schedule - Math&middot;U&middot;See</title>');
			scheduleWindow.document.write('<center><table width="540" cellpadding="2" cellspacing="1" border="1" bordercolor="#000000"><tr><td align="center" colspan="4"><h2>Amortization Schedule</h2></td></tr><tr align="center"><td width="25%"><strong>Payment number</strong></td><td width="25%"><strong>Principal</strong></td><td width="25%"><strong>Interest</strong></td><td width="25%"><strong>New Balance</strong></td></tr>');
			for (var thisBalance = balance; (!(thisBalance <= 0));) {
				scheduleWindow.document.write('<tr><td align="center">' + paymentCounter);
				thisInterest = (thisBalance * interest);
				thisPrincipal = (payment - thisInterest) + extra;
				newBalance = (thisBalance - thisPrincipal);
				totalInterest += thisInterest;
					if (newBalance <= 0) {
						thisPrincipal = (thisPrincipal + newBalance);
						newBalance = 0; 
					}
				scheduleWindow.document.write('</td><td align="center">' + format(thisPrincipal) + '</td><td align="center">' + format(thisInterest) + '</td><td align="center">' + format(newBalance) + '</td></tr>');
					if (newBalance == 0) {
						scheduleWindow.document.write('<tr><td colspan="4" align="right"><strong>Total interest paid = ' + format(totalInterest) + '</strong></td></tr></table>');
					}
				paymentCounter += 1;
				thisBalance = newBalance;
			}
			scheduleWindow.document.write('</center></body></html>');
			scheduleWindow.document.close();
		}
		
function format(thisNumber) {
	thisNumber = (Math.round(thisNumber * 100) / 100);	
	thisNumber = thisNumber.toString();
	if (thisNumber.indexOf(".") == -1) {
		thisNumber = thisNumber + "." + "00";
	}
	if (thisNumber.length - thisNumber.indexOf(".") == 2) {
		thisNumber = thisNumber + "" + "0";
	}
	return thisNumber;
}

function validateCalcForm() {
			if (isNaN(parseInt(document.forms[0].balance.value)) || isNaN(parseInt(document.forms[0].interest.value)) || isNaN(parseInt(document.forms[0].termlength.value))) {
				alert("Please fill in the top 3 text fields using numeric values.");
				var bad = true;
			}
			if (bad == true) {
				return false;
			}
			return true;
}

