function validateAmount(divId, objVal, acceptZero) {
	var amount = objVal.value;
	var amount2 = removeCharacter(amount); 
	newVal=""; 
	
	amount=newVal;	
	newVal=trimAll(amount2); 

	if (objVal.value != "")
	{ 	
		if(!IsAmountFieldFormatValid(objVal) || (!acceptZero && (objVal.value <= 0)) )
		{ 
			document.getElementById(divId).innerHTML = j0174;  
			document.getElementById(divId).style.display='block';
			return false;
		} else{
			objVal.value=newVal;
			document.getElementById(divId).innerHTML = '&nbsp;';  
			document.getElementById(divId).style.display='none';
			return true;
		}
	}
	else { 
		document.getElementById(divId).innerHTML = '&nbsp;';  
	}
	return true;
}


function trimAll( strValue ) { 
 var objRegExp = /^(\s*)$/; 
 
    //check for all spaces 
    if(objRegExp.test(strValue)) { 
       strValue = strValue.replace(objRegExp, ''); 
       if( strValue.length == 0)
          return strValue; 
    } 
     
   //check for leading & trailing spaces 
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/; 
   if(objRegExp.test(strValue)) { 
       //remove leading and trailing whitespace characters 
       strValue = strValue.replace(objRegExp, '$2'); 
    } 
  return strValue; 
}

function removeCharacter (strValue) 
{ 
 var objRegExp = / /g;  
 var objRegExpVirgule=/,/g;
   
   //replace all matches with empty strings 
   if(objRegExp.test(strValue)) 
   {  
		return strValue.replace(objRegExp,''); 
   } 
   else if (objRegExpVirgule.test(strValue))
   {
   		return strValue.replace(objRegExpVirgule,''); 
   }
   	else return strValue; 
}

// **********************************************************************************
// DEBUT DE LA LIBRAY DE FORMATAGE ET VALIDATION DE MONTANT MONETAIRE
// AUTEUR           : Martin Rajotte
// POUR             : Banque Nationale du Canada
// DATE D'ECRITURE  : 16 JAN 2004
//
//  DESCRIPTION DU PROGRAMME: Fct. divers pour le traitement des valeurs monetaire en JavaScript.
//  
//  boolean IsAmountFieldFormatValid(TextBox txtField): Valide le contenue d'un TextBox pour savoir si c'est une valeur monetaire valide. 
//                                                      Si valide corrige les leading et taling zero.
//
//  String formatAmountLeadingZeroes(String str):       Eneve les zero non sigificatif avant le point d'une string contenant une valeur monetaire valide.
//
//  String formatAmountTailingZeroes(String str):       Formate une valeur monetaire valide avec 2 digit apres le point.
//
//  boolean IsAmountFormatValid(String strValue):       Valide le contenue d'une String pour savoir si c'est une valeur monetaire valide. 
//
//  String AmountValidToNumericFormat(String strValue)  Converti une valeur monetaire valide et retourne un 
//                                                      String contenant la valeur recu SANS les SPACE et les VIRGULE ex: 5000.55
//
//  Integer AmountValidToRawInt(String strValue)        Converti une valeur monetaire valide et retourne un 
//                                                      String contenant la valeur recu SANS les SPACE , les VIRGULE , le POINT et
//                                                      Multiplie par 100
//
//  String rawIntToStrAmount(Integer iValue)            Divise le rawInt par 100 et le convertit en String ex: 500 ==> 5.00
//
// **********************************************************************************


// **********************************************************************************
// *	function IsAmountFieldFormatValid(txtField)
// *	Entre: champ contenant une valeur monetaire
// *	Sortie: True: Si le format est valide False si invalide
// *         Si Format Valide applique le formatAmountLeadingZeroes et formatAmountTailingZeroes
// *         Sur la VALEUR du CHAMP
// **********************************************************************************

function IsAmountFieldFormatValid(txtField) {
	var strFieldValue = txtField.value;

	if(IsAmountFormatValid(strFieldValue)){

		
		strFieldValue = formatAmountLeadingZeroes(strFieldValue);
		
		// Desactive pour ne pas ajouter le .00 a la fin		
		// strFieldValue = formatAmountTailingZeroes(strFieldValue) 
		
		
		txtField.value = strFieldValue;
		return true;
	}
	
	return false;
}

// **********************************************************************************
// *	function formatAmountLeadingZeroes(str)
// *	Entre: string contenant une valeur monetaire VALIDE
// *	Sortie: string contenant la valeur monetaire  sans les zero non sigificatif avant le point.
// **********************************************************************************

function formatAmountLeadingZeroes(str) {
	if (str == "") {
		return str;
	}
	else {
		while (str.charAt(0) == '0' || str.charAt(0) == ',') {
			str = str.substring(1,str.length);
		}
		//si on a un nombre comme 0.50 on laisse le 0 devant
		if (str.charAt(0) == '.') {
			str = '0' + str;
		}
		if (str == "") {
			return "0";
		}
		else {	
			return str;
		}
	}
}

