function validateFormOnSubmit(theForm) {
  
  var reason = "";
  
  reason += validateEmail(theForm.email);
  reason += validateEmailx(theForm.emailx);
  reason += validateEmailCompare(theForm.email,theForm.emailx);
  var errmsg = "You did not give a title or given name.\n";
  reason += validateEmpty(theForm.realname, errmsg);
  var errmsg = "You did not give a family name.\n";
  reason += validateEmpty(theForm.famname, errmsg);

  var RoomCount = document.theForm.quantity_1.value + document.theForm.quantity_2.value + document.theForm.quantity_3.value + document.theForm.quantity_4.value + document.theForm.quantity_5.value + document.theForm.quantity_6.value;

  reason += validateRoomNumber(RoomCount);

  reason += validateTermsChecked(theForm.terms);
    
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  var agreeBooking=confirm("You are about to make a firm booking that may require a deposit. This will be explained on the next page. Are you sure you wish to continue?");

  if (agreeBooking){ 
    return true ;
  } else {
    return false ;
  }  
}

function validateTermsChecked(fld) {
    if (fld.checked == false) {
        fld.style.background = 'Yellow'; 
        var error = "You must agree the terms and conditions.\n";
    } else {
        fld.style.background = 'White';
        var error = "";
    }
    return error;   
}


function validateEmailCompare(fld1,fld2){
   var error = "";
   if (fld1.value != fld2.value) {
       error = "E Mail Addresses do not match.\n";
   } else {
       error = "";
   }     
return error;
}

function validateRoomNumber(fld) {
    
    if (fld == 0) {
         
        var error = "You must select at least one Room.\n";
    } else {
        
        var error = "";
    }
    return error;   
}



function validateEmpty(fld,error) {
    
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        
    } else {
        fld.style.background = 'White';
        var error = "";
    }
    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 validateEmailx(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 a confirmation email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid confirmation email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The confirmation email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}



function confirmSubmitbook(theForm)
{
  var reason = theForm.currency.value;

  if (reason == "XXX") {
    alert("You have not selected the currency in which you wish to pay, please select and try again:\n");
    return false;
  }

  return true;

}

