// FormChek.js
//
// Provided by Netscape DevEdge
//
//
// 18 Feb 97 created Eric Krock
//
// (c) 1997 Netscape Communications Corporation

// VARIABLE DECLARATIONS

digits = "0123456789";
floatdigits = digits + ".";

lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";

uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

emailcharacters = digits + lowercaseLetters + uppercaseLetters + ".@";

// whitespace characters
whitespace = " \t\n\r";

// decimal point character differs by language and culture
decimalPointDelimiter = ".";

// non-digit characters which are allowed in phone numbers
phoneNumberDelimiters = "()- ";

// characters which are allowed in US phone numbers
validUSPhoneChars = digits + phoneNumberDelimiters;

// U.S. phone numbers have 10 digits.
// They are formatted as 123 456 7890 or (123) 456-7890.
digitsInUSPhoneNumber = 10;

defaultEmptyOK = false;

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"

mPrefix = "You did not enter a value into the ";
mSuffix = " field. This is a required field. Please enter it now.";

// i is an abbreviation for "invalid"

iStateCode = "This field must be a valid two character U.S. state abbreviation (like CA for California). Please reenter it now.";
iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now.";
iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now.";
iEmail = "This field must be a valid e-mail address (like name@server.com). Please reenter it now.";

// Check whether string s is empty.

function isEmpty(s){
   return (isNull(s) || isUndefined(s) || (s.length == 0) || (s == ""))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s) {
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    // numbers are considered whitespace if set to 0
    if (isUndefined(s.length)) {
		if (s == 0)
			return true;
		else
			return false;
    }

    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isNull( aValue ) {

   // for true null, "" + aValue should be equal to "null"
   // for true undefined, "" + aValue should be equal to "undefined" and equal to null
   return(
      ( ( "" + aValue ) == "null" ) && ( aValue != "null" ) && 
      ( ( "" + aValue ) != "undefined" ) && ( aValue != "undefined" )
   );
}

function isUndefined( aValue ) {

   // for true null, "" + aValue should be equal to "null"
   // for true undefined, "" + aValue should be equal to "undefined" and equal to null
   return( ( "" + aValue ) == "undefined" );
}

function isDigit (c){
	if (digits.indexOf(c) == -1) return false;
	return true;
}

function isFloatDigit (c){
	if (floatdigits.indexOf(c) == -1) return false;
	return true;
}

function isInteger (s) {
   var i;

   if (isEmpty(s)) 
      if (isInteger.arguments.length == 1)
         return defaultEmptyOK;
      else
         return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

   for (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;
}

function isFloat (s) {
   var i;

   if (isEmpty(s)) 
      if (isFloat.arguments.length == 1)
         return defaultEmptyOK;
      else
         return (isFloat.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

   for (i = 0; i < s.length; i++) {   
      // Check that current character is number.
      var c = s.charAt(i);

      if (!isFloatDigit(c)) return false;
   }

   // All characters are numbers.
   return true;
}

function isUndefined( aValue ) {

// for true null, "" + aValue should be equal to "null"
// for true undefined, "" + aValue should be equal to "undefined" and equal to null
        return( ( "" + aValue ) == "undefined" );
}

function isUSPhoneNumber (s) {
   if (isEmpty(s)) 
      if (isUSPhoneNumber.arguments.length == 1)
         return defaultEmptyOK;
      else
         return (isUSPhoneNumber.arguments[1] == true);
   return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}



// Removes all characters which do NOT appear in string bag 
// from string s.

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

// FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES.


// Display prompt string s in status bar.

function prompt (s)
{   window.status = s
}



// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}


function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}


function warnInvalid (theField, s) {
   theField.focus();
   theField.select();
   alert(s);
   return false;
}

// FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS.

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {
      //var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
       var parsedEmail = stripCharsInBag(theField.value, emailcharacters);
       if (!isEmpty(parsedEmail, false)) 
          return warnInvalid (theField, iEmail);
       else 
       {
          return true;
       }
    }
}


function checkUSPhone (theField, emptyOK)
{   if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {
      //var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
       var normalizedPhone = stripCharsNotInBag(theField.value, digits);
       if (!isUSPhoneNumber(normalizedPhone, false)) 
          return warnInvalid (theField, iUSPhone);
       else 
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          //theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }
    }
}

function checkNotEmpty (theField, s, emptyOK) {
   // Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   if (checkNotEmpty.arguments.length == 2) emptyOK = defaultEmptyOK;
   if ((emptyOK == true) && (isEmpty(theField.value))) return true;
   if (isEmpty(theField.value))
      return warnEmpty (theField, s);
   else return true;
}

function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnInvalid (theField, s);
    else return true;
}

function checkInteger (theField, s, emptyOK) {
   // Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   if (checkInteger.arguments.length == 2) emptyOK = defaultEmptyOK;
   if ((emptyOK == true) && (isEmpty(theField.value))) return true;
   if (!isInteger(theField.value))
      return warnInvalid (theField, s);
   else return true;
}

function checkForValidLength (theField, size, s) {
   // Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   var string = theField.value;
   if (isEmpty(theField.value)) return true;
   if (string.length > size)
      return warnInvalid (theField, s);
   else return true;
}

function checkForExactLength (theField, size, s) {
   // Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   var string = theField.value;
   if (isEmpty(theField.value)) return true;
   if (string.length != size)
      return warnInvalid (theField, s);
   else return true;
}


