<?php
// this defines some of your basic setup
include_once 'config.php';
// the facebook client library
include_once $BIB.'facebook.php';
include_once $BIB.'bib_generic.php3';
include_once $BIB.'bib_quiz_facebook.php3';
include_once $BIB.'bib_facebook_user.php3';
include_once $BIB.'bib_prono.php3';
include_once $BIB.'bib_drapeaux.php3';
include_once $BIB.'bib_constants.php3';




//Affiche l'enregistrement du joueur
function print_enregistrement($playerId)
{
	global $NB_TEAM_POOL,$NB_MATCH_POOL,$POOLS,$NB_ETAPE_PREBET,$FINALES,$SERVER_URL;
	//On recupere tout les nom des Coupe_Equipes
	$TABEAQUIPENAME = array();
	$query = "SELECT * FROM Coupe_Equipes";
	$rows = main_db_query($query);
	while($row = db_fetch_assoc($rows))
	{
		$TABEAQUIPENAME[$row["Id"]] = $row["Name"];
	}
	
	
	//On Récupere la chaine des reponses déjà données
	$betrow = db_fetch_array(main_db_query("SELECT Bet FROM Coupe_Paris WHERE PlayerId =".$playerId));

	$TabAnswers = explode("-",substr($betrow["Bet"],1));
	
	$place = 1;
	$pool = 0;
	
	echo "<div class='question clearfix'>";
	echo "<h3>VOICI L'EURO 2008 DE VOS REVES :</h3>\n";

	echo "<table class=smalldark cellpadding=10 cellspacing=10 border=0><tr><td>";
	
	echo "<b>Poule ".$POOLS[0]."</b><br>";
	for($i=0; $i < $NB_MATCH_POOL; $i++)
	{
		if($place > $NB_TEAM_POOL)
		{
			echo "</td><td>";
			$place = 1;
			$pool++;
			echo "<b>Poule ".$POOLS[$pool]."</b><br>";
		}
		echo $place." - ".$TABEAQUIPENAME[$TabAnswers[$i]]."<br>";
		$place++;		
	}
	echo "</td></tr></table>";
	
	$FLASH_URL = $SERVER_URL."Flash/arbre.swf?";
	
	$i = 1;
	$etape = $NB_MATCH_POOL;
	
	foreach($FINALES AS $finale)
	{
		$Finalists = explode("-",$finale["id"]);
		$Team1 = $TabAnswers[$Finalists[0]-1];
		$Team2 = $TabAnswers[$Finalists[1]-1];
		
		$FLASH_URL .= "txt".($i)."=".$TABEAQUIPENAME[$Team1]."&";
		$FLASH_URL .= "txt".($i+1)."=".$TABEAQUIPENAME[$Team2]."&";
		
		if($TabAnswers[$etape]==$Team1)
		{
			$FLASH_URL .= "win_".($i)."=1&";
		}
		else if($TabAnswers[$etape]==$Team2)
		{
			$FLASH_URL .= "win_".($i+1)."=1&";
		}
		$i = $i+2;
		$etape++;
	}
	echo "<div align=center>";
	echo "<b>Phases finales :</b><br>";	
	
	echo	"<fb:swf ".
			"swfbgcolor='000000' ".
			"swfsrc='".$FLASH_URL."' ".
			"width='265' height='300' />";
	echo "</div>";
	echo "</div>";

}


