/****************************************************************************************************
																																					Librairie JAVASCRIPT

	Version 1.0 (05/10/2000) :
	function replaceByEnd(pString, pValueToReplace, pNewValue)
	function trim(pString)
	function isCorrectEmailString(pString)
	function isAlphaNumeric(pString)
	function trimAlphaNumeric(pString)
	function isFieldAlphaValid(pField, pFieldName, pFieldLengthMin, pFieldLengthMax, isFieldMandatory)
	
	Version 1.1 (23/01/2001) :
	function StringTokenizer(pString, pDelimiter)

****************************************************************************************************/


/****************************************************************************************************
	function replaceByEnd(pString, pValueToReplace, pNewValue)
	Cette fonction permet de remplacer la dernière sous-chaine de caractères (pValueToReplace) 
	contenue dans une chaine de caractères (pString) par une nouvelle sous-chaine (pNewValue)
	En entrée : pString = une chaine de caractères qui va être modifiée (ex: 'ma moto est morte')
									 pValueToReplace = une sous-chaine de caractères à remplacer (ex: 'mo')
									 pNewValue = une nouvelle sous-chaine de caractères de remplacement (ex: 'fo')
	En sortie : Renvoie une chaine de caractères résultat de l'opération (ex: 'ma moto est forte')

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					29/09/2000 			OLD							IE4+ et Netscape3+				Code original
*/
function replaceByEnd(pString, pValueToReplace, pNewValue) {
	var vString = '';
	
	if ((typeof(pString) == 'string') && (pString.length > 0)) {
		if ((typeof(pValueToReplace) == 'string') && (typeof(pNewValue) == 'string') && (pValueToReplace.length > 0) && (pNewValue.length > 0)) {
			var vPos = pString.lastIndexOf(pValueToReplace);

			if (vPos > -1) {
				vBeginString = pString.substring(0, vPos);
				vEndString = pString.substring(vPos + pValueToReplace.length);
				vString = vBeginString + pNewValue + vEndString;
			}
			else 
				vString = pString;
		}
		else
			vString = pString;
	}
	return vString;
}



/****************************************************************************************************
	function trim(pString)
	Cette fonction permet de nettoyer des espaces de début et de fin la chaine de caractères pString
	En entrée : pString = une chaine de caractères à nettoyer
	En sortie : la chaine de caractères nettoyée

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					25/09/2000 			OLD							IE4+ et Netscape3+				Code original
*/
function trim(pString) {
	var vString = '';

	if ((typeof(pString) == 'string') && (pString.length > 0)) {
	  var vCptStart = 0;
	  while ((vCptStart < pString.length) && (pString.charAt(vCptStart) == ' ')) {
	    vCptStart++;
	  }
	  var vCptEnd = pString.length;
	  while ((vCptEnd > 0) && (pString.charAt(vCptEnd - 1) == ' ')) {
	    vCptEnd--;
	  }
		vString = pString.substring(vCptStart, vCptEnd);
	}
  return vString;
}



/****************************************************************************************************
	function isCorrectEmailString(pString)
	Cette fonction permet de vérifier que la chaine de caractères pString correspond à une adresse eMail
	En entrée : pString = une chaine de caractères qui doit être de la forme *@*.* uniquement (* étant un caractère générique) !
	En sortie : "true" si pString correspond à une adresse eMail et "false" sinon

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					29/09/2000 			OLD							IE4+ et Netscape3+				Code original
*/
function isCorrectEmailString(pString) {
	var vEmailOk = true;
	var vRegExpSyntax =  "^[a-z0-9_\-]+([\.][a-z0-9_\-]+)*@[a-z0-9_\-]+([\.][a-z0-9_\-]+)*[\.][a-z]{2,6}$";
	
	if (!new RegExp(vRegExpSyntax).test(pString)) 
		vEmailOk = false;
	
	/*if ((typeof(pString) == 'string') && (pString.length > 0)) {
		var vPosPoint = pString.indexOf('.');
	  var vPosAt = pString.indexOf('@');
		var vPosLastPoint = pString.lastIndexOf('.');
		var vPosTwoPoints = pString.indexOf('..');
		var vPosSpace = pString.indexOf(' ');
		if (	(pString.length < 6) || // vérifie que l'adresse email comporte au moins 6 caractères (*@*.**)
					 (vPosPoint < 1) || // vérifie que l'adresse email comporte un point et qu'il n'est pas au début
					 (vPosAt < 1) || // vérifie que l'adresse email comporte le @ et qu'il n'est pas au début
					 (vPosLastPoint <= vPosAt + 1) || // vérifie que le dernier point de l'adresse email ne se trouve pas avant le @ et pas non plus juste après le @
					 (vPosLastPoint == pString.length - 1) || // vérifie que le point n'est pas le dernier caractère de l'adresse email
					 (vPosTwoPoints > -1) || // vérifie qu'il n'y a pas deux points qui se suivent dans l'adresse email
					 (vPosSpace > -1)) // vérifie qu'il n'y a pas d'espace dans l'adresse email
			vEmailOk = false;

		vPos = 0;
		while ((vEmailOk) && (vPos < pString.length)) {
			vEmailOk = ((pString.charAt(vPos) > ' ') && (pString.charAt(vPos) < '~'));
			vPos++;
		}
	}
	else
		vEmailOk = false;
	*/
	return vEmailOk;
}



