

function validatePhone(fld) {
   var error = "";
   var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     
   re = /^\d{0,10}$/;

   if (fld.value == "") {
        error = "A required field has not been filled in - contact phone number.\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';
    } else if (!stripped.match(re)) {
        error = "The phone number contains invalid characters.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = '#eedcc5';
    } 
    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$/ ;
    //  may be worth a test from dynamicdrive var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
    // from PHP if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $data["clientEmail"]) == false) {

    //  previous version now match php version emailFilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
    emailFilter=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "A required field has not been filled in - email.\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 = '#eedcc5';
    }
    return error;
}	 

function validateEmpty(fld, msg) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "A required field has not been filled in - " + msg + ".\n"
    } else {
        fld.style.background = '#eedcc5';
    }
    return error;   
}



function validateFirstName(fld) {
    var error = "";
		
    // regular expression to match required time format
    re = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ]{0,18}$/;
    fld.style.background = '#eedcc5';
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "A required field has not been filled in - First Name .\n";}
	  if(fld.value != '' && !fld.value.match(re)) {
       fld.style.background = 'Yellow'; 
       error = "First Name has invalid characters. \n";}
    return error;
}

function validateLastName(fld) {
    var error = "";
		
    // regular expression to match required time format
    re = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \-]{0,18}$/;
    fld.style.background = '#eedcc5';
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "A required field has not been filled in - Last Name.\n";}
	  if(fld.value != '' && !fld.value.match(re)) {
       fld.style.background = 'Yellow'; 
       error = "Last Name has invalid characters. \n";}
    return error;
}

function validateBookingName(fld) {
    var error = "";
		
    // regular expression to match required time format
    re = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' -]{0,24}$/;
    fld.style.background = '#eedcc5';
	  if( !fld.value.match(re)) {
       fld.style.background = 'Yellow'; 
       error = "Booking Name has invalid characters.\n";}
    return error;
}


function validateFormOnSubmit(theForm) {
	 var reason = "";
	 
   reason += validateFirstName(theForm.firstName);
   reason += validateLastName(theForm.lastName);
   
   if (typeof theForm.contactFormHandlingFlags == 'undefined') {
      reason += validatePhone(theForm.clientContact); }
   else {
      if (theForm.contactFormHandlingFlags.value == "internationalSource") {
         reason += validateEmpty(theForm.clientContact, "phone number") }
      else {      
         reason += validatePhone(theForm.clientContact);
      } 
   }
   
   
   reason += validateEmail(theForm.clientEmail);
     
   if (reason != "") {
      alert("Some fields need correction:\n\n" + reason);
      return false;
   }
   return true;
}


 

