/*********************************
 * Name:    Christopher R. Beran *
 * Date:    May 2007             *
 * Purpose: Awana Inquiry Form   *
 *********************************/

var isDone = null;
var errorText = "";

// This function checks the specified "thisField" to see if it is empty.  If it is empty, it displays an "errorField" with an error message for "thisField".  It hides the "errorField" when "thisField" is not empty.
function reqFill(thisField,errorField)
{
	fieldValue = document.getElementById(thisField)
	if(fieldValue.value == '')
	{
		document.getElementById(errorField).style.display = 'block';
		document.getElementById(errorField).innerHTML = "This is a required field.";
	}
	else
	{
		document.getElementById(errorField).style.display = 'none';
	};
};

// This function checks the value of an input text field to see if it is a phone number with the correct syntax.  If the syntax is not correct, but is close, it will fix the syntax for the user.  If the syntax is unrecognizeable, the function will return an appropriate error in a nearby layer.  It passes a variable that determines if the field is being checked for syntax or as a required field.  Syntax checking is 0, required field checking is 1.
function validatePhone(val)
{
	phoneValue = document.getElementById("phone").value
	var phoneFix = phoneValue.replace(/\D/g,"")
	var phoneRegEx = /^\d{10}$/.test(phoneFix)
	var falsePhone = /^d{7}$/.test(phoneFix)
	if(phoneRegEx == true)
	{
		var phoneSlice = phoneValue.replace(/\D/g,"");
		areaCode = phoneSlice.substr(0,3);
		phone3digits = phoneSlice.substr(3,3);
		phone4digits = phoneSlice.substr(6,4);
		phoneFinal = "("+areaCode+")"+phone3digits+"-"+phone4digits;
		document.getElementById("phone").value = phoneFinal;
		requiredField += val
	}
	else if(falsePhone == true)
	{
		document.getElementById("phoneReq").innerHTML = "* Please include your area code.";
		document.getElementById("phoneReq").style.display = 'block';
	}
	else
	{
		document.getElementById("phoneReq").style.display = 'block';
		document.getElementById("phoneReq").innerHTML = "* This is not a valid phone number.";
	}
}

// This function checks the value of an input text field to see if it is an e-mail with the correct syntax.  If the syntax is not correct, the function will return an appropriate error in a nearby layer.  It passes a variable that determines if the field is being checked for syntax or as a required field.  Syntax checking is 0, required field checking is 1.
function validateEmail(emailID,val) 
{
	emailValue = document.getElementById(emailID).value;
	var emailRegEx = /^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/.test(emailValue)
	if(emailRegEx == false)
	{
		isDone = false;
		document.getElementById("emailReq").style.display = 'block';
		document.getElementById("emailReq").innerHTML = "* This is not a valid e-mail.";
	}
	if(document.getElementById("email2").value != ""  && isDone != false)
	{
		compareEmails();
	}
}

// This function checks the value of an input text field to see if it is empty.  If the field is empty, the function will return an appropriate error in a nearby layer.
function validateRegularText(regTextID)
{
	var regTextValue = document.getElementById(regTextID).value
	if(regTextValue == "")
	{
		isDone = false;
	}
}

// This function checks the value of an input text field to see if it is a zip code with the correct syntax.  If the syntax is not correct, but is close, it will fix the syntax for the user.  If the syntax is unrecognizeable, the function will return an appropriate error in a nearby layer.
function validateZipCode(zipID)
{
	var zipValue = document.getElementById(zipID).value
	var zipRegEx = /^\d{5}$|^\d{5}\-?\d{4}$/.test(zipValue)
	if (zipRegEx == true && zipValue.length == 9)
	{
		var zip5 = zipValue.substr(0,5);
		var zip4 = zipValue.substr(5,4);
		var zipFinal = zip5+"-"+zip4;
		document.getElementById(zipId).value = zipFinal;
	}
	if(zipRegEx == false)
	{
		isDone = false;
		document.getElementById("zipReq").style.display = 'block';
		document.getElementById("zipReq").innerHTML = "* This is not a valid zip code.";
	}
}