// $Id: forms.js,v 1.15 2007/03/12 10:45:35 malek Exp $
// Utilitaires pour les formulaires
traduction_erreur_generale_multilangue="err:";

// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// FONCTIONS SUBMIT / CONTROLES / SET ACTION                                           //
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //


// theForm : element Form
// txt     : texte du mesage de confirmation
// return  : false (submit fait ou pas de submit a faire du tout)
function confirmAndSubmit(theForm,txt){
alert(theForm);
	if (theForm == undefined){
		alert(traduction_erreur_generale_multilangue+" theForm="+theForm);
		return(false);
	}
	if (confirm(txt)){
		theForm.submit();
	}
	return(false);
}

// theForm : element Form
// txt1     : texte du mesage de confirmation : début
// theField : texte du mesage de confirmation : champs dont la valeur doit être intercallée
// txt2     : texte du mesage de confirmation : fin
// return  : false (submit fait ou pas de submit a faire du tout)
function confirmAndSubmit2(theForm,txt1,theField,txt2){
	if (theField == undefined){
		alert(traduction_erreur_generale_multilangue+"  theField="+theField);
		return(false);
	}
	return(confirmAndSubmit(txt1+theField.value+txt2));
}

// txt		: texte du mesage de confirmation 
// txtOK	: texte du mesage close
// txtKO	: texte du message cancel
// return	: false (close fait ou pas de submit a faire du tout)
function confirmAndClose(txt,txtOK,txtKO){
	if (confirm(txt)){
		if (txtOK!=""){ alert(txtOK); }
		window.close();
	} else {
		if (txtKO!=""){ alert(txtKO); }
	}
	return(false);
}

// txt		: texte du mesage de confirmation
// txtKO	: texte du message cancel
// return	: false (close fait ou pas de submit a faire du tout)
function confirmAndBackToGeneralMenu(txt,txtKO){
	if (confirm(txt)){
		window.location=navigation_filRougeUrl;
	} else {
		if (txtKO!=""){ alert(txtKO); }
	}
	return(false);
}

function cancelAndBackTo(txt,txtOK,txtKO,urlBack){
	if (confirm(txt)){
		if (txtOK!=""){ alert(txtOK); }
		window.location=urlBack;
	} else {
		if (txtKO!=""){ alert(txtKO); }
	}
	return(false);
}

// theForm : element Form
// url     : url de l'attribut action a positionner
// return  : true (aucune raison de bloquer), false si theForm est undefined
function setAction(theForm,url){
	if (theForm == undefined){
		alert(traduction_erreur_generale_multilangue+" theForm="+theForm);
		return(false);
	}
	theForm.action=url;
	return(true);
}

// theForm : element Form
// return  : false (submit fait)
function submitAction(theForm,url){
	if(! setAction(theForm,url)) {return(false);}
	Sablier();
	return(theForm.submit());
}

// theForm : element Form
// txt     : texte du mesage de confirmation
// return  : false (submit fait ou pas de submit a faire du tout)
function confirmAndSubmitAction(theForm,url,txt){
	if(! setAction(theForm,url)) {return(false);}
	if (theForm == undefined){
		alert(traduction_erreur_generale_multilangue+" theForm="+theForm);
		return(false);
	}
	if (confirm(txt)){
		theForm.submit();
	}
	return(false);
}

// theForm : element Form
// txt1     : texte du mesage de confirmation : début
// theField : texte du mesage de confirmation : champs dont la valeur doit être intercallée
// txt2     : texte du mesage de confirmation : fin
// return  : false (submit fait ou pas de submit a faire du tout)
function confirmAndSubmitAction2(theForm,url,txt1,theField,txt2){
	if(! setAction(theForm,url)) {return(false);}
	if (theField == undefined){
		alert(traduction_erreur_generale_multilangue+" theField="+theField);
		return(false);
	}
	return(confirmAndSubmit(txt1+theField.value+txt2));
}

// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// FONCTIONS DE LECTURE / TEST / ECRITURE SUR CHAMPS MONO/MULTI VALUES                 //
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //

