/* -----------------------------------------------------------------------------------------------
Author:	Brent McClintock - bmcclintock@legis.state.pa.us
Date:	07/23/2002
Purpose: collection of general string functions
Includes: 	
	1) LTrim(string)
	2) RTrim(string)
	3) Trim(string)
	4) stripZeros(string)
	5) compareTextToSelect(dropdown id, textbox id)
----------------------------------------------------------------------------------------------- */

//trim whitespace off the LEFT of a string and return it	
function LTrim(str)
{
	for(i=0; str.substr(i,1) == " " && i<str.length; i++)
	{
		if( str.substr(i,1) == " " )
		{ 
			str = str.substr(i+1,str.length-1); 
			i--;
		}
	}
	return str;
}
//-----------------------------------------------------------------

//trim whitespace off the RIGHT of a string and return it
//changed RTrim(str) function - found bug. It wasn't taking the last space off of the value.
// pjg 6/2004  changed from a for loop to a while loop.
function RTrim(str)
{
	while(str.substr(str.length-1,1) == " ")
	{ str = str.substr(0,str.length-1); }
	return str;
}
//-----------------------------------------------------------------

//trim whitespace off BOTH SIDES of a string and return it	
function Trim(str) 
{ return LTrim(RTrim(str)); }     

//-----------------------------------------------------------------

//strips LEADING zeros from string
function stripZeros(str)
{
	for(i=0; str.substr(i,1) == "0" && i<str.length; i++)
	{
		if( str.substr(i,1) == "0" )
		{ 
			str = str.substr(i+1,str.length-1); 
			i--;
		}
	}
	return str;
}

//---------------------------------------------------------
//use this function when you want to test a value in a text box against a value in a list box.
//I created this function to validate when using Brent's suggest function.
//pjg 06/27/2007 
function compareTextToSelect(listId, textId){
	var matches = 0;  //switch which will be set if we have a match.			
	var reg = new RegExp("me", "i");	//create a regular expression object.
	var pattern = ''; //initialize pattern variable
	var listObj = document.getElementById(listId);
	var txtObj = document.getElementById(textId);
	//alert(document.getElementById(textId).value + " - " + document.getElementById(listId).value);
	var txtVal = txtObj.value;
	for(var i=0;i<listObj.length;i++){  //loop through the list to see if we have a match.
		pattern = "^" + listObj.options[i].text + "$";	//create the pattern using the value in the dropdown.
		//alert('pattern = ' + pattern + "; textVal = " + txtVal);
		reg.compile(pattern, "i");	//replace the pattern that we had before in the reg exp object.
		if(reg.test(txtVal)){	//test the pattern against the text in the text box.
			matches = 1;	//if it matches, set switch and get out of here.
			break;
		}
	}
	//alert("Matches? " + matches);
	if(matches == 0){	//if we didn't find a hit, send an alert message.
		alert("The value you have typed in does not match any values provided.");
		txtObj.focus();
		txtObj.select();
		return false;
	} else {
		return true;
	}	
}//end function compareTextToSelect()
//---------------------------------------------------------
