function validateFormOnSubmit(theForm) {

var reason = "";

  reason += validateEmail(theForm.Email);
  reason += validateEmpty(theForm.FirstName);
  reason += validateEmpty(theForm.LastName);
  reason += validateSelection(theForm.BuyingHorizon);
  reason += validateEmpty(theForm.Company);
  reason += validateSelection(theForm.Industry);
  reason += validateSelection(theForm.EmployeeSize);
  reason += validateSelection(theForm.AnnualRevenue);
  reason += validateSelection(theForm.JobFunction);
  reason += validateSelection(theForm.JobRole);
  reason += validatePhone(theForm.Phone1);
  reason += validateEmpty(theForm.Website);
  reason += validateEmpty(theForm.Addr1);
  reason += validateEmpty(theForm.City);
  reason += validateSelection(theForm.State);
  reason += validateEmpty(theForm.ZipCode);
  reason += validateMeeting(theForm.Webex);
    
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason );
    return false;
  }
  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#c2efcb'; 
        error = "Please enter your " + fld.id + ".\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateSelection(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#c2efcb'; 
        error = "Please select an option for your " + fld.id + ".\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 = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    var hotmailEmail = /@hotmail\.com/i;
    var liveEmail = /@live\.com/i;
    var gmailEmail = /@gmail\.com/i;
    var yahooEmail = /@yahoo\.com/i;
    var macEmail = /@mac\.com/i;
    var verizonEmail = /@verizon\.net/;
    var comcastEmail = /@comcast\.net/;
    var gamilEmail = /@gamil\.com/;
    var bellsouthEmail = /@bellsouth\.com/;
   
    if (fld.value == "" || fld.value == "Your Email") {
        fld.style.background = '#c2efcb';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#c2efcb';
        error = "The email address you entered is not valid.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#c2efcb';
        error = "Your email address contains illegal characters.\n";
    } else if (fld.value.match(hotmailEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(liveEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(gmailEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(yahooEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(macEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(verizonEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(comcastEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(gamilEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else if (fld.value.match(bellsouthEmail)) {
        fld.style.background = '#c2efcb';
        error = fld.value + ":\n\nPlease use your Business / Office / Work  email address.\n\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var ph = fld.value;
    ph = ph.replace( /\D/g,'');  //strip non-numeric characters except hyphen
     
    if (fld.value.length == 0) {
        fld.style.background = '#c2efcb'; 
        error = "Please enter your telephone number.\n"
    } else if (ph.length < 10) {
        fld.style.background = '#c2efcb';
        error = "Please enter a valid telephone number.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateMeeting(fld) {
    var error = "";
    var radio_choice = false;
    
    for (counter = 0; counter < fld.length; counter++) {
      if (fld[counter].checked)
      radio_choice = true;
    }

    if (!radio_choice) {
        error = "Please select Yes or No for Schedule a LiveMeeting.\n"
    } else {}
    return error;
}