//------------------------------------
// Contains:
//   AllowEmail6
//   BoxHasEmail6
//   ErrIfBadEmail6
// Needs:
//   BoxIsBlank6
//   IsAStandardKey
//------------------------------------

function AllowEmail6(keyEvent) {
   // This limits keyDowns to allow only characters that are valid for email address names.
   //
   // Params: none
   // Returns: If acceptable, allows character to appear.
   //          If not a valid email address character key, blocks the character from
   //          appearing.  No error message is shown.
   //
   // Works only on IE and Netscape.  Other browsers are blocked completely from
   // typing anything.
   //
   // Acceptable keys are:
   //    a..z      (all letters)
   //    0..9      (all numbers)
   //    @         (strudel)
   //    -         (dash)
   //    _         (underscore)
   //    .         (dot)
   //    All standard keys
   //
   // All other keys are blocked.
   //
   // To work properly (in both IE and NS), the following line should appear in the form's
   // onLoad function:
   //    document.formname.textboxname.onkeydown = AllowEmail;
   // where 'formname' and 'textboxname' must be customized.  Placing this line in the
   // form's onLoad function causes this assignment to take place only after the object
   // exists.
   //
   // This does not validate the final email address entered, it only checks individual
   // keystrokes.

   var ie = false;
   ie = (navigator.appName.indexOf('Microsoft') > -1);
   // Do it...
   if (!ie) { return true; }
   
   if (IsAStandardKey(event.keyCode)) { return true; }
   
   switch (event.keyCode) {
      case 95:  // [_] key!
      case 50:  // [@] key!
      case 189: // [-] key!
      case 109: // [-] key! (right side)
      case 190: // [.] key!
      case 110: // [.] key! (right side)
      case 8:   // [BACK] key!
      case 35:  // [END] key!
      case 46:  // [DELETE] key!
      case 36:  // [HOME] key!
      case 9:   // [TAB] key!
         return true;
      case 48:
      case 49:
      case 50:
      case 51:
      case 52:
      case 53:
      case 54:
      case 55:
      case 56:
      case 57: // is a number key!
         // Disallow symbols above number keys...
         if (event.shiftKey) { return false; }
         return true;
      case 96:
      case 97:
      case 98:
      case 99:
      case 100:
      case 101:
      case 102:
      case 103:
      case 104:
      case 105: // is a number key! (right side)
      case 65:
      case 66:
      case 67:
      case 68:
      case 69:
      case 70:
      case 71:
      case 72:
      case 73:
      case 74:
      case 75:
      case 76:
      case 77:
      case 78:
      case 79:
      case 80:
      case 81:
      case 82:
      case 83:
      case 84:
      case 85:
      case 86:
      case 87:
      case 88:
      case 89:
      case 90: // is a letter key!
         return true;
   }
   // Every other key is junk...
   return false;
}


function ErrIfBadEmail6(inEmail) {
   // This verifies that the text passed in is a grammatically correct email address.
   //
   // That is, it does NOT actually test the email to ensure that it works, it merely tests
   // for improper characters, etc.
   //
   // Params: inEmail: text string of email address to be checked.
   //
   // Returns: Error Message (translates as true) or false.
   //
   // Things that will fail include:
   //    blank string
   //    email contains 2 strudels (@ signs)
   //    email has no strudel
   //    strudel is 1st character in email (no username)
   //    email has no dot (period -- means no domain extension was specified)
   //    email has 2 dots after the strudel (confused extensions)
   //    dot appears in last, or next-to-last position in email (no extension)
   //    email contains characters other than: abcdefghijklmnopqrstuvwxyz0123456789@.-_

   // VARIABLES...
   var strudel1; // where the @ symbol appears
   var strudel2; // hopefully is none, can't have 2 strudels
   var dot1;     // where the . symbol appears (should be only one)
   var goodChars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_"; // acceptable characters
   var OneLetter;

   // FIND LOCATIONS...
   strudel1 = inEmail.indexOf('@');
   strudel2 = inEmail.indexOf('@', strudel1 + 1);
   dot1 = inEmail.indexOf('.', strudel1); // takes 1st one AFTER the @ sign

   // CHECK DATA...
   if (inEmail == '') {
      return 'Email address is blank.';
   } else if (strudel2 > -1) {
      return 'Invalid email address!\n\nEmail address may not contain more than one strudel (@) symbol.';
   } else if (strudel1 == -1) {
      return 'Invalid email address!\n\nEmail address must include a strudel (@) symbol.';
   } else if (strudel1 == 0) {
      return 'Invalid email address!\n\nEmail address must contain a UserName.\n\nEx: username@hostname.ext';
   } else if (dot1 == -1) {
      return 'Invalid email address!\n\nEmail address requires a dot1 (.) separating the hostname from the extension.\n\nEx: username@hostname.extension';
   } else if (dot1 == strudel1 + 1) {
      return 'Invalid email address!\n\nEmail address requires a hostname.\n\nEx: username@hostname.ext';
   } else if (dot1 >= inEmail.length - 2) {
      return 'Invalid email address!\n\nEmail address requires a proper extension.\n\nEx: username@hostname.ext';
   } else {
      // CHECK FOR INVALID CHARACTERS...
      for (var xx = 0; xx < inEmail.length; xx++) {
         OneLetter = inEmail.charAt(xx).toLowerCase();
         if (goodChars.indexOf(OneLetter) == -1) {
            return 'Invalid email address!\n\nCharacter in position #' + (xx + 1) + ' (' + OneLetter + ') is invalid.';
         }
      }
   }

   // If no error raised, then email is good, and this function is false...
   return false;
}

function BoxHasEmail6(inBox, isRequired) {
   // This verifies that the box passed in contains a grammatically correct email address.
   //
   // This merely takes in the box object and passes it's string value along to the IsEmail
   // function that checks it.  This displays any error message returned.
   //
   // Params: inBox:      html text object that is to be tested.
   //         isRequired: Boolean determines if blank box is acceptable.
   //
   // Returns: True/False
   //
   // If the box is blank and not required, it is ignored and considered valid.

   var ErrMsg;

   if (BoxIsBlank6(inBox, isRequired)) {
      return !isRequired;
   }
   
   ErrMsg = ErrIfBadEmail6(inBox.value);

   if (ErrMsg) {
      alert(ErrMsg);
      inBox.focus();
      inBox.select();
   }
   
   return !ErrMsg;
}
