// JavaScript Document - validation scripts for email forms


re = /\S+/;
reEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
reVal = /^[a-zA-Z0-9]+$/;
rePhone = /^\d\d\d\.\d\d\d\.\d\d\d\d$/;
reUsername = /^[a-zA-Z0-9]+$/;
rePassword = /^[a-zA-Z0-9]+$/;

noSub = "The form could not be submitted because\n";
noComp = "you did not complete ";
inValEmail = "you did not enter a valid Email address.";
tryAgain = "\n\nPlease do so and try again.";
tooShort = "\nis too short. It must be at least 4 characters.";
inValChar = "\ncontains invalid characters such as punctuations marks or symbols.";
inValPhone = "you entered\nis not in the required format.";
phoneExample ="\n\nExample of a correctly formatted phone number:\n\n401.331.9123";
tryAgain2 = "\n\nPlease correct this problem and then submit the form again.";
otherCity = "\n\nIf your city is not listed in the City dropdown menu,\nenter it in the field below that menu.";
doubleCity = "you selected a City name from the dropdown menu\nbut also entered a City name in the text box.";
doubleCity2 = "\n\nPlease do one or the other but not both.";
doubleCity3 = "\n\nIf you are entering a City name in the text box,\nselect Other from the City dropdown menu\n(last item in menu).";

//validate the login form
function validate(theForm) {
	
	if (!re.test(theForm.Username.value)) {
			alert(noSub + noComp + "the Username field." + tryAgain);
			theForm.Username.focus();
			return false;
	}
	
	if (!re.test(theForm.Password.value)) {
			alert(noSub + noComp + "the Password field." + tryAgain);
			theForm.Password.focus();
			return false;
	}	
}

// validate the contact form or the form for requesting info about volunteering
function valRequest(theForm) {
	if (!re.test(theForm.First_Name.value)) {
		alert(noSub + noComp + "the First Name field." + tryAgain);
		theForm.First_Name.focus();
		return false;
	}
	if (!re.test(theForm.Last_Name.value)) {
		alert(noSub + noComp + "the Last Name field." + tryAgain);
		theForm.Last_Name.focus();
		return false;
	}
	if (!re.test(theForm.Email_Address.value)) {
		alert(noSub + noComp + "the Email Address field." + tryAgain);
		theForm.Email_Address.focus();
		return false;
	}
	if (!reEmail.test(theForm.Email_Address.value)) {
		alert(noSub + inValEmail + tryAgain);
		theForm.Email_Address.focus();
		return false;
	}
	
	if (!re.test(theForm.subject.value)) {
		alert(noSub + noComp + "the Subject field." + tryAgain);
		theForm.subject.focus();
		return false;
	}
	
	if (!re.test(theForm.comments.value)) {
		alert(noSub + noComp + "the Message field." + tryAgain);
		theForm.comments.focus();
		return false;
	}
return true;
}

