﻿var ffx;

ffx = false;        // Initialized to the form object when the page loads

function FormSetup()
{
  ffx = document.forms[0];
}


function checkEmailSyntax(em)
{
  var t_split, t_name, t_domain, t_result;
  var re_bad = /(@)|(\.\.)|(^\.)|(\.$)/;
  var re_ok1 = /^[^\x00-\x1f <>"]+$/;
  var re_ok2 = /^([A-Za-z0-9\-_]+\.){1,5}[A-Za-z]{2,5}$/;
  
  t_split = em.split("@");
  t_name = t_split[0];
  t_domain = t_split[1];
  
  t_result = false;
  
  if (t_name && t_domain && (t_split.length == 2)) {
    if (!re_bad.test(t_name)   &&
        re_ok1.test(t_name)    &&
        !re_bad.test(t_domain) &&
        re_ok2.test(t_domain)  ) {
      t_result = true;
    }
  }
  return t_result;
}


function CheckEmailBox(tb)
{
  if (checkEmailSyntax(tb.value) == false) {
    alert("The email address you entered is invalid. Please enter a valid address, or leave this field blank.");
    tb.focus();
    tb.select();
  }
}

function CheckNameBox(tb)
{
  var t;
  
  t = tb.value;
  
  if (t.length < 2) {
    alert("Please enter your full name.");
    tb.focus();
    tb.select();
  }
}


function CheckMiniForm()
{
  var haveName, haveEmail, havePhone, haveMessage;
  
  if (ffx.email.value.length != 0) {
    if (checkEmailSyntax(ffx.email.value) == false) {
      CheckEmailBox(ffx.email);
      return false;
    }
  }
  
  haveName = (ffx.full_name.value.length > 1);
  haveEmail = (ffx.email.value.length > 1);
  havePhone = (ffx.phone.value.length > 5);
  haveMessage = (ffx.message.value.length > 1);
  
  if (!haveName) {
    alert("Please enter your name and contact details before submitting this form.");
    ffx.full_name.focus();
    ffx.full_name.select();
    return false;
  }
  
  if (!havePhone && !haveEmail) {
    alert("Please enter your phone number and/or email address before submitting this form.");
    ffx.phone.focus();
    ffx.phone.select();
    return false;
  }
  
  if (!haveMessage) {
    alert("Don't forget to type in your message first!");
    ffx.message.focus();
    ffx.message.select();
    return false;
  }
  
  return true;
}


function LTrim(str)
{
  for (var k=0; k < str.length && str.charAt(k) <= " "; k++);
  return str.substring(k,str.length);
}

function RTrim(str)
{
  for (var j=str.length-1; j>=0 && str.charAt(j)<=" "; j--);
  return str.substring(0,j+1);
}

function Trim(str)
{
  return LTrim(RTrim(str));
}

function v() {
  var a = 'end an em';
  a = 'Click to s'+a;
  a += 'ail';
  window.status = a;
  return true;
}

function w() {
  window.status=window.defaultStatus;
  return true;
}

function x(e) {
  var r='';
  var c;
  for (var i=0; i<e.length; i++) {
    c = e.charCodeAt(i);
    r += String.fromCharCode(c&0xe0|(c&0x1c)>>2|(c&0x03)<<3);
  }
  if (confirm('Send an e'+r.substr(0,4)+' '+r.substr(4,2)+' '+r.substr(7)+'?')) location.href=r;
  return false;
}

function y() {
  document.write('\</A\>');
}

function z(e) {
  document.write('\<A href="/" onclick="return x(\''+e+'\');"');
  document.write(' onmouseover="return v();" onmouseout="return w();"');
  document.write(' onfocus="return v();" onblur="return w();"\>');
}
