//validate the whole form

function checkWholeForm(theForm) {

    var why = "";
    why += isEmpty(theForm.farmName.value, "farm name");
    why += isEmpty(theForm.firstName.value, "first name");
    why += isEmpty(theForm.lastName.value, "last name");
    why += isEmpty(theForm.address.value, "address");
    why += isEmpty(theForm.city.value, "city");
    why += isEmpty(theForm.zipcode.value, "zip code");
    why += checkEmail(theForm.email.value);
    why += checkPhone(theForm.phone.value, "phone");
    why += checkPhone(theForm.fax.value, "fax");
    why += checkPhone(theForm.cell.value, "cell");
    why += checkStateDropdown(theForm.state.selectedIndex);
    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "Please enter your email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "Your email address contains illegal characters.\n";
       }
    }
return error;    
}

// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng, item) {
var error = "";
if (strng == "") {
   error = "Please enter a phone number.\n";
   return error;
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "Your "+item+" number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "Your "+item+" number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}

// textbox - check for emptiness

function isEmpty(strng, item){
var error = "";
  if (strng == "") {
     error = "Please enter your " + item + ".\n";
  }
return error;	  
}

// valid selector from dropdown list

function checkStateDropdown(choice) {
var error = "";
    if (choice == "") {
    error = "Please select a State.\n";
    }    
return error;
}    