/*
	function 			: 	1. to trim leading and/or trailing white spaces 
										2. to replace multiple white spaces between strings into one white space
										3. to trim a specific character/zero
	created by 		:		jason wong (jasonwong02@hotmail.com)
	date created	:		30 April 2003
	date modified	:		02 May 2003
	version				:		0.1
	
	syntax				: 	trim 		( str, id )
										trimOld	( str, id )	
										LTrim 	( str )
										RTrim 	( str )
										trimCh 	( str, chr, scope )
										LTrimCh ( str, chr )
										RTrimCh ( str, chr )
									
	parameters		:		str		(required) indicates the string value 
										id 		(optional) indicates the identifier; "x" to preserve consecutive spaces entered in between string
										chr		(optional) indicates the character to be trimmed; otherwise zero will be trimmed
										scope (optional) indicates the scope; "a" to trim all, "l" to trim leading and/or "r" to trim trailing specific character/zero
										
	example				: -
	
	prerequisite	:	-
*/

function trim( str, id )
{
  var temp = str;
  var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
  if ( obj.test(temp) ) 
		{ temp = temp.replace(obj, '$2'); }
	if ( !id )
	{
  	var obj = / +/g;
  	temp = temp.replace(obj, " ");
	}
  if (temp == " ") { temp = ""; }
  return temp;
}

var trimvar     = "\f\n\r\t\v "; // form feed, linefeed, carriage return, horizontal/vertical tab

// trim leading spaces
function LTrim ( retVal )
{
	if ( typeof retVal != "string" ) 
		{ return retVal; }
	else if ( retVal.length < 1 )
		{ return ""; }

  var ch = retVal.substring(0, 1); // first character
	
  while (trimvar.indexOf(ch) != -1)
  {
		retVal = retVal.substring(1, retVal.length);
    ch = retVal.substring(0, 1);
  }
	
	if ( retVal == "" )
		{ return ""; }
	else
		{ return retVal; }
}

// trim trailing spaces
function RTrim ( retVal )
{
	if ( typeof retVal != "string" ) 
		{ return retVal; }
	else if ( retVal.length < 1 )
		{ return ""; }

	var ch = retVal.substring(retVal.length-1, retVal.length); // last character
	
 	while (trimvar.indexOf(ch) != -1)
  {
		retVal = retVal.substring(0, retVal.length-1);
    ch = retVal.substring(retVal.length-1, retVal.length);
  }
	
	if ( retVal == "" )
		{ return ""; }
	else
		{ return retVal; }
}

function trimCh( str, chr, sc ) 
{
	if ( typeof str != "string" ) 
		{ return str; }
	else if ( str.length < 1 )
		{ return ""; }
		
	if ( chr )
		var trimchar = chr;
	else
		var trimchar = "0";
		
	if ( !sc  ) var sc = 'a';	

	if ( sc == "a" || sc == "r" )
		str = RTrimCh( str, trimchar );
		
	if ( sc == "a" || sc == "l" )
		str = LTrimCh( str, trimchar );
		
	if ( sc == "a" )
	{
		while (str.indexOf(trimchar) != -1)
			{ str = str.substring(0, str.indexOf(trimchar)) + str.substring(str.indexOf(trimchar)+1, str.length); }
	}
  
	if ( str == "" )
		{ return ""; }
	else
		{ return str; }
}

// trim leading character/zero
function LTrimCh ( retValue, chr )
{
	if ( typeof retValue != "string" ) 
		{ return retValue; }
	else if ( retValue.length < 1 )
		{ return ""; }

	if ( chr ) 
		var trimchar = chr;
	else 
		var trimchar = "0";
		
	var ch = retValue.substring(0, 1);
	
  while (trimchar.indexOf(ch) != -1)
  {
		retValue = retValue.substring(1, retValue.length);
    ch = retValue.substring(0, 1);
  }
	
	if ( retValue == "" )
		{ return ""; }
	else
		{ return retValue; }
}

// trim trailing character/zero
function RTrimCh ( retValue, chr )
{
	if ( typeof retValue != "string" ) 
		{ return retValue; }
	else if ( retValue.length < 1 )
		{ return ""; }
	
	if ( chr ) 
		var trimchar = chr;
	else 
		var trimchar = "0";

	var ch = retValue.substring(retValue.length-1, retValue.length);
	
 	while (trimchar.indexOf(ch) != -1)
  {
		retValue = retValue.substring(0, retValue.length-1);
    ch = retValue.substring(retValue.length-1, retValue.length);
  }
	
	if ( retValue == "" )
		{ return ""; }
	else
		{ return retValue; }
}

// archived

function trimOld( str, id ) 
{
	if ( typeof str != "string" ) 
		{ return str; }
	else if ( str.length < 1 )
		{ return ""; }
		
	str = LTrim(RTrim(str));
	
	if ( id != "txa" )
	{
		var doublespace = "  ";
		
		while (str.indexOf(doublespace) != -1)
			{ str = str.substring(0, str.indexOf(doublespace)) + str.substring(str.indexOf(doublespace)+1, str.length); }
	}
  
	if ( str == "" )
		{ return ""; }
	else
		{ return str; }
}

function regTrimOld(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, " "); }
   return temp;
}