//*****************************************************************************
//
// Fonctions de contrôle des champs ('input' des formulaires Html).
// Toutes ces fonctions gèrent:
//          - positionnement des propriétés de chaque input (surbrillance,
//            obligatoire, numerique,...)
//          - contrôle que tous les champs respectent ces propriétés juste
//            avant le submit du formulaire.
//
// Date de création      : 21/03/2002
// Date de modification  :
// Auteur                : Mohammed Alkass.
// Version 1.0
//
// Date de modification  : 28/04/2003 - migration pour struts
// Auteur                : Thong Daniel
// Version 1.1
//
//
//*****************************************************************************

// txt par defaut
var txtInputObligatoire="Le champ <b>(0)</b> est obligatoire.";
var txtInputNumeric="Le champ <b>(0)</b> doit contenir un nombre avec ou sans s&eacute;parateur decimal.";
var txtInputEntier="Le champ <b>(0)</b> doit contenir un entier";
var txtInputDecimal="Le champ <b>(0)</b> doit contenir un num&eacute;rique avec chiffres <b>(1)</b> apr&egrave;s le point.";
var txtInputSuperieur="Le champ <b>(0)</b> doit &ecirc;tre sup&eacute;rieur ou &eacute;gal &agrav; <b>(1)</b>.";
var txtInputSuperieurStrict="Le champ <b>(0)</b> doit &ecirc;tre sup&eacute;rieur strict &agrav; <b>(1)</b>.";
var txtInputInferieur="Le champ <b>(0)</b> doit &ecirc;tre inf&eacute;rieur ou &eacute;gal &agrav; <b>(1)</b>.";
var txtInputInferieurStrict="Le champ <b>(0)</b> doit &ecirc;tre inf&eacute;rieur strict &agrav; <b>(1)</b>.";
var txtInputDate = "Le champ <b>(0)</b> doit &ecirc;tre une date au format jj/mm/aaaa et la date doit &ecirc;tre coh&eacute;rente.";
var txtInputHeure = "Le champ <b>(0)</b> doit &ecirc;tre une heure au format hh:mm et l'heure doit &ecirc;tre coh&eacute;rente.";
var txtInputLongueurMax= "La longueur du champ <b>(0)</b> doit &ecirc;tre inf&eacute;rieure &agrave; <b>(1)</b>."
var txtInputLongueurMin= "La longueur du champ <b>(0)</b> doit &ecirc;tre sup&eacute;rieure &agrave; <b>(1)</b>."
var txtInputLongueurExacte= "La longueur du champ <b>(0)</b> doit &ecirc;tre de <b>(1)</b>."
var txtInputDoubleListeObligatoire="Vous devez selectionner au moins 1 <b>(0)</b>."
var txtInputDoubleListeMin="Vous devez selectionner au moins (0) <b>(1)</b>."
var txtInputDoubleListeMax="Vous devez selectionner au plus (0) <b>(1)</b>."

//ajout de sne
function input_smartAddSne(oSneList, sne) {
	if (oSneList=="") return sne
	var sneArray=oSneList.split("|");
	for(var i=0; i<sneArray.length; i++) {
		if (sneArray[i]==sne) return oSneList
	}
	return oSneList+"|"+sne
}
function input_smartDelSne(oSneList, sne) {
	var result="";
	if (oSneList!="") {
		var sneArray=oSneList.split("|");
		for(var i=0; i<sneArray.length; i++) {
			if (sneArray[i]!=sne) {
				if (result.length>0) {
					result=result+"|"
				}
				result=result+sneArray[i]
			}
		}
	}
	return result
}
//setter obligatoire
function input_setObligatoire(oInput) {
	oInput.obligatoire=true;
}

function input_setDoubleListeObligatoire(oInput) {
	oInput.doubleListeObligatoire=true;
}
function input_setDoubleListeMin(oInput, value) {
	oInput.doubleListeMin=true;
	oInput.doubleListeMinValue=value;	
}
function input_setDoubleListeMax(oInput, value) {
	oInput.doubleListeMax=true;
	oInput.doubleListeMaxValue=value;	
}

