//======================================================================
//	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;
		
  			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;
	}

//======================================================================
//	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=i-1;
 			
 			// 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;  	
	}

//======================================================================
//	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;
	}

//======================================================================
//	FUNCTION:	NumToString 
//
//	INPUT: 		number (number) : any valid number
//
//	RETURNS:	the number as a string
//
//	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 NumToString( number )
	{
 		number += "";
 		return number;
	}

//======================================================================
//	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;
	}

//======================================================================
//	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;
	}

//======================================================================
//	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;
	}

//======================================================================
//	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);
//
//	PLATFORMS:	Netscape Navigator 3.01 and higher,
//			  	Microsoft Internet Explorer 3.02 and higher,
//			  	Netscape Enterprise Server 3.0,
//			  	Microsoft IIS/ASP 3.0.
//======================================================================
	// Make StringReplace a method of all String objects 
	String.prototype.replace = StringReplace;

	function StringReplace( findText, replaceText )
	{ 
		var originalString = new String(this);
		var pos = 0;
		var i = 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;	
	}

//======================================================================
//	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);
 
		}

		return resultStr;
	}

//======================================================================
//	FUNCTION:	StripNonPrintable
// 
//	INPUT:  	str (string) - the input string
//
//	RETURN: 	A string that contains only printable ASCII characters;
//				returns null if invalid arguments were passed
//
//	DESC:		This function removes any non printable characters 
//				from a string.
//======================================================================
	function StripNonPrintable( str )
	{
		var resultStr = "";
		var ch = '';

		// 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 string and keep only printable chars	
		for (var i=0; i < str.length; i++)
		{
			ch = str.charAt(i);
			if ((ch >= " ") && (ch <= "~"))
				resultStr += ch;
		}
	
		return resultStr;
	}

//======================================================================
// FUNCTION: StripNonPrintable
// 
// INPUT:	 str (string) - the input string
//
// RETURN: 	 A string that contains only printable ASCII characters;
//			 returns null if invalid arguments were passed
//
// DESC:	 This function removes any non printable characters 
//			 from a string.
//======================================================================
	function StripNonPrintable( str )
	{
		var resultStr = "";
		var ch = '';

		// 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 string and keep only printable chars	
		for (var i=0; i < str.length; i++)
		{
			ch = str.charAt(i);
			if ((ch >= " ") && (ch <= "~"))
				resultStr += ch;
		}
	
		return resultStr;
	}
