/*********************************
 * 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,fieldType)
{
	var fieldValue = document.getElementById(thisField)

	if(fieldType == null || fieldType == "input")
	{
		if(fieldValue.value == "")
		{
			document.getElementById(thisField+"Req").style.display = 'block';
			document.getElementById(thisField+"Req").innerHTML = "This is a required field.";
		}
		else
		{
			document.getElementById(thisField+"Req").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(phoneID)
{
	phoneValue = document.getElementById(phoneID).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(phoneID).value = phoneFinal;
	}
	else if(falsePhone == true)
	{
		document.getElementById(phoneID+"Req").innerHTML = "* Please include your area code.";
		document.getElementById(phoneID+"Req").style.display = 'block';
	}
	else
	{
		document.getElementById(phoneID+"Req").style.display = 'block';
		document.getElementById(phoneID+"Req").innerHTML = "This is not a valid phone number.";
		isDone = false;
	}
	if(isDone != false)
	{
		isDone = true;
	}
}

// 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) 
{
	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(emailID+"Req").style.display = 'block';
		document.getElementById(emailID+"Req").innerHTML = "This is not a valid e-mail.";
	}
	if(document.getElementById("email2"))
	{
		if(document.getElementById("email2").value != ""  && isDone != false)
		{
			compareEmails();
		}
	}
	if(isDone != false)
	{
		isDone = true;
	}
}

// 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.";
	}
}

// On submit, this function clears the previous submit attempt in preparation for another submit attempt.
function validateRefresh()
{
	returnValue = true;
	isDone = null;
}

// When the form is submitted, this function checks every required field in the form (defined in the "fieldIDs" array) and makes sure that they are not empty.  If they are empty, this function displays an appropriate error message beneath the field.  It will also prevent the form from submitting til all required fields have been filled.
function jsValidate(args)
{
	var fieldIDs = args.split(",");

	for(i=0;i<fieldIDs.length;i++)
	{
		if (document.getElementById(fieldIDs[i]).value == "")
		{
			document.getElementById(fieldIDs[i]+"Req").style.display = "block";
			returnValue = false;
		}
	}
}

function formSubmit(formID,jsValidateArgs)
{
	
	var formToSubmit = document.getElementById(formID);
	
	validateRefresh();
	jsValidate(jsValidateArgs);
	validatePhone('phone');
	validateEmail('email');
	if(isDone != null)
	{
		returnValue = isDone;
	}
	return returnValue;
}



























