//form validation

function formValidation(form) {

	var	name = form.Name;
  var	email = form.Email;
	var	straddress = form.Address;
	var	city = form.City;
	var	state = form.State;
	var	zip = form.Zip;
	var	phone = form.Phone;
       
	var	rallies =	 form.Rallies;
	var	nonunion = form.NonUnion;
	var	phonecalls = form.PhoneCalls;
	var	leaflets = form.Leaflets;
	var	community = form.Community;
			
	var	member = form.Member;		

	var	job = form.Job;
	var	building = form.Building;
	var	hours = form.Hours;

//all visitors must select an activity they would like to be notified about
	var activities = new Array(rallies,nonunion,phonecalls,leaflets,community);
	
for(i=0;i<activities.length;i++) {
	if (activities[i].checked) {
		break;
	}
	if (!activities[i].checked) {
		if (i == (activities.length - 1)) {
			alert("Please check the appropriate boxes to indicate which volunteer activities you are interested in, and then resubmit this form.");
			return false;
		}
	}
}
	
//require all of these fields	
 if (!name.value){
		alert("Please enter your name and resubmit this form.");
		name.focus();
		return false;
	}

	else if(!straddress.value){
		alert("Please enter your street address and resubmit this form.");
		straddress.focus();
		return false;
	}
	
	else if(!city.value){
		alert("Please enter your city and resubmit this form.");
		city.focus();
		return false;
	}
	
	else if(!state.value){
		alert("Please enter your state and resubmit this form.");
		state.focus();
		return false;
	}
 
  	else if(!zip.value){
		alert("Please enter your zip code and resubmit this form.");
		zip.focus();
		return false;
	} 
	
  	else if(!phone.value){
		alert("Please enter your phone number and resubmit this form.");
		phone.focus();
		return false;
	} 	

	else if(!isPhoneNo(phone.value)){
		alert("The phone number that you entered is not valid. Please make sure that you include your full phone number plus area code.")
		phone.focus();
		return false;
	}	
					
		else if (!email.value){
		alert("Please enter your email address and resubmit this form.");
		email.focus();
		return false;
	}

	else if(!isEmail(email.value)){
		alert("The email address that you entered is not valid. Please make sure that your address is entered correctly and resubmit this page.")
		email.focus();
		return false;
	}
	
	else if (member[0].checked) {

		if (!job.value){
			alert("Please enter your job and resubmit this form.");
			job.focus();
			return false;
		}
		else if(!building.value){
			alert("Please enter the name of the building at which you work and resubmit this form.")
			building.focus();
			return false;
		}
		else if(!hours.value){
			alert("Please enter the hours you work and resubmit this form.")
			hours.focus();
			return false;
		}
	
	}	
		else {
    return true;
  }
}  

//if member, require member-specific fields



function isEmail(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

/* use this segment to test for a valid SS#
else if(!isSS(phone.value)){
		alert("You must enter a valid Social Security Number")
		phone.focus();
		return false;
	}
*/

function isSS(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return true;
  var r1 = new RegExp("[0-9]{9}");
  var r2 = new RegExp("[0-9]{3}-[0-9]{2}-[0-9]{4}");
  return (r1.test(str) || r2.test(str));
}

function isPhoneNo(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return true;
	var r2 = new RegExp("[0-9]{3}.*[0-9]{3}.*[0-9]{4}");
  return (r2.test(str));
}