/****************************************************************************************************
	function isAlphaNumeric(pString)
	Cette fonction permet de vérifier que la chaine de caractères définie par pString ne contient que des caractères alphanumériques
	(c'est à dire les caractères: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
	En entrée : pString = une chaine de caractères
	En sortie : "true" si pString ne contient que des caractères alphanumériques et "false" sinon (par ex: -12 renvoie false)

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					02/10/2000 			OLD							IE4+ et Netscape3+				Code original
*/
function isAlphaNumeric(pString) {

	if ((typeof(pString) == 'string') && (pString != '')) {
		var vAsciiString = escape(pString);
		var vIndex = vAsciiString.indexOf('%');
		
		if (vIndex > -1) 
			return false;
		else {
			var vIndex2 = pString.indexOf('*');
			if (vIndex2 < 0) vIndex2 = pString.indexOf('@');
			if (vIndex2 < 0) vIndex2 = pString.indexOf('-');
			if (vIndex2 < 0) vIndex2 = pString.indexOf('_');
			if (vIndex2 < 0) vIndex2 = pString.indexOf('+');
			if (vIndex2 < 0) vIndex2 = pString.indexOf('.');
			if (vIndex2 < 0) vIndex2 = pString.indexOf('/');
		
			if (vIndex2 > -1)
				return false;
		}
		return true;
	}
	else 
		return false;
}



/****************************************************************************************************
	function trimAlphaNumeric(pString)
	Cette fonction permet de vérifier que la chaine de caractères définie par pString ne contient que des caractères alphanumériques
	(c'est à dire les caractères: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
	En entrée : pString = une chaine de caractères
	En sortie : une chaine de caractères vide si pString ne contient que des caractères alphanumériques et 
	un message d'erreur sinon

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					02/10/2000 			OLD							IE4+ et Netscape3+				Code original
*/
function trimAlphaNumeric(pString) {
	var vMessage = '';
	
	if (typeof(pString) == 'undefined') 
		vMessage = 'Le paramètre pString est indéfini';
	else {
		if (typeof(pString) == 'string') {	
			var vAsciiString = escape(pString);
			var vIndex = vAsciiString.indexOf('%');
			
			if (vIndex > -1) {
				var vAsciiChar = unescape(vAsciiString.substring(vIndex, 3));
				vMessage = 'Le caractère \'' + vAsciiChar + '\' (code ascii=' + vAsciiString.substring(vIndex + 1, vIndex + 3) + ') est interdit.';
				//vMessage = 'Le caractère \'' + vAsciiChar + '\' (code ascii=' + vAsciiChar.charCodeAt(0) + ') est interdit.'; // ne fonctionne pas en Netscape 3
			}
			else {
				var vIndex2 = pString.indexOf('*');
				if (vIndex2 < 0) vIndex2 = pString.indexOf('@');
				if (vIndex2 < 0) vIndex2 = pString.indexOf('-');
				if (vIndex2 < 0) vIndex2 = pString.indexOf('_');
				if (vIndex2 < 0) vIndex2 = pString.indexOf('+');
				if (vIndex2 < 0) vIndex2 = pString.indexOf('.');
				if (vIndex2 < 0) vIndex2 = pString.indexOf('/');
			
				if (vIndex2 > -1) {			
					var vAsciiChar = pString.charAt(vIndex2);

					// Code de remplacement à la fonction "charCodeAt()" pour Netscape 3
					if (vAsciiChar == '*') vAsciiCodeChar = '42';
					if (vAsciiChar == '@') vAsciiCodeChar = '40';
					if (vAsciiChar == '-') vAsciiCodeChar = '45';
					if (vAsciiChar == '_') vAsciiCodeChar = '95';
					if (vAsciiChar == '+') vAsciiCodeChar = '43';
					if (vAsciiChar == '.') vAsciiCodeChar = '46';
					if (vAsciiChar == '/') vAsciiCodeChar = '47';

					vMessage = 'Le caractère \'' + vAsciiChar + '\' (code ascii=' + vAsciiCodeChar + ') est interdit.';
					//vMessage = 'Le caractère \'' + vAsciiChar + '\' (code ascii=' + vAsciiChar.charCodeAt(0) + ') est interdit.'; // ne fonctionne pas en Netscape 3
				}
			}
		}
		else
			vMessage = 'Le paramètre pString n\'est pas une chaine de caractères';
	}
	return vMessage;
}



