/*  (stringObject).trim() prototype function
	version 1.0, 24 Jan 02
	compatible with Netscape 3+, IE 4+
	author: Mike Mertsock, mjm2@alfred.edu
	to use:
	 ...on a string variable:
			var thisString =  "stringValue";
			thisString.trim(); returns the trimmed string
	 ...on a string literal:
			"this is a string".trim(); returns the trimmed string
*/

function trim() {
	
	//"this" is the string or variable that called trim()
	var thisString = new String( this.toString() );
	var thisLen = thisString.length;
	var countSpacesL = 0, countSpacesR = thisLen;
	
	//loop from the left side
	for ( var lPos = 0; lPos < thisLen; lPos++ )
		if ( thisString.charAt(lPos) == " " )
			countSpacesL++;
		else
			break;
	
	//loop from right side. don't go to left of countSpacesL
	for ( var rPos = thisLen - 1; rPos >= 0; rPos-- )
		if ( thisString.charAt(rPos) == " " && countSpacesR > countSpacesL )
			countSpacesR--;
		else
			break;
	
	thisString = thisString.substring( countSpacesL, countSpacesR );
	
	return thisString;
	
}

//this declares the trim() function as a behavior of the class String
//String.prototype reference is in "JavaScript Bible 3rd ed." page 542-3
String.prototype.trim = trim;
