function validateFormOnSubmit(theForm) {
var reason = "";


  if (theForm.chkDownloadSource.checked)
  {
  reason += validateEmpty(theForm.fname,"First Name");
  reason += validateEmpty(theForm.lname,"Last Name");
  reason += validateEmail(theForm.email,"Email");  
  } 
  if(theForm.chkEmail.checked) 
  {
  reason = ""
  reason += validateEmpty(theForm.fname,"First Name");
  reason += validateEmpty(theForm.lname,"Last Name");
  reason += validateEmail(theForm.email,"Email");  
  } 
  if(theForm.chkPrintSource.checked)
  {
  reason = ""
  reason += validateEmpty(theForm.fname,"First Name");
  reason += validateEmpty(theForm.lname,"Last Name");
  reason += validateEmpty(theForm.company,"Company");  
  reason += validateEmpty(theForm.addr1,"Address Line 1");
  reason += validateEmpty(theForm.city,"City");
  reason += validateEmptyDrop(theForm.State,"State");    
  reason += validateEmpty(theForm.zip,"Zip");  
  reason += validateEmail(theForm.email,"Email");  
  }
          
  if (reason != "") {
    alert("The following fields are required or contain errors:\n" + reason);
    return false;
  }
  if((!theForm.chkPrintSource.checked)  && (!theForm.chkDownloadSource.checked) && (!theForm.chkEmail.checked)) {
    alert("You must check at least one Catalog Request Type") 
    return false;
  }
  
  return true;
}

function validateEmpty(fld,reason) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = reason + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateEmptyDrop(fld,reason) {
    var error = "";
    if (fld.selectedIndex==0) {
        fld.style.background = 'Yellow'; 
        error = reason + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateEmail(fld, reason) {
    var error="";
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = reason + "\n"
        return error;
    } 
    else {
        fld.style.background = 'White';
        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 = "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 trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


function validatePhone(fld,reason) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
        error = reason + "\n"
        return error;
    } 
    else {    
      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;
    }
}