//-----------------------------------------------------------------------------------------------------------------//
//Fonction générant les liens de reponse pour les matchs de pool
function generate_answers_form($pool,$place,$etape,$user)
{
	global $POOLS,$IMAGES_DIR,$NB_TEAM_POOL;

	$query = "SELECT Coupe_Equipes.* FROM Coupe_Equipes LEFT JOIN Coupe_Poules ON Coupe_Poules.Id=Coupe_Equipes.IdPool WHERE Coupe_Poules.Name LIKE '".$POOLS[$pool]."'";
	
	$rows = main_db_query($query);
		
		
	while($row = db_fetch_array($rows))
	{
			$answer[$i] = $row;
			$i++;
	}
	echo "<div class='question clearfix'>";
	echo "<h3>Classez les équipes du groupe ".$POOLS[$pool]." :</h3>";
	echo "<br>";
	echo "<form method='POST'>";
	
	echo "<table border=0 align=center><tr align=center>";
	$i = 0;
	foreach($answer as $team )
	{
		if(($i%2) != 0)
		{
			echo "<td align=left ><img src='".$IMAGES_DIR.$team["Image"]."' />&nbsp;".$team["Name"]."</td>";
		}
		else
		{
			echo "<td align=left ><img src='".$IMAGES_DIR.$team["Image"]."' />&nbsp;".$team["Name"]."</td>";
		}
		if($i == 1)
		{
			echo "</tr><tr>";
		}
		$i++;
	}
	echo "</tr></table>";
	echo "<table align=center border=0><tr align=center><td colspan=2>";	
	echo "<tr ><td ><br></td></tr>";
	for($i=0;$i<$NB_TEAM_POOL;$i++)
	{
		echo "<tr align=center><td >".($i+1).".</td><td> <select name='Team".$i."' class='inputtext'>\n";
		//$answer = array();
		foreach($answer as $team )
		{
			echo "<option  value='".$team["Id"]."' class='inputbg2'>".$team['Name']."</option>\n";
		}
		
		echo "</select></td></tr>\n";		
	}
	echo "<input type='hidden' value='x' name='send'>";
	echo "<input type='hidden' value='".$etape."' name='etape'>";
	echo "<tr align=center><td align=center colspan=2><input value='continuer >>' name='continuer' type='submit' class='inputBouton'/></td></tr>";
	echo "</table>";
	echo "</form>";
	echo "</div>";
	
}
//-----------------------------------------------------------------------------------------------------------------//
//Fonction générant les liens de reponse post matchs de pool
function generate_final_links($Team1,$Team2,$match,$etape,$user)
{
	global $IMAGES_DIR,$QUESTIONS_TYPE,$FINALES;

	//$match++;
	$query = "SELECT * FROM Coupe_Equipes WHERE Id = ".$Team1;
	$row = db_fetch_array(main_db_query($query));
	$query2 = "SELECT * FROM Coupe_Equipes WHERE Id = ".$Team2;
	$row2 = db_fetch_array(main_db_query($query2));
	
	$Question = $QUESTIONS_TYPE[1].$FINALES[$match]["intitule"];
	echo "<br>";
	echo "<div class='question clearfix'>";
	echo "<h3>".$Question." : </h3>";
	echo "<br>";
	
	echo "<table border=0 class='smalldark' align=center><tr>\n";	
	
	echo "<tr><td align=left ><a href='prono_coupe.php?answerId=".$Team1."&match=".$match."&etape=".$etape."' >".$row["Name"]."</a></td>\n";
	echo "<td align=right ><img src='".$IMAGES_DIR.$row["Image"]."' /></td>\n";
	echo "<td align=center>-</td>\n";
	echo "<td align=left ><img src='".$IMAGES_DIR.$row2["Image"]."' /></td>\n";
	echo "<td align=right ><a href='prono_coupe.php?answerId=".$Team2."&match=".$match."&etape=".$etape."' >".$row2["Name"]."</a></td>\n";
	echo "</tr>";
	echo "<tr ><td colspan=5 ><center><i>(cliquez sur le gagnant)</i></center></td></tr>";
	echo "</table>";
	echo "</div>";
	echo "<br>";

}
//-----------------------------------------------------------------------------------------------------------------//
//Affichage des liens pour la finale des perdants
function generate_perdants_links($TabAnswers,$match,$etape,$user)
{
	global $IMAGES_DIR,$QUESTIONS_TYPE,$FINALES;
	
	//on recupere les 2 Coupe_Equipes qui joueront la finale
	$Team1 = $TabAnswers[(45-1)];
	$Team2 = $TabAnswers[(46-1)];
	$count = 0;
	//on compare ce 2 Coupe_Equipes finalistes au 4 Coupe_Equipes qui sont en 1/2 de finale pour en deduire les perdants des 1/2
	for($i=41-1;$i<=44-1;$i++)
	{
		//debug("TabAnswer=>".$TabAnswers[$i]." Team1=>".$Team1." Team2=>".$Team2);
		if($TabAnswers[$i] != $Team1 && $TabAnswers[$i] != $Team2)
		{
			$Selection[$count] = $TabAnswers[$i];
			$count++;
		}		
	}
	$query = "SELECT * FROM Coupe_Equipes WHERE Id = ".$Selection[0];
	$row = db_fetch_array(main_db_query($query));
	$query2 = "SELECT * FROM Coupe_Equipes WHERE Id = ".$Selection[1];
	$row2 = db_fetch_array(main_db_query($query2));
	
	$Question = $QUESTIONS_TYPE[1].$FINALES[$match]["intitule"];
	
	echo "<br>";
	echo "<table border=0 class='smalldark' width=80% align=center><tr>\n";
	echo "<td colspan=5 align=center class='smallbolddark'>".$Question." : </td></tr>\n";
	echo "<tr><td colspan=5><br></td></tr>";
	echo "<tr><td align=right width='40%'><a href='prono_coupe.php?answerId=".$Selection[0]."&match=".$match."&etape=".$etape."' class=smallBoldLinkWhite>".$row["Name"]."</a></td>\n";
	echo "<td align=right width='5%'><img src='".$IMAGES_DIR.$row["Image"]."' /></td>\n";
	echo "<td align=center width='10%'>-</td>\n";
	echo "<td align=left width='5%'><img src='".$IMAGES_DIR.$row2["Image"]."' /></td>\n";
	echo "<td align=left width='40%'><a href='prono_coupe.php?answerId=".$Selection[1]."&match=".$match."&etape=".$etape."' class=smallBoldLinkWhite>".$row2["Name"]."</a></td>\n";
	echo "</tr>";
	echo "<tr ><td colspan=5 ><center><i>(cliquez sur le gagnant)</i></center></td></tr>";
	echo "</table>";
	echo "<br>";

	
	
}


