/*
*	String object extension
*
*	Copyright(C) 2000, Ong Kian Soon. All rights reserved.
*
*	You are grant a non-exclusive, royalty free, license to use, modify or
*	redistribute this code in any form, provided that this copyright notice
*	and license appear on all copies of the code.
*/

new String("");							// fix for Netscape Navigator 3.x
String.prototype.Trim = strTrim;
String.prototype.TrimStart = strTrimStart;
String.prototype.TrimEnd = strTrimEnd;
String.prototype.RoundOff = strRoundOff;

//
//	Trim away white spaces at the start and end of this string
//	Return:
//		Trimmed String
//
function strTrim()
{
	var i = 0, l = this.length;
	while( (i < l) && (" \t\n\r".indexOf( this.charAt(i) ) != -1) ) i++;
	while( l > i )
	{
		l--;
		if( " \t\n\r".indexOf( this.charAt(l) ) < 0 ) break;
	}
	return this.substring( i, l+1 )
}

//
//	Trim away white spaces at the start of this string
//	Return:
//		Trimmed String
//
function strTrimStart()
{
	var i = 0, l = this.length;
	while( (i < l) && (" \t\n\r".indexOf( this.charAt(i) ) != -1) ) i++;
	return this.substring( i );
}

//
//	Trim away white spaces at the end of this string
//	Return:
//		Trimmed String
//
function strTrimEnd()
{
	var l = this.length;
	while( l > 0 )
	{
		l--;
		if( " \t\n\r".indexOf( this.charAt(l) ) < 0 ) break;
	}
	return this.substring( 0, l+1 )
}

//
//	Round off this string to the required precision
//	Parameters:
//		p = precision
//	Return:
//		Rounded string for +ve precision, original string for -ve precision
//	Note:
//		The required portion of the decimal number is shift up as
//		integer and store back to 'value'.
//
//			Math.round( (parseFloat(this) - i) * precision );
//
//		However, decimal number that start with 0s will have the leading 0s
//		filter off in the above operation (eg .001 shift by 3 become 1). To
//		construct the actual decimal number while converting to a string,
//		the precision (10 powered) is added (eg. 1 + (10 ^ 3) = 1001). After
//		that, the first character of the string is discarded.
//
//			String(i) + "." + String(v + precision).substr( 1 );
//
function strRoundOff( p )
{
	if( p >= 0 )
	{
		var v, i = parseInt( this );
		p = Math.pow( 10, parseInt(p) );
		v = Math.round( (parseFloat( this ) - i) * p );
		if( v >= p ) i++;
		return (p > 1) ? String(i) + "." + String(v + p).substring( 1 ) : String(i);
	}
	return this;
}
