//Copyright 1996-2006 Numara Software Inc.   

/* *************************************************************
** Contains some javascript derived from Eric Krock's FormChek.js at
** developer.netscape.com/docs/examples/javascript/formval/overview.html
** Eric Krock  1.0  2/18/97  original JS 1.0-only version
** (c) 1997 Netscape Communications Corporation
************************************************************* */

/* *************************************************************
** Functions to Validata Form-Element Data:
**   isLetter(c) - true if string c is a letter
**   isEmail(s) - true if string s is an Email Address
**   isEmpty(s) - true if string s is empty
**   isBlank(s) - true if string s is empty or blank (all whitespace chars)
**   isDigit(c) - true if char c is a digit 
**   isInteger(s) - true if string s is a signed/unsigned number
**   isIntegerInRange(s, a, b) - true if string s (integer) is a <= s <= b
**   isNumeric - true if string is all digits
**   isFloat(s) - true if string s is a signed/unsigned floating-point number
************************************************************* */


/* ********************************************************** */
/* Functions ************************************************ */
/* ********************************************************** */


// Returns true if string c is a letter

function isLetter(c) 
  {
  return ( ((c >= "a") && (c <= "z")) || ( (c >= "A") && (c <= "Z") ));
  }


// Returns true if string s is a valid email address
// The way I understand this code, an address is valid if it:
//   1. has a first character that is a letter, number, or underscore
//   2. contains an @ sign
//   3. contains at least one character and then a '.' after the @ sign
//   4. contains at least two more characters after the first '.'
// So "a@..bc", "a@!.bc", and "a#%#$%#$%@#$%.bc" are all valid.
// We might want to fix this, but we need to be careful not to be too
// restrictive, since not everyone's internal mail rules are as strict
// as internet mail rules.
function isEmail(s)
   {
   if ( isLetter(s.charAt(0)) || isDigit(s.charAt(0)) || s.charAt(j) == "_" )
	for (var i=1; i<s.length; i++)
	  if (s.charAt(i) == "@") {                  // does it have an @ symbol?
	      for (var j=i+2; j<s.length; j++)
	        if (s.charAt(j) == ".") {            // does it contain a "."
			for (var k=j+1; k<s.length; k++)
			  if ( (isLetter(s.charAt(k))) && (isLetter(s.charAt(k+1))) )
								   // is it fully qualified?
	  return true; }}                         
      return false;
   }


// Returns true if an email doesnot contain the forbidden @ sign
function noAtSign(s)
{
    for (var i=1; i<s.length; i++)
      if (s.charAt(i) == "@")                   // does it have an @ symbol?
	 return false;

   return true;
}

// Returns true if string s is empty

function isEmpty(s)
  {
  return ((s == null) || (s.length == 0));
  }


// Returns true if string s is empty or all blank chars

function isBlank(s) {     

   if (isEmpty(s)) return true;
   s = s.replace(/\s/g, "");

   // exit early if we see an image tag ... before we remove all html tags        
   if (s.search(/<img/i) != -1)
        return false;

   // remove all html tags
   s = s.replace(/<[^>]*>/g, ""); // can't use non-greedy pattern match on .* because the question-mark kills
   s = s.replace(/&nbsp;/g, "");
        
   // Is s now empty?
   return (isEmpty(s));
}

// Returns true if character c is a digit (0 .. 9)

function isDigit(c)
  {
  return ((c >= "0") && (c <= "9"));
  }


// Returns true if all chars in string s are numbers;
// first character is *not* allowed to be + or -;

function isNumeric(s)
  {
  if (isBlank(s))
    return false;

  // Search through string's chars one by one until we find a 
  // non-numeric char, then return false; if we don't, return true

  for (var i=0; i<s.length; i++)
    {   
    // Check that current character is number
    var c = s.charAt(i);
    if (!isDigit(c)) 
      return false;
    }
  // All characters are numbers
  return true;
  }


// Returns true if all chars in string s are numbers;
// first character is allowed to be + or -; does not 
// accept floating point, exponential notation, etc.

function isInteger(s)
  {
  if (isBlank(s))
    return false;

  // skip leading + or -
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    var i = 1;
  else
    var i = 0;

  // Search through string's chars one by one until we find a 
  // non-numeric char, then return false; if we don't, return true
  for (i; i<s.length; i++)
    {   
    // Check that current character is number
    var c = s.charAt(i);
    if (!isDigit(c)) 
      return false;
    }
  // All characters are numbers
  return true;
  }


// True if string s is an unsigned floating point (real) number; 
// first character is allowed to be + or -; no exponential notation.

function isFloat(s)
  { 
  var seenDecimalPoint = false;
  var decimalPointDelimiter = ".";

  if (isBlank(s)) 
    return false;
  if (s == decimalPointDelimiter) 
    return false;

  // skip leading + or -
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    var i = 1;
  else
    var i = 0;

  // Search through string's chars one by one until we find a 
  // non-numeric char, then return false; if we don't, return true

  for (i; i<s.length; i++)
    {   
    // Check that current character is number
    var c = s.charAt(i);

    if ((c == decimalPointDelimiter) && !seenDecimalPoint) 
      seenDecimalPoint = true;
    else if (!isDigit(c)) 
      return false;
    }
  // All characters are numbers
  return seenDecimalPoint;
  }



// length of input's value must be within range
//
// I changed "return;" to "return 'nooo';" because ns 4.05 complains that
// the function needs to always return a value if it might sometimes return
// a value. - Joe
//
function checkLength(obj, min, max)
{
    var length = obj.value.length;
    if (length == 0 ) return 'nooo';
    if ( min != 0 )
    {
	if ( min == max && length != min ) 
	    return 'ne';
	if ( length < min ) 
	    return 'lt';
    }	    
    
    if (  max != 0 ) {
	if ( length > max ) 
	    return 'gt';
    }
    return 'nooo';
}




// Returns true if string s is an integer such that a <= s <= b

function isIntegerInRange (s, a, b)
  { 
  if (isBlank(s)) 
    return false;
  if (!isInteger(s)) 
    return false;
  intS = parseInt(s, 10);
  intA = parseInt(a, 10);
  intB = parseInt(b, 10);
  if ((intS >= intA) && (intS <= intB))
    return true;
  return false;
  }
