// Ce code implémente la classe javascript champ
//
// attributs : 
// - formulaire
// - nom
// - erreur
//
// méthode
// - vide()
// - focus()


function champ(formulaire, nom){
 this.formulaire=formulaire;
 this.nom = nom;
 this.vide = champ_vide;
 this.focus = champ_focus;

// dans le cas d'un champ de type : 
// - text
// - select-one
// - radio
// l'élément porte le nom "nom"
//
// dans le cas d'un champ de type  :
// - checkbox
// l'élément porte le nom "nom[i]"
 
 with( document.forms[this.formulaire]){

  if(typeof elements[this.nom] == "object"){

   if(elements[this.nom].type == "text"){
    this.type = "text";
    this.erreur = "";
    return true;
   } 
   
   if(elements[this.nom].type == "hidden"){
    this.type = "text";
    this.erreur = "";
    return true;
   } 

   if(elements[this.nom].type == "select-one"){
    this.type = "select-one";
    this.erreur = "";
    return true;
   } 

   if( typeof elements[this.nom][0] == "object"){
    if(elements[this.nom][0].type == "radio"){
     this.type = "radio";
     this.erreur = "";
     return true;
    } 
   }

  }

  var nom2 = this.nom + "[0]"; 
  if(typeof elements[nom2] == "object"){
   if( elements[nom2].type == "checkbox"){ 
    this.type = "checkbox";
    this.erreur = "";
    return true;
   }
  }
 }

 this.erreur = "type non reconnu";
 alert(this.nom + " :  " + this.erreur);
 return false;

}


function champ_focus(){

 with(document.forms[this.formulaire]){

// type text et select-one
  if((this.type == "text") || (this.type == "select-one")){
   elements[this.nom].focus();
  }

// type radio
  if(this.type == "radio"){
   elements[this.nom][i].focus();
  } // fin de radio

// type case à cocher
  if(this.type == "checkbox"){
    elements[this.nom + '[0]'].focus();
  }

 }

}


function champ_vide(){


 with(document.forms[this.formulaire]){

// type radio
  if(this.type == "radio"){
   for (i = 0; i < elements[this.nom].length; i++){
    if (elements[this.nom][i].checked){
     return false;
    }
   }
   return true;
  } // fin de radio

// type case à cocher
  if(this.type == "checkbox"){

   var motif = this.nom + '\\[[0-9]+\\]';
   re = new RegExp(motif);   
   var i;
   for (i = 0 ; i < elements.length ; i++){
    var nom_champ = elements[i].name;
    if(re.test(nom_champ)){
     if(elements[i].checked){
      return false;
     }
    }
   }
   return true;
  } // fin de checkbox

 } // fin du with

 with (document.forms[this.formulaire].elements[this.nom]){

  if(this.type == "text"){
   if( value != ""){
    return false;
   }
   return true;
  }

  if(this.type == "select-one"){
  // les valeurs "" et "0" correspondent à un contenu nul 
   if( (options[selectedIndex].value != "0") && (options[selectedIndex].value != "")) {
    return false;
   }
   return true;
  }

 } // fin du with

 return true;

}












