/*********************************
 * Name:    Christopher R. Beran *
 * Date:    May 2007             *
 * Purpose: Awana Inquiry Form   *
 *********************************/

var isDone = null;
var errorText = "";
var returnValue = true;

// 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.";
			returnValue = false;
		}
		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;
		document.getElementById(phoneID+"Req").style.display = 'none';
	}
	else if(falsePhone == true)
	{
		document.getElementById(phoneID+"Req").innerHTML = "* Please include your area code.";
		document.getElementById(phoneID+"Req").style.display = 'block';
		returnValue = false;
	}
	else
	{
		document.getElementById(phoneID+"Req").style.display = 'block';
		document.getElementById(phoneID+"Req").innerHTML = "This is not a valid phone number.";
		returnValue = false;
	}
}

// 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)
	{
		document.getElementById(emailID+"Req").style.display = 'block';
		document.getElementById(emailID+"Req").innerHTML = "This is not a valid e-mail.";
		returnValue = false;
	}
	else if(emailRegEx == true)
	{
		document.getElementById(emailID+"Req").style.display = 'none';
	}
}

function compareEmails(emailID,emailConfirm)
{
	if(document.getElementById(emailID).value != document.getElementById(emailConfirm).value)
	{
		document.getElementById(emailConfirm+"Req").innerHTML = "These email addresses do not match";
		document.getElementById(emailConfirm+"Req").style.display = "block";
		returnValue = false;
	}
	else if(document.getElementById(emailID).value == document.getElementById(emailConfirm).value)
	{
		document.getElementById(emailConfirm+"Req").style.display = "none";
	}
}

// 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.";
	}
}

//  This function checks the value of an input text field to see if it is a Church Registration number with the correct syntax.
function validateRegNumber(regID)
{
	var regValue = document.getElementById("00N50000001gOcE").value;
	var regRegEx = /US[0-9]{6}/.test(regValue);
	if(!regRegEx)
	{
		document.getElementById(regID+"Req").style.display = "block";
		document.getElementById(regID+"Req").innerHTML = "This is not a correct Registration Number.  If can find your Registration Number <a href=\"http://awana.findlocation.com/\" target=\"_blank\">here</a>.";
		returnValue = false;
	}
	else if(regRegEx)
	{
		document.getElementById(regID+"Req").style.display = "none";
	}
}

// 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,fieldArgs,phoneArgs,emailArgs,zipArgs,regArgs,compareArgs)
{
	validateRefresh();
	if(document.getElementById("00N50000001uwuM"))
	{
		var gsuMonth = document.getElementById("monthSelector").value;
		var gsuDay = document.getElementById("daySelector").value;
		var gsuYear = document.getElementById("yearSelector").value;
		
		document.getElementById("00N50000001uwuM").value = gsuMonth.toString()+"/"+gsuDay.toString()+"/"+gsuYear.toString();
	}
	if(fieldArgs)
	{
		var fieldIDs = fieldArgs.split(",");
		for(i=0;i<fieldIDs.length;i++)
		{
			reqFill(fieldIDs[i]);
		}
	}
	if(phoneArgs)
	{
		var phoneIDs = phoneArgs.split(",");
		for(i=0;i<phoneIDs.length;i++)
		{
			validatePhone(phoneIDs[i]);
		}
	}
	if(emailArgs)
	{
		var emailIDs = emailArgs.split(",");
		for(i=0;i<emailIDs.length;i++)
		{
			validateEmail(emailIDs[i]);
		}
	}
	if(zipArgs)
	{
		var zipIDs = zipArgs.split(",");
		for(i=0;i<zipIDs.length;i++)
		{
			validateZipCode(zipIDs[i]);
		}
	}
	if(regArgs)
	{
		var regIDs = regArgs.split(",");
		for(i=0;i<regIDs.length;i++)
		{
			validateRegNumber(regIDs[i]);
		}
	}
	if(compareArgs)
	{
		var compareIDs = compareArgs.split(",");
		for(i=0;i<compareIDs.length;i+=2)
		{
			compareEmails(compareIDs[i],compareIDs[i+1]);
		}
	}
	if(returnValue == false)
	{
		document.getElementById("finalSubmit").style.display = "block";
	}
	else
	{
		document.getElementById("finalSubmit").style.display = "none";
	}
	return returnValue;
}