/* college cost */

function check_radio(radio) {
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) {
			return radio[i].value;
		}
	}
	return null;
}

function fv(pv,yrs,intr) {
	return (parseInt(pv) * Math.pow(1 + parseFloat(intr), parseFloat(yrs)));
}

function first_year_curr() {

	/* Declare variables for form items to make the code
	   easier to read. The JavaScript "with" method will
	   not work here because it is not recognized inside
	   function calls. */

	
	var pub_cost = document.getElementById('pub_cost');
	var pub_room = document.getElementById('pub_room');
	var priv_cost = document.getElementById('priv_cost');
	var priv_room = document.getElementById('priv_room');

	if (document.getElementById('FormsRadioButton5').checked)
		if (document.getElementById('FormsRadioButton7').checked)
			return parseInt(pub_cost.value) +
				parseInt(pub_room.value);
		else
			return pub_cost.value;
	else
		if (document.getElementById('FormsRadioButton7').checked)
			return parseInt(priv_cost.value) +
				parseInt(priv_room.value);
		else
			return priv_cost.value;
}

function first_year(yrs,intr) {
	var pv = first_year_curr();
	return Math.round(fv(pv,yrs,intr));
}

function four_years(first_year,intr) {
	var second_year = fv(first_year,1,intr);
	var third_year = fv(first_year,2,intr);
	var fourth_year = fv(first_year,3,intr);
	var total = parseInt(first_year) +
			parseFloat(second_year) +
			parseFloat(third_year) +
			parseFloat(fourth_year);
	return Math.round(total);
}

// 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' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('years')) == false) {
		alert("You have entered an invalid number in the 'Years' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (isFieldBlank(document.getElementById('inflation')) == true) {
		alert("You have left the 'Inflation' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('inflation')) == false) {
		alert("You have entered an invalid number in the 'Inflation' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (document.getElementById('FormsRadioButton5').checked) {
		if (isFieldBlank(document.getElementById('pub_cost')) == true) {
			alert("You have left the 'Public College Tuition' field blank. Please enter a value and click the 'Calculate' button.");
			return false;
		}
		if (isValidNumber(document.getElementById('pub_cost')) == false) {
			alert("You have entered an invalid number in the 'Public College Tuition' field. Please check your entry and click the 'Calculate' button.");
			return false;
		}
		if (document.getElementById('FormsRadioButton7').checked){
			if (isFieldBlank(document.getElementById('pub_room')) == true) {
				alert("You have left the 'Public Room and Board' field blank. Please enter a value and click the 'Calculate' button.");
				return false;
			}
			if (isValidNumber(document.getElementById('pub_room')) == false) {
				alert("You have entered an invalid number in the 'Public Room and Board' field. Please check your entry and click the 'Calculate' button.");
				return false;
			}
		}
	} else {
		if (isFieldBlank(document.getElementById('priv_cost')) == true) {
			alert("You have left the 'Private College Tuition' field blank. Please enter a value and click the 'Calculate' button.");
			return false;
		}
		if (isValidNumber(document.getElementById('priv_cost')) == false) {
			alert("You have entered an invalid number in the 'Private College Tuition' field. Please check your entry and click the 'Calculate' button.");
			return false;
		}
		if (document.getElementById('FormsRadioButton7').checked){
			if (isFieldBlank(document.getElementById('priv_room')) == true) {
				alert("You have left the 'Private Room and Board' field blank. Please enter a value and click the 'Calculate' button.");
				return false;
			}
			if (isValidNumber(document.getElementById('priv_room')) == false) {
				alert("You have entered an invalid number in the 'Private Room and Board' field. Please check your entry and click the 'Calculate' button.");
				return false;
			}
		}
	}
	return true;
}


function calculate() {

	if (isFormValid() == true) {
		var intr = parseFloat(document.getElementById('inflation').value) / 100;
		document.getElementById('first_year_cost').value = first_year(document.getElementById('years').value,intr);
		document.getElementById('four_year_cost').value = four_years(document.getElementById('first_year_cost').value,intr);
	}
}