//setter decimal (ie numerique avec n decimal)
function input_setDecimal(oInput, decimal) {
	input_setNumeric(oInput);
	oInput.decimal=true;
	oInput.decimalValue=decimal;
}
//setter decimal (ie numerique avec 0 decimal)
function input_setEntier(oInput) {
	input_setNumeric(oInput);
	oInput.entier=true;
}
//setter numeric (que des chiffres)
function input_setNumeric(oInput) {
	oInput.numeric=true;
}
//setter numeric superieur ou egal
function input_setSuperieur(oInput, value) {
	input_setNumeric(oInput);
	oInput.superieur=true;
	oInput.superieurValue=value;	
}
//setter numeric superieur strict
function input_setSuperieurStrict(oInput, value) {
	input_setNumeric(oInput);
	oInput.superieurStrict=true;
	oInput.superieurValue=value;
}
//setter numeric inferieur ou egal
function input_setInferieur(oInput, value) {
	input_setNumeric(oInput);
	oInput.inferieur=true;
	oInput.inferieurValue=value;
}
//setter numeric inferieur strict
function input_setInferieurStrict(oInput, value) {
	input_setNumeric(oInput);
	oInput.inferieurStrict=true;
	oInput.inferieurValue=value;
}
//setter date coherente
function input_setDate(oInput) {
	oInput.date=true;
}
//setter heure coherente
function input_setHeure(oInput) {
	oInput.heure=true;
}
//setter longueur max du champ
function input_setLongueurMin(oInput, value) {
	oInput.longueurMin=true;
	oInput.longueurMinValue=value;
}
//setter longueur min du champ
function input_setLongueurMax(oInput, value) {
	oInput.longueurMax=true;
	oInput.longueurMaxValue=value;
}
//setter longueur exact du champ
function input_setLongueurExacte(oInput, value) {
	oInput.longueurExacte=true;
	oInput.longueurExacteValue=value;	
}

    //*************************************************************************
    //
    // Vérifie tous les champs du formulaire.
    //
    //@param oFormulaire Objet formulaire dont on souhaite valider les champs.
    //@return true si le formulaire est OK et false sinon.
    //
    //*************************************************************************

function input_validerSaisie(oFormulaire){
	for (var i=0; i<oFormulaire.elements.length; i++){
		var oInput = oFormulaire.elements[i];
		if (!input_validerSaisieInput(oInput)) {
			return false;
		}
	}
	return true;
}


    //*************************************************************************
    //
    // Vérification d'un champ du formulaire.
    //
    // Exemple d'utilisation:
    // input_validerSaisieInput(fc.nomInput);
    //
    //*************************************************************************

