function checkNumeric(inputBoxID)
{
    var re_Int = /^[0-9\.]+$/;
    return re_Int.test(document.getElementById(inputBoxID).value);
}

function validateInput(e,inputBoxID)
{
    resetAPRValues();

    var keypressed = window.event ? e.keyCode : e.which;
    if ((keypressed == 9) || (keypressed == 13) || (keypressed == 16) || (keypressed == 144))
	return true;

    var clearInput = false;
    if ((keypressed == 46) || (keypressed == 8))
	clearInput = true;

    var validInput = true;

    var inString = new String();
    inString = document.getElementById(inputBoxID).value;

    if ((checkNumeric(inputBoxID)) || clearInput)
    {
	var searchResult = inString.indexOf(".");

	if (searchResult == -1)
	{
		if (inString.length > 9)
		{
			document.getElementById(inputBoxID).style.border = "red thin inset";
			document.getElementById(inputBoxID).value = " ";
			document.getElementById(inputBoxID).focus();
			alert("The field highlighted in red contains too many digits. Please enter up to 9 digits before the decimal point.");
			validInput = false;
		}
	}
	else
	{
		if ((inString.length - 1) > searchResult)
		{
			var chkString = new String();
			chkString = inString.substr((searchResult + 1),(inString.length - (searchResult + 1)));
			if (chkString.length > 2)
			{
				document.getElementById(inputBoxID).style.border = "red thin inset";
				document.getElementById(inputBoxID).value = " ";
				document.getElementById(inputBoxID).focus();
				alert("The field highlighted in red contains too many digits to the right of the decimal. Please enter only 2 digits to the right of the decimal.");
				validInput = false;
			}
		}
	}

	var periodCount = 0;

	while (searchResult != -1)
	{
		periodCount = periodCount + 1;
		inString = inString.replace(".","");
		searchResult = inString.indexOf(".");
	}

	if (periodCount > 1)
	{
		document.getElementById(inputBoxID).style.border = "red thin inset";
		document.getElementById(inputBoxID).value = " ";
		document.getElementById(inputBoxID).focus();
		alert("The field highlighted in red contains too many periods. Please enter only digits and up to one decimal point in the field.");
		validInput = false;
	}
    }
    else
    {
	document.getElementById(inputBoxID).style.border = "red thin inset";
	document.getElementById(inputBoxID).value = " ";
	document.getElementById(inputBoxID).focus();

	if ((inString.length == 1) && (inString == '-'))
		alert("The field highlighted in red must contain a positive number.");
	else
		alert("The field highlighted in red contains an invalid character. Please enter only digits and up to one decimal point in the field.");

	validInput = false;
    }

    if (validInput)
	resetInputBox(inputBoxID);

    return validInput;
}

function allInputFieldsEntered()
{
    if (document.getElementById("AAmt").value == ".")
    {
	document.getElementById("AAmt").style.border = "red thin inset";
	document.getElementById("AAmt").value = " ";
	document.getElementById("AAmt").focus();
	alert("The annual premium entered is invalid. Please enter a number greater than 0 in the annual premium field.");
	return false;
    }
    if (document.getElementById("SAmt").value == ".")
    {
	document.getElementById("SAmt").style.border = "red thin inset";
	document.getElementById("SAmt").value = " ";
	document.getElementById("SAmt").focus();
	alert("The semiannual premium entered is invalid. Please enter a number greater than 0 in the semiannual premium field.");
	return false;
    }
    if (document.getElementById("QAmt").value == ".")
    {
	document.getElementById("QAmt").style.border = "red thin inset";
	document.getElementById("QAmt").value = " ";
	document.getElementById("QAmt").focus();
	alert("The quarterly premium entered is invalid. Please enter a number greater than 0 in the quarterly premium field.");
	return false;
    }
    if (document.getElementById("MAmt").value == ".")
    {
	document.getElementById("MAmt").style.border = "red thin inset";
	document.getElementById("MAmt").value = " ";
	document.getElementById("MAmt").focus();
	alert("The monthly premium entered is invalid. Please enter a number greater than 0 in the monthly premium field.");
	return false;
    }
    if (document.getElementById("AAmt").value == "")
    {
	document.getElementById("AAmt").style.border = "red thin inset";
	document.getElementById("AAmt").value = " ";
	document.getElementById("AAmt").focus();
	alert("The annual premium is required and must contain a number greater than 0.");
	return false;
    }
    if (parseFloat(document.getElementById("AAmt").value) == 0)
    {
	document.getElementById("AAmt").style.border = "red thin inset";
	document.getElementById("AAmt").value = " ";
	document.getElementById("AAmt").focus();
	alert("The annual premium is required and must contain a number greater than 0.");
	return false;
    }
    if (((document.getElementById("SAmt").value == "") || (parseFloat(document.getElementById("SAmt").value) == 0)) &&
	((document.getElementById("QAmt").value == "") || (parseFloat(document.getElementById("QAmt").value) == 0)) &&
	((document.getElementById("MAmt").value == "") || (parseFloat(document.getElementById("MAmt").value) == 0)))
    {
	if (document.getElementById("SAmt").value != "")
		document.getElementById("SAmt").focus();
	else if (document.getElementById("QAmt").value != "")
		document.getElementById("QAmt").focus();
	else if (document.getElementById("MAmt").value != "")
		document.getElementById("MAmt").focus();
	else
		document.getElementById("SAmt").focus();
	alert("Please enter at least one amount greater than 0 in either the semiannual premium, the quarterly premium, or the monthly premium field.");
	return false;
    }
    if ((parseFloat(document.getElementById("AAmt").value) > 0) && (parseFloat(document.getElementById("SAmt").value) > 0))
    	if (parseFloat(document.getElementById("AAmt").value) == parseFloat(document.getElementById("SAmt").value))
	{
		document.getElementById("SAmt").focus();
		alert("The APR cannot be calculated with the amounts entered. Please check your annual and semiannual premium amounts.");
		return false;
	}
    if ((parseFloat(document.getElementById("AAmt").value) > 0) && (parseFloat(document.getElementById("QAmt").value) > 0))
	if ((5 * parseFloat(document.getElementById("AAmt").value)) == (2 * parseFloat(document.getElementById("QAmt").value)))
	{
		document.getElementById("QAmt").focus();
		alert("The APR cannot be calculated with the amounts entered. Please check your annual and quarterly premium amounts.");
		return false;
	}
    return true;
}