/*
http://simonwillison.net/2004/May/26/addLoadEvent/
*/
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  }
  else
  {
    window.onload = function()
    {
      if (oldonload)
      {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(form_setup);

function form_setup()
{
  var elObj;
  if (!document.getElementById)
  {
    return;
  }
  if (elObj = document.getElementById("footer"))
  {
    if (elObj = document.getElementById("Submit"))
    {
      elObj.onclick = function () { return checkForm(this); }
    }
  }
  else
  {
    setTimeout("form_setup()",10);
  }
}


var err;

function checkForm(sendid)
{
  err = true;
  formid = sendid.form;

  if (formid.name == 'contactform')
  {
    if (formid.enquiry.value.length < 1)
    {
      error('You haven\'t made any comment.');
      formid.enquiry.focus();
      formid.enquiry.select();
    }
    else if (formid.contactname.value.length < 1)
    {
      error('You haven\'t provided a contact name.');
      formid.contactname.focus();
      formid.contactname.select();
    }
    else if ((formid.email_address.value.length < 1) && (formid.phone_number.value.length < 1))
    {
      error('You haven\'t provided any contact details.');
      formid.email_address.focus();
      formid.email_address.select();
    }

    if (err)
    {
/*
      if (formid.email_address.value.length < 5)
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else 
*/
      if (formid.email_address.value.length > 0)
      {
        if ((formid.email_address.value.indexOf('@') == -1) || (formid.email_address.value.indexOf('.') == -1))
        {
          error('The email address you have entered is not a valid email address.');
          formid.email_address.focus();
          formid.email_address.select();
        }
        else if (stringcontains(formid.email_address.value,'()[]<>;:," ') == true)
        {
          error('The email address you have entered is not a valid email address.');
          formid.email_address.focus();
          formid.email_address.select();
        }
      }
    }
  }

  if (err)
  {
    err = window.confirm('Are you sure you have entered everything correctly?');
  }

  return err;
}

function stringcontains(haystack,needles)
{
  var result;
  result = false;

  for (i=0;i<needles.length;i++)
  {
    if (haystack.indexOf(needles.charAt(i))>0)
    {
      result = true;
    }
  }

  return result;
}

function error(txt)
{
  err = false;
  window.alert(txt);
}
