// query a buttongroup and find the selected button value
function getSelectedValue(buttonGroup)
{
	for (var buttonvalue = 0; buttonvalue < buttonGroup.length; buttonvalue++)
	{
		if (buttonGroup[buttonvalue].checked)
		{
			return buttonvalue;
		}
	}
	return false;
}

// determine whether a text field on a form is empty
function isEmpty(inputStr)
{
	if (inputStr == null || inputStr == "")
	{
		return true;
	}
	return false;
}

// determine whether a string contains only positive integers
function isPosInteger(inputValue)
{
	inputStr = inputValue.toString()
	for (var digit = 0; digit < inputStr.length; digit++)
	{
		var oneChar = inputStr.charAt(digit);
		if (oneChar < "0" || oneChar > "9")
		{
			return false;
		}
	}
	return true;
}

// determine whether a field that is supposed to receive only numeric input actually contains only positive integers
// by calling the "isPosInteger" function
function validateNumber(inputValue)
{
	var inputStr = "";
	
	inputStr = (inputValue);
	if (!isPosInteger(inputStr))
	{
		return true;
	}
	return false;
}

// begin to validate the form
function validateForm()
{
	// establish the variables
	var Form = "";
	var NameValue = "";
	var EmailValue = "";
	var validRegExp = "";	
	var AreaCodeValue = "";
	var PhonePrefixValue = "";
	var PhoneMainValue = "";
	var JoinEmailList = "";	
	var AlertMessage = "";
	var bFormValidate = "";

	// assign initial values to the variables
	Form = document.forms[0];
	NameValue = Form.Name.value;
	EmailValue = Form.Sender.value;
	AreaCodeValue = Form.Area_code.value;
	PhonePrefixValue = Form.Phone_prefix.value;
	PhoneMainValue = Form.Phone_end.value;
	JoinEmailList = getSelectedValue(Form.list);
	bFormValidate = Boolean(true);
	
	// establish the beginning message that should be included in the alert message -
	AlertMessage = "We cannot process your information request\nbecause the following items are missing from\nyour form submittal:\n\n";

	// if the "name" field is empty, add to the alert message and set the form validation to "false"
	if (isEmpty(NameValue))
	{
		AlertMessage += "Your name\n";
		bFormValidate = false;
	}

	// validate the phone number
	// first whether it's empty and second, whether it contains only a numeric value
	// then add to the alert message and set the form validation to "false"
	if (isEmpty(AreaCodeValue) || isEmpty(PhonePrefixValue) || isEmpty(PhoneMainValue))	
	{
		AlertMessage += "A complete or properly formatted phone number\n";
		bFormValidate = false;
	}
	
	// check to see if they've asked to join the mailing list
	if (JoinEmailList == "0")
	{
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		if(EmailValue.search(validRegExp) == -1)
   		{
			AlertMessage += "You asked to join the e-mail list, but did not provide a valid e-mail address\n";
			bFormValidate = false;
		}
	}

	// if any alert strings were generated, then construct the message and send it in an alert box
	if (bFormValidate == false)
	{
		alert(AlertMessage+ "\n\nPlease check the form, and then click the\n\"Submit\" button again. Thanks.");
	}
	//otherwise, process the form
	if (bFormValidate == true)
	{
			Form.submit();
	}
}