// cette fonction marche que le champ soit un champ monovalué ou multivalué, mais il doit exister
// theField : champs dont la valeur doit être changée
// val      : txt valeur à setter
// return   : true (aucune raison de bloquer), false si theField est undefined ou valeur non settable (checkbox non trouvée)
function setFieldSingleValue(theField,val){
	if (theField == undefined){            // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" theField="+theField);
		return(false);
	}
	if (theField.value==undefined){
		if (theField.length==undefined){    // cas ni mono ni multi value
			alert(traduction_erreur_generale_multilangue+" theField="+theField+" typeof theField="+ (typeof theField));
			return(false);			
		} else {                            // cas multivalue >1 val
			var found=0;
		    for (var i=0;i<theField.length;i++) {
		    	if (theField[i].value == val) {
		    	    theField[i].checked = true;
		    	    found++;
		    	} else {
		    	    theField[i].checked = false;
		    	}
		    }		
	    	if (found==0){                 // sous-cas valeur non trouvée dans la liste
				alert(traduction_erreur_generale_multilangue+" theField="+theField+" theField.length="+theField.length+" val="+val);
				return(false);					    	
	    	}
		}
	} else {                           
		if (theField.checked==undefined || theField.type=='hidden'){  // cas mono value (input classique)
			theField.value=val;
		} else {                           // cas mono value (une checkbox)
	    	if (theField.value != val){    // sous-cas valeur différente
				theField.checked=false;
				alert(traduction_erreur_generale_multilangue+" theField="+theField+" theField.value="+theField.value+" val="+val);
				return(false);					    	
	    	} else {
	    		theField.checked=true;
	    	}
		}
	}
	return(true);
}

// private ?
// cette fonction marche que le champ soit un champ monovalué ou multivalué, mais il doit exister
// dans le cas d'un champ monovalué a multiple assignation, une concaténation avec séparateur '|'
// theField : champs dont la valeur doit être changée
// val      : txt valeur à setter
// return   : true (aucune raison de bloquer), false si theField est undefined ou valeur non settable (checkbox non trouvée)
function addFieldSingleValue(theField,val){
	if (theField == undefined){             // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" theField="+theField);
		return(false);
	}
	if (theField.value==undefined){
		if (theField.length==undefined){    // cas ni mono ni multi value
			alert(traduction_erreur_generale_multilangue+" theField="+theField+" typeof theField="+ (typeof theField));
			return(false);			
		} else {                            // cas multivalue >1 val
			var found=0;
		    for (var i=0;i<theField.length;i++) {
		    	if (theField[i].value == val) {
		    	    theField[i].checked = true;  // TODO ? traiter le cas plusieurs fois la même valeur ?
		    	    found++;
		    	    break;
		    	}
		    }		
	    	if (found==0){                 // sous-cas valeur non trouvée dans la liste
				alert(traduction_erreur_generale_multilangue+" theField="+theField+" theField.length="+theField.length+" val="+val);
				return(false);					    	
	    	}
		}
	} else {                           
		if (theField.checked==undefined || theField.type=='hidden'){  // cas mono value (input classique)
			if (theField.value==""){
				theField.value=val;
			} else {
				theField.value+="|"+val;
			}
		} else {                           // cas mono value (une checkbox)
	    	if (theField.value != val){    // sous-cas valeur différente
	    		// TODO : JG : voir si il n'y a pas des cas similaires (hidden mono setté avec multi)
	    		if (theField.value == ""){
					theField.value=val;	    		
	    		} else {
					theField.value+="|"+val;
				}
				//alert(traduction_erreur_generale_multilangue+" theField="+theField+" theField.value="+theField.value+" val="+val);
				//return(false);					    	
	    	} else {
	    		theField.checked=true;
	    	}
		}
	}
	return(true);
}


// cette fonction marche que le champ soit un champ monovalué ou multivalué, mais il doit exister
// theField : champs dont la valeur doit être changée
// return   : true (aucune raison de bloquer), false si theField est undefined ou valeur non settable (checkbox non trouvée)
function resetFieldToEmpty(theField){
	if (theField == undefined){          // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" theField="+theField);
		return(false);
	}
	//alert('DEBUG2 : theField.value='+theField.value);
	
	if (theField.value==undefined){
		if (theField.length==undefined){    // cas ni mono ni multi value
			alert(traduction_erreur_generale_multilangue+" theField="+theField+" typeof theField="+ (typeof theField));
			return(false);			
		} else {                            // cas multivalue >1 val
		    for (var i=0;i<theField.length;i++) {
	    	    theField[i].checked = false;
		    }		
		}
	} else {                           
	//alert('DEBUG2 : theField.checked='+theField.checked+' typeof (theField)'+(typeof (theField))+ ' theField.getClass()='+theField.type);
//	|| vals instanceof String

		if (theField.checked==undefined || theField.type=='hidden'){  // cas mono value (input classique) // BUG CORRIGE POUR HIDDEN
			theField.value="";
		} else {                           // cas mono value (une checkbox)
			theField.checked=false;
		}
	}
	return(true);
}

