// Function pour obtenir la traduction d'un tag
function get_Traduction(dRoot, LibelleEst) {
	var tagEst = "LANG/LIB[@TYP='" + LibelleEst + "']/TX[@LG=../../../INFO_GEN/LG]"
	var TraductionEst = LibelleEst;
	if (dRoot.getElementsByTagName(tagEst).item(0)!=null) {
		TraductionEst = dRoot.getElementsByTagName(tagEst).item(0).text;
	}
	return TraductionEst;
}

// Function to check the maximum number of character in a text field
function checkchars(champ,nb) {
	var maxchar=nb;
	var nbtotal=champ.value.length;
	var TxtAlert="";
	
	if (nbtotal > maxchar) {
		var LibelleEst = "Alert_The_Maximum_Number_Of_Character_Is";
		TxtAlert = get_Traduction(dRoot, LibelleEst);
		TxtAlert=TxtAlert+ " " + maxchar +".";
		
		LibelleEst = "Alert_Your_Number_Of_Character_Is";
		TxtAlert=TxtAlert+ " " + get_Traduction(dRoot, LibelleEst) + " " + nbtotal+".";
				
		LibelleEst = "Alert_Please_Shorten_Your_Text";
		TxtAlert=TxtAlert+ " " + get_Traduction(dRoot, LibelleEst);
		
		alert(TxtAlert);
 		champ.focus();
 		return false;
   	}
	else return true;
}

// Function to check if it is a number
function checkNumb(field) {
	// remplace la virgule par un point
	replaceChars(field);
	
	var valid = "-.0123456789"
	var ok = "yes";
	var temp;
	var TxtAlert="";
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
			}
	if (ok == "no") {
		var LibelleEst = "Alert_This_Field_Must_Be_A_Number";
		TxtAlert = get_Traduction(dRoot, LibelleEst);
		alert(TxtAlert);
		field.focus();
		//field.select();
  	}
}

// Function to check if it is a number
function checkNumbEntier(field) {
	// remplace la virgule par un point
	replaceChars(field);
	
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	var TxtAlert="";
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
			}
	if (ok == "no") {
		var LibelleEst = "Alert_This_Field_Must_Be_An_Integer";
		TxtAlert = get_Traduction(dRoot, LibelleEst);
		alert(TxtAlert);
		field.focus();
		//field.select();
  	}
}

// alert if the line has not been selected
function checkSelected(field) {
	if (field==''){
		var LibelleEst = "Alert_Click_On_A_Line";
		TxtAlert = get_Traduction(dRoot, LibelleEst);
		alert(TxtAlert);
		return false;
	}
}

// Select a Value from a List
function CopyValueToList(FieldToCopy,elementid) {
	//var i = FieldToCopy.selectedIndex;
	//var value = FieldToCopy.options[i].value;

	var value = FieldToCopy;
	for ( i = 0; i < elementid.length; i++) {
		if (value == elementid.options[i].value) {
			elementid.options[i].selected = true;
			return;
		}
	}
}

// Copy a value from a list to another
function CopyValue(FieldToCopy,elementid) {
	var i = FieldToCopy.selectedIndex;
	var value = FieldToCopy.options[i].value;

	for ( i = 0; i < elementid.length; i++) {
		if (value == elementid.options[i].value) {
			elementid.options[i].selected = true;
			return;
		}
  	}
}

// Replace the comma by a dot
function replaceChars(entry) {
	out = ","; // replace this
	add = "."; // with this
	temp = "" + entry.value; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	entry.value = temp;
} 

// Replace the problematic characters by non accentuated ones (pb with xml processor)
function checkCharValidAndModify(field) {
	var input=field.value;
	var output = "";
	var numero_char="0";
//alert(input.charCodeAt(0));
	for (var i = 0; i < input.length; i++) {
		var okBonChar=1;
		// Convertit les Retours a la ligne en espace
		if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {	
			i++;
			output += " ";
			okBonChar=0;
		} 
		else {
			numero_char=input.charCodeAt(i);
   			// Remplace les accents et les caracteres speciaux mal codes sous XML
			switch (numero_char) {
				// e accent aigu
				case 233: {
					output += "e";
   					okBonChar=0;
   				}
   				break;
   				// e accent grave
				case 232: {
					output += "e";
   					okBonChar=0;
   				}
   				break;
				// a accent aigu
				case 224: {
					output += "a";
   					okBonChar=0;
   				}
   				break;
				// e accent circonflexe
				case 234: {
					output += "e";
   					okBonChar=0;
   				}
   				break;
   				// u accent grave
				case 249: {
					output += "u";
   					okBonChar=0;
   				}
   				break;
   				//c cedille
				case 231: {
					output += "c";
   					okBonChar=0;
   				}
   				break;
   				// e trema
				case 235: {
					output += "e";
   					okBonChar=0;
   				}
   				break;
   				// '
				case 39: {
					output += " ";
   					okBonChar=0;
   				}
   				break;
   				// o accent circonflexe
				case 244: {
					output += "o";
   					okBonChar=0;
   				}
   				break;
   				// le signe EURO
				case 8364: {
					output += "EUR";
   					okBonChar=0;
   				}
   				break;
   				// le signe Numero 
				case 176: {
					output += " ";
   					okBonChar=0;
   				}
   				break;
				// u accent circonflexe
				case 251: {
					output += "u";
   					okBonChar=0;
   				}
   				break;
   				// a accent circonflexe
				case 226: {
					output += "a";
   					okBonChar=0;
   				}
   				break;
   				// i accent circonflexe
				case 238: {
					output += "i";
   					okBonChar=0;
   				}
   				break;
   			}
   		}
   		
   		// si ok met le chararcter original
   		if (okBonChar==1) {
   			output += input.charAt(i);
   		} 				   		
	}
	field.value=output;
	//alert('oups')
}

