/* bond yield calc */

function check_radio(radio) {
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) {
			return radio[i].value;
		}
	}
	return null;
}

// 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('yield')) == true) {
		alert("You have left the 'Yield' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('yield')) == false) {
		alert("You have entered and invalid number in the 'Yield' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (isFieldBlank(document.getElementById('fed_rate')) == true) {
		alert("You have left the 'Federal Tax Rate' field blank. Please enter a value and click the 'Calculate' button.");
		return false;
	}
	if (isValidNumber(document.getElementById('fed_rate')) == false) {
		alert("You have entered and invalid number in the 'Federal Tax Rate' field. Please check your entry and click the 'Calculate' button.");
		return false;
	}

	if (document.getElementById('st_exemptno').checked) {
		if (isFieldBlank(document.getElementById('st_rate')) == true) {
			alert("You have left the 'State Tax Rate' field blank. Please enter a value and click the 'Calculate' button.");
			return false;
		}
		if (isValidNumber(document.getElementById('st_rate')) == false) {
			alert("You have entered and invalid number in the 'State Tax Rate' field. Please check your entry and click the 'Calculate' button.");
			return false;
		}
	}
	return true;
}

function calculate() {

	var cost_st;
	var yield_bef_fed;
	var cost_fed;
	var st_rate_used;

	if (isFormValid() == true) {
		

			// reset all output boxes
			document.getElementById('before_tax_input').value = "";
			document.getElementById('after_yield').value = "";
			document.getElementById('after_tax_input').value = "";
			document.getElementById('before_yield').value = "";

			if (document.getElementById('st_exemptyes').checked)
				st_rate_used = 0;
			else
				st_rate_used = parseFloat(document.getElementById('st_rate').value);
		
			if (document.getElementById('FormsRadioButton10').checked) {
				document.getElementById('after_tax_input').value = document.getElementById('yield').value;
				document.getElementById('before_yield').value = parseFloat(document.getElementById('yield').value) / (1 - ((st_rate_used + parseFloat(document.getElementById('fed_rate').value)) / 100));
			} else {
				document.getElementById('before_tax_input').value = document.getElementById('yield').value;
				document.getElementById('after_yield').value = parseFloat(document.getElementById('yield').value) * (1 - ((st_rate_used + parseFloat(document.getElementById('fed_rate').value)) / 100));
			}
	
	}
}