﻿function Form1_Validator(theForm)
{

  if (theForm.Name.value == "")
  {
    alert("Please enter a value for the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  if (theForm.Name.value.length < 8)
  {
    alert("Please enter at least 8 characters in the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
  var checkStr = theForm.Name.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter, digit, whitespace and \"-\" characters in the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  if (theForm.Email.value == "")
  {
    alert("Please enter a value for the \"Email\" field.");
    theForm.Email.focus();
    return (false);
  }

  if (theForm.Email.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"Email\" field.");
    theForm.Email.focus();
    return (false);
  }

  if (theForm.Tel.value == "")
  {
    alert("Please enter a value for the \"Telephone number\" field.");
    theForm.Tel.focus();
    return (false);
  }

  if (theForm.Tel.value.length < 7)
  {
    alert("Please enter at least 7 characters in the \"Telephone number\" field.");
    theForm.Tel.focus();
    return (false);
  }

  if (theForm.Tel.value.length > 20)
  {
    alert("Please enter at most 20 characters in the \"Telephone number\" field.");
    theForm.Tel.focus();
    return (false);
  }

  var checkOK = "0123456789()-+- \t\r\n\f";
  var checkStr = theForm.Tel.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digit, whitespace and \"+-\" characters in the \"Telephone number\" field.");
    theForm.Tel.focus();
    return (false);
  }

  if (theForm.Mes.value == "")
  {
    alert("Please enter a value for the \"Message\" field.");
    theForm.Mes.focus();
    return (false);
  }

  if (theForm.Mes.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"Message\" field.");
    theForm.Mes.focus();
    return (false);
  }
  theForm.OK.value='YES';
  return (true);
}
