/* ======================================================================
FUNCTION:  	IsAlpha
INPUT:	str (string) - the string to be tested
RETURN:  	true, if the string contains only alphabetic characters 
		false, otherwise.
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsAlpha( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  	for (i = 0; i < str.length; i++) {
		// Alpha must be between "A"-"Z", or "a"-"z"
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
      			}
   } // end for loop
   
	return isValid;
}  // end IsAlpha 

/* ======================================================================
FUNCTION:	IsAlphaNum
INPUT:	str (string) - a string that will be tested to ensure that
      			each character is a digit or a letter.
RETURN:  	true, if all characters in the string are a character from 0-9
     			or a-z or A-Z;  
		false, otherwise
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsAlphaNum( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;
	
	// convert to a string for performing string comparisons.
   	str += "";	

	// Loop through length of string and test for any alpha numeric 
	// characters
   	for (i = 0; i < str.length; i++)
   	{
			// Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
      	if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))))
			{
				isValid = false;
				break;
			}	
   	} // END for   
   
   	return isValid;
}  // end IsAlphaNum

/* ======================================================================
FUNCTION:	IsAlphaNumOrUnderscore
INPUT:	str (string) - the string to be tested
RETURN:  	true, if the string contains only alphanumeric characters or underscores.
		false, otherwise.
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsAlphaNumOrUnderscore( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.
	// Loop through string one character at a time. If non-alpha numeric
	// is found then, break out of loop and return a false result

	for (i = 0; i < str.length; i++)
   	{
		// Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
      		if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
      			(str.charAt(i) == "_") ) )
      		{
   				isValid = false;
         		break;
      		}

	} // END for   
   
	return isValid;

}  // end IsAlphaNumOrUnderscore

/* ======================================================================
FUNCTION:	IsBlank
INPUT:	val - the value to be tested
RETURN:  	true, if the string is null, undefined or an empty string, ""
      	false, otherwise.
CALLS:	IsNull(), IsUndef() which are defined elsewhere in the Script Library
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsBlank( str ) {
	var isValid = false;
	if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
 		isValid = true;
		
	return isValid;
}  // end IsBlank

/* ======================================================================
FUNCTION:  	IsInt
INPUT:  	numstr (string/number) 	 - the string that will be tested to ensure 
      		that each character is a digit
		allowNegatives (boolean) - (optional) when true, allows numstr to be
			negative (contain a '-').  When false, any negative number or 
			a string starting	with a '-' will be considered invalid.
RETURN:  	true, if all characters in the string are a character from 0-9,
			regardless of value for allowNegatives true, if allowNegatives 
			is true and the string starts with a '-', and all other characters 
			are 0-9. 
		false, otherwise.
PLATFORMS:	Netscape Navigator 3.01 and higher,
		Microsoft Internet Explorer 3.02 and higher,
		Netscape Enterprise Server 3.0,
		Microsoft IIS/ASP 3.0.
====================================================================== */
function IsInt( numstr, allowNegatives ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	// Default allowNegatives to true when undefined or null
	if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")	
		allowNegatives = true;

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for negative numbers (first char == '-').   
	for (i = 0; i < numstr.length; i++) {
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) || 
				(numstr.charAt(i) == "-" && !allowNegatives)) {
       	isValid = false;
       	break;
      }
         	         	       
   } // END for   
   
   	return isValid;
}  // end IsInt

