/**************************************************************************************************
' File: string.js
' Created: 9/02/2006
' Developer: Bryan Walker
' Description:
'   This file contains general purpose javascript string functions.
'**************************************************************************************************/

// This function trims any leading whitespace from the
// string that is passed to it. (spaces, line feeds, tabs, etc.)
// The trimmed string is returned.
function ltrim(str)
{
   return str.replace(/^\s*/g, "");
}

// This function trims any trailing whitespace from the
// string that is passed to it. (spaces, line feeds, tabs, etc.)
// The trimmed string is returned.
function rtrim(str)
{
   return str.replace(/\s*$/g, "");
}

// This function trims any leading and trailing whitespace from the
// string that is passed to it. (spaces, line feeds, tabs, etc.)
// The trimmed string is returned.
function trim(str)
{
   return str.replace(/^\s*|\s*$/g, "");
}

// This function returns true if the string consists entirely of
// whitespace character. Otherwise, it returns false.
function isWhitespace(str)
{
   return !/\S/.test(str);
}

// This function returns true if the string contains any
// whitespace characters. Otherwise, it returns false.
function hasWhitespace(str)
{
   return /\s/.test(str);
}

// This function returns true if the string passed to it is a
// number. Otherwise, it returns false.
function isNumber(str)
{
   return /^\-*\d*\.*\d+$/.test(str);
}

// This function returns true if the string passed to it only
// contains digits. Otherwise, it returns false.
function isNumeric(str)
{
   return /^\d+$/.test(str);
}

// This function returns true if the string passed to it is whitespace,
// also displaying a prompt and passing the focus to the passed object.
// Set the title of the object to display an accurate prompt.
// It returns false if the string contains non-whitespace characters.
function isBlank(string, obj) {
  if (isWhitespace(string)) {
    window.event.cancelBubble = true;
    alert(obj.title + " has no value.  Please enter a value.");
    obj.focus();
    return true;
  }
  return false;
}

// This function returns true if the string passed to it is a valid
// form for an email address. Email addresses must be of the form:
// name@somewhere.com.  0-9, a-z, and - are the only valid characters
// in a domain name.  The underscore is also allowed in the username.
// Other characters may need to be added to the username portion.
function isValidEmail(str)
{
   return /^[a-z][a-z_0-9\.\-]+@[a-z0-9\.\-]+\.[a-z]{2,3}$/i.test(str);
}

// This function formats a phone number. The phone number is passed in
// as a string. If a valid phone number can be determined from the
// string (it must contain 10 digits including the area code), the
// phone number is returned. The first case handles an extension of up
// to 3 digits.  The second case handles a phone number with no extension.
// Otherwise the function returns the empty string. The phone number 
// returned is of the form: (nnn) nnn-nnnn
function formatPhoneNumber(str)
{
   // Remove any character that is not a digit.
   str = str.replace(/[^\d]/g, "");

   if(/^\d{11,14}$/.test(str))
   {
      return "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6, 4) + " Ext. " + str.substr(10, str.length - 10);
   }

   if(/^\d{10}$/.test(str))
   {
      return "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6, 4);
   }

   return "";
}

// This function notifies you if any characters other than letters or
// the underscore are used.
function containsSpecialChars(str)
{
  return /^\w*\W+.*$/.test(str)
}