// goodEmail -- Tests an email address string for obvious formatting errors.
//
// Returns true if the string looks good, false otherwise.
//
// Arguments:
//            emStr - the string to be tested
//
// Conditions that will cause a "false" return:
//  - string ridiculously short
//  - invalid characters
//  - no "@" or "." or more than one "@"
//  - "@" or "." too close to the ends
//  - "@" or "." too close to each other or in the wrong order
//
// V1.0   05 Oct 1999   Initial release
// V1.1   24 Nov 1999   Add ampersand to list of valid characters
//
// William K. Walker
// wkwalker@nvdi.com
// 
function goodEmail(emStr)
{
  var testOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.@_%!&";
  var atIndex = 0;
  var dotIndex = 0;

  if (emStr.length < 6) return false;

  for (i = 0;  i < emStr.length;  i++)
  {
    ch = emStr.charAt(i);

    for (j = 0;  j < testOK.length;  j++)
      if (ch == testOK.charAt(j)) break;

    if (j == testOK.length) return false;
    if (ch=="@")
    {
      if ( (i == 0) || (atIndex != 0) ) return false;
      atIndex = i;
    }
    if (ch == ".") dotIndex = i;
  }

  if ( (atIndex == 0) || (dotIndex == 0) || ((i-dotIndex) < 3) || ((dotIndex-atIndex) < 2) ) return false;

  return true;
}