//validate the event registration form
function valRegister(theForm) {

  if (theForm.PayPal_Implemented.value == "yes") {
	   
		if (theForm.Member_Status[0].checked == false && theForm.Member_Status[1].checked == false && theForm.Member_Status[2].checked == false) {
			alert(noSub + "you did not indicate your Membership Status." + tryAgain);
			theForm.Member_Status[0].focus();
			return false;
		}
		
   }   
   else {
			
		if (theForm.Member_Status[0].checked == false && theForm.Member_Status[1].checked == false) {
			alert(noSub + "you did not indicate your Membership status." + tryAgain);
			theForm.Member_Status[0].focus();
			return false;
		}
   }
		
	if (!re.test(theForm.FirstAndInit.value)) {
		alert(noSub + noComp + "the First Name field." + tryAgain);
		theForm.FirstAndInit.focus();
		return false;
	}
	if (!re.test(theForm.LastName.value)) {
		alert(noSub + noComp + "the Last Name field." + tryAgain);
		theForm.LastName.focus();
		return false;
	}
	if (!re.test(theForm.CompanyAddress.value) && !re.test(theForm.Address2.value)) {
			alert(noSub + noComp + "the Address field." + tryAgain);
			theForm.CompanyAddress.focus();
			return false;
	}
	
	if (theForm.ID_City.value == 0 && !re.test(theForm.Other_City.value)) {
			alert(noSub + noComp + "the City field." + tryAgain + otherCity);
			theForm.ID_City.focus();
			return false;
	}
	
	if (theForm.ID_City.value != 0 && re.test(theForm.Other_City.value)) {
			alert(noSub  + doubleCity + doubleCity2 + doubleCity3);
			theForm.ID_City.focus();
			return false;
	}
	
	if (theForm.ID_State.value == 0) {
			alert(noSub + noComp + "the State field." + tryAgain);
			theForm.ID_State.focus();
			return false;
	}
	if (!re.test(theForm.CompanyZipCode.value)) {
			alert(noSub + noComp + "the Zip Code field." + tryAgain);
			theForm.CompanyZipCode.focus();
			return false;
	}
	
	if (!re.test(theForm.CompanyPhone.value) && !re.test(theForm.HomePhone.value)) {
		alert(noSub + noComp + "the Phone field.\n\nYou must provide either a company\nphone number or home phone number." + tryAgain);
		theForm.CompanyPhone.focus();
		return false;
	}
	if (!re.test(theForm.Author_email.value)) {
		alert(noSub + noComp + "the Email Address field." + tryAgain);
		theForm.Author_email.focus();
		return false;
	}
	if (!reEmail.test(theForm.Author_email.value)) {
		alert(noSub + inValEmail + tryAgain);
		theForm.Author_email.focus();
		return false;
	}
	
	if (theForm.PayPal_Implemented.value == "yes") {
	  
	    if (theForm.Payment_Method[0].checked == false && theForm.Payment_Method[1].checked == false) {
			alert(noSub + "you did not indicate your Payment Method." + tryAgain);
			theForm.Payment_Method[0].focus();
			return false;
		}
	}

return true;
}

//validate the member profile update form 
function valEditDir(theForm) {

	if (!re.test(theForm.FirstAndInit.value)) {
		alert(noSub + noComp + "the First Name field." + tryAgain);
		theForm.FirstAndInit.focus();
		return false;
	}
	
	if (!re.test(theForm.LastName.value)) {
		alert(noSub + noComp + "the Last Name field." + tryAgain);
		theForm.LastName.focus();
		return false;
	}
		
	//this is really just the Address1 field - does not have to be company address - could be home address
		if (!re.test(theForm.CompanyAddress.value) && !re.test(theForm.Address2.value)) {
			alert(noSub + noComp + "the Address field." + tryAgain);
			theForm.CompanyAddress.focus();
			return false;
		}
		
		if (!re.test(theForm.CompanyZipCode.value)) {
			alert(noSub + noComp + "the Zip Code field." + tryAgain);
			theForm.CompanyZipCode.focus();
			return false;
		}
		
		if (!re.test(theForm.CompanyPhone.value) && !re.test(theForm.HomePhone.value)) {
			alert(noSub + noComp + "a Phone Number field.\n\nYou must provide either a Company Phone Number\nor a Home Phone Number." + tryAgain);
			theForm.CompanyPhone.focus();
			return false;
		}
		
		if (!re.test(theForm.Author_email.value)) {
		alert(noSub + noComp + "the Email Address field." + tryAgain);
		theForm.Author_email.focus();
		return false;
	}
	
	if (re.test(theForm.Author_email.value) && !reEmail.test(theForm.Author_email.value)) {
		alert(noSub + inValEmail + tryAgain);
		theForm.Author_email.focus();
		return false;
	}
	
	//only if this is the administrative add new member form 
	if (theForm.Admin_Form.value == 1) {
			//and there is a company phone number entered 
			if (re.test(theForm.CompanyPhone.value)) {
					//make sure the format is correct
					if (!rePhone.test(theForm.CompanyPhone.value)) {
						alert(noSub + "the Company Phone number " + inValPhone + tryAgain2 + phoneExample);
						theForm.CompanyPhone.focus();
						return false;
					}				
			}
			//and there is a fax number entered
			if (re.test(theForm.Fax.value)) {
					//make sure the format is correct
					if (!rePhone.test(theForm.Fax.value)) {
						alert(noSub + "the Fax number " + inValPhone + tryAgain2 + phoneExample);
						theForm.Fax.focus();
						return false;
					}				
			}
			//and there is a Home phone number entered
			if (re.test(theForm.HomePhone.value)) {
					//make sure the format is correct
					if (!rePhone.test(theForm.HomePhone.value)) {
						alert(noSub + "the Home Phone number " + inValPhone + tryAgain2 + phoneExample);
						theForm.HomePhone.focus();
						return false;
					}				
			}
	}
	
	//only if this is the administrative edit member form -- check the username and password fields
	if (theForm.Admin_Form.value == 3) {
		
			if (!re.test(theForm.Username.value)) {
					alert(noSub + "you left the Username field blank." + tryAgain);
					theForm.Username.focus();
					return false;
			}
	
			if (re.test(theForm.Username.value) && !reUsername.test(theForm.Username.value)) {
				alert(noSub + "you entered a username containing a punctuation\nmark, symbol, or space. These are not allowed." + tryAgain);
				theForm.Username.focus();
				return false;
			}
			
			if (!re.test(theForm.Password.value)) {
				alert(noSub + "you left the Password field blank." + tryAgain);
				theForm.Password.focus();
				return false;
			}
	
			if (re.test(theForm.Password.value) && !rePassword.test(theForm.Password.value)) {
				alert(noSub + "you entered a password containing a punctuation\nmark, symbol, or space. These are not allowed." + tryAgain);
				theForm.Password.focus();
				return false;
			}
	}
	
return true;
}

