//Derniere modification le 23/07/2003.

/* Fonctions presentes dans ce fichier:
   trim(string)
   lTrim(string)
   rTrim(string)
   replaceAll(strOld, strFind, strReplace)
   countCharacter(strTxt, strFind) */


// ============================================================================
// trim(string) : Returns a copy of a string without leading or trailing spaces
//
// PURPOSE: Remove trailing and leading blanks from our string.
// IN: str - the string we want to trim
// RETVAL: a trimmed string
// ============================================================================

function trim(str) {
	return rTrim(lTrim(str));
}


// ============================================================================
// lTrim(string) : Returns a copy of a string without leading spaces.
//
// PURPOSE: Remove leading blanks from our string.
// IN: str - the string we want to lTrim
// ============================================================================

function lTrim(str) {
	// We don't want to trip JUST spaces, but also tabs,
	// line feeds, etc. Add anything else you want to
	// "trim" here in Whitespace
	var whitespace = new String(" \f\n\r\t");

	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
		// We have a string with leading blank(s)...

		var j=0, i = s.length;

		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
			j++;
		}

		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}

	return s;
}


// ============================================================================
// rTrim(string) : Returns a copy of a string without trailing spaces.
//
// PURPOSE: Remove trailing blanks from our string.
// IN: str - the string we want to rTrim
// ============================================================================

function rTrim(str) {
	// We don't want to trip JUST spaces, but also tabs,
	// line feeds, etc.  Add anything else you want to
	// "trim" here in Whitespace
	var whitespace = new String(" \f\n\r\t");

	var s = new String(str);

	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		// We have a string with trailing blank(s)...

		// Get length of string
		var i = s.length - 1;

		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
			i--;
		}

		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}

	return s;
}


// ---------------------------------------
// replaceAll(strOld, strFind, strReplace)
// ---------------------------------------
// Fonction : remplace toutes les occurences de strFind contenues dans strOld par strReplace.
// IN : strOld : string dans laquelle s'effectue la recherche
//      strFind : string a rechercher
//      strReplace : string qui remplace strFind

function replaceAll(strOld, strFind, strReplace) {
	var position = 0;
	var strNew = '';

	while(strOld.indexOf(strFind, position) != -1) {
		strNew += strOld.substring(position, strOld.indexOf(strFind, position));
		strNew += strReplace;
		position = (strOld.indexOf(strFind, position) + strFind.length);
	}

	strNew += strOld.substring(position, strOld.length);
	return strNew;
}


// ---------------------------------------
// countCharacter(strTxt, strFind)
// ---------------------------------------
// Fonction : compte le nombre d'occurences d'un caractere dans une chaîne.
// IN : strTxt : string dans laquelle s'effectue la recherche
//      strFind : string a rechercher
// OUT : le nombre d'occurences de strFind dans strTxt

function countCharacter(strTxt, strFind) {
	var position = 0;
	var counter = 0;

	while(strTxt.indexOf(strFind, position) != -1) {
		position = (strTxt.indexOf(strFind, position) + strFind.length);
		counter++;
	}

	return counter;
}