// cette fonction marche que le champ soit un champ monovalué ou multivalué, mais il doit exister
// dans le cas d'un champ monovalué a multiple assignation, une concaténation avec séparateur '|'
// theField : champs dont la valeur doit être changée
// vals     : array de valeurs à setter (si pas array un split su '|' est effectué)
// return   : true (aucune raison de bloquer), false si theField est undefined ou valeur non settable (checkbox non trouvée)
function setFieldMultipleValue(theField,vals){

	var valArray=new Array();
	if (vals == undefined){          // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" vals="+vals);
		return(false);
	}
	
	if (vals.length==undefined || vals instanceof String ){
		if (vals.value == undefined){      // cas input vals : "a|b|c" ou "a"
			valArray = (""+vals).split(/\|/);
		} else {
			valArray = (""+vals.value).split(/\|/); // cas input vals  champ monovalue de value : "a|b|c" ou "a"
		}
	} else {
		//alert('DEBUG: vals.length='+vals.length);
		if (vals.length > 0){
			if (vals[0].value == undefined){       // cas input vals : ("a","b","c")
				for (var i=0;i<vals.length;i++){
					var tmpS=""+vals[i];
					if (tmpS != ""){
						valArray.push(tmpS);
					} 
				}	
			} else {                               // cas input vals  : array teh checbox / checkbox group
				for (var i=0;i<vals.length;i++){
					var tmpS=""+vals[i].value;
					if (vals[i].checked == undefined || vals[i].checked ){  // TODO : JG : verify not other pb
						if (tmpS != ""){
							valArray.push(tmpS);
						}
					} 
				}
			}	
		}
	}
	//alert('DEBUG: valArray.length='+valArray.length);
	
	if (valArray.length==0){
		return(resetFieldToEmpty(theField));
	} else {
		rval=resetFieldToEmpty(theField);
		//alert('DEBUG: empty -> theField.value='+theField.value);
		if (rval==false) { return(rval); }
		for (var i=0;i<valArray.length;i++){
			rval=rval&&addFieldSingleValue(theField,valArray[i]); // on essaye toutes les valeurs ... sachant que si le champ n'existe pas, le reset a retourné false
		}
		if (rval==false) { return(rval); }
	}
	return(true);
}

// cette fonction marche que le champ soit un champ monovalué ou multivalué
// theField : champs dont la valeur doit être changée
// return   : true ou false selon que le champ est vide / non sélectionné . 
//            true si theField=="" ou undefined
//            false si le champ n'est pas analysable (n'existe pas)
function isFieldEmpty(theField){
	if (theField == undefined){          // cas champ non existant ou checklist vide
		return(true);
	}
	if (theField.value==undefined){
		if (theField.length==undefined){    // cas ni mono ni multi value
			// todo ? trouver meilleure réponse ?
			if (theField==""){ return(true); }
			else             { return(false); }			
		} else {                            // cas multivalue >1 val
			for (var i=0;i<theField.length;i++) {
				if (theField[i].checked == true){
					return(false); 
				}
			}		
			return(true); 
		}
	} else {                           
		if (theField.checked==undefined || theField.type=='hidden'){  // cas mono value (input classique)
			if (theField.value==""){ return(true); }
			else                   { return(false); }			
		} else {                           // cas mono value (une checkbox)
			if (theField.checked == true){
				return(false); 
			} else {
				return(true); 
			}
		}
	}
	return(true);
}

// cette fonction marche que le champ soit un champ monovalué ou multivalué
// theField : champs dont la valeur doit être changée
// return   : int.  
//            0 si theField=="" ou undefined
//            1 si le champ n'est pas analysable (n'existe pas). 
//            si le champ est monovalué (un split sur "|" est fait donc le résultat peut être >1)
function countFieldSelectedElems(theField){
	if (theField == undefined){          // cas champ non existant ou checklist vide
		return(0);
	}
	if (theField.value==undefined){
		if (theField.length==undefined){    // cas ni mono ni multi value
			// todo ? trouver meilleure réponse ?
			if (theField==""){ return(0); }
			else             { return((""+theField).split(/\|/).length); }			
		} else {                            // cas multivalue >1 val
			var count=0;
			for (var i=0;i<theField.length;i++) {
				if (theField[i].checked == true){
					count++;
				}
			}		
			return(count); 
		}
	} else {                           
		if (theField.checked==undefined || theField.type=='hidden'){  // cas mono value (input classique)
			if (theField.value==""){ return(0); }
			else                   { return((""+theField.value).split(/\|/).length); }			
		} else {                           // cas mono value (une checkbox)
			if (theField.checked == true){
				return(1); 
			} else {
				return(0); 
			}
		}
	}
	return(0);
}

