/*
	String prototypes

	02/12/2003 - Created by Timothée Groleau
	(c) 2003 - ICUS Pte Ltd - All rights reserved.

	Adds new function to Strings, for example trim

*/

//define the class constants
String.ALL = {};
String.LEFT = {};
String.RIGHT = {};

String.prototype.trim = function() {
	var charsToTrim = " \n\r\t";    // default chars to trim
	var mode = String.ALL;          // default mode
	var argsIndex = 0;

	// Trimming mode specified?
	if (typeof(arguments[0]) == "object") {
		mode = arguments[0]
		argsIndex = 1;
	}

	// Custom characters specified?
	if (typeof(arguments[argsIndex]) == "string")
		charsToTrim = arguments[argsIndex];

	// test for full blank string
	var r = new RegExp("^[" + charsToTrim + "]*$");
	if (this.search(r) > -1) return "";
	
	// we know string contains at least one non-blank character
	// the regexp below describes the string as 3 blocks, blankspace/content/blankspace
	r = new RegExp("^([" + charsToTrim + "]*)([^" + charsToTrim + "]|[^" + charsToTrim + "].*[^" + charsToTrim + "])([" + charsToTrim + "]*)$");

	// depending on which side to trim, build the trim pattern
	var trimPattern = 
		(((mode == String.ALL) || (mode == String.LEFT)) ? "" : "$1") +
		"$2" + 
		(((mode == String.ALL) || (mode == String.RIGHT)) ? "" : "$3");
		
	return this.replace(r, trimPattern);
}

