
function validateFormOnSubmit(theForm) {
var reason = "";
  

  
  
  reason += validatefirstname(theForm.first_name);
  reason += validatelastname(theForm.last_name);
  reason += validatecompany(theForm.company);
  reason += validatePhone(theForm.phone);
  reason += validateEmail(theForm.email);
  reason += validatedescription(theForm.description);
  

      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
   
   
 //  alert("Submitting");
 
 //document.getElementById("ajaxcontentarea").innerHTML = "<div style=" + "padding-left:100px;padding-top:150px;color:blue;font-size:16px" + "> Your Message was Sent. Thank You. </div>";
// expandtab('maintab', 0)  


 
  return true;
}



function validatedescription(fld) {
    var error = "";
    
   //  var illegalChars = /\W/; // allow letters, numbers, and underscores
    
    
    var illegalChars= /[\(\)\<\>\:\\\"\[\]]/ ;
    
   // var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
  
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter any comments.\n";
    } else if ((fld.value.length < 15) || (fld.value.length > 250)) {
        fld.style.background = 'Yellow'; 
        error = "You entered (" + fld.value.length + ") letters in your comments. - Min:15 Characters. Max:250\n";
    } 
    
     else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Your comments contains illegal characters.\n";
    }
    
    
    else {
        fld.style.background = 'White';
    } 
    return error;
}











function validatecompany(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
   // var illegalChars = /[\w\x20]+/;
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your company name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 35)) {
        fld.style.background = 'Yellow'; 
        error = "You entered (" + fld.value.length + ") letters as your company name. - Min:2 Characters. Max:35\n";

    } 
        else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Your company name contains illegal characters.\n";
    }
    

    
    
    else {
        fld.style.background = 'White';
    } 
    return error;
}



function validatefirstname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your first name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "Your first name is the wrong length or too short.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Your first name contains illegal characters. Please remove empty space.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}


function validatelastname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your last name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "Your last name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Your last name contains illegal characters. Please remove empty space\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= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    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 validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');   
     
    phoneerror = "The phone number contains illegal characters.\n";
    
    phonenumbers_array = new Array();
    
    
    for (var i = 0; i < stripped.length; i++) 
    { 
      phonenumbers_array[i] = stripped.slice(i,i+1)
       //alert(phonenumbers_array[i]);
      if (isNaN(phonenumbers_array[i])==1) {
        return phoneerror;
          fld.style.background = 'Yellow';
        //alert("Phone Number Contains Illegal characters.\n");
      }
   
    }
      
   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(" + stripped.length + "). Please enter a 10 digit number. Make sure to include your area code.\n";
       fld.style.background = 'Yellow';
    } 
    return error;
}

