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|a(\W){0,3}r(\W){0,3}s(\W){0,3}e|l(\W){0,3}u(\W){0,3}c(\W){0,3}i(\W){0,3}f(\W){0,3}e(\W){0,3}r|s(\W){0,3}a(\W){0,3}t(\W){0,3}a(\W){0,3}n|h(\W){0,3}a(\W){0,3}i(\W){0,3}l|b(\W){0,3}o(\W){0,3}n(\W){0,3}e(\W){0,3}r|w(\W){0,3}i(\W){0,3}c(\W){0,3}k(\W){0,3}e(\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|a(\W){0,3}r(\W){0,3}s(\W){0,3}e/i;
  var re3 = />|<font/i;
  var re4 = /[a-z]/;
  var re5 = /http:|www\.|\.org|\.com|\.co\.uk|\.net/i;
  var re6 = /[&]/i;
  var re7 = /l(\W){0,3}u(\W){0,3}c(\W){0,3}i(\W){0,3}f(\W){0,3}e(\W){0,3}r|s(\W){0,3}a(\W){0,3}t(\W){0,3}a(\W){0,3}n|b(\W){0,3}o(\W){0,3}n(\W){0,3}e(\W){0,3}r|w(\W){0,3}i(\W){0,3}c(\W){0,3}k(\W){0,3}e(\W){0,3}d/i;
  var re8 = /h(\W){0,3}a(\W){0,3}i(\W){0,3}l/i;

  if (re.test(field.value))
  {
   alert('This message cannot be sent! 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 message 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! Either you haven\'t entered information in all the boxes \nor else you\'ve tried to send part of the submission in all capital letters.\nSteps to take: Start your submission from the first input box (i.e. Your name),\nfill in all the boxes \nand ensure no part of your submission is in all capital letters.\nFor example, HELLO! is not allowed but Hello! is. \nThanks.');
  }
  else if (re5.test(field.value))
  {
   alert('Sorry! You can\'t put web addresses or email addresses\nin any part of your message\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\".');
  }
  else if (re7.test(field.value))
  {
   alert('This message cannot be sent. It appears to relate to a topic about which we aren\'t accepting submissions.');
  }
  else if (re8.test(field.value))
  {
   alert('Sorry. Submission from you are not being accepted.');
  }
 field.focus();
 error = true;
 }

function validateRequestForm(form)
{
 var titleStatus = validateTitle(form.residence);
 var descriptionStatus = validateDescription(form.message);
 var submitterStatus = validateSubmitter(form.submitter);
 var emailStatus = validateEmail(form.email);

  if (submitterStatus&&emailStatus&&titleStatus&&descriptionStatus)
 {
  return confirm('Your message is ready to send.\n\nClick CANCEL if you want to alter your message. \n\nClick OK to send your message \n\nThen await response.');

 }
 else
 {
  error = false;
  if (!submitterStatus&&!error) errorSubmitter(form.submitter);
  if (!emailStatus&&!error) errorEmail(form.email);
  if (!titleStatus&&!error) errorTitle(form.residence);
  if (!descriptionStatus&&!error) errorDescription(form.message);
  return false;
 }
}

function validateResponseForm(form)
{
 var titleStatus = validateTitle(form.respondertitle);
 var descriptionStatus = validateDescription(form.responderopening);
 var submitterStatus = validateSubmitter(form.responder);
 var emailStatus = validateEmail(form.responderemail);

  if (submitterStatus&&emailStatus&&titleStatus&&descriptionStatus)
 {
  return confirm('Your message is ready to send.\n\nClick CANCEL if you want to change your message. \n\nClick OK to send your message \n\t and then WAIT for details! \n\n(Sorry: Submissions avoiding the maximum length \nby pasting in text will be removed.)');

 }
 else
 {
  error = false;
  if (!submitterStatus&&!error) errorSubmitter(form.responder);
  if (!emailStatus&&!error) errorEmail(form.responderemail);
  if (!titleStatus&&!error) errorTitle(form.respondertitle);
  if (!descriptionStatus&&!error) errorDescription(form.responderopening);
  return false;
 }
}
function validateJoinForm(form)
{
 var submitterStatus = validateSubmitter(form.subscribername);
 var emailStatus = validateEmail(form.address);

  if (submitterStatus&&emailStatus)
 {
  return confirm('Your message is ready to send.\n\nClick CANCEL if you want to change your details. \n\nClick OK to send your message \n\t and then WAIT for details!');

 }
 else
 {
  error = false;
  if (!submitterStatus&&!error) errorSubmitter(form.subscribername);
  if (!emailStatus&&!error) errorEmail(form.address);
  return false;
 }
}

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

function validateDescription(field)
{
  if (field.value.length == 0)
    {
      return false;
    }
  else
    {
      return validate_begin(field);
    } 
}

function validateSubmitter(field)
{
 if (field.value.length == 0)
 {
  return false;
 }
 else
 {
  var re = /[^ .,\-a-zA-Z]/;
  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\-.]+\.[a-zA-Z_0-9\-.]+$/;
  return re.test(field.value);
 }
}

function errorTitle(field)
{
  if (field.value.length == 0)
    {
      alert("Please enter your residence - where you are at present.");
    }
  else
  {
    var re = /[^ \'.,!@\-a-zA-Z_0-9]/;
    alert('What you type as your town and country cannot contain a \"'+ re.exec(field.value)[0]+ '\" as one of the characters in it!');
  }
  field.focus();
  error = true;
}

function errorDescription(field)
{
  if (field.value.length == 0)
    {
      alert('Please enter in your message details - what you want to communicate.');
    }
  else
    {
      return catch_error(field);
    }
}

function errorSubmitter(field)
{
 if (field.value.length == 0)
 {
  alert("Please enter your name. \n\nYou may use an online name if you wish.");
 }
 else
 {
  var re = /[^ .,\-a-zA-Z]/;
  alert('Your name cannot contain a \"'+ re.exec(field.value)[0]+ '\" as one of the characters in it!');
 }
 field.focus();
 error = true;
}

function errorEmail(field)
{
  var nodeObj2 = document.getElementById('title');
 if (field.value.length == 0)
 {
  alert('Please enter your email address.\n\t This will not be displayed.\n\t It will only be for our reference.');
 }
 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!');
   nodeObj2.focus();
   return true;
  }
 }
 field.focus();
 error = true;
}
