<!--
// javascript functions for client-side form validation
// functions include:
//    check_email
//    check_www
//    check_number
//    check_password
//    check_username
//    check_groupname

  var NUMBER_GENERAL = 0;
  var NUMBER_INTEGER = 1;
  var NUMBER_POS_FLOAT = 2;
  var NUMBER_POS_INTEGER = 3;
  var NUMBER_CURRENCY = 4;

  function check_number(oField, type, bRequired)
  {
    var sError, sType;
    var regInvalidNumber;
    var regNumber;

    switch(type)
    {
      case 0:	//NUMBER_GENERAL:
        // signed or unsigned floating point number or
        // signed or unsigned integer
        regInvalidNumber = new RegExp("([^0-9.eE+-])", "g");
        regNumber = new RegExp("^-?[0-9]+\.?[0-9]*([eE][+-]?[0-9]+)?$", "g");
        sType = 'number';
        break;
      case 1:	//NUMBER_INTEGER:
        // signed or unsigned integer
        regInvalidNumber = new RegExp("([^0-9-])", "g");
        regNumber = new RegExp("^-?[0-9]+$", "g");
        sType = 'whole number';
        break;
      case 2:	//NUMBER_POS_FLOAT:
        // unsigned floating point number
        regInvalidNumber = new RegExp("([^0-9.eE+])", "g");
        regNumber = new RegExp("^[0-9]+\.?[0-9]*([eE][+-]?[0-9]+)?$", "g");
        sType = 'positive number';
        break;
      case 3:	//NUMBER_POS_INTEGER:
        // unsigned integer
        regInvalidNumber = new RegExp("([^0-9])", "g");
        regNumber = new RegExp("^[0-9]+$", "g");
        sType = 'positive whole number';
        break;
      case 4:	//NUMBER_CURRENCY:
        // currency
        regInvalidNumber = new RegExp("([^0-9$.-])", "g");
        regNumber = new RegExp("^-?[$]?[0-9]*\.[0-9]{2}$|^-?[$]?[0-9]+$", "g");
        sType = 'currency';
        oField.value = oField.value.replace(/[$|¢]/g,'');
        break;
      default:
        // signed or unsigned floating point number or
        // signed or unsigned integer
        regInvalidNumber = new RegExp("([^0-9.eE+-])", "g");
        regNumber = new RegExp("^-?[0-9]+\.?[0-9]*([eE][+-]?[0-9]+)?$", "g");
        sType = 'number';
        break;
    }

    // Check the Number
    if(bRequired && oField.value.length < 1)
    {
        sError = 
          'Please enter a valid ' + sType + '.';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
    }
    else if(oField.value.length > 0)
    {
      if(regInvalidNumber.test(oField.value))
      {
        sError = 
          '"' + oField.value + '" is not a valid ' + sType + '.\n' +
          'It contains an illegal character - ' + RegExp.$1 + '\n' +
          'Please enter a valid ' + sType + '.';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
      }
      else if(! regNumber.test(oField.value))
      {
        sError = 
          '"' + oField.value + '" is not a valid ' + sType + '.\n' +
          'Please enter a valid ' + sType + '.';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
      }
    }
    return true;
  }

  function check_text(oField, sFieldDisplayName, nMinLength, nMaxLength)
  {
    var sError;
    nMinLength = parseInt(nMinLength);
    nMaxLength = parseInt(nMaxLength);

    // Check the minimum length for the text
    if(!isNaN(nMinLength) && oField.value.length < nMinLength)
    {
      if(nMinLength == 1)
      {
        sError = 
          sFieldDisplayName + ' is required.' +
          '\nPlease enter a value.';
      }
      else
      {
        sError = 
          sFieldDisplayName + ' must have at least ' + nMinLength + ' characters.' +
          '\nThe value you entered has ' + oField.value.length + ' characters.';
      }
      oField.style.border = '2px solid red';
      alert(sError);
      oField.focus();
      return false;
    }

    // Check the maximum length for the text
    if(!isNaN(nMaxLength) && oField.value.length > nMaxLength)
    {
      sError = 
        sFieldDisplayName + ' must have no more than ' + nMaxLength + ' characters.' +
        '\nThe value you entered has ' + oField.value.length + ' characters.';
      oField.style.border = '2px solid red';
      alert(sError);
      oField.focus();
      return false;
    }
    return true;
  }

  function check_email(oField, bRequired)
  {
    var sError;
    var regInvalid = new RegExp("([^a-zA-Z0-9_@.-])", "g");
    var regEmail = new RegExp("[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+[.][a-zA-Z0-9_.-]+", "g");

    // Check the E-Mail address
    if(bRequired && oField.value.length < 1)
    {
        sError = 
          'The e-mail address is required.\n' + 
          'Please enter a valid e-mail address ' + 
          'in the format:\nname@server.something\n';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
    }
    else if(oField.value.length > 0)
    {
      if(regInvalid.test(oField.value))
      {
        sError = 
          'The e-mail address "' + oField.value +
          '" has an illegal character "' + RegExp.$1 + '".\n' +
          'The characters can be a-z, A-Z, ., or _ with one @.\n' +
          'Please re-enter the e-mail address.';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
      }
      else if(! regEmail.test(oField.value))
      {
        sError = 
          'The e-mail address "' + oField.value +
          '" is not in a valid format.\n' +
          'The address must be of the format:\nname@server.something\n' +
          'Please re-enter the e-mail address.';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
      }
    }
    return true;
  }

  function check_www(oField, bRequired)
  {
    var sError;
    var regWWW = new RegExp("^http[s]?[:][/][/].*", "g");

    // Check the web address
    if(bRequired && oField.value.length < 1)
    {
        sError = 
          'The web address is required.\n' + 
          'Please enter a valid web address ' + 
          'that starts with:\nhttp://\n';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
    }
    else if(oField.value.length > 0)
    {
      if(!regWWW.test(oField.value))
      {
        sError = 
          'The web address "' + oField.value +
          '" is not in a valid format.\n' +
          'The address must start with \nhttp://\n' +
          'Please re-enter the web address.';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
      }
    }
    return true;
  }

  function check_phone(oField, sFieldDisplayName, bRequired)
  {
    var sError;
    var regInvalid = new RegExp("([^0-9(). -])", "g");
    if(!sFieldDisplayName)
    {
      sFieldDisplayName = 'The phone number';
    }

    // Check the Phone number
    if(bRequired && oField.value.length < 1)
    {
        sError = 
          sFieldDisplayName + ' is required.\n' + 
          'Please enter a valid phone number ' + 
          'in the format:\n555-555-5555 or (555) 555-5555\n';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
    }
    else if(oField.value.length > 0)
    {
      if(regInvalid.test(oField.value))
      {
        sError = 
          'The value "' + oField.value + '" for ' + sFieldDisplayName +
          ' has an illegal character "' + RegExp.$1 + '".\n' +
          'Please enter a valid phone number ' + 
          'in the format:\n  222-555-5555 or (222) 555-5555\n';
        oField.style.border = '2px solid red';
        alert(sError);
        oField.focus();
        return false;
      }
    }
    return true;
  }

  function check_password(newPass, confirmPass, requiredCharacterCategoryCount, minLength, maxLength)
  {
    var regLower = new RegExp("[a-z]", "g");
    var regUpper = new RegExp("[A-Z]", "g");
    var regDigits = new RegExp("[0-9]", "g");
    var regPunct = new RegExp("[`~!@#$%^&*()_+|;:,.<>? ]", "g");
    // The following regular expression looks for characters that are illegal
    // in the password
    // (more characters are legal than what's in the 4 categories)
    var regIllegal = new RegExp("([^\x20-\x7F]|['])", "g");
    var sError;
    
    var categoryCount = 0;
    if(!requiredCharacterCategoryCount)
    {
      requiredCharacterCategoryCount = 3;
    }
    if(!minLength)
    {
      minLength = 6;
    }
    if(!maxLength)
    {
      maxLength = 18;
    }
    
    if(newPass.value!=confirmPass.value)
    {
      sError = 'The new password and the confirmation don\'t match';
      newPass.style.border = '2px solid red';
      alert(sError);
      newPass.focus();
      newPass.select();
      return false;
    }
    else if(newPass.value.length < minLength)
    {
      sError = 'The new password should be at least ' + minLength + ' characters.';
      newPass.style.border = '2px solid red';
      alert(sError);
      newPass.focus();
      newPass.select();
      return false;
    }
    else if(newPass.value.length > maxLength)
    {
      sError = 'The new password should be no more than ' + maxLength + ' characters.';
      newPass.style.border = '2px solid red';
      alert(sError);
      newPass.focus();
      newPass.select();
      return false;
    }
    else if(regIllegal.test(newPass.value))
    {
      sError = 'The new password has an illegal character - ' + RegExp.$1;
      newPass.style.border = '2px solid red';
      alert(sError);
      newPass.focus();
      newPass.select();
      return false;
    }
    
    if(regLower.test(newPass.value))
    {
      categoryCount++;
    }
    if(regUpper.test(newPass.value))
    {
      categoryCount++;
    }
    if(regDigits.test(newPass.value))
    {
      categoryCount++;
    }
    if(regPunct.test(newPass.value))
    {
      categoryCount++;
    }

    if(categoryCount < requiredCharacterCategoryCount)
    {
      sError = 
        'The new password has characters from only ' + categoryCount + ' of the 4 sets.' +
        '\n\nPlease use characters from at least ' + requiredCharacterCategoryCount + ' of the following 4 sets:' +
        '\n 1. Lower case alphabetic characters a-z' +
        '\n 2. Upper case alphabetic characters A-Z' +
        '\n 3. Digits 0-9' +
        '\n 4. The following characters: `~!@#$%^&*()_+|;:,.<>?';

      newPass.style.border = '2px solid red';
      alert(sError);
      newPass.focus();
      newPass.select();
      return false;
    }
  
    return true;
  }

  function check_username(Username)
  {
    // The following regular expression looks for characters that aren't in the list
    //  (which are considered illegal for a username)
    var regIllegal = new RegExp("([^a-zA-Z0-9`~!@#$%^&*()_+|;:,.<>? ])", "g");
    var sError;
    
    var nMinLength,nMaxLength;
    nMinLength = 3;
    nMaxLength = 18;
    
    if(Username.value.length < nMinLength)
    {
      sError = 'The new username should be at least ' + nMinLength + ' characters.';
      Username.style.border = '2px solid red';
      alert(sError);
      Username.focus();
      Username.select();
      return false;
    }
    else if(Username.value.length > nMaxLength)
    {
      sError = 'The new username should be no more than ' + nMaxLength + ' characters.';
      Username.style.border = '2px solid red';
      alert(sError);
      Username.focus();
      Username.select();
      return false;
    }
    else if(regIllegal.test(Username.value))
    {
      sError = 'The new username has an illegal character - ' + RegExp.$1;
      Username.style.border = '2px solid red';
      alert(sError);
      Username.focus();
      Username.select();
      return false;
    }

    return true;
  }

  function check_groupname(Groupname, sDelim)
  {
    // The following regular expression looks for characters that aren't in the list
    //  (which are considered illegal for a Groupname)
    var sIllegal = eval('"([^a-zA-Z0-9`~!@#$%^&*()_|;,<>? ]|[' + sDelim + '])"');
    var regIllegal = new RegExp(sIllegal, "g");
    var sError;
    
    var nMinLength,nMaxLength;
    nMinLength = 3;
    nMaxLength = 18;
    
    if(Groupname.value.length < nMinLength)
    {
      sError = 'The new Groupname should be at least ' + nMinLength + ' characters.';
      Groupname.style.border = '2px solid red';
      alert(sError);
      Groupname.focus();
      Groupname.select();
      return false;
    }
    else if(Groupname.value.length > nMaxLength)
    {
      sError = 'The new Groupname should be no more than ' + nMaxLength + ' characters.';
      Groupname.style.border = '2px solid red';
      alert(sError);
      Groupname.focus();
      Groupname.select();
      return false;
    }
    else if(regIllegal.test(Groupname.value))
    {
      sError = 'The new Groupname has an illegal character - ' + RegExp.$1;
      Groupname.style.border = '2px solid red';
      alert(sError);
      Groupname.focus();
      Groupname.select();
      return false;
    }

    return true;
  }

  function check_selectoption(oSelect, sFieldDisplayName)
  {
    var sError;

    if(oSelect.selectedIndex < 0 || oSelect.options[oSelect.selectedIndex].value.length < 1)
    {
      sError = 
        'Please select a valid choice for ' + sFieldDisplayName;
      oSelect.style.border = '2px solid red';
      alert(sError);
      oSelect.focus();
      return false;
    }

    return true;
  }

