
var fen_debug = "";

function aff_trace (objet, texte )
{

    if (fen_debug == "")
    {
      fen_debug = window.open("","Trace_JS",
			      "height=200,width=400,resizable,scrollbars");
    }

    var pos_prem = texte.indexOf('<');
    var texte_aff;

    if (pos_prem == -1)
    {
      texte_aff = texte;
    }
    else
    {
        texte_aff = "";
        temp = texte.split('<');

        for ( ii = 0; ii < temp.length - 1; ii++ )
        {
            texte_aff = texte_aff + temp[ii] + '&lt;';
        }

        texte_aff = texte_aff + temp[ii];
    }

    fen_debug.document.write("<br>"+texte_aff);
    fen_debug.scrollBy(0,25);
}


//=============================================================================
// Objet combo_elem
// Description d'un element de combo.
//=============================================================================
function combo_elem ( iLabel, iValue, iAction )
{

    // Donnees membres

    this.label = iLabel;
    this.value = iValue;
    this.action = iAction;
}


//=============================================================================
// Objet combo
// Description d'un combo.
//=============================================================================
function combo_variante ()
{
    this.nbelem = 0;
    this.elem_actif = 0;
    this.elems = new Array();
}


//=============================================================================
// Objet combo
// Description d'un combo.
//=============================================================================
function combo_desc (iNom)
{
    this.nom = "";
    if (iNom)
        this.nom = iNom;

    this.nbvariantes = 0;
    this.variante_active = 0;
    this.variantes = new Array();
}


//=============================================================================
// Objet multicombo
// 
//=============================================================================
function multicombo (iNomInstance, iNbCombos)
{
    // aff_trace(this, "Entree Constructeur ");
    // Donnees membres

    this.instance = iNomInstance;
    this.nomform = "f_"+this.instance;
    this.nbcombos = iNbCombos;
    
    this.combos = new Array();

    for (ii = 0; ii < this.nbcombos; ii++)
    {
        this.combos[ii] = new combo_desc("s_"+this.instance+"_"+ii);
    }

    // Methodes

    this.add_combo_variante = add_combo_variante;
    this.affich = affiche_combos;
    this.maj_combos = maj_combos;
    this.selectcombocb = selectcombocb;

    // aff_trace(this, "Sortie Constructeur ");
}


function add_combo_variante (iRang, iSelect, varargs)
{
    // aff_trace(this, "Entree  add_combo_variante");

    var ii;
    var id_variante = this.combos[iRang-1].nbvariantes;
    this.combos[iRang-1].variantes[id_variante] = new combo_variante();

    if (iSelect)
        this.combos[iRang-1].variantes[id_variante].elem_actif = iSelect - 1;

    for (ii = 2; ii<arguments.length; ii++)
    {
        this.combos[iRang-1].variantes[id_variante].elems[ii-2] = arguments[ii];
    }

    this.combos[iRang-1].variantes[id_variante].nbelems = ii - 2;
    this.combos[iRang-1].nbvariantes++;

    // aff_trace(this, "Sortie add_combo_variante");
}


// Affichage du menu

function affiche_combos ()
{
    // aff_trace(this, "Entree affich ");
  
    // Creation du "<form>" et de l'ensemble des combos ("<select>") "vides".

    document.write ("<form method=\"post\" id=\""+this.nomform+"\" style=\"display: inline;\">");

    for ( ii = 0; ii < this.nbcombos; ii++ )
    {
        document.write ("<select id=\""+this.combos[ii].nom+"\" onChange=\""+this.instance+".selectcombocb("+ii+");\">");
        document.write ("</select>");
    }

    document.write ("</form>");

    // "Remplissage" de tous les combos

    this.maj_combos(0);

    // aff_trace(this, "Sortie affich ");
}

// Mise a jour des combo

function maj_combos (iRangDepart)
{
    // aff_trace(this, "Entree maj_combos; iRangDepart=" + iRangDepart);
    // aff_trace(this, "Nombre de combos " + this.nbcombos);

    for ( ii = iRangDepart; ii < this.nbcombos; ii++ )
    {
        // aff_trace(this, "Combo #"+ii);

        var html_nom_combo = this.combos[ii].nom;
        // aff_trace (this, "html_nom_combo = " +html_nom_combo + ", combo :" + document.getElementById(html_nom_combo));
        var combo_courrant = document.getElementById(html_nom_combo);

        // Suppression de la variante precedente

        for (jj = 0; jj < combo_courrant.length; jj++)
        {
            combo_courrant.options[jj] = null;
        }

        id_variante = this.combos[ii].variante_active;
        // aff_trace(this, "Chargement variante #"+id_variante);
        combo_courrant.length = this.combos[ii].variantes[id_variante].nbelems;

        for (jj = 0; jj<this.combos[ii].variantes[id_variante].nbelems; jj++)
        {
            combo_courrant.options[jj].text = this.combos[ii].variantes[id_variante].elems[jj].label;

            if (this.combos[ii].variantes[id_variante].elems[jj].value)
                combo_courrant.options[jj].value = this.combos[ii].variantes[id_variante].elems[jj].value;

            if (this.combos[ii].variantes[id_variante].elem_actif == jj)
                combo_courrant.options[jj].selected = true;

        }

        if (ii+1 < this.nbcombos)
        {
            if (this.combos[ii].variantes[id_variante].elem_actif)
            {
                this.combos[ii+1].variante_active = this.combos[ii].variantes[id_variante].elem_actif;
            }
            else
            {
                this.combos[ii+1].variante_active = 0;
            }
        }

    }

    // aff_trace(this, "Sortie maj_combos ");
}

// Callbacks

// selectcombocb

function selectcombocb (iIdCombo)
{
    //var html_nom_combo = (this.nomform)+"."+(this.combos[iIdCombo].nom);
    var html_nom_combo = this.combos[iIdCombo].nom;
    // aff_trace (this, "html_nom_combo = " +html_nom_combo + ", combo :" + document.getElementById(html_nom_combo));
    var sel_id = document.getElementById(html_nom_combo).selectedIndex;

    // aff_trace (this, "Action sur combo num." + iIdCombo+" : " +
    //            this.combos[iIdCombo].nom  +  ", "+ html_nom_combo +
    //            ", elem selectionné : " +  sel_id );

    var id_variante = this.combos[iIdCombo].variante_active;
    var action = this.combos[iIdCombo].variantes[id_variante].elems[sel_id].action;

    // aff_trace(this, "Action sur combo #"+iIdCombo+" : "+this.combos[iIdCombo].nom + ", variante " + id_variante +" : "+ action );

    if ("MAJ" == action)
    {
        // Mise a jour des combos de rang inferieur
        this.combos[iIdCombo+1].variante_active = sel_id;
        this.maj_combos(iIdCombo+1);
    }
    else if ("URL" == action)
    {
        var nouv_url = document.getElementById(html_nom_combo).value;
        // aff_trace (this, "changement de page, nouvelle url : " + nouv_url );
        location.href = nouv_url;
    }

}


function init()
{
    // aff_trace(this, "Entree init ");

    // etat initial : sous menus masques
    //                chrono arrete


    // aff_trace(this, "Sortie init ");
}