function input_validerSaisieInput(oInput){
	if (oInput.obligatoire) {
		if ((oInput.type=="select-one") || (oInput.type=="select-multiple")) {
			if (!input_validerObligatoireListe(oInput)) {
				return input_msgErreurListe(oInput, txtInputObligatoire.replace("(0)", oInput.alt));
			}
		}else{
			if (!input_validerObligatoire(oInput)) {
				return input_msgErreur(oInput, txtInputObligatoire.replace("(0)", oInput.alt));
			}
		}
	}

	if (oInput.doubleListeObligatoire && !input_validerObligatoireDoubleListe(oInput)) {
		if (!isUndefined(oInput.from)) return input_msgErreurListe(eval(oInput.from), txtInputDoubleListeObligatoire.replace("(0)", oInput.alt))
		if (!isUndefined(oInput.select)) return input_msgErreurListe(eval(oInput.select), txtInputDoubleListeObligatoire.replace("(0)", oInput.alt))		
	}
	if (oInput.doubleListeMin && !input_validerMinDoubleListe(oInput)) {
		if (!isUndefined(oInput.from)) return input_msgErreurListe(eval(oInput.from), txtInputDoubleListeMin.replace("(1)", oInput.alt).replace("0", oInput.doubleListeMinValue))
		if (!isUndefined(oInput.select)) return input_msgErreurListe(eval(oInput.select), txtInputDoubleListeMin.replace("(1)", oInput.alt).replace("0", oInput.doubleListeMinValue))		
	}
	if (oInput.doubleListeMax && oInput.value != "" && !input_validerMaxDoubleListe(oInput)){
		if (!isUndefined(oInput.to)) return input_msgErreurListe(eval(oInput.to), txtInputDoubleListeMax.replace("(1)", oInput.alt).replace("0", oInput.doubleListeMaxValue))
		if (!isUndefined(oInput.select)) return input_msgErreurListe(eval(oInput.select), txtInputDoubleListeMax.replace("(1)", oInput.alt).replace("0", oInput.doubleListeMaxValue))		
	}

	if (oInput.longueurExacte && oInput.value != "" && !input_validerLongueurExacte(oInput)){
		return input_msgErreur(oInput, txtInputLongueurExacte.replace("(0)", oInput.alt).replace("(1)", oInput.longueurExacteValue));
	}	

	if (oInput.numeric && oInput.value != "" && !input_validerNumeric(oInput)){
		return input_msgErreur(oInput, txtInputNumeric.replace("(0)", oInput.alt));
	}

	if (oInput.entier && oInput.value != "" && !input_validerEntier(oInput)) {
		return input_msgErreur(oInput, txtInputEntier.replace("(0)", oInput.alt));
	}

	if (oInput.decimal && oInput.value != "" && !input_validerDecimal(oInput)){
		return input_msgErreur(oInput, txtInputDecimal.replace("(0)", oInput.alt).replace("(1)", oInput.decimalValue))
	}

	if (oInput.date && oInput.value != "" && !input_validerDate(oInput)){
		return input_msgErreur(oInput, txtInputDate.replace("(0)", oInput.alt));
	}
	
	if (oInput.heure && oInput.value != "" && !input_validerHeure(oInput)){
		return input_msgErreur(oInput, txtInputHeure.replace("(0)", oInput.alt));
	}
	
	if (oInput.superieur && oInput.value != "" && !input_validerSuperieur(oInput)){
		return input_msgErreur(oInput, txtInputSuperieur.replace("(0)", oInput.alt).replace("(1)", oInput.superieurValue));
	}

	if (oInput.superieurStrict && oInput.value != "" && !input_validerSuperieurStrict(oInput)){
		return input_msgErreur(oInput, txtInputSuperieurStrict.replace("(0)", oInput.alt).replace("(1)", oInput.superieurValue));
	}

	if (oInput.inferieur && oInput.value != "" && !input_validerInferieur(oInput)){
		return input_msgErreur(oInput, txtInputInferieur.replace("(0)", oInput.alt).replace("(1)", oInput.inferieurValue));
	}

	if (oInput.inferieurStrict && oInput.value != "" && !input_validerInferieurStrict(oInput)){
		return input_msgErreur(oInput, txtInputInferieurStrict.replace("(0)", oInput.alt).replace("(1)", oInput.inferieurValue));
	}

	if (oInput.longueurMax && oInput.value != "" && !input_validerLongueurMax(oInput)){
		return input_msgErreur(oInput, txtInputLongueurMax.replace("(0)", oInput.alt).replace("(1)", oInput.longueurMaxValue));
	}

	if (oInput.longueurMin && oInput.value != "" && !input_validerLongueurMin(oInput)){
		return input_msgErreur(oInput, txtInputLongueurMin.replace("(0)", oInput.alt).replace("(1)", oInput.longueurMinValue));
	}

	if (oInput.longueurExacte && oInput.value != "" && !input_validerLongueurExacte(oInput)){
		return input_msgErreur(oInput, txtInputLongueurExacte.replace("(0)", oInput.alt).replace("(1)", oInput.longueurExacteValue));
	}		
	
	//par défaut, on retourne true
	return true;

}
    //*************************************************************************
    //
    // Vérifie si le champ oInput est vide.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est renseigné et false sinon.
    //
    //*************************************************************************
function input_validerObligatoire(oInput){
	return (oInput.value.length != 0);
}

    //*************************************************************************
    //
    // Vérifie si un élément de la la liste est renseigné.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est renseigné et false sinon.
    //
    //*************************************************************************
function input_validerObligatoireListe(oInput){
	return (oInput.selectedIndex >= 0);
}

    //*************************************************************************
    //
    // Gestion des doubles listes
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est renseigné et false sinon.
    //
    //*************************************************************************

function input_validerObligatoireDoubleListe(oInput){
	return (input_nbDoubleListe(oInput) > 0);
}

function input_validerMinDoubleListe(oInput){
	return (input_nbDoubleListe(oInput) >= oInput.doubleListeMinValue);
}

function input_validerMaxDoubleListe(oInput){
	return (input_nbDoubleListe(oInput) <= oInput.doubleListeMaxValue);
}

