// Some code from:
// Javascript form validation routines.
// Author: Stephen Poley
// http://www.xs4all.nl/~sbpoley/webmatters/formval.html


//array of input fields to check for non-empty
var inputFieldsArray = new Array('title', 'howmany', 'datebegan', 'dateended', 'duration', 'thename', 'thetitle', 'organization', 'mailingaddress', 'citystatezip', 'phonenumber', 'emailaddress', 'description', 'outcomes', 'instructorname', 'instructoraffiliation', 'instcitystatezip', 'instphone', 'instemail')

var emptyString = /^\s*$/;
var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;

function verify_inputFields() {
	var isValid = true;
	var fieldIsEmpty;
	var headCellStyle;
	
	for (var x=0; x < inputFieldsArray.length; x++) { 
		fieldIsEmpty = emptyString.test(document.forms.CEUcredit[inputFieldsArray[x]].value)
		isValid = isValid && !fieldIsEmpty;
		headCellStyle = document.getElementById(inputFieldsArray[x] + "Head").style;
		if (fieldIsEmpty) {
			headCellStyle.color = "#FF0000";
			headCellStyle.fontWeight = "bold";
		} else {
			headCellStyle.color = "#333333";
			headCellStyle.fontWeight = "normal";
		}
		// alert("Valid Form: " + isValid + "Is empty: " + emptyString.test(document.forms.CEUcredit[inputFieldsArray[x]].value) + " | Form Value = \""  + document.forms.CEUcredit[inputFieldsArray[x]].value + "\"");
	}
	
	return isValid;
}

function verify_ApplicationRadio() {
	var isValid = (document.forms.CEUcredit.applicationtype[0].checked) || (document.forms.CEUcredit.applicationtype[1].checked);
	var FirstTimeApplicationHead = document.getElementById("FirstTimeApplicationHead").style;
	var SubsequentApplicationHead = document.getElementById("SubsequentApplicationHead").style;
	
	if (!isValid) {
			FirstTimeApplicationHead.color = "#FF0000";
			FirstTimeApplicationHead.fontWeight = "bold";

			SubsequentApplicationHead.color = "#FF0000";
			SubsequentApplicationHead.fontWeight = "bold";
		} else {
			FirstTimeApplicationHead.color = "#333333";
			FirstTimeApplicationHead.fontWeight = "normal";

			SubsequentApplicationHead.color = "#333333";
			SubsequentApplicationHead.fontWeight = "normal";
		}
		
	return isValid;
}

function verify_proof() {
	var isValid = (document.forms.CEUcredit["certificateofcompletion"].checked) || (document.forms.CEUcredit["registrationfeereceipt"].checked);
	var attendanceHead = document.getElementById("attendanceHead").style;
	
	if (!isValid) {
			attendanceHead.color = "#FF0000";
			attendanceHead.fontWeight = "bold";
		} else {
			attendanceHead.color = "#333333";
			attendanceHead.fontWeight = "normal";
		}
		
	return isValid;
}

function verify_payment() {
	var isValid = (document.forms.CEUcredit.paymenttype[0].checked) || (document.forms.CEUcredit.paymenttype[1].checked);
	var paymenttypeHead = document.getElementById("paymenttypeHead").style;
	
	if (!isValid) {
			paymenttypeHead.color = "#FF0000";
			paymenttypeHead.fontWeight = "bold";
		} else {
			paymenttypeHead.color = "#333333";
			paymenttypeHead.fontWeight = "normal";
		}
		
	return isValid;
}

function verify_certification() {
	var isValid = document.forms.CEUcredit["certificationbox"].checked;
	var certificationboxHead = document.getElementById("certificationboxHead").style;
	
	if (!isValid) {
			certificationboxHead.color = "#FF0000";
			certificationboxHead.fontWeight = "bold";
		} else {
			certificationboxHead.color = "#333333";
			certificationboxHead.fontWeight = "normal";
		}
		
	return isValid;
}

function verify_emailAddress() {
	var isValid = (email.test(document.forms.CEUcredit["emailaddress"].value));
	var emailaddressHead = document.getElementById("emailaddressHead").style;
	
	if (!isValid) {
			emailaddressHead.color = "#FF0000";
			emailaddressHead.fontWeight = "bold";
		} else {
			emailaddressHead.color = "#333333";
			emailaddressHead.fontWeight = "normal";
		}

	return isValid;
}

function validate_form() {
	var bottomIndicator = document.getElementById("bottomFormValidate").style;
	var topIndicator = document.getElementById("topFormValidate").style;

	

	var isValid = verify_inputFields();
	isValid = (verify_proof() && isValid);
	isValid = (verify_payment() && isValid);
	isValid = (verify_certification() && isValid);
	isValid = (verify_ApplicationRadio() && isValid);
	isValid = (verify_emailAddress() && isValid);
	if (!isValid) {
		bottomIndicator.display = "block";
		topIndicator.display = "block";
	}
	return isValid;
}