//  Extend a new method to the String Class isNull 
String.prototype.isNull=string_isNull;

String.prototype.strip=string_strip;

String.prototype.isNum=string_isNum;
String.prototype.isWholeNum=string_isWholeNum;

String.prototype.trim=string_trim;

function string_trim() {
	return this.replace(/^\s*|\s*$/g, "");
}

// Method Name: alertAndFocus                                                                       
// Description:  It alerts the given msg, focus the form element, and returns false
//               NOTE:  The caller should/may want to return the returned value of false
// Prototype of: String                                                                      
// Syntax:  return alertAndFocus(aform.password, "Your password and password confirmation must match.");                                                                          
// Returns: false = to stop execution after the alert
function alertAndFocus(obj, msg) {
	alert(msg);
	
	try {
		obj.focus();
		obj.select();
	} catch (err) {
		// Not being able to set the focus isn't critical. No-op.
	}
	
	return false;
}

// Name: checkLen                                                                            
// Syntax: checkLen(<documents.form.fieldname>, <name of field to be put in alert>, <maxLen>, <requiredField>)
// Returns: true = field is not too long, false = fields is too long
// If requiredField is passed in and is non-zero then the field will also be checked
// to make sure is not empty.
function checkFieldLen(fld, fieldName, maxLen, requiredField) { 
	if (fld.value.length > maxLen) {
		return alertAndFocus(fld, "The '" + fieldName + "' field cannot be longer than " + maxLen + " characters (it is currently " + fld.value.length + ").");
	}
	if (requiredField) {
		return noNull(fld, fieldName);
	}
	return true;
}

// Name: noNull                                                                            
// Description:  Calls isNull and gives alert msg is the given fields is NULL.             
// Syntax: noNullisNull(<documents.form.fieldname>, name of fields to be put in alert.)    
// Returns: true = field is NOT NULL,          false = fields is NULL                                                         
function noNull(fld, fieldName) { 
	if (fld.value.isNull())	{
		return alertAndFocus(fld, "The '" + fieldName + "' field cannot be empty.");
	}
	return true;
}

// Method Name: isNull                                                                       
// Description:  returns whether or not the string is null.  If only tabs and spaces         
//               are present, the string is still considered null.                           
// Prototype of: String                                                                      
// Syntax: isNull()                                                                          
// Returns: true = string is null,     false = string is not null                                                       
function string_isNull() { 
	var str = this;
	var i, ch, isnull = true;
	if (typeof str == 'number') return(false);
	if ((str != null) && (str != 'undefined')) {
		for (i = 0; i < str.length; ++i) {
			ch = str.charAt(i);
			if ((ch != ' ') && (ch != '\t')) {
				isnull = false;
				break;
			}
		}
	}
	return isnull;
}

// method strip:
// description: Returns a copy of the string with the spaces and tabs removed.
//                      Can also strip other characters if provided with the 'character'  parameter. 
//  prototype of: String
//  syntax: strip(option, character)
//  inputs: option - Optional parameter. 
//                     "B" = remove both leading and trailing.  Default. 
//                     "L" = remove only from the left of the string
//                     "R" = remove only to the right of the string
//                     "A" = remove all of the specified characters from the string
//          character - character to strip from the string.  Optional, but you must 
//                      specify the option parameter in order to use this one.  
//                      By default will use spaces and tabs.
// returns:  A string containing the original string, without any spaces or tabs, 
//           or without any of the characters provided witht the 'character' 
//           parameter.
// custom functions called: isNull()
function string_strip(option, character) {
  var oldStr = this
  var newStr='',tempStr='',i,j;

  if ( option != null) { option = option.toUpperCase() }
  else { option = "B" }
  if ( oldStr == null ) 
  {
    newStr = null;
  }
  else 
  {
    newStr.value = "";
    if ( character != null )
       badchars = character;
    else  
       badchars=" \t";
    if ( (option == "B") || (option == "L") || (option == "A") )     
    {
      for(i=0; i < oldStr.length; i++) 
      {
        curchar = oldStr.charAt(i);
        if( badchars.indexOf(curchar) == -1 ) 
        {
          newStr = oldStr.substring(i,oldStr.length);
          break;
         }
      }
    }  else {newStr = oldStr}
    if ( (option == "B") || (option == "R") || (option == "A"))
    {
      for(i=newStr.length-1;i>=0; i-- ) 
      {
        curchar = newStr.charAt(i);
        if( badchars.indexOf(curchar) == -1 ) 
        {
          newStr = newStr.substring(0,i+1);
          break;
        }
      }
    }
    if ( option == "A") 
    {
      tempStr = newStr;
      newStr = '';
      for (j=0; j < tempStr.length; j++) 
      {
        curchar = tempStr.charAt(j);
        if (badchars.indexOf(curchar) == -1)
          newStr +=  curchar;
      }
    }  
  }
  return (newStr);
}

// method isNum:  
// description:  returns whether or not the string is strictly numeric 
//               this includes all digits (0..9) and the decimal point (.)
//               Will allow blanks and still be considered numeric by default,
//               but this can be disabled with the allowBlanks parameter.
//
// prototype of: String
//
// syntax: isNum(allowBlanks, addChars)
//
// inputs:    allowBlanks - Optional.  Defaults to 'Y'
//                             'Y' - Yes, allow blanks and still consider numeric
//                             'N' - No, don't allow blanks and still consider numeric.  
//            addChars - Optional.  String of additional characters to allow and 
//                       still be considered striclty numeric.  The allowBlanks 
//                       parameter must be specified to use this option.  
//
// returns: true = string is strictly numeric
//          false = string is not strictly numeric
function string_isNum(allowBlanks, addChars)
{  
  var param1 = allowBlanks, param2;
  if (allowBlanks == null)
    param1 = 'Y';
  if (addChars == null)
    param2 = '.'
  else
    param2 = addChars.toString()+'.';
  return this.isWholeNum(param1, param2);
}

// method isWholeNum:  
// description:  returns whether or not the string is a whole number.
//               This includes all digits (0..9).  Will allow blanks and still 
//               be considered numeric by default, but this can be disabled with 
//               the allowBlanks parameter.
//
// prototype of: String
//
// syntax: isWholeNum(allowBlanks, addChars)
//
// inputs:    allowBlanks - Optional.  Defaults to 'Y'
//                             'Y' - Yes, allow blanks and still consider numeric
//                             'N' - No, don't allow blanks and still consider numeric.  
//            addChars - Optional.  String of additional characters to allow and 
//                       still be considered a whole number.  The allowBlanks 
//                       parameter must be specified to use this option.  
//
// returns: true = string is a whole number
//          false = string is not a whole number 
function string_isWholeNum(allowBlanks, addChars)
{
   var numStr = this; 
   var digits = "0123456789", retval = true, i, ch;
   if (typeof addChars != 'undefined') digits += addChars;
   if (typeof(numStr) == 'number') return true;
   if (allowBlanks == null || allowBlanks == 'y' || allowBlanks == 'Y') 
      allowBlanks = 'Y';
   else 
      allowBlanks = 'N'
   if (allowBlanks == 'N')
      if ( this.strip().isNull()) 
	   		return false;	  
	 

   numStr = String(numStr);
   numStr = numStr.strip();

   for (i=0; i < numStr.length; i++)
   {   
       ch = numStr.charAt(i);
       if ( digits.indexOf(ch) == -1 )
       {  
          retval = false;
          break;
       }
   }
   return (retval)
}
