/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.util;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.AddressException;

public class IDUtils
{
  /**
   * The list of valid characters in Venice identifiers other than alphanumerics.
   */
  private static final String EXTRA_VALID = "-_~*'$";

  public static boolean isValidVeniceID(String id)
  {
    if ((id==null) || (id.length()==0))
      return false;  // null values and null strings are NEVER valid
    for (int i=0; i<id.length(); i++)
    { // test each character in turn
      char c = id.charAt(i);
      if ((c>='A') && (c<='Z'))
	continue;  // upper-case letters are OK
      if ((c>='a') && (c<='z'))
	continue;  // lower-case letters are OK
      if ((c>='0') && (c<='9'))
	continue;  // digits are OK
      if (EXTRA_VALID.indexOf(c)<0)
	return false;

    } // end for

    return true;   // all tests passed - ship it!

  } // end isValidVeniceID

  public static boolean isValidEmailAddress(String addr)
  {
    try
    { // take advantage of the JavaMail address parser
      InternetAddress tmp = new InternetAddress(addr);

    } // end try
    catch (AddressException e)
    { // if we get an AddressException, it's not valid, is it?
      return false;

    } // end catch

    return true;  // if get here, it's valid

  } // end isValidEmailAddress

  public static boolean isValidConfirmationNumber(int num)
  {
    return ((num>=1000000) && (num<=9999999));

  } // end isValidConfirmationNumber

  public static boolean isValidVeniceIDChar(char c)
  {
    if ((c>='A') && (c<='Z'))
      return true;  // upper-case letters are OK
    if ((c>='a') && (c<='z'))
      return true;  // lower-case letters are OK
    if ((c>='0') && (c<='9'))
      return true;  // digits are OK
    return (EXTRA_VALID.indexOf(c)>=0);

  } // end isValidVeniceIDChar

  public static boolean isValidPostLinkChar(char c)
  {
    if ((c>='A') && (c<='Z'))
      return true;  // upper-case letters are OK
    if ((c>='a') && (c<='z'))
      return true;  // lower-case letters are OK
    if ((c>='0') && (c<='9'))
      return true;  // digits are OK
    if (EXTRA_VALID.indexOf(c)>=0)
      return true;  // other characters from Venice IDs are OK
    return ((c=='.') || (c=='!'));  // also dots and bangs

  } // end isValidPostLinkChar

} // end class IDUtils
