function validateEmpty(fld,fieldname) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "- " + fieldname + "\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

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

function validateEmail(fld,fieldname) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "- " + fieldname + "\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "- " + fieldname + "\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "- " + fieldname + "\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

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

   if (fld.value == "") {
        error = "- " + fieldname + "\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "- " + fieldname + "\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "- " + fieldname + "\n";
        fld.style.background = 'Yellow';
    }
    return error;
}



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

  reason += validateEmpty(theForm.company,"Company");
  reason += validateEmpty(theForm.name, "Name");
  reason += validateEmail(theForm.email, "Email");
  reason += validatePhone(theForm.telephone, "Telephone");
    
  if (reason != "") {
    alert("The following field(s) are required or need correction:\n\n" + reason);
    return false;
  }

  return true;
}


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

  reason += validateEmpty(theForm.Company,"Company");
  reason += validateEmpty(theForm.Name, "Name");
  reason += validateEmail(theForm.Emailaddress2, "Email");
  reason += validatePhone(theForm.Telephone, "Telephone");
  if (reason != "") {
    alert("The following field(s) are required or need correction:\n\n" + reason);
    return false;
  }

  return true;
}


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

  reason += validateEmpty(theForm.name, "Name");
  reason += validatePhone(theForm.telephone, "Telephone");
  reason += validateEmail(theForm.email, "Email");
    
  if (reason != "") {
    alert("The following field(s) are required or need correction:\n\n" + reason);
    return false;
  }

  return true;
}


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

  reason += validateEmpty(theForm.AgentName, "Agent Name");
  reason += validateEmail(theForm.AgentEmail, "Agent Email");
  reason += validatePhone(theForm.AgentTel, "Agent Telephone");
  
  reason += validateEmpty(theForm.ClientName, "Client Name");
  reason += validateEmail(theForm.ClientEmail, "Client Email");  
  reason += validatePhone(theForm.ClientTel, "Client Telephone");
    
  if (reason != "") {
    alert("The following field(s) are required or need correction:\n\n" + reason);
    return false;
  }

  return true;
}
