/* savings goal */

function fv(pv,yrs,intr) {
	return (parseInt(pv) * Math.pow(1 + parseFloat(intr), parseInt(yrs)));
}

function pv(fv,yrs,intr) {
	return (parseInt(fv) / Math.pow(1 + parseFloat(intr), parseInt(yrs)));
}

// Check that a number is valid
function isValidNumber(theField) {
	inLen = theField.value.length;
	decimal = 0;

	for(var i=0; i<inLen; i++) {
		var ch = theField.value.substring(i,i+1)
	
		if (ch == ".") {
			decimal += 1;
			if (decimal > 1)
				return false;
		} else if (ch == ",") {
				theField.value = theField.value.substring(0,i) + theField.value.substring(i+1,inLen);
				i -= 1;
				inLen -= 1;
		} else if ((ch < "0" && ch != ".") || "9" < ch)
				return false;
	}
	return true;
}

// Check for an empty field
function isFieldBlank(theField) {
	if(theField.value == "")
		return true;
	else
		return false;
}

function isFormValid() {

	if (isFieldBlank(document.getElementById('years')) == true) {
		alert("You have left the 'Years Until Goal' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('years')) == false) {
		alert("You have entered and invalid number in the 'Years Until Goal' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (isFieldBlank(document.getElementById('goal')) == true) {
		alert("You have left the 'Goal Amount' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('goal')) == false) {
		alert("You have entered and invalid number in the 'Goal Amount' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (isFieldBlank(document.getElementById('inflation')) == true) {
		alert("You have left the 'Inflation Rate' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('inflation')) == false) {
		alert("You have entered and invalid number in the 'Inflation Rate' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (isFieldBlank(document.getElementById('return_rate')) == true) {
		alert("You have left the 'Growth Rate' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('return_rate')) == false) {
		alert("You have entered and invalid number in the 'Growth Rate' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (isFieldBlank(document.getElementById('pres_save')) == true) {
		alert("You have left the 'Present Savings' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('pres_save')) == false) {
		alert("You have entered and invalid number in the 'Present Savings' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}
	return true;
}

function calculate() {

	/* Declare variables for form items to make the code
	   easier to read. */

	var lump_sum;
	var months;
	var monthly_int;
	var int_factor;
	var future_goal;

	if (isFormValid() == true) {
		var infl = parseInt(document.getElementById('inflation').value) / 100;
		var ret = parseInt(document.getElementById('return_rate').value) / 100;

		future_goal = fv(document.getElementById('goal').value,document.getElementById('years').value,infl);
		document.getElementById('fut_goal').value = Math.round(future_goal);
		// lump_sum = Math.round(pv(f.fut_goal.value,f.years.value,ret) - parseInt(f.pres_save.value));
		lump_sum = future_goal - fv(document.getElementById('pres_save').value,document.getElementById('years').value,ret);

		months = parseInt(document.getElementById('years').value) * 12;
		monthly_int = ret / 12;
		int_factor = (Math.pow(1+monthly_int,months)-1) / monthly_int;
		document.getElementById('monthly').value = Math.round(parseInt(lump_sum) / int_factor);
	}
}