

// ------------------------------------------------------------------------------------------------ 
// These functions validate the form after the user submits but before data is transmitted
// ------------------------------------------------------------------------------------------------ 

// Initialize an error message to be displayed in a js alert window
var errorMsg = " ";

// Validate "Request" field
function validatePrayerTopic() {
	var strValue = document.prayerRequest.prayerTopic.value;
	// If the form field is empty or is the "Required FIeld" messaging...
	if ((strValue == '') || (strValue == 'Required Field')) {
		// Update the style and content in the text field to call attention to it
		document.getElementById('prayerTopic').style.backgroundColor="#AA272F";
		document.getElementById('prayerTopic').style.color="#FFFFFF";
		document.getElementById('prayerTopic').value="Required Field";
		// Add this field into the error message
		errorMsg += "\"Prayer Request\" is a required field. ";
		return false;
	}
	// If the form field  was filled out correctly...
	else {
		// Reset the form element's style back to normal if the user has gone back and input the info correctly
		document.getElementById('prayerTopic').style.backgroundColor="#EDEDED";
		document.getElementById('prayerTopic').style.color="#616365";
		return true;
	}
}

// Validate "Name" field
function validateName() {
	var strValue = document.prayerRequest.name.value;
	// If the form field is empty or is the "Required FIeld" messaging...
	if ((strValue == '') || (strValue == 'Required Field')) {
		// Update the style and content in the text field to call attention to it
		document.getElementById('name').style.backgroundColor="#AA272F";
		document.getElementById('name').style.color="#FFFFFF";
		document.getElementById('name').value="Required Field";
		// Add this field into the error message
		errorMsg += "\"Name\" is a required field. ";
		return false;
	}
	// If the form field  was filled out correctly...
	else {
		// Reset the form element's style back to normal if the user has gone back and input the info correctly
		document.getElementById('name').style.backgroundColor="#EDEDED";
		document.getElementById('name').style.color="#616365";
		return true;
	}
}

// Validate "Email" field
function validateEmail() {
	var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
	var strValue = document.prayerRequest.email.value;
	// If the form field is empty or is the "Required FIeld" messaging...
	if ((strValue == '') || (strValue == 'Required Field')) {
		// Update the style and content in the text field to call attention to it
		document.getElementById('email').style.backgroundColor="#AA272F";
		document.getElementById('email').style.color="#FFFFFF";
		document.getElementById('email').value="Required Field";
		// Add this field into the error message
		errorMsg += "\"Email Address\" is a required field. ";
		return false;
	}
	// Or if the form field has user input, but isn't in the right format for an email address...
	else if (!(objRegExp.test(strValue))) {
		// Update the style and content in the text field to call attention to it
		document.getElementById('email').style.backgroundColor="#AA272F";
		document.getElementById('email').style.color="#FFFFFF";
		// Add this field into the error message
		errorMsg += "Please check \"Email Address\" for errors. ";
		return false;
	}
	// If the form field  was filled out correctly...
	else if (objRegExp.test(strValue)){
		// Reset the form element's style back to normal if the user has gone back and input the info correctly
		document.getElementById('email').style.backgroundColor="#EDEDED";
		document.getElementById('email').style.color="#616365";
		return true;
	}	
}


function submitForm() {
	
	// Reset the error message if the user is giving it another shot
	errorMsg = "Please make sure required fields are filled in: ";
	
	// Set variable up here. If one or more of the form elements isn't set correctly, 'passValidation' will be set to 'false'
	var passValidation = true;
	
	if (!(validatePrayerTopic()))	{passValidation = false;}
	if (!(validateName()))			{passValidation = false;}
	if (!(validateEmail()))		{passValidation = false;}
	
	// If one or more of the form elements isn't set correctly, 'passValidation' will be set to 'false', alert the user
	if (!(passValidation)) {
		// Display the final error message in a js window
		alert (errorMsg);
	}	

	// If 'passValidation' is 'true' go ahead and submit the form...
	//return passValidation;
	else {
		document.prayerRequest.submit();
	}
}
