function isValidEmail(sEmailAddress)
{
 var bValid = true;
 var nAtIndex = sEmailAddress.indexOf('@');

 if (nAtIndex == -1)
 {
  bValid = false;
 }
 else
 {
  var sLocal     = sEmailAddress.substring(0, nAtIndex);
  var sDomain    = sEmailAddress.substring(nAtIndex + 1);
  var nLocalLen  = sLocal.length;
  var nDomainLen = sDomain.length;
  var sLocal2    = sLocal.replace(/\\\\/, "");

  if ((nLocalLen < 1) || (nLocalLen > 64))
  {
   /*
    * Local part length exceeded
    */
   bValid = false;
  }
  else if ((nDomainLen < 1) || (nDomainLen > 255))
  {
   /*
    * Domain part length exceeded
    */
   bValid = false;
  }
  else if ((sLocal.substring(0, 1) == '.') || (sLocal.substring(-1, 1) == '.'))
  {
   /*
    * Local part starts or ends with a '.'
    */
   bValid = false;
  }
  else if (sLocal.match(/\.\./))
  {
   /*
    * Local part has two consecutive dots
    */
   bValid = false;
  }
  else if (!sDomain.match(/^[A-Za-z0-9\-\.]+$/))
  {
   /*
    * Character not valid in domain part
    */
   bValid = false;
  }
  else if (sDomain.match(/\.\./))
  {
   /*
    * Domain part has two consecutive dots
    */
   bValid = false;
  }
  else if (!sDomain.match(/[A-Za-z0-9]\.[A-Za-z]{2,}$/))
  {
   /*
    * Domain part does not have letter/number followed by a dot and then 2 or
    * more letters
    */
   bValid = false;
  }
  else if (!sLocal2.match(/^(\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/))
  {
   /*
    * Character not valid in local part unless local part is quoted
    */
   if (!sLocal2.match(/^"(\\"|[^"])+"$/))
   {
    bValid = false;
   }
  }
 }

 return bValid;
}

function isValidMobile(sMobile)
{
 var bValid = true;
 var sTemp  = sMobile.replace(/ /g, "");

 if (!sTemp.match(/07[0-9]{9}/))
 {
  bValid = false;
 }

 return bValid;
}

function ltrim(stringToTrim)
{
 return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim)
{
 return stringToTrim.replace(/\s+$/,"");
}

function trim(stringToTrim)
{
 return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validateContactForm()
{
 /*
  * Ensure that all mandatory fields have been completed
  */
 var aMessages = [];
 var hForm     = document.forms['contact'];

 /*
  * Clear the highlight on the mandatory fields
  */
 hForm.email.className   = "normal";
 hForm.message.className = "normal";

 /*
  * A message must be supplied
  */
 if (trim(hForm.message.value) == "")
 {
  aMessages.push("You must enter a message");
  hForm.message.className = "mandatory";
  hForm.message.focus();
 }

 /*
  * A valid e-mail address must be supplied
  */
 if (trim(hForm.email.value) == "")
 {
  aMessages.push("You must enter an e-mail address");
  hForm.email.className = "mandatory";
  hForm.email.focus();
 }
 else if (!isValidEmail(hForm.email.value))
 {
  aMessages.push("You must enter a valid e-mail address");
  hForm.email.className = "mandatory";
  hForm.email.focus();
 }

 if (aMessages.length == 0)
 {
  hForm.submit();
 }
 else
 {
  alert("The following errors occurred when trying to submit your form:\n\n" + aMessages.join("\n\n"));
 }

 return false;
}

function validateSubscribeForm()
{
 /*
  * Ensure that all mandatory fields have been completed
  */
 var bComplete = true;
 var sMessage  = "You must complete all mandatory fields";
 var hForm     = document.forms['subscribe'];

 hForm.email.className  = "normal";
 hForm.mobile.className = "normal";

 /*
  * One or both of E-mail address and Mobile no. must be supplied
  */
 if ((trim(hForm.email.value)  == "") &&
     (trim(hForm.mobile.value) == ""))
 {
  sMessage = "You must enter an e-mail address and/or mobile no.";

  hForm.email.className  = "mandatory";
  hForm.email.focus();
  hForm.mobile.className = "mandatory";
  bComplete = false;
 }
 else
 {
  /*
   * We have at least an e-mail address or mobile no. - now validate them
   */
  if (trim(hForm.email.value) != "")
  {
   if (!isValidEmail(hForm.email.value))
   {
    sMessage = "You must enter a valid e-mail address";

    hForm.email.className = "mandatory";
    hForm.email.focus();
    bComplete = false;
   }
  }

  if (hForm.mobile.value != "")
  {
   if (!isValidMobile(hForm.mobile.value))
   {
    sMessage = "You must enter a valid mobile no.";

    hForm.mobile.className = "mandatory";
    hForm.mobile.focus();
    bComplete = false;
   }
  }
 }

 if (bComplete)
 {
  hForm.submit();
 }
 else
 {
  alert(sMessage);
 }

 return false;
}

function validateUnsubscribeForm()
{
 /*
  * Ensure that all mandatory fields have been completed
  */
 var bComplete = true;
 var sMessage  = "You must complete all mandatory fields";
 var hForm     = document.forms['unsubscribe'];

 hForm.email.className  = "normal";
 hForm.mobile.className = "normal";

 /*
  * One or both of E-mail address and Mobile no. must be supplied
  */
 if ((trim(hForm.email.value)  == "") &&
     (trim(hForm.mobile.value) == ""))
 {
  sMessage = "You must enter an e-mail address and/or mobile no.";

  hForm.email.className  = "mandatory";
  hForm.email.focus();
  hForm.mobile.className = "mandatory";
  bComplete = false;
 }
 else
 {
  /*
   * We have at least an e-mail address or mobile no. - now validate them
   */
  if (trim(hForm.email.value) != "")
  {
   if (!isValidEmail(hForm.email.value))
   {
    sMessage = "You must enter a valid e-mail address";

    hForm.email.className = "mandatory";
    hForm.email.focus();
    bComplete = false;
   }
  }

  if (hForm.mobile.value != "")
  {
   if (!isValidMobile(hForm.mobile.value))
   {
    sMessage = "You must enter a valid mobile no.";

    hForm.mobile.className = "mandatory";
    hForm.mobile.focus();
    bComplete = false;
   }
  }
 }

 if (bComplete)
 {
  hForm.submit();
 }
 else
 {
  alert(sMessage);
 }

 return false;
}