// **********************************************************************************
// *	function IsAmountFormatValid(strValue)
// *	Entre: String contenant une valeur monetaire
// *	Sortie: True: Si le format est valide
// *	Sortie: False: Si le format est invalide
// *	Dans les champs de saisies monetaire, les formats acceptes sont :
// *
// *		999 999 999
// *		999 999 999.9
// *		999 999 999.99
// *
// *		999,999,999
// *		999,999,999.9
// *		999,999,999.99
// *
// *		999999999
// *		999999999.9
// *		999999999.99
// *
// *		         .9
// * - Les leading zero non significatif doivent etre enleve meme si il sont considere comme valide.
// * - Il doit y avoir 2 decimal apres le point meme si des decimal manquant sont considere comme valide.
// **********************************************************************************

function IsAmountFormatValid(strValue) {
	var strDecimal = ".";
	var strMillieme = ",";
	var strMilliemeSpace = " ";
	var iDecimalPos = strValue.indexOf(strDecimal);
	var iDecimalDigit = 0;

	var strValid    = " ,.0123456789";
	var iStrDecimalCount = 0;
	var iStrMilliemeCount = 0;
	var iStrMilliemeSpaceCount = 0;
	
	var strIntegerPart = "";
	var strDecimalPart = "";		
	
	// Valider si string vide
	if (strValue == "") {
		return false;
	}

	// valider les caracteres entre (0-9 . , )
	for (j=0;j<strValue.length;j++)	{
		if (strValid.indexOf(strValue.charAt(j))==-1) {
			return false;
		}

		if (strValue.charAt(j) == strDecimal) {
			iStrDecimalCount = iStrDecimalCount + 1;
		}

		if (strValue.charAt(j) == strMillieme) {
			iStrMilliemeCount = iStrMilliemeCount + 1;
		}

		if (strValue.charAt(j) == strMilliemeSpace){
			iStrMilliemeSpaceCount = iStrMilliemeSpaceCount + 1;
		}

	}

	// Valider si la string contient plus d'un point
	if (iStrDecimalCount > 1) {
		return false;
	}

	// Valider si la string contient des Spaces et des virgules
	if ( (iStrMilliemeCount > 0) && (iStrMilliemeSpaceCount > 0)) {
		return false;
	}

	// ***** Si il y a un point, on verifie le format de la partie decimal ***** 
	if (iStrDecimalCount == 1) {
		var strValidDecimalPart = "0123456789";

		strDecimalPart = strValue.substring(iDecimalPos + 1);  // on prend ce qu'il y a apres le point
		strIntegerPart = strValue.substring(0,iDecimalPos);  // on prend ce qu'il y a avant le point (assignation de la partie entiere incluant les spaces et separteur de milier)

		// valider les caracteres entre (0-9) pour la partie decimal
		for (j=0;j<strDecimalPart.length;j++) {
			if (strValidDecimalPart.indexOf(strDecimalPart.charAt(j))==-1) {
				return false;
			}
		}
				
		// Un ValidAmount contient 0 ou 1 ou 2 chiffre dans la partie decimal
		iDecimalDigit = strDecimalPart.length;
		if ((iDecimalDigit == 0) || (iDecimalDigit > 2)){
			return false;
		}
				
	}
	else {  // Si il n'y a pas de point
		strIntegerPart = strValue  // assignation de la partie entiere incluant les spaces et separteur de milier
	}

	// ***** Verification de la partie entiere

	if ( (iStrMilliemeCount > 0) || (iStrMilliemeSpaceCount > 0)) {  // Si la partie entiere contient des strMillieme ou des strMilliemeSpace
		var strMilliemChar = "";
			
		if (iStrMilliemeCount > 0) {
			strMilliemChar = strMillieme;
		}
		if (iStrMilliemeSpaceCount > 0) {
			strMilliemChar = strMilliemeSpace;
		}

		// Valider si il y a une leading virgule ou space invalid
		if (strIntegerPart.charAt(0) == strMilliemChar){
			return false;
		}

		// Valider si il y a une leading virgule ou space invalid
		if (strIntegerPart.charAt(strIntegerPart.length-1) == strMilliemChar){
			return false;
		}
		
		var iPosIndex = 0;
		var iPosInStrIntegerPart = strIntegerPart.length -1;
			
		// On teste la position des strMilliemChar
		
		var milipos = strIntegerPart.indexOf(strMilliemChar);
		if (milipos > 3){
			return false;
		}
		
		for (var i=milipos+1; i < strIntegerPart.length; i++){
			if (strMilliemChar == strIntegerPart.charAt(i)){
				if (i-milipos!=4){
					return false;
				} else {
					milipos = i;
				}				
			}
		}
		
		if (strIntegerPart.length-milipos!=4){
			return false;
		}
		
		while (iPosInStrIntegerPart > -1) {
			if ((iPosIndex == 3) && (strMilliemChar != strIntegerPart.charAt(iPosInStrIntegerPart))) {
				return false;
			}
			iPosInStrIntegerPart = iPosInStrIntegerPart -1;
			iPosIndex = iPosIndex + 1
			if (iPosIndex > 3) { // on reset le count 
				iPosIndex = 0;
			}
		}
	}
	return true;
}