/* ======================================================================
FUNCTION:	IsNull
INPUT:	val - the value to be tested
RETURN:  	true, if the value is null;
      	false, otherwise.
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsNull( val ) {
	var isValid = false;

 	if (val+"" == "null")
 		isValid = true;
		
	return isValid;
}  // end IsNull

/* ======================================================================
FUNCTION:  	IsNum
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
      		that the value is a number (int or float)
RETURN:  	true, if all characters represent a valid integer or float
     		false, otherwise.
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   
   
   	return isValid;
}  // end IsNum

/* ======================================================================
FUNCTION:	IsUndef
 
INPUT:	val - the value to be tested

RETURN:  	true, if the value is undefined
      	false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
		  	Microsoft Internet Explorer 3.02 and higher,
		  	Netscape Enterprise Server 3.0,
		  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsUndef( val ) {
	var isValid = false;

 	if (val+"" == "undefined")
 		isValid = true;
		
	return isValid;
}  // end IsUndef

/* ======================================================================
FUNCTION:  	IsValid5DigitZip
 
INPUT:    	str (string) - a 5-digit zip code to be tested

RETURN:  	true, if the string is 5-digits long
		false, otherwise

CALLS:	IsBlank(), IsInt() which are defined elsewhere in the Script Library

PLATFORMS:	Netscape Navigator 3.01 and higher,
		  	Microsoft Internet Explorer 3.02 and higher,
		  	Netscape Enterprise Server 3.0,
		  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsValid5DigitZip( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;
	
	var isValid = true;

	// Rules: zipstr must be 5 characters long, and can only contain numbers from
   // 0 through 9
   if (IsBlank(str) || (str.length != 5) || !IsInt(str, false))
		isValid = false;
   
   return isValid;
} // end IsValid5DigitZip

/* ======================================================================
FUNCTION:  	IsValid5Plus4DigitZip
 
INPUT:    	str (string) - a 5+4 digit zip code to be tested

RETURN:  	true, if the string contains 5-digits followed by a dash followed by 4 digits
		false, otherwise

CALLS:	IsBlank(), IsInt() which are defined elsewhere in the Script Library

PLATFORMS:	Netscape Navigator 3.01 and higher,
		  	Microsoft Internet Explorer 3.02 and higher,
		  	Netscape Enterprise Server 3.0,
		  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsValid5Plus4DigitZip( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";

	// Rules: The first five characters may contain only digits 0-9,
	// the 6th character must be a dash, '-', and 
	// the last four characters may contain only digits 0-9.
	// The total string length must be 10 characters.
   	if (IsBlank(str) || (str.length != 10) || 
			!IsInt(str.substring(0,5), false) || str.charAt(5) != '-' ||
			!IsInt(str.substring(6,10), false))
		isValid = false;
   
   	return isValid;
} // end IsValid5Plus4DigitZip

/* ======================================================================
FUNCTION:  	IsValidEmail
 
INPUT:    	str (string) - an e-mail address to be tested

RETURN:  	true, if the string contains a valid e-mail address which is a string
			plus an '@' character followed by another string containing at least 
			one '.' and ending in an alpha (non-punctuation) character.
		false, otherwise

CALLS:	IsBlank(), IsAlpha() which are defined elsewhere in the Script Library

PLATFORMS:	Netscape Navigator 3.01 and higher,
		  	Microsoft Internet Explorer 3.02 and higher,
		  	Netscape Enterprise Server 3.0,
		  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsValidEmail( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";

	namestr = str.substring(0, str.indexOf("@"));  // everything before the '@'
	domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'

	// Rules: namestr cannot be empty, or that would indicate no characters before the '@',
	// domainstr must contain a period that is not the first character (i.e. right after
	// the '@').  The last character must be an alpha.
   	if (IsBlank(str) || (namestr.length == 0) || 
			(domainstr.indexOf(".") <= 0) ||
			(domainstr.indexOf("@") != -1) ||
			!IsAlpha(str.charAt(str.length-1)))
		isValid = false;
   
   	return isValid;
} // end IsValidEmail

/* ======================================================================
FUNCTION:  	IsValidPhone
 
INPUT:    	str (string) - an phone number to be tested
		incAreaCode (boolean) - if true, area code is included (10-digits);
						if false or undefined, area code not included

RETURN:  	true, if the string contains a 7-digit phone number and incAreaCode == false
			or is undefined
		true, if the string contains a 10-digit phone number and incAreaCode == true
		false, otherwise

CALLS:	StripNonNumeric(), which is defined elsewhere in the Script Library

PLATFORMS:	Netscape Navigator 3.01 and higher,
		  	Microsoft Internet Explorer 3.02 and higher,
		  	Netscape Enterprise Server 3.0,
		  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsValidPhone( str, incAreaCode ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	// Set default value for incAreaCode to false, if undefined or null
	if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null")	
		incAreaCode = false;

	var isValid = true;

	str += "";

	// After stripping out non-numeric characters, such as dashes, the
	// phone number should contain 7 digits (no area code) or 10 digits (area code)
	str = StripNonNumeric(str+"");
	if (incAreaCode && str.length != 10)
		isValid = false;
   if (!incAreaCode && str.length != 7)
		isValid = false;

   	return isValid;
} // end IsValidPhone

/* ======================================================================
FUNCTION:  	IsValidSSN
INPUT:    	str (string) - an phone number to be tested
		incDashes (boolean) - if true, str includes dashes (e.g. 111-12-3456);
				if false, str contains only digits
RETURN:  	true, if the string contains digits and dashes in the form 111-12-3456;
		true, if the string contains a 9-digit number and incDashes is false;
		false, otherwise
CALLS:	IsInt(), which is defined elsewhere in the Script Library
PLATFORMS:	Netscape Navigator 3.01 and higher,
	  	Microsoft Internet Explorer 3.02 and higher,
	  	Netscape Enterprise Server 3.0,
	  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsValidSSN( str, incDashes ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	// Set default value for incDashes to true, if undefined or null
	if (incDashes+"" == "undefined" || incDashes+"" == "null")	
		incDashes = true;

	str += "";	// make sure it's a string

	if (!incDashes && (!IsNum(str) || str.length != 9))
		isValid = false;

	var part1 = str.substring(0,3);
	var part2 = str.substring(4,6);
	var part3 = str.substring(7,str.length);

	// Ensure that the first part is a number and 3 digits long,
	// the second part is a number and 2 digits long,
	// the third part is a number and 4 digits long, e.g. 111-22-3333
	if (incDashes && ((!IsInt(part1, false) || part1.length != 3) ||
			(!IsInt(part2, false) || part2.length != 2) || 
			(!IsInt(part3, false) || part3.length != 4)) )
		isValid = false;

   	return isValid;
} // end IsValidSSN
