function validateForm(theForm) {
var reason = "";

  reason += validateName(theForm.yourName);
  reason += validatePhone(theForm.bestPhone);
  reason += validateEmail(theForm.email);
  reason += validateQ01(theForm.Q01);
  reason += validateQ02(theForm.Q02);
  reason += validateQ03(theForm.Q03);
  reason += validateCheckBoxes();
     
  if (reason != "") {
    alert("Please correct the following to continue:\n" + reason);
	document.forms.theForm.yourName.focus();
    return false;
  }

  return true;
}

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

function validateName(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFF568'; 
        error = " - Enter your full name.\n"
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;   
}

function validatePhone(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFF568'; 
        error = " - Enter the best phone number to reach you.\n"
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;   
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);								// value of field with white space trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#FFF568';
        error = " - Enter a valid email address.\n";		// error for blank email address
    } else if (!emailFilter.test(tfld)) {					// test email for illegal characters
        fld.style.background = '#FFF568';
        error = " - Enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFF568';
        error = " - Remove illegal characters from your email address.\n";
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;
}

function validateQ01(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFF568'; 
        error = " - Answer which product was used.\n"
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;   
}

function validateQ02(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFF568'; 
        error = " - Answer when the injuries first occurred.\n"
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;   
}

function validateQ03(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFF568'; 
        error = " - Answer if the injuries were diagnosed by a doctor.\n"
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;   
}

function validateCheckBoxes() {
	var error = "";
	
	if (document.forms.theForm.agree1.checked == true) {
	} else {
		error = " - Agree to the disclaimer by checking the box.\n";
	}
	return error;
}