function input_nbDoubleListe(oInput) {
	if (oInput.value.length==0) {
		return 0
	}else{
		var arrayResult=oInput.value.split("|");
		return arrayResult.length; 
	}
}


    //*************************************************************************
    //
    // Vérifie si le champ est numeric.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est numeric et false sinon.
    //
    //*************************************************************************
function input_validerNumeric(oInput){
	if (isNaN(oInput.value)) {
		return false;
	}else{
		return true;
	}
}

    //*************************************************************************
    //
    // Vérifie si le champ est entier.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est entier et false sinon.
    //
    //*************************************************************************
function input_validerEntier(oInput){
	var pointPosition=oInput.value.indexOf(".");
	var virgulePosition=oInput.value.indexOf(",");		
	if (pointPosition<0 && virgulePosition<0) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // Vérifie si le champ est decimal a n chiffres.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est numeric et false sinon.
    //
    //*************************************************************************
function input_validerDecimal(oInput){
	var pointPosition=oInput.value.indexOf(".");
	var virgulePosition=oInput.value.indexOf(",");		
	var position=oInput.value.length-oInput.decimalValue-1;
	if (pointPosition==position || virgulePosition==position) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // Vérifie si le champ est superieur ou egal a n.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est numeric et false sinon.
    //
    //*************************************************************************
function input_validerSuperieur(oInput){
	if (oInput.value>=oInput.superieur) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // Vérifie si le champ est superieur strict a n.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est numeric et false sinon.
    //
    //*************************************************************************
function input_validerSuperieurStrict(oInput){
	if (oInput.value>oInput.superieur) {
		return true;
	}else{
		return false;
	}
}
    //*************************************************************************
    //
    // Vérifie si le champ est inferieur ou egal a n.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est numeric et false sinon.
    //
    //*************************************************************************
function input_validerInferieur(oInput){
	if (oInput.value<=oInput.inferieur) {
		return true;
	}else{
		return false;
	}
}
    //*************************************************************************
    //
    // Vérifie si le champ est inferieur strict a n.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est numeric et false sinon.
    //
    //*************************************************************************
function input_validerInferieurStrict(oInput){
	if (oInput.value<oInput.inferieur) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // Vérifie si le champ a le bon format de date.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est au bon format de date et false sinon.
    //
    //*************************************************************************

function input_validerDate(oInput){
	var val = oInput.value;
	var formatdate="JJ/MM/AAAA";
	
	// S'il est vide, c'est ok
	if (val.length == 0){
		return true
	}
     
	// Si la longueur n'est pas bonne, pas la peine d'aller plus loin
	if (val.length != formatdate.length){
		return false
	}

	// Vérifie chaque caractère en fonction de son format
	for (var i=0; i<val.length; i++){
		var fchar = formatdate.charAt(i);
		var vchar = val.charAt(i);
		switch(fchar){
			case "A" :  
				if (isNaN(vchar)){
					return false
				}
                break;
			case "M" :
				if (isNaN(vchar)){
					return false
				}
				break;
			case "J" :
				if (isNaN(vchar)){
					return false
				}
				break;
			default : 
				if (fchar != vchar){
					return false
				}
		}
	}
	var valA = val.substring(formatdate.indexOf('A'),formatdate.indexOf('A')+4);
	var valM = val.substring(formatdate.indexOf('M'),formatdate.indexOf('M')+2);
	if ( (valM < 1) || (valM > 12) ){
		return false
	}

	var valJ = val.substring(formatdate.indexOf('J'),formatdate.indexOf('J')+2);
	if ( (valM==1) || (valM==3) || (valM==5) || (valM==7) || (valM==8) || (valM==10) || (valM==12) ){
		if ( (valJ < 1) || (valJ > 31) ){
			return false
		}
	}
	if ( (valM==4) || (valM==6) || (valM==9) || (valM==11) ){
		if ( (valJ < 1) || (valJ > 30) ){
			return false
		}
	}
	if (valM==2){
		if ( (valA%4)==0){
			if ( (valJ < 1) || (valJ > 29) ){
				return false
			}
		} else {
			if ( (valJ < 1) || (valJ > 28) ){
				return false
			}
		}
	}
	return true
}

// valide la date du champ
function verifierDate(oInput){
	var dateOk = input_validerDate(oInput);
	if( dateOk != true){									
		var message = ((txtInputDate.replace("(0)", oInput.alt)).replace("<b>", "'")).replace("</b>", "'");
		alert (message);
		event.keyCode=0;
		window.event.returnValue=false;	
		oInput.focus()
		 //return input_msgErreur(oInput, txtInputDate.replace("(0)", oInput.alt);
	}	
}

//valide l'heure du champ
function verifierHeure(oInput){
	var timeOk = input_validerHeure(oInput);
	if( timeOk != true){									
		var message = ((txtInputHeure.replace("(0)", oInput.alt)).replace("<b>", "'")).replace("</b>", "'");
		alert (message);
		event.keyCode=0;
		window.event.returnValue=false;	
		oInput.focus()
		// return input_msgErreur(oInput, txtInputHeure.replace("(0)", oInput.alt);
	}
}


    //*************************************************************************
    //
    // Vérifie si le champ a le bon format de date.
    //
    //@param oInput Objet input du formulaire à valider.
    //@return true si le champ est au bon format de date et false sinon.
    //
    //*************************************************************************

function input_validerHeure(oInput){
	var val = oInput.value;
	
	// S'il est vide, c'est ok
	if (val.length == 0){
		return true
	}
	
	// Si la longueur n'est pas bonne, pas la peine d'aller plus loin
	if (val.length != 5){
		return false
	}
     
	// Vérifie chaque caractère en fonction de son format
	if (val.charAt(2)!=':') return false;
	
	var heure=val.slice(0,2)
	if (isNaN(heure)) return false;
	if (heure<0 || heure>23) return false;
	
	var minute=val.slice(3,5)
	if (isNaN(minute)) return false;
	if (minute<0 || minute>59) return false;


	return true
}

    //*************************************************************************
    //
    // verifie la longueur d'un champ
    //
    //@param oInput   Objet input du formulaire.
    //@param sMessage Message d'erreur à afficher.
    //
    //*************************************************************************
function input_validerLongueurMax(oInput) {
	if (oInput.value.length <= oInput.longueurMaxValue) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // verifie la longueur d'un champ
    //
    //@param oInput   Objet input du formulaire.
    //@param sMessage Message d'erreur à afficher.
    //
    //*************************************************************************
function input_validerLongueurMin(oInput) {
	if (oInput.value.length >= oInput.longueurMinValue) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // verifie la longueur d'un champ
    //
    //@param oInput   Objet input du formulaire.
    //@param sMessage Message d'erreur à afficher.
    //
    //*************************************************************************
function input_validerLongueurExacte(oInput) {
	if (oInput.value.length == oInput.longueurExacteValue) {
		return true;
	}else{
		return false;
	}
}

    //*************************************************************************
    //
    // Affiche l'erreur et sélectionne le champ.
    //
    //@param oInput   Objet input du formulaire.
    //@param sMessage Message d'erreur à afficher.
    //
    //*************************************************************************

function input_msgErreur(oInput, sMessage){
	try {
		oInput.focus();
		oInput.select();
	} catch(ex) {
	}
	dialog_alert(sMessage);
	return false;
}
    //*************************************************************************
    //
    // Affiche l'erreur et sélectionne la liste.
    //
    //@param oInput   Objet input du formulaire.
    //@param sMessage Message d'erreur à afficher.
    //
    //*************************************************************************

function input_msgErreurListe(oInput, sMessage){
	oInput.focus();
	dialog_alert(sMessage);
	return false;
}

    //*************************************************************************
    //
    // Protection de caracteres pour transimission manuelle dans la request via l'uri
    //
    //@param sChaine chaine a transformer
    //@return chaine transformée
    //
    //*************************************************************************
function input_formattageRequest(sChaine) {
	newChamp = new String("");
	
	for (var i=0; i < sChaine.length; i++) {
		// test du caractere courant de la sous-chaine
		switch(sChaine.charAt(i)){
			case "#" :
				newChamp = newChamp + "%23";
				break;

			case "&" :
				newChamp = newChamp + "%26";
				break;

			case "+" :
				newChamp = newChamp + "%2B";
				break;

			case "=" :
				newChamp = newChamp + "%3D";
				break;

			case "%" :
				newChamp = newChamp + "%25";
				break;

			case "_" :
				newChamp = newChamp + "%5F";
				break;

			default  :
				newChamp = newChamp + sChaine.charAt(i);
				break;
		}
	}
	return newChamp;
}
function isUndefined(input) {
	return 'undefined'==(typeof input);
}

 // Fin du package JavaScript 'input.js'.