//
// This is the javascript validation for the Contact form (contact.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function validate_booking_form() {
  //Form validation
  var bError = false;
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }  
  if ($j('#role').val() == '') { addError('role'); bError = true; } else { removeErrorClass('role'); }
  if ($j('#phone').val() == '') { addError('phone'); bError = true; } else { removeErrorClass('phone'); }

  if ($j('#org_name').val() == '') { addError('org_name'); bError = true; } else { removeErrorClass('org_name'); }
  if ($j('#org_address_1').val() == '') { addError('org_address_1'); bError = true; } else { removeErrorClass('org_address_1'); }
  if ($j('#org_city').val() == '') { addError('org_city'); bError = true; } else { removeErrorClass('org_city'); }
  if ($j('#org_state').val() == '') { addError('org_state'); bError = true; } else { removeErrorClass('org_state'); }
  if ($j('#org_zip').val() == '') { addError('org_zip'); bError = true; } else { removeErrorClass('org_zip'); }
  if ($j('#org_country').val() == '') { addError('org_country'); bError = true; } else { removeErrorClass('org_country'); }

  if ($j('#event_title').val() == '') { addError('event_title'); bError = true; } else { removeErrorClass('event_title'); }
  if ($j('#event_date').val() == '') { addError('event_date'); bError = true; } else { removeErrorClass('event_date'); }
  if ($j('#event_attendance').val() == '') { addError('event_attendance'); bError = true; } else { removeErrorClass('event_attendance'); }

  if(!$j("input[name^='mrf']:checked").size()){ addError('mrf'); bError = true; } else { removeErrorClass('mrf'); }
  if(!$j("input[name^='arrangements']:checked").size()){ addError('arrangements'); bError = true; } else { removeErrorClass('arrangements'); }
  if(!$j("input[name^='recorded']:checked").size()){ addError('recorded'); bError = true; } else { removeErrorClass('recorded'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

  if ($j('#book_security_code').val() == '') { addError('book_security_code'); bError = true; } else { removeErrorClass('book_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

function removeErrorClass(control_id) {
  $j('#'+control_id).parent().parent().removeClass('error');
  return true;
}

function addError(control_id) {
  $j('#'+control_id).select().focus();
  $j('#'+control_id).parent().parent().addClass('error');
  return true;
}