//----------------------------------------------------------------------------------------------
// Desc: Validation for booking form
// Auth: Anto Heley
//----------------------------------------------------------------------------------------------
function validateFormOnSubmit(theForm) {
	var reason = "";

  reason += validateEmpty(theForm.contact_name);
  reason += validateEmpty(theForm.school);
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.telephone);
  	
  if (reason != "") {
    //alert("Some fields need correction:\n" + reason);
		document.getElementById('error').style.display='';
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'url(images/asterix.gif) center right no-repeat #f8e4e5';
        fld.style.border = '1px solid #e00020';
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.border = '1px solid #016eb0';
        fld.style.background = '#ffffff';
    }
    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 = 'url(images/asterix.gif) center right no-repeat #f8e4e5';
        fld.style.border = '1px solid #e00020';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'url(images/asterix.gif) center right no-repeat #f8e4e5';
        fld.style.border = '1px solid #e00020';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'url(images/asterix.gif) center right no-repeat #f8e4e5';
        fld.style.border = '1px solid #e00020';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.border = '1px solid #016eb0';
        fld.style.background = '#ffffff';
    }
    return error;
}