#####################################
# verifie sil y a des doublons dans la liste 
# du pronostic De la course
# retourne 1 si c est le cas, 0 sinon
#####################################
function hasDoublons_pre($tabProno)
{
	global $NB_MENU_CRS;
	
	$cpt=0;
	$hasDoublon = 0;
	//echo "yoDOUBLON";
	while( ($cpt<4) && ($hasDoublon != 1) )
	{
		$varCompare = $tabProno[$cpt];
		//echo "var compare = ".$varCompare;
		$i=$cpt;
		
		while( ($i<4) && ($hasDoublon != 1) )
		{
			$answerid = $tabProno[$i+1];
			//echo $varCompare." == ".$answerid;
			if($varCompare == $answerid)
				$hasDoublon = 1;	
			$i++;
		}
		$cpt++;
	}
//	echo "doublon? = ".$hasDoublon;
	return $hasDoublon;
}





//-----------------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------------//



include 'header.php';


if($Raz == 1 && $DATE_DEBUT_PRONO > date("Y-m-d H:i:s"))
{
	main_db_query("UPDATE Coupe_Paris SET Bet='' WHERE PlayerId =".$user);
}
/*else
{*/
	//On Récupere la chaine des reponses déjà données
	$betrow = db_fetch_array(main_db_query("SELECT Bet FROM Coupe_Paris WHERE PlayerId =".$user));

	//si aucune etape du pari n'a été saisie on crée un enregistrement vide pour le player
	if($betrow["Bet"]=="")
	{
		$Now = date("Y-m-d H:i");
		main_db_query("REPLACE INTO Coupe_Paris(PlayerId,MatchId,Bet,Date) VALUES (".$user.",0,'','".$Now."')");
		$TabAnswers = array();
		$EtapeEncours = 0;
	}
	//sinon on recupere les résultats déjà enregistrés...
	else
	{
		$TabAnswers = explode("-",substr($betrow["Bet"],1));
		$EtapeEncours = count($TabAnswers);
	}


	//si on vient de poster le formulaire (phrase match de pool)
	//echo $EtapeEncours."==".$etape." >".$send;
	if($send=="x" && $EtapeEncours==$etape)
	{
	
		$tabProno = array($Team0,$Team1,$Team2,$Team3);
		if(hasDoublons_pre($tabProno))
		{
			$erreur = "Vous avez placé une même équipe à 2 places différentes";
		}
		else
		{
			//on met a jour le paris du joueur
			$query = "UPDATE Coupe_Paris SET Bet = CONCAT(Bet,'-".$tabProno[0]."-".$tabProno[1]."-".$tabProno[2]."-".$tabProno[3]."') WHERE PlayerId=".$user;
			main_db_query($query);
			//on recharge la page....
			$EtapeEncours = $EtapeEncours+$NB_TEAM_POOL;
			//header("prono_coupe.php");
		}
		
	}


	//Validation pour les phases finales
	if(strlen($answerId) > 0 && $EtapeEncours==$etape)
	{
		$query = "UPDATE Coupe_Paris SET Bet = CONCAT(Bet,'-".$answerId."') WHERE PlayerId =".$user;
		main_db_query($query);
		array_push($TabAnswers,$answerId);
		$EtapeEncours = $EtapeEncours+1;
	}


	//On Ajoute la derniere reponse
	//Cas des Coupe_Poules
	if($EtapeEncours < $NB_MATCH_POOL)
	{
		$Question = $QUESTIONS_TYPE[0];
	
		//on conpose l'intitulé de la question à poser
		$pool = intval($EtapeEncours / $NB_TEAM_POOL);
		$place = ($EtapeEncours % $NB_TEAM_POOL)+1;	
		//debug("Pool:".$pool."--Etape:".$EtapeEncours);
	
		if($place == 1)
			$Question .= $place."ère du groupe ".$Coupe_Poules[$pool];
		else
			$Question .= $place."ème du groupe ".$Coupe_Poules[$pool];

		
		//on crée un restriction sql sur les reponses déjà données pour cette pool
		$selectIN = "";
		for($i=($EtapeEncours-($place-1));$i<$EtapeEncours;$i++)
		{
			$selectIN .= $TabAnswers[$i].",";
		}
		$selectIN = substr($selectIN, 0, strlen($selectIN)-1);

	}
	//Cas phase finale
	else
	{
		$match = $EtapeEncours-$NB_MATCH_POOL;
		$Final = $FINALES[$match]["id"];
		$Finalists = explode("-",$Final);
		$Team1 = $TabAnswers[$Finalists[0]-1];
		$Team2 = $TabAnswers[$Finalists[1]-1];
	}
	$Question .= " ?";
//}




include("prono_coupe.inc");

include 'footer.php';

close_page($sess);