function bodyLoaded() {
	document.forms[0].contrib.disabled = true;
	document.forms[0].investment.disabled = true;
	document.forms[0].doTable.disabled = true;
}
function lump () {
	document.forms[0].contrib.disabled = true;
	document.forms[0].contrib.value = 0;
	document.forms[0].investment.disabled = false;
	document.forms[0].investment.value = "";
	document.getElementById("switched").style.visibility = "visible";
	document.getElementById("compound").disabled = false;
	document.getElementById("compound").value = '';
	document.getElementById("contribType").value = "lump";
}	
function periodic() {	
	document.forms[0].contrib.disabled = false;
	document.forms[0].contrib.value = "";
	document.forms[0].investment.disabled = true;
	document.forms[0].investment.value = 0;
	document.getElementById("switched").style.visibility = "hidden";
	document.getElementById("compound").disabled = true;
	document.getElementById("compound").value = 12;
	document.getElementById("continuous").checked = false;
	document.getElementById("contribType").value = "periodic";
} 
function computeInvestment() {	
var m = parseFloat(document.forms[0].contrib.value);		
var p = parseFloat(document.forms[0].investment.value);
var r = parseFloat((document.forms[0].interest.value)/100);
var n = parseInt(document.forms[0].compound.value);
var y = parseInt(document.forms[0].term.value);
			if (validateInvestCalcForm()) {
				//Lump contribution with continuously compounding interest
				if (document.forms[0].continuous.checked) {
					f = p * (Math.pow(Math.E, (y * r)));					
					document.forms[0].finalValue.value = format(f);	
				}else{ //It can't be both continuous and compounding
					//Lump contribution with periodic compounding interest 
					if (document.forms[0].contribType.value == "lump") {
						f = p * (Math.pow((1 + (r / n)), (y * n)));
						document.forms[0].finalValue.value = format(f);
					}
					//Periodic contribution with interest compounding equal to contribution frequency
					if (document.forms[0].contribType.value == "periodic") {
						f = m * (Math.pow((1 + (r/n)), (y * n)) - 1) * (n / r);
						document.forms[0].finalValue.value = format(f);
					}
				}
			}
			if ((document.getElementById("continuous").checked == false) && (document.getElementById("contribType").value == "lump")) {
				document.forms[0].doTable.disabled = false;
			}
		}
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 validateInvestCalcForm() {
	var bad = "";
	var ErrMsg = "";
			//In any case, One of the 2 radio buttons must be selected as well as an interest rate and 
			//some kind of indication as to what the frequency of compounding will be, as well as
			//the length of time the investment will be running.
			if (document.getElementById("contribType").value == "") {
				ErrMsg += "Please choose either one time investment, or periodic contributions.\r\n";
				bad = true;
			}
			if (isNaN(parseInt(document.forms[0].interest.value))) {
				ErrMsg += "Please enter a numeric value in the field labeled \"Interest\".\r\n";
				bad = true;
			}
			if ((isNaN(parseInt(document.forms[0].compound.value)) && (document.forms[0].continuous.checked == false))) {
				ErrMsg += "Please enter a numeric value in the field labeled \"Compounding\", or check the box for continuous compounding.\r\n";
				bad = true;
			}
			if (isNaN(parseInt(document.forms[0].term.value))) {
				ErrMsg += "Please enter a numeric value in the field labeled \"Number of Years\".\r\n";
				bad = true;
			}
			//If we are talking about a 1-time contribution, we need to know how much it is.
			if (document.getElementById("contribType").value == "lump") {
				if (isNaN(parseInt(document.forms[0].investment.value))) {
					ErrMsg += "Please enter a numeric value in the field labeled \"One Time Investment\".\r\n";
					bad = true;
				}
			}
			//If we are talking about periodic contributions, we need to know how much they is.
			if (document.getElementById("contribType").value == "periodic") {
				if (isNaN(parseInt(document.forms[0].contrib.value))) {
					ErrMsg += "Please enter a numeric value in the field labeled \"Periodic Contributions\".\r\n";
					bad = true;
				}
			}
			if (bad == true) {
				alert(ErrMsg);
				return false;
			}
			return true;
		}
function disableInputs() {
	document.forms[0].compound.disabled = document.forms[0].continuous.checked;
	if (document.forms[0].continuous.checked) {
		document.forms[0].doTable.disabled = true;
	}
}
function makeTable() {
	var p = parseFloat(document.forms[0].investment.value);
	var r = parseFloat((document.forms[0].interest.value)/100);
	var n = parseInt(document.forms[0].compound.value);
	var y = parseInt(document.forms[0].term.value);
	var i = (r/n); //Variable to hold interest rate per time period
	var rows = n * y; //Variable to hold number of iterations
	//Table column variables:
	var a = 1;
	var b = parseFloat(document.forms[0].investment.value);
	var c = i * b;
	var d = parseFloat(b) + parseFloat(c);	
			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>Investment Table - 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>Investment Table</h2></td></tr><tr align="center"><td width="25%"><strong>Time Period</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 (a = 1; a < (rows + 1); a++) {
				scheduleWindow.document.write('<tr><td align="center">' + a + '</td>');
				scheduleWindow.document.write('<td align="center">' + format(b) + '</td>');
				scheduleWindow.document.write('<td align="center">' + format(c) + '</td>');
				scheduleWindow.document.write('<td align="center">' + format(d) + '</td></tr>');
				b = d;
				c = (Math.round(i * b * 100) / 100); 
				d = parseFloat(b) + parseFloat(c);	
			}
			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;
}
