//  onsubmit="return checkForm(this);"

var err;

function checkForm(formid)
{
  err = true;

  if (formid.name == 'cardform')
  {
    if (formid.sender.value.length < 1)
    {
      error('You haven\'t said who to send the card from.');
      formid.sender.focus();
    }
    else if (formid.s_email.value.length < 1)
    {
      error('You haven\'t specified the sender\'s email address.');
      formid.s_email.focus();
    }
    else if (formid.recipient.value.length < 1)
    {
      error('You haven\'t said who to send the card to.');
      formid.recipient.focus();
    }
    else if (formid.r_email.value.length < 1)
    {
      error('You haven\'t specified the recipient\'s email address.');
      formid.r_email.focus();
    }

    if (err && (formid.s_email.value.length > 0))
    {
      if (formid.s_email.value.length < 5)
      {
        error('The sender\'s address you have entered is not a valid email address.');
        formid.s_email.focus();
        formid.s_email.select();
      }
      else if ((formid.s_email.value.indexOf('@') == -1) || (formid.s_email.value.indexOf('.') == -1))
      {
        error('The sender\'s address you have entered is not a valid email address.');
        formid.s_email.focus();
        formid.s_email.select();
      }
      else if (formid.s_email.value.indexOf('@') > formid.s_email.value.lastIndexOf('.'))
      {
        error('The sender\'s address you have entered is not a valid email address.');
        formid.s_email.focus();
        formid.s_email.select();
      }
      else if (stringcontains(formid.s_email.value,'()[]<>;:," ') == true)
      {
        error('The sender\'s address you have entered is not a valid email address.');
        formid.s_email.focus();
        formid.s_email.select();
      }

    if (err && (formid.r_email.value.length > 0))
    {
      if (formid.r_email.value.length < 5)
      {
        error('The recipient\'s address you have entered is not a valid email address.');
        formid.r_email.focus();
        formid.r_email.select();
      }
      else if ((formid.r_email.value.indexOf('@') == -1) || (formid.r_email.value.indexOf('.') == -1))
      {
        error('The recipient\'s address you have entered is not a valid email address.');
        formid.r_email.focus();
        formid.r_email.select();
      }
      else if (formid.r_email.value.indexOf('@') > formid.r_email.value.lastIndexOf('.'))
      {
        error('The recipient\'s address you have entered is not a valid email address.');
        formid.r_email.focus();
        formid.r_email.select();
      }
      else if (stringcontains(formid.r_email.value,'()[]<>;:," ') == true)
      {
        error('The recipient\'s address you have entered is not a valid email address.');
        formid.r_email.focus();
        formid.r_email.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);
}