// From "FormChek.js" by Netscape

/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()

/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()




/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()





/*  ================================================================
    FUNCTION:  isAmericanExpress()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.
		    
	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()




/*  ================================================================
    FUNCTION:  isDinersClub()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Diner's
		    Club number.
		    
	      false, otherwise

    Sample number: 30000000000004 (14 digits)
    ================================================================ */

function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}



/*  ================================================================
    FUNCTION:  isCarteBlanche()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Carte
		    Blanche number.
		    
	      false, otherwise
    ================================================================ */

function isCarteBlanche(cc)
{
  return isDinersClub(cc);
}




/*  ================================================================
    FUNCTION:  isDiscover()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Discover
		    card number.
		    
	      false, otherwise

    Sample number: 6011000000000004 (16 digits)
    ================================================================ */

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()





/*  ================================================================
    FUNCTION:  isEnRoute()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid enRoute
		    card number.
		    
	      false, otherwise

    Sample number: 201400000000009 (15 digits)
    ================================================================ */

function isEnRoute(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
  return false;
}



/*  ================================================================
    FUNCTION:  isJCB()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid JCB
		    card number.
		    
	      false, otherwise
    ================================================================ */

function isJCB(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) &&
      ((first4digs == "3088") ||
       (first4digs == "3096") ||
       (first4digs == "3112") ||
       (first4digs == "3158") ||
       (first4digs == "3337") ||
       (first4digs == "3528")))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isJCB()



