/* ======================================================================
FUNCTION:  	IsAlpha

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only alphabetic characters 
				false, otherwise.


====================================================================== */

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


====================================================================== */
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.


====================================================================== */
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:	IsUndef
 
INPUT:		val - the value to be tested

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

====================================================================== */
function IsUndef( val ) {
	var isValid = false;

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

/* ======================================================================
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


====================================================================== */
function IsBlank( str ) {
	var isValid = false;

 	if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
 		isValid = true;
		
	return isValid;
}  // end IsBlank


/* ======================================================================
FUNCTION:       IsDate

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


====================================================================== */
function IsDate( str ) {
        var isValid = false;

        if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
                isValid = true;

        return isValid;
}  // end IsDate

/* ======================================================================
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 allowNegativestrue, if allowNegatives is true and the string starts 				with a '-', and all other characters are 0-9.
     			false, otherwise.


====================================================================== */
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.


====================================================================== */
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.

====================================================================== */
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:  	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


====================================================================== */
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:	GetActualDay 
 
INPUT: 		none.

RETURN:		the actual day of the week as a number from 1-7

DESC:		The method returns the day of the week.
		This is intended to be used as a method of a Date object.

USAGE:		today = new Date();
				document.write(today.getActualDay());

====================================================================== */
// Make GetActualDay a method of all Date objects 
Date.prototype.getActualDay = GetActualDay;
function GetActualDay() { 
	// Date.getDay() returns the actual day minus 1, so add 1 to get the proper day
	return this.getDay() + 1;
} // end GetActualDay 




/* ======================================================================
FUNCTION:	NumToString 
 
INPUT: 		number (number) : any valid number

RETURNS:		the number as a string

====================================================================== */ 
function NumToString( number )  {
 	 number += "";
 	 return number;
} // end NumToString


/* ======================================================================
FUNCTION:	Pad
 
INPUT:  	str (string): the string to be altered
		ch	(char): the character to use for padding
		num (int): the number of characters to pad

RETURN: 	The input string padded on both sides with input char, ch;
		returns null if invalid arguments were passed

DESC:		This function pads a string, str, on both sides with a given 
		number, num, of characters, ch.

CALLS:		This function depends on the functions PadLeft & PadRight.  
		They must be included in order for this function to work properly.
====================================================================== */
function Pad( str, ch, num ) {
	var resultStr = "";
	
	resultStr = PadLeft(str, ch, num);
	resultStr = PadRight(resultStr, ch, num);
	
	return resultStr;
} // end Pad


/* ======================================================================
FUNCTION:	PadLeft
 
INPUT:  	str (string): the string to be altered
		ch	(char): the character to use for padding
		num (int): the number of characters to pad

RETURN: 	The input string padded on the left with input char, ch;
		returns null if invalid arguments were passed

DESC:		This function left pads a string, str, with a given number, num, of characters, 		ch.
====================================================================== */
function PadLeft( str, ch, num ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;
	if (ch+"" == "undefined" || ch == null)	
		return null;
	if (typeof(num) != typeof(0)) // check to see if num is numeric
		return null;	

	// Make sure the string arguments are strings
	str += "";
	ch += "";

	var resultStr = str;

	// Make sure the argument is a string
	str += "";

	for (var i=0; i < num; i++)
		resultStr = ch + resultStr;

	return resultStr;
} // end PadLeft

/* ======================================================================
FUNCTION:	PadRight
 
INPUT:  	str (string): the string to be altered
		ch	(char): the character to use for padding
		num (int): the number of characters to pad

RETURN: 	The input string padded on the right with input char, ch;
		returns null if invalid arguments were passed

DESC:		This function right pads a string, str, with	a given number, num, 
		of characters, ch.
====================================================================== */
function PadRight( str, ch, num ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;
	if (ch+"" == "undefined" || ch == null)	
		return null;
	if (typeof(num) != typeof(0)) // check to see if num is numeric
		return null;	

	// Make sure the string arguments are strings
	str += "";
	ch += "";

	var resultStr = str;
		
	for (var i=0; i < num; i++)
		resultStr += ch;

	return resultStr;
} // end PadRight


/* ======================================================================
FUNCTION:	StringReplace 
 
INPUT: 		findText (string) 	: the substring to be replaced
		replaceText (string)	: the substring to use as replacement

RETURNS:	the string with the replaced text, if findText was found;
		the original string, otherwise.

DESC:		This method searches a string for findText and replaces it with	replaceText if 				found.  This is intended to be used as a method	of a String object.

USAGE:		var mystr = "Netscape";
				var result = mystr.replace("scape", "Objects");
				// write out result string, "NetObjects"
				write(result);

NOTE:			For the Windows 95 version of Microsoft Internet Explorer 3.02, you must
				use this method on a String object, not merely a string literal.  For
				example:

				// Create a string object first
				var mystr = new String("Netscape");

				var result = mystr.replace("scape", "Objects");
				// write out result string, "NetObjects"
				write(result);

====================================================================== */ 
// Make StringReplace a method of all String objects 
String.prototype.replace = StringReplace;
function StringReplace( findText, replaceText ) { 
	var originalString = new String(this);
	var pos = 0;

	// Validate parameter values
	if (findText+"" != "undefined" || findText == null || findText == "")
		return originalString;
	if (replaceText+"" != "undefined" || replaceText == null)
		return originalString;

	var len = findText.length;
	var limit = originalString.length;
	
	pos = originalString.indexOf(findText);
	while (pos != -1 && i < limit) { 
		// Get the first and last parts of the string:  preString + findText + postString
		// then change to preString + replaceText + postString to replace findText
		preString = originalString.substring(0, pos);
		postString = originalString.substring(pos+len, originalString.length);
		originalString = preString + replaceText + postString;
		pos = originalString.indexOf(findText); 
		i++;
	} 
	
	return originalString;	
} // end StringReplace

/* ======================================================================
FUNCTION:	StripNonNumeric
 
INPUT:  	str (string) - a string to be altered

RETURN: 	a string containing only numeric characters 0-9;
		returns null if invalid arguments were passed

DESC:		This function removes all non-numeric characters from a given string.  It is 				useful for removing dashes, parentheses, etc. from input strings such as credit 		card numbers or phone nubmers.
====================================================================== */
function StripNonNumeric( str ) {
	var 	resultStr = "";

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	// Loop through entire string, adding each character from the original
	// string if it is a number
	for (var i=0; i < str.length; i++)
	{
   	if ( (str.charAt(i) >= "0") && (str.charAt(i) <= "9") )
      			resultStr = resultStr + str.charAt(i);
 
   } // end for loop      

   return resultStr;
}  // end StripNonNumeric


/* ======================================================================
FUNCTION:	Trim
 
INPUT:  	str (string): the string to be altered

RETURN: 	A string with no leading or trailing spaces;returns null if invalid 					arguments were passed	
DESC:		This function removes all leading and tralining spaces from a string.

CALLS:		This function depends on the functions TrimLeft & TrimRight They must be included 		in order for this function to work properly.
====================================================================== */
function Trim( str ) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
} // end Trim