// cette fonction marche que le champ soit un champ monovalué ou multivalué
// theField : champs dont la valeur doit être changée
// return   : int.  
//            0 si theField=="" ou undefined
//            1 si le champ n'est pas analysable (n'existe pas). 
//            si le champ est monovalué (un split sur "|" est fait donc le résultat peut être >1)
function countFieldPossibleElems(theField){
	if (theField == undefined){          // cas champ non existant ou checklist vide
		return(0);
	}
	if (theField.value==undefined){
		if (theField.length==undefined){    // cas ni mono ni multi value
			// todo ? trouver meilleure réponse ?
			if (theField==""){ return(0); }
			else             { return((""+theField).split(/\|/).length); }			
		} else {                            // cas multivalue >1 val
			return(theField.length); 
		}
	} else {                           
		if (theField.checked==undefined || theField.type=='hidden'){  // cas mono value (input classique)
			if (theField.value==""){ return(0); }
			else                   { return((""+theField.value).split(/\|/).length); }			
		} else {                           // cas mono value (une checkbox)
			if (theField.checked == true){
				return(1); 
			} else {
				return(0); 
			}
		}
	}
	return(0);
}

// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// FONCTIONS LIEES A DES TABLEAUX HTML                                                 //
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //

// theTable     : table cible
// columnNumber : numéro de la colonne dont les checkbox doivent être changées
// wantedStatus : true pour tout checker , false pour tout dechecker
// return   : true (aucune raison de bloquer à priori), 
//            false si theTable est undefined
function checkAllInTableColumn(theTable, columnNumber, wantedStatus) {
	//var theTable = document.getElementById(id);
	if (theTable == undefined){          // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" theTable="+theTable);
		return(false);
	}
	var tblCell = null;
	var inputElem  = null;
	for (var i = 0; i < theTable.rows.length; i++) {
		tblCell = theTable.rows[i].cells[columnNumber];
		if( tblCell == undefined) continue;
   		for (j = 0; j < tblCell.childNodes.length; j++){
   			inputElem = tblCell.childNodes[j];
			if ( inputElem.checked != undefined) { // inputElem.tagName == "INPUT";				
				inputElem.checked = wantedStatus;
			}
		}
	}
	return true;
}

// theTable     : table cible
// columnNumber : numéro de la colonne dont les checkbox "checked" doivent être comptées
// return   : int
//            0 si theTable est undefined
function countCheckedInTableColumn(theTable, columnNumber) {
	if (theTable == undefined){          // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" theTable="+theTable);
		return(0);
	}
	var count=0;
	var tblCell = null;
	var inputElem  = null;
	for (var i = 0; i < theTable.rows.length; i++) {
		tblCell = theTable.rows[i].cells[columnNumber];  	
		if(tblCell != undefined){
    		for (j = 0; j < tblCell.childNodes.length; j++){
    			inputElem = tblCell.childNodes[j];
    			if ( inputElem.checked != undefined) { // inputElem.tagName == "INPUT";				
    				if ( inputElem.checked == true){
    					count++;
    				}
    			}
    		}
		}
	}
	return count;
}
// theTable     : table cible
// columnNumber : numéro de la colonne dont les checkbox doivent être comptées
// return   : int
//            0 si theTable est undefined
function countCheckableInTableColumn(theTable, columnNumber) {
	if (theTable == undefined){          // cas champ non existant
		alert(traduction_erreur_generale_multilangue+" theTable="+theTable);
		return(0);
	}
	var count=0;
	var tblCell = null;
	var inputElem  = null;
	for (var i = 0; i < theTable.rows.length; i++) {
		tblCell = theTable.rows[i].cells[columnNumber];  	
		for (j = 0; j < tblCell.childNodes.length; j++){
			inputElem = tblCell.childNodes[j];
			if ( inputElem.checked != undefined) { // inputElem.tagName == "INPUT";				
				count++;
			}
		}
	}
	return count;
}

// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// FIN                                                                                 //
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // //


true;