/*  ================================================================
    FUNCTION:  isAnyCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is any valid credit
		    card number for any of the accepted card types.
		    
	      false, otherwise
    ================================================================ */

function isAnyCard(cc)
{
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
    return false;
  }
  return true;

} // END FUNCTION isAnyCard()


/*  ================================================================
    FUNCTION:  isCardMatch()
 
    INPUT:    cardType - a string representing the credit card type
	      cardNumber - a string representing a credit card number

    RETURNS:  true, if the credit card number is valid for the particular
	      credit card type given in "cardType".
		    
	      false, otherwise
    ================================================================ */

function isCardMatch (cardType, cardNumber)
{

	cardType = cardType.toUpperCase();
	var doesMatch = true;

	if ((cardType == "VISA") && (!isVisa(cardNumber)))
		doesMatch = false;
	if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))
		doesMatch = false;
	if ( ( (cardType == "AMERICANEXPRESS") || (cardType == "AMEX") )
                && (!isAmericanExpress(cardNumber))) doesMatch = false;
	if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
		doesMatch = false;
	if ((cardType == "JCB") && (!isJCB(cardNumber)))
		doesMatch = false;
	if ((cardType == "DINERS") && (!isDinersClub(cardNumber)))
		doesMatch = false;
	if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber)))
		doesMatch = false;
	if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber)))
		doesMatch = false;
	return doesMatch;

}  // END FUNCTION CardMatch()

//-->