/* ======================================================================
FUNCTION:	TrimLeft
 
INPUT:  		str (string): the string to be altered

RETURN: 		A string with no leading spaces;
			returns null if invalid arguments were passed

DESC:			This function removes all leading spaces from a string.
====================================================================== */
function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {	
  		// Loop through string starting at the beginning as long as there
  		// are spaces.
//	  	len = str.length - 1;
		len = str.length;
		
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;

   	// When the loop is done, we're sitting at the first non-space char,
 		// so return that char plus the remaining chars of the string.
  		resultStr = str.substring(i, len);
  	}

  	return resultStr;
} // end TrimLeft


/* ======================================================================
FUNCTION:	TrimRight
 
INPUT:  	str (string): the string to be altered

RETURN: 	A string with no trailing spaces;
		returns null if invalid arguments were passed

DESC:		This function removes all trailing spaces from a string.
====================================================================== */
function TrimRight( str ) {
	var resultStr = "";
	var i = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
  		// Loop through string starting at the end as long as there
  		// are spaces.
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;
 			
 		// When the loop is done, we're sitting at the last non-space char,
 		// so return that char plus all previous chars of the string.
  		resultStr = str.substring(0, i + 1);
  	}
  	
  	return resultStr;  	
} // end TrimRight


/* ======================================================================
FUNCTION:	URLDecode
 
INPUT:		str (string): the string to be decoded

RETURN:  	URL-decoded version of str;
				returns null if invalid arguments were passed

DESC:		This function augments the default JavaScript/JScript unescape 
		function.  unescape does not handle literal '+' characters embedded 
		in URLs.  This function pre-processes a URL-encoded string by 						converting all '+' characters to spaces, and then calls unescape 		to complete the decode process.
====================================================================== */
function URLDecode ( str ) {
	var resultStr = "";

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";
	
	// Loop through each char in string and replace '+' with ' '
	for (var i = 0; i < str.length; i++) {
		if (str.charAt(i) == "+")
			resultStr += " ";
		else
			resultStr += str.charAt(i);
	}
	
	return unescape(resultStr);
}  // END URLDecode


function IsEmailValid(FormName)
{
        if(!FormName.value.match(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$/i)) {
          return false;
        }

        var EmailOk  = true
        var Temp     = FormName;
        var AtSym    = Temp.value.indexOf('@')
        var Period   = Temp.value.lastIndexOf('.')
        var Space    = Temp.value.indexOf(' ')
        var Length   = Temp.value.length - 1   // Array is from 0 to length-1


        if ((AtSym < 1) ||                     // '@' cannot be in first position
    (AtSym != Temp.value.lastIndexOf('@')) ||
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
  {
      EmailOk = false
      Temp.focus()
  }
        return EmailOk
}


function IsAlphaOrSpace( 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" or a space
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) || str.charAt(i) == " ") ) {
         				isValid = false;
         				break;
      			}
   } // end for loop
   
	return isValid;
}  // end IsAlphaOrSpace


