//similar to VB's trim function
function trimString (str) {
  while (str.charAt(0) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
  return str;
}

function inString(start, stringBeingSearch, stringToSearchFor){
	
	return stringBeingSearch.indexOf(stringToSearchFor, start) + 1;

}

//-- replaceChars only works with IE5 and newer browsers
function replaceChars(entry,find,replacewith,ignorecase) {      
	var expParameters;       

    entry = "" + entry;    

    if (ignorecase) {
      expParameters = 'gi';
    } else {
      expParameters = 'g';
    }

    var replaceRegExp = new RegExp(find,expParameters);

    entry = entry.replace(replaceRegExp,replacewith);

    return entry;    
}

//-- for older browsers, use oldreplaceChars
function oldreplaceChars(entry,find,replacewith) {    
    entry = "" + entry;    
    
    while (entry.indexOf(find)>-1) {
      pos = entry.indexOf(find);
      entry = "" + (entry.substring(0, pos) + replacewith + entry.substring((pos + find.length), entry.length));
    }
	return entry;    
}

function leftString(stringSource, number){	
	if (number > stringSource.length)	{

		number = stringSource.length;
	}
		
	return stringSource.substring(0, number);
}

function rightString(stringSource, number){
		
	if (number > (stringSource.length)) {

		number = stringSource.length;
	}
		
	return stringSource.substring(stringSource.length - number, stringSource.length);
}

function getFormattedName(fullName,rtnValue) {

	var fName = '';
	var lName = '';	

	// Take out any extra spaces
	fullName = trimString(fullName);

	if (fullName.length > 0) {            

	  // Remove any periods
	  if (fullName.charAt((fullName.length) - 1) == '.') {
		fullName = fullName.substring(0, fullName.length - 1);
	  }

	  //Take out any extra spaces
	  fullName = trimString(fullName);      

	  //Strip out Jr suffixes (if it exists)      
	  if (fullName.substr(fullName.length - 2,2) == 'Jr') {
		fullName = fullName.substring(0, fullName.length - 3);
	  }

	  //Take out any extra spaces
	  fullName = trimString(fullName);   

	  //Strip out Sr suffixes (if it exists)      
	  if (fullName.substr(fullName.length - 2,2) == 'Sr') {
		fullName = fullName.substring(0, fullName.length - 3);
	  }

	  //Take out any extra spaces
	  fullName = trimString(fullName);   

	  //Strip out III suffixes (if it exists)      
	  if (fullName.substr(fullName.length - 3,3) == 'III') {
		fullName = fullName.substring(0, fullName.length - 4);
	  }

	  //Take out any extra spaces
	  fullName = trimString(fullName);   

	  //Strip out II suffixes (if it exists)      
	  if (fullName.substr(fullName.length - 2,2) == 'II') {
		fullName = fullName.substring(0, fullName.length - 3);
	  }      

	  //Get first name
	  space = inString(0,fullName,' ');
	  if (space > 0) {
		fName = leftString(fullName, space - 1);	
	  } else {
		fName = ' ';
	  }	  

	  //Get last name
	  if (space > 0) {
		lName = fullName.substring(space, fullName.length);
	  } else {
		lName = fullName;
	  }
	  
	  space = inString(0,lName,' ');
	  if (space > 0){
		  lName = rightString(lName, lName.length - space);
		  lName = trimString(lName);
	  }


	}

	if (rtnValue == 'f') {
	  return fName;
	} else {
	  return lName;
	}
}

//-- JavaScript code written by Alan Simpson - www.coolnerds.com
function formatCurrency(anynum) {
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr 
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return "$"+retval
}