//validate the subscription form
function valSubscribe(theForm) {
	if (!re.test(theForm.F_Name.value)) {
		alert(noSub + noComp + "the First Name field." + tryAgain);
		theForm.F_Name.focus();
		return false;
	}
	if (!re.test(theForm.L_Name.value)) {
		alert(noSub + noComp + "the Last Name field." + tryAgain);
		theForm.L_Name.focus();
		return false;
	}
	if (!re.test(theForm.E_Mail.value)) {
		alert(noSub + noComp + "the Email Address field." + tryAgain);
		theForm.E_Mail.focus();
		return false;
	}
	if (!reEmail.test(theForm.E_Mail.value)) {
		alert(noSub + inValEmail + tryAgain);
		theForm.E_Mail.focus();
		return false;
	}
	
return true;
}

//validate the job posting form
function valJobPost(theForm) {

	if (!re.test(theForm.Position_Name.value)) {
		alert(noSub + noComp + "the Position Name field." + tryAgain);
		theForm.Position_Name.focus();
		return false;
	}
	if (!re.test(theForm.Company_Name.value)) {
		alert(noSub + noComp + "the Company Name field." + tryAgain);
		theForm.Company_Name.focus();
		return false;
	}
	if (!re.test(theForm.Location.value)) {
		alert(noSub + noComp + "the Location field." + tryAgain);
		theForm.Location.focus();
		return false;
	}
	if (!re.test(theForm.Posting_Date.value)) {
		alert(noSub + noComp + "the Posting Date field." + tryAgain);
		theForm.Posting_Date.focus();
		return false;
	}
	
	if (!re.test(theForm.Hiring_Name.value)) {
		alert(noSub + noComp + "the Hiring Manager Name field." + tryAgain);
		theForm.Hiring_Name.focus();
		return false;
	}
	if (!re.test(theForm.Hiring_Contact.value)) {
		alert(noSub + noComp + "the field for\nHow to Contact the Hiring Manager." + tryAgain);
		theForm.Hiring_Contact.focus();
		return false;
	}
	
	if (!re.test(theForm.Position_Description.value)) {
		alert(noSub + noComp + "the Position Description field." + tryAgain);
		theForm.Position_Description.focus();
		return false;
	}
	if (!re.test(theForm.Requirements.value)) {
		alert(noSub + noComp + "the Requirements field." + tryAgain);
		theForm.Requirements.focus();
		return false;
	}
	if (!re.test(theForm.Submitter_Name.value)) {
		alert(noSub + noComp + "the Submitter's Name field." + tryAgain);
		theForm.Submitter_Name.focus();
		return false;
	}
	if (!re.test(theForm.Submitter_Email.value)) {
		alert(noSub + noComp + "the Submitter's E-Mail Address field." + tryAgain);
		theForm.Submitter_Email.focus();
		return false;
	}
	if (!re.test(theForm.Submitter_Phone.value)) {
		alert(noSub + noComp + "the Submitter's Phone field." + tryAgain);
		theForm.Submitter_Phone.focus();
		return false;
	}
return true;
}




