// JavaScript Document
function validateFormOnSubmit(vsu) {
var reason = "";
//syntax examples:
//reason += validateUsername(theForm.username);
//reason += validatePassword(theForm.pwd);
//reason += validateEmail(theForm.email);
//reason += validatePhone(theForm.phone);
//reason += validateEmpty(theForm.from);

reason += validateEmpty(vsu.vsu_volunteer_name);
reason += validateEmpty(vsu.vsu_volunteer_gender);
reason += validateEmpty(vsu.vsu_volunteer_address);
reason += validateEmpty(vsu.vsu_volunteer_citystate);
reason += validateEmpty(vsu.vsu_volunteer_zip);
reason += validatePhone(vsu.vsu_volunteer_phone);
reason += validatePhone(vsu.vsu_volunteer_cell);
reason += validateEmail(vsu.vsu_volunteer_email);
reason += validateEmpty(vsu.vsu_volunteer_tshirtsize);
reason += validateEmpty(vsu.vsu_volunteer_past_participant);
reason += validateEmpty(vsu.vsu_volunteer_rotarian);
reason += validateAge(vsu.vsu_volunteer_rotarian,vsu.vsu_rotoract_age);
reason += validateEmpty(vsu.vsu_volunteer_position);
reason += validateEmpty(vsu.vsu_volunteer_club_name);
reason += validateVolGuide(vsu.vsu_volunteer_read_guidelines);
reason += validateTrainingDay(vsu.vsu_volunteer_training_day);

  if (reason != "") {
    alert("Form not complete:\n" + reason);
	return false;    
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "A required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateAge(fld1,fld2) {
    var error = "";
	
	if (fld1.value=="Rotoract"){
  
    if (fld2.value.length == 0) {
        fld2.style.background = 'Yellow'; 
        error = "Rotoract Members must enter their age.\n"
    } else {
        fld2.style.background = 'White';
    }
	
	}
	
    return error;   
}

function validateAgreement(fld) {
    var error = "";
  
    if (fld.checked==false) {
        fld.style.background = 'Yellow'; 
        error = "You must agree to the RYLA Behavior Code before submitting your registration.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateVolGuide(fld) {
    var error = "";
  
    if (fld.checked==false) {
        fld.style.background = 'Yellow'; 
        error = "You must agree to the Volunteer Guidelines before signing up.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateTrainingDay(fld) {
    var error = "";
  
    if (fld.checked==false) {
        fld.style.background = 'Yellow'; 
        error = "You must agree to attend a training day before signing up.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\s]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}