var error = false;

function validate_begin(field)
{
 //  Two regexp checks now. First one checks for bad words and second checks if
 // there are any lowercase characters and negates the result (Hence equivalent
 // to checking if all the characters are uppercase). Notice no 'i' flag in the
 // second. Also the regexps can now match strings such as 'o - r - a - l' - try
 // it, pretty cool, eh?
 var re = /f(\W){0,3}u(\W){0,3}c(\W){0,3}k|s(\W){0,3}u(\W){0,3}c(\W){0,3}k|c(\W){0,3}o(\W){0,3}c(\W){0,3}k|s(\W){0,3}h(\W){0,3}i(\W){0,3}t|c(\W){0,3}u(\W){0,3}n(\W){0,3}t|b(\W){0,3}a(\W){0,3}s(\W){0,3}t(\W){0,3}a(\W){0,3}r(\W){0,3}d|&|>|<font|http:|www\.|\.org|\.com|\.co\.uk|\.net/i;
 var re2 = /[a-z]/;
 return !((re.test(field.value))||(!re2.test(field.value)));
 }

function catch_error(field)
{
  //  Four regexps now, grouped for readability. First checks for bad words,
  // second for inappropriate topics, third for formatting, and last for
  // uppercase evil. They have been separated for friendlier user-feedback.
  var re = /f(\W){0,3}u(\W){0,3}c(\W){0,3}k|s(\W){0,3}u(\W){0,3}c(\W){0,3}k|c(\W){0,3}o(\W){0,3}c(\W){0,3}k|s(\W){0,3}h(\W){0,3}i(\W){0,3}t|c(\W){0,3}u(\W){0,3}n(\W){0,3}t|b(\W){0,3}a(\W){0,3}s(\W){0,3}t(\W){0,3}a(\W){0,3}r(\W){0,3}d/i;
  var re3 = />|<font/i;
  var re4 = /[a-z]/;
  var re5 = /http:|www\.|\.org|\.com|\.co\.uk|\.net/i;
  var re6 = /[&]/i;

  if (re.test(field.value))
  {
   alert('This submission cannot be posted! There\'s a line which appears to contain an abuse or swear word.');
  }

  else if (re3.test(field.value))
  {
   alert('We aren\'t able to accept your submission like this! You appear to be using formatting - e.g. the symbol \> - in one of the lines. Please enter plain text.');
  }
  else if (!re4.test(field.value))
  {
   alert('Sorry! We don\'t accept any parts of a submission in all capitals!\n\nWith your Caps Lock key off please re-type\n\nthe part of your message\n\nwhich you\'ve at present got typed in all capitals.\n\nThanks!');
  }
  else if (re5.test(field.value))
  {
   alert('Sorry! You can\'t put web addresses or email addresses\nin any part of your submission\nother than in the box where you are asked\nto give your own email address.\nPlease correct this.');
  }
  else if (re6.test(field.value))
  {
   alert('You cannot use \"\&\" in your details. Please use \"and\".');
  }
 field.focus();
 error = true;
 }

function validateForm(form)
{
 var nameStatus = validateName(form.name);
 var addressStatus = validateAddress(form.residence);
 var emailStatus = validateEmail(form.email);
 var messageStatus = validateMessage(form.message);

  if (nameStatus&&addressStatus&&emailStatus&&messageStatus)
 {
  return confirm('Your booking enquiry is ready to send.\n\nClick CANCEL if you want to change your enquiry. \n\nClick OK to submit your message \n\t and then WAIT for confirmation!');

 }
 else
 {
  error = false;
  if (!nameStatus&&!error) errorName(form.name);
  if (!addressStatus&&!error) errorAddress(form.residence);
  if (!emailStatus&&!error) errorEmail(form.email);
  if (!messageStatus&&!error) errorMessage(form.message);
  return false;
 }
}

function validateName(field)
{
 if (field.value.length == 0)
 {
  return false;
 }
 else
 {
  var re = /[^ &\-a-zA-Z_0-9]/;
  return !re.test(field.value);
 }
}

function validateAddress(field)
{
 if (field.value.length == 0)
 {
  return false;
 }
 else
 {
  var re = /[^ &\-a-zA-Z_0-9]/;
  return !re.test(field.value);
 }
}

function validateEmail(field)
{
 if (field.value.length == 0)
 {
  return false;
 }
 else
 {
  var re = /^[a-zA-Z_0-9\-.]+@[a-zA-Z_0-9\-.]+\.[a-zA-Z_0-9\-.]+$/;
  return re.test(field.value);
 }
}

function validateMessage(field)
{
 if (field.value.length != 0)
   {
     return validate_begin(field);
   } else {
    return true;
  }
}

function errorName(field)
{
 if (field.value.length == 0)
 {
  alert("Please enter your full name.");
 }
 else
 {
  var re = /[^ &\-a-zA-Z_0-9]/;
  alert('Your name cannot contain a \"'+ re.exec(field.value)[0]+ '\" as one of the characters in it!');
 }
 field.focus();
 error = true;
}

function errorAddress(field)
{
 if (field.value.length == 0)
 {
  alert("Please enter your town and country.");
 }
 else
 {
  var re = /[^ &\-a-zA-Z_0-9]/;
  alert('Your place of residence cannot contain a \"'+ re.exec(field.value)[0]+ '\" as one of the characters in it!');
 }
 field.focus();
 error = true;
}

function errorEmail(field)
{
 if (field.value.length == 0)
 {
  alert('Please enter your email address.\n\t This will make it possible for us to reply.');
 }
 else
 {
  var re = /[^a-zA-Z_0-9\-.@]/;
  reResult = re.exec(field.value);
  if (reResult == null)
  {
   if (!/@/.exec(field.value)) alert('Your email address must contain\n\t an @ character!');
   else if (!/@.*\./.exec(field.value)) alert('Your email address must contain\n\t  a . character after the @!');
   else if (!/.+@/.exec(field.value)) alert('Your email address must contain\n\t  a useresidence before the @ character!');
   else if (!/@[^@]+\..+/.exec(field.value)) alert('Your email address must contain\n\t  a valid domain name after the @ character!');
  }
  else
  {
   alert('Your email address cannot contain a '+ reResult[0]+ ' character!');
  }
 }
 field.focus();
 error = true;
}

function errorMessage(field)
{
     return catch_error(field);
}

function errorDetails(field)
{
     return catch_error(field);
}

