/* Import Common Data JS file */
document.write("<script language=\"JavaScript\" src=\"scripts/commondata.js\"></script>");

function confirmCancel() {

	var confirmAnswer = confirm("Are you sure you want to cancel your submission?")
	if (confirmAnswer)
	{
		document.location.href = "http://www.shoredemocrats.org/index.shtml";
	}
	else {}
}

function validateForm(formObject) {

	var formIsValid = true;
	var errorMsg = "";
	var errorMsgBoxText = "";
	var defaultErrorHeader = "Sorry, there was a problem with your form submission.  See notes below:\n"
	var defaultErrorFooter = "";
	var errorEnumeration = 0;
	for (var x = 0; x < formElements.length; x++ )
	{	
		/* if this form element is required, but blank, return as error */
		if ((formElements[x].required) && (isBlank(formElements[x].elementObject.value)))
		{
				++errorEnumeration;
				if (typeof(formElements[x].errorMessage) != "undefined")
				{
					errorMsg = errorMsg + "\n" + errorEnumeration + ". " + formElements[x].errorMessage;
				}
				else
				{
					errorMsg = errorMsg + "\n" + errorEnumeration + ". " + formElements[x].name + ", a required field, is blank.  Please enter a value.";
				}
				
				formIsValid = false;
				if (typeof(errorElementBackgroundColor) != "undefined") { formElements[x].elementObject.style.backgroundColor = errorElementBackgroundColor; }
				continue;				
		}
		/* check to see if this element has a validity function, if so, run it */
		if (formElements[x].validityFunction != null)
		{
			/* Run the validity function */
			if (eval(formElements[x].validityFunction) == false)
			{
				++errorEnumeration;
				/* if this element has a custom validity error message, append it to the error box */
				if (typeof(formElements[x].validityErrorMessage) != "undefined")
				{
					errorMsg = errorMsg + ("\n" + errorEnumeration + ". " + formElements[x].validityErrorMessage);
				}
				else
				{
					errorMsg = errorMsg + ("\n" + errorEnumeration + ". " + formElements[x].name + " field is invalid.  Please enter a valid value.");
				}
				if (typeof(errorElementBackgroundColor) != "undefined") { formElements[x].elementObject.style.backgroundColor = errorElementBackgroundColor; }
				formIsValid = false;
			}
		}
	}
	if (formIsValid) 
	{
		return true;
	}
	else {
			if (typeof(customErrorHeader) != "undefined")
			{
				errorMsgBoxText = customErrorHeader + errorMsg;
			}
			else
			{
				errorMsgBoxText = defaultErrorHeader + errorMsg;
			}
			
			if (typeof(customErrorFooter) != "undefined")
			{
				errorMsgBoxText = errorMsgBoxText + customErrorFooter;
			}
			else
			{
				errorMsgBoxText = errorMsgBoxText + defaultErrorFooter;			
			}
			
			alert(errorMsgBoxText);
			return false;
		}
}

function verify2CharacterState (state) {

	var states = new Array();	
	states = returnStateList();

	for (var x = 0; x < states.length; x++)
	{
		if (state.toUpperCase() == states[x]) { return true; }
	}
	return false;
}

function verify5DigitZip(zip) {

	var validZip = /\d\d\d\d\d/;	
	return validZip.test(zip);

}

function verifyEmailAddress(email) {

	var validEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return validEmail.test(email);
}

function isBlank(value) {

	var blankValue = /^\s*$/;
	return blankValue.test(value);

}
