$equipe_g = equipe_gagnante($id_finale_winner);
                     echo '<td background=images/arbre_ligneH.gif style="background-position: center;"><img src=images/spacer.gif></td>';
                     echo '<td valign="middle">';
                     echo '<table cellspacing="2" cellpadding="5" width="100%" border="0" height="30">';
                     echo '<tr>';
                     echo '<td class="header" width="120"><img src="images/smallcup.gif" align="absmiddle"> ' . $show($equipe_g, $op, '') . '</td>';
                     echo '</tr>';
                     echo '</table>';
                     echo '</td>';
                 }
             } else {
                 echo '<td background="images/arbre_coinhaut.gif" style="background-repeat: no-repeat;background-position: bottom;"><img src="images/spacer.gif"></td>';
             }
         } elseif ($finale == 0) {
             $id_grandfinale = id_match_finale('W', 0, 1, $s_tournois, $op, $status);
             if (match_fini($id_grandfinale)) {
                 $equipe_g = equipe_gagnante($id_grandfinale);
                 echo '<td background=images/arbre_ligneH.gif style="background-position: center;"><img src=images/spacer.gif></td>';
                 echo '<td valign=middle>';
                 echo '<table cellspacing="2" cellpadding="5" width="100%" border="0" height="30">';
                 echo '<tr>';
                 echo '<td class="header" width="120" nowrap><img src="images/smallcup.gif" align="absmiddle"> ' . $show($equipe_g, $op, '') . '</td>';
                 echo '</tr>';
                 echo '</table>';
                 echo '</td>';
             }
         }
     }
 } else {
     if ($e % pow(2, $f + 1) == pow(2, $f)) {
         if (!($finale == 1 && $modeelimination_tournois == 'S')) {
function calcul_finales($id)
{
    global $db, $dbprefix, $s_tournois, $modeelimination_tournois, $nb_finales_winner_tournois, $nb_finales_looser_tournois;
    /*** verification securite ***/
    //verif_admin_tournois($s_joueur,$s_tournois);
    /*** creation du match suivant ***/
    $type_match = type_match($id);
    if (match_fini($id) && $type_match != 'P') {
        $n_finale = no_finale($id);
        $no = no_match($id);
        $equipe_g = equipe_gagnante($id);
        $equipe_p = equipe_perdante($id);
        $forfait_perdant = 0;
        /** pour ne pas envoyer le perdant disqualifi&eacute;/forfait winner dans le looser **/
        if (status_participe($equipe_p, $s_tournois) != 'P') {
            $forfait_perdant = 1;
        }
        /*echo "final: 1/$n_finale $type_match<BR>";
        		echo "n°: $no<BR>";
        		echo "equipe gagnante: $equipe_g<BR>";
        		echo "equipe perdante: $equipe_p<BR><BR>";
         		*/
        // si le match n'est pas nul
        if (!($equipe_g == 0 && $equipe_p == 0)) {
            // si le match est dans le winner
            if ($type_match == 'W') {
                // pour les matchs gagnants du winner
                if (!($modeelimination_tournois == 'S' && $n_finale == 1) && !($modeelimination_tournois == 'D' && $n_finale == 0)) {
                    $next_finale = floor($n_finale / 2);
                    $next_no = round($no / 2);
                    if ($no % 2 == 1) {
                        $next_pos = 1;
                    } else {
                        $next_pos = 2;
                    }
                    /*
                    echo "equipe gagnante: $equipe_g<BR>";
                    echo "next final: 1/$next_finale W <BR>";
                    echo "next n°: $next_no<BR>";
                    echo "next pos°: $next_pos<BR>";
                    */
                    $next_id = id_match_finale('W', $next_finale, $next_no, $s_tournois, 'admin');
                    //echo "next id: $next_id<BR><BR>";
                    $db->update("{$dbprefix}matchs");
                    $db->set("equipe{$next_pos} = {$equipe_g}");
                    $db->where("id = {$next_id}");
                    $db->exec();
                }
                // pour les perdants des matchs du winner du 1er tour
                if ($modeelimination_tournois == 'D' && $n_finale == $nb_finales_looser_tournois) {
                    $next_finale = $n_finale / 2;
                    $next_no = $n_finale / 2 + 1 - round($no / 2);
                    if ($no % 2 == 0) {
                        $next_pos = 1;
                    } else {
                        $next_pos = 2;
                    }
                    /*echo "equipe perdante: $equipe_p<BR><BR>";
                    		echo "next final: 1/$next_finale L<BR>";
                    		echo "next n°: $next_no<BR>";
                    		echo "next pos°: $next_pos<BR>";
                    		*/
                    $next_id = id_match_finale('L', $next_finale, $next_no, $s_tournois, 'admin');
                    //echo "next id: $next_id<BR><BR>";
                    $db->update("{$dbprefix}matchs");
                    $db->set("equipe{$next_pos} = {$equipe_p}");
                    if ($forfait_perdant) {
                        $db->set("statusequipe = 'F{$next_pos}'");
                    }
                    $db->where("id = {$next_id}");
                    $db->exec();
                } elseif ($modeelimination_tournois == 'D' && $n_finale < $nb_finales_looser_tournois && $n_finale != 0) {
                    $next_finale = $n_finale;
                    $next_no = $no + $n_finale;
                    $next_pos = 1;
                    /*echo "equipe perdante: $equipe_p<BR><BR>";
                    		echo "next final: 1/$next_finale L<BR>";
                    		echo "next n°: $next_no<BR>";
                    		echo "next pos°: $next_pos<BR>";
                    		*/
                    $next_id = id_match_finale('L', $next_finale, $next_no, $s_tournois, 'admin');
                    //echo "next id: $next_id<BR><BR>";
                    $db->update("{$dbprefix}matchs");
                    $db->set("equipe{$next_pos} = {$equipe_p}");
                    if ($forfait_perdant) {
                        $db->set("statusequipe = 'F{$next_pos}'");
                    }
                    $db->where("id = {$next_id}");
                    $db->exec();
                }
            } else {
                // pour les matchs gagnants du looser autre que la finale (la L 1.2)
                if ($n_finale != 1 || $no != 2) {
                    // si c le 1er tour
                    if ($no <= $n_finale) {
                        $next_finale = $n_finale;
                        $next_no = $no + $n_finale;
                        $next_pos = 2;
                        /*echo "equipe gagnante: $equipe_g<BR>";
                        		echo "next final: 1/$next_finale L <BR>";
                        		echo "next n°: $next_no<BR>";
                        		echo "next pos°: $next_pos<BR>";
                        		*/
                        $next_id = id_match_finale('L', $next_finale, $next_no, $s_tournois, 'admin');
                        //echo "next id: $next_id<BR><BR>";
                        $db->update("{$dbprefix}matchs");
                        $db->set("equipe{$next_pos} = {$equipe_g}");
                        $db->where("id = {$next_id}");
                        $db->exec();
                    } else {
                        $next_finale = floor($n_finale / 2);
                        $next_no = round(($no - $n_finale) / 2);
                        if ($no % 2 == 1) {
                            $next_pos = 1;
                        } else {
                            $next_pos = 2;
                        }
                        /*echo "equipe gagnante: $equipe_g<BR>";
                        		echo "next final: 1/$next_finale L <BR>";
                        		echo "next n°: $next_no<BR>";
                        		echo "next pos°: $next_pos<BR>";
                        		*/
                        $next_id = id_match_finale('L', $next_finale, $next_no, $s_tournois, 'admin');
                        //echo "next id: $next_id<BR><BR>";
                        $db->update("{$dbprefix}matchs");
                        $db->set("equipe{$next_pos} = {$equipe_g}");
                        $db->where("id = {$next_id}");
                        $db->exec();
                    }
                } elseif ($n_finale == 1 && $no == 2) {
                    $next_finale = 0;
                    $next_no = 1;
                    $next_pos = 2;
                    /*echo "equipe gagnante: $equipe_g<BR>";
                    		echo "next final: GRAND FINAL W <BR>";
                    		echo "next n°: $next_no<BR>";
                    		*/
                    $next_id = id_match_finale('W', $next_finale, $next_no, $s_tournois, 'admin');
                    //echo "next id: $next_id<BR><BR>";
                    $db->update("{$dbprefix}matchs");
                    $db->set("equipe{$next_pos} = {$equipe_g}");
                    $db->where("id = {$next_id}");
                    $db->exec();
                }
            }
        }
    }
}