/****************************************************************************************************
	function isFieldAlphaValid(pField, pFieldName, pFieldLengthMin, pFieldLengthMax, isFieldMandatory)
	Cette fonction permet de vérifier que la chaine de caractères correspondant à la valeur du champ pField est correctement 
	formatée et ne contient que des caractères alphabétiques
	En entrée : pField = le nom d'un composant HTML de type <INPUT> (ex: 'document.monForm.monChamp')
									 pFieldName = Le libellé du composant HTML défini par pField
									 pFieldLengthMin = un entier définissant la taille minimale de la valeur du champ
									 pFieldLengthMax = un entier définissant la taille maximale de la valeur du champ
									 isFieldMandatory = un booléen qui définit le caractère obligatoire ou non de la valeur du champ.
									 	S'il est à "true", le champ doit obligatoirement avoir une valeur et non si à "false"
	En sortie : "true" si la valeur du champ est correctement formatée et "false" sinon

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					03/10/2000 			OLD							IE4+ et Netscape3+				Code original
*/
function isFieldAlphaValid(pField, pFieldName, pFieldLengthMin, pFieldLengthMax, isFieldMandatory) {
	var vFieldLength = pField.value.length;
	var vFieldValue = pField.value;
	var vIsValid = true;

	if (vFieldValue.length > 0) {
		var vIndex = -1;
		for (i = 0; i < vFieldValue.length; i++) {
			/* Ne passe pas en Netscape 3
			vCodeAscii = vFieldValue.charCodeAt(i);
			if ( ((vCodeAscii < 65) || (vCodeAscii > 122)) || (( vCodeAscii > 90) && (vCodeAscii < 97)) ) {
			*/
			vCodeAscii = vFieldValue.charAt(i);
			if ( ((vCodeAscii < 'A') || (vCodeAscii > 'z')) || (( vCodeAscii > 'Z') && (vCodeAscii < 'a')) ) {
				vIndex = i;
				break;
			}
		}
		if (vIndex > -1) {
			pField.focus();
			alert('Le champ ' + pFieldName +' ne peut contenir qu\'une valeur alpha.');
			vIsValid = false; 
		}
	}
	
	if (vIsValid)	{
		if (vFieldLength > 0) { 
			if (pFieldLengthMin > 0) { 
				if (vFieldLength < pFieldLengthMin) {
					alert('La taille minimum du champ ' + pFieldName + ' est de ' + pFieldLengthMin + ' caractères.'); 
					vIsValid = false; 
				} 
			}
			if (vFieldLength > pFieldLengthMax) {
				alert('La taille maximum du champ ' + pFieldName + ' est de ' + pFieldLengthMax + ' caractères.'); 
				vIsValid = false; 
			} 
		}
		else { // if (vFieldLength > 0)
			if (isFieldMandatory) {
		  	alert('Le champ ' + pFieldName + ' est obligatoire.'); 
				vIsValid = false;
			}
		}
	}
	
	if (!vIsValid) pField.focus();

	return vIsValid;
}


/****************************************************************************************************
	function StringTokenizer(pString, pDelimiter)
	Cette fonction permet d'extraire plusieurs chaines de caractères d'une chaine de caractère initiale en fonction d'un délimiteur.
	En réalité, cette fonction n'en est pas une, il s'agit d'un objet contenant deux champs :
	- tokens = un tableau regroupant les chaines découpées de la chaine initiale pString en fonction du délimiteur pDelimiter
	- countTokens = le nombre d'éléments du tableau tokens
	En entrée : pString = la chaine de caractères qui va être découpée (ex: 'ma moto est morte')
									 pDelimiter = une sous-chaine de caractères servant de délimiteur (ex: ' ')
	En sortie : Renvoie une instance de cet objet StringTokenizer

	Exemple de code :
		var vParams  = new StringTokenizer('maFonction(a,b,c)', ',');
		if (vParams.tokens != null) { // La fonction a des paramètres entrants
			for (var i = 0; i < vParams.countTokens; i++) {
				vParams.tokens[i] = trim(vParams.tokens[i]);
			}
		}

	Version				Date								Auteur					Navigateurs										Description des modifications
	---------		 ----------------		-----------			--------------------------		--------------------------------------------------
		1.0 					23/01/2001 			OLD							IE4+ et Netscape3+				Code original
*/
function StringTokenizer(pString, pDelimiter) {
	this.tokens = new Array();
	this.countTokens = 0;

	if ((typeof(pString) == 'string') && (typeof(pDelimiter) == 'string') && (pString != '') && (pDelimiter != '') && (pString.length >= pDelimiter.length)) {
		var vOldPos  = pString.indexOf(pDelimiter);

		if (vOldPos > -1) { // Le delimiter existe dans la chaine
			this.tokens[this.countTokens] = pString.substring(0, vOldPos);
			vOldPos++;
			this.countTokens++;
			while ((vPos  = pString.indexOf(pDelimiter, vOldPos)) > -1) {
				this.tokens[this.countTokens] = pString.substring(vOldPos, vPos);
				vOldPos = vPos + 1;
				this.countTokens++;
			}
			this.tokens[this.countTokens] = pString.substring(vOldPos);
			this.countTokens++;
		}
		else { // Le delimiter n'existe pas dans la chaine
			this.tokens[this.countTokens] = pString;
			this.countTokens++;
		}
		return this;
	}
	else {
		this.tokens = null;
		return;
	}
}