<?php
	session_start();
	 
	//include('correlation.php');
	
	include('database.php');
	DB_connexion(); 
	error_reporting( 0 );
	
	/*if(!isset($_SESSION['logged']) )
	{		header ('Location: interdiction.php');
	}
	
	if($_SESSION['logged']== "no")
	{
		header ('Location: interdiction.php');
	}
	*/
/******** eviter des soumission intempestives lors d'un reload de la page F5  ************************************/	
	if(!empty($_POST) )
	{
		$_SESSION['sauvegarde_formulaire'] = $_POST ;
     
		$page_actuelle = $_SERVER['PHP_SELF'] ;
		if(!empty($_SERVER['QUERY_STRING']))
		{
			$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		}
     
		header('Location: ' . $page_actuelle);
		exit;
	}
	
	if(isset($_SESSION['sauvegarde_formulaire']))
	{
		$_POST = $_SESSION['sauvegarde_formulaire'] ;
		unset($_SESSION['sauvegarde_formulaire']);		
	}	
/************************************************************************/	
	  
?>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Description" content="Site perso,cours,ouvrages" />
<meta name="Keywords" content="WEBSITE perso " />
<meta name="Distribution" content="Global" />
<meta name="Robots" content="" />
<meta http-equiv="Content-Language" content="fr" />
<meta name="rating" content="general" />
<meta name="author" content="Patrice DELPY" />
<meta name="copyright" content="Copyright 2013" />
<meta name="generator" content="Patoche" />
<title>Patoche WEBSITE - 2013 - Template du site - en cours de développemment ...</title>


<META http-equiv="Cache-Control" content="no-cache"> 

<!--
en php
header("Cache-Control: no-cache, must-revalidate"); 
-->

<!---------------------------------------------------------->	
<?php
		include('include_css_template_commun.php');
		include('include_css.php');
		include('css_tools_skill.php');
		include('include_javascript.php');

?> 

<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<style type="text/css">
#bloc_table_forum { float: left;
					width: 831px; 
					padding: 50px 50px 25px 150px; // 50px 50px 25px 50px; 
					//text-align: center;
					// margin: auto;
					}	
					
#navigation_tournoi{
					float: left;
					width: 910px; // 870
					padding: 10px 0px 0px 10px;
					text-align: center;
		}
					
.marge {
    margin: auto;
    width: 60%;
    padding: 10px ;
	}
	
label {
		display:block;
		float:left;
		width:100px;
	  }

}			
					
</style>

<script type="text/javascript">

function call_update()
{
			scruter();
			setTimeout("call_update()", 1000); //appelle la fonction 1 fois par  seconde
}

function scruter()
{
			var xhr = getXhr()
			// On défini ce qu'on va faire quand on aura la réponse
			xhr.onreadystatechange = function()
			{
					// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
					if(xhr.readyState == 4 && xhr.status == 200)
					{
						retour = xhr.responseText;
						
						if(retour == 0)
						{  	
		
							$(function()
							{
									$( "#message_expiration" ).dialog({
									modal: true,
									title: "Infos",
									buttons: {
												Ok: function()
												{
													$( this ).dialog( "close" );
													 document.location.href = "logout_expire.php";
												}
											 }
										});
							});
						}
					}
			}
					
			xhr.open("GET","session_timeout.php",true);
			xhr.send(null);
}

function getXhr()
{
			var xhr = null; 
			if(window.XMLHttpRequest) // Firefox et autres
			   xhr = new XMLHttpRequest(); 
			else if(window.ActiveXObject)
			{ // Internet Explorer 
			   try 
			   {
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
			   } 
			   catch (e) 
			   {
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
			   }
			}
			else 
			{ 
				// XMLHttpRequest non supporté par le navigateur 
				alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
				xhr = false; 
			} 

			return xhr;
}

function sleep(delay)
{
			var start = new Date().getTime();
			while 
			(new Date().getTime() < start + delay);
}

function active()
{
			document.getElementById("score").style.visibility='visible';
			//starting();
}
		
function active_sauver()
{
			document.getElementById('send').disabled = false;
}


function verif_doublon()
{
			
			jour = document.getElementById('jour').value;
			mois = document.getElementById('mois').value;
			annee = document.getElementById('annee').value;
			
			var xhr = getXhr();
								
			xhr.onreadystatechange = function()
			{
						// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
						if(xhr.readyState == 4 && xhr.status == 200)
						{
								retour = xhr.responseText;
								alert(retour);
								
								if(retour == 1)
								{
									
									$(function()
									{
												$( "#message_doublon" ).dialog({
												modal: true,
												title: "Infos",
												buttons: {
															Ok: function()
															{
																$( this ).dialog( "close" );
																window.location.reload();
															}
														 }
													});
									});
								}
								else
								{
									alert("Sauvegarde la partie");
								}
								
						}
			}

					xhr.open("POST","verif_doublon_partie.php",true);
					xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					
					jour = document.getElementById('jour').value;
					mois = document.getElementById('mois').value;
					annee = document.getElementById('annee').value;
					
					sel = document.getElementById('J1');
					J1 = sel.options[sel.selectedIndex].value;
					sel = document.getElementById('J2');
					J2 = sel.options[sel.selectedIndex].value;
					
					sel = document.getElementById('selection_score');
					selection_score = sel.options[sel.selectedIndex].value;

					//xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_date="+selection_date+"&selection_score="+selection_score);
					xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_score="+selection_score);
					
			
}
	
function start()
{
			/* Checker le selecteur de date*/
			jour = document.getElementById('jour').value;
			mois = document.getElementById('mois').value;
			annee = document.getElementById('annee').value;
			
			sel = document.getElementById('J1');
			J1 = sel.options[sel.selectedIndex].value;
			sel = document.getElementById('J2');
			J2 = sel.options[sel.selectedIndex].value;
			
			sel = document.getElementById('selection_score');
			selection_score = sel.options[sel.selectedIndex].value;
			
			if( jour == "-- Jour --" || jour == "-- Mois --"  )
			{
				$(function()
						{
									$( "#message_date_invalide" ).dialog({
									modal: true,
									title: "Infos",
									buttons: {
												Ok: function()
												{
													$( this ).dialog( "close" );
													window.location.reload();
												}
											 }
										});
						});
				
			}
			
/*			
			else if( J1 == "joueur1"   || J2 == "joueur2"  || selection_score == "resultat" )
			{
				alert("BUG" + "   " + J1 + "  " + J2 + "  " + selection_score);
			}
*/			
			else
			{		
					//alert("TRACE");
			
					document.getElementById("image").style.display='block';
					
					var xhr = getXhr();
								
					xhr.onreadystatechange = function()
					{
						// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
						if(xhr.readyState == 4 && xhr.status == 200)
						{
							retour = xhr.responseText;
							document.getElementById("image").style.display='none';		
							
							if(retour == 1)
							{
								//affiche = "La nouvelle partie a été enregistrée dans la base.";
								$(function()
								{
											$( "#message_ajout" ).dialog({
											modal: true,
											title: "Infos",
											buttons: {
														Ok: function()
														{
															$( this ).dialog( "close" );
															window.location.reload();
														}
													 }
												});
								});
							}
							
							if(retour == 2)
							{
								//affiche = "La date n'est pas valide !.";
								$(function()
								{
											$( "#message_date_non_valide" ).dialog({
											modal: true,
											title: "Infos",
											buttons: {
														Ok: function()
														{
															$( this ).dialog( "close" );
															window.location.reload();
														}
													 }
												});
								});
							}
							
							if(retour == 3)
							{
								//affiche = "La partie est déjà dans la database !.";
								$(function()
								{
											$( "#message_doublon" ).dialog({
											modal: true,
											title: "Infos",
											buttons: {
														Ok: function()
														{
															$( this ).dialog( "close" );
															window.location.reload();
														}
													 }
												});
								});
							}
							
							if(retour == 4)
							{
								//affiche = "La partie est déjà dans la database !.";
								$(function()
								{
											$( "#message_probleme" ).dialog({
											modal: true,
											title: "Infos",
											buttons: {
														Ok: function()
														{
															$( this ).dialog( "close" );
															window.location.reload();
														}
													 }
												});
								});
							}
							
							
								
						}
					}

					xhr.open("POST","traitement_ajouter_partie02.php",true);
					xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					
					jour = document.getElementById('jour').value;
					mois = document.getElementById('mois').value;
					annee = document.getElementById('annee').value;
					
					sel = document.getElementById('J1');
					J1 = sel.options[sel.selectedIndex].value;
					sel = document.getElementById('J2');
					J2 = sel.options[sel.selectedIndex].value;
					
					
					
					sel = document.getElementById('selection_score');
					selection_score = sel.options[sel.selectedIndex].value;

					//xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_date="+selection_date+"&selection_score="+selection_score);
					xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_score="+selection_score);
					
			}		
}			

function affiche_joueur()
{
			document.getElementById("masque_joueur").style.visibility='visible';
}			
			
function go()
{
			var xhr = getXhr();
			document.getElementById("souscategorie").style.visibility='visible';

			// On défini ce qu'on va faire quand on aura la réponse
			xhr.onreadystatechange = function()
			{
				if(xhr.readyState == 4 && xhr.status == 200)
				{
					leselect = xhr.responseText;
					// On se sert de innerHTML pour rajouter les options a la liste
					document.getElementById('souscategorie').innerHTML = leselect;
				}
			}

			xhr.open("POST","ajax_joueur.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			sel = document.getElementById('J1');
			idcategorie = sel.options[sel.selectedIndex].value;
			xhr.send("idcategorie="+idcategorie);
}
			
/*function starting()
{
			var xhr = getXhr();
			// On défini ce qu'on va faire quand on aura la réponse
						
			document.getElementById("probabilite").style.visibility='visible';
						
			xhr.onreadystatechange = function()
			{
				// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
				if(xhr.readyState == 4 && xhr.status == 200)
				{
					leselect = xhr.responseText;
					// On se sert de innerHTML pour rajouter les options a la liste
					document.getElementById('probabilite').innerHTML = leselect;
				}
			}

			
			xhr.open("POST","echec03.php",true); // proba
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			sel = document.getElementById('J1');
			J1 = sel.options[sel.selectedIndex].value;
			sel = document.getElementById('J2');
			J2 = sel.options[sel.selectedIndex].value;
			xhr.send("J1="+J1+"&J2="+J2);
}
*/
</script>

<!---------------------------------------------------------->	
</head>

<body>
<!---------------------------------------------------------->	
	<div class="main"> <!-- main -->
<!---------------------------------------------------------->	

          <div class="header">
                <div class="balise_titre">Patoche</div>
                <div class="balise_citation">
                &quot;Etre conscient que l'on est ignorant est un grand pas vers le savoir&quot;
                </div>
          </div> 
 
          <div class="banniere"></div>  

<!---------------------------------------------------------->
<?php
/*	include("include_barre_login.php");
	echo '<div id="box_icon">';
	include("debug_connexion_membre.php");
	echo '</div>';
	include("include_menu.php");*/
	
	echo '<div id="box_infos">';
			include("include_menu.php");
			
			echo '<div id="box_icon2">';
			include("connexion_membre.php");
			echo	'</div>';
			
			echo '<div id="box_icon3">';
			include("include_barre_login.php");
			echo	'</div>';

	echo	'</div>';
	$_SESSION['page_active'] = basename (__FILE__ );
?>
		

<!---------------------------------------------------------->	
            <div class="contenu"> <!-- contenu -->
<!---------------------------------------------------------->	
	
			<div class="barre_vide"></div>
			
			<div id="navigation_tournoi">	
			<ul id="menu_horiz">		
			<li class="tournoi_partie_tag_selected"><a href="partie.php" title="aller à la section">Duel</a></li>		
			<li class="tournoi_simulation_tag"><a href="simulation.php" title="aller à la section">Sim</a></li>			
			<li class="tournoi_classement_tag"><a href="classement02.php" title="aller à la section">ELO</a></li>	
			<li class="tournoi_master_tag"><a href="on_chess_master02.php" title="aller à la section">Master</a></li>	
			<li class="tournoi_liste_joueurs_tag"><a href="joueurs_echec.php" title="aller à la section">Players</a></li>	
			<li class="tournoi_stats_tag"><a href="historique.php" title="aller à la section">Games</a></li>	
			<li class="tournoi_reglement_tag"><a href="reglement.php" title="aller à la section">Rules</a></li>		
			<li class="tournoi_securite_tag"><a href="admin.php" title="aller à la section">Admin</a></li>	
			</ul>			
			</div>
			
			<div id="bloc_table_forum" >
	<!--
			<form method="post" id="form_selection_joueurs" action="partie.php" onsubmit="return start(this)"> <!-- onsubmit="return valider_inscription(this)" -->
			
			<!---------->
<?php
			// listing des parties du jour

			$date = date("d-m-Y");
			list($jour, $mois, $annee) = explode('-', $date);
			$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
			//echo $timestamp;
			//echo "<br>";
			
			//$sql = "SELECT * from parties  WHERE date = '$timestamp'"; 
			$sql = "SELECT * from " .$_SESSION['parties']  ." WHERE date = '$timestamp'"; 
						
			$resultat = mysql_query($sql);
			$n = mysql_num_rows($resultat);
						
			if($n !=0)
			{
					
					echo "Pour informations, voici la liste des parties saisies aujourd'hui: " .date('d/m/Y') ."<br><br>";
					/*
					while($data = mysql_fetch_assoc($resultat))
					{
						// w_playerID 	w_name 	b_playerID 	b_name 	w_brating 	w_arating 	b_brating 	b_arating 	result 	date 
						$date_partie = date('d/m/Y', $data['date']);
						$resultat = $data['result'];
						
						echo $data['w_name'] ." " .$data['b_name'] ." " .$data['result'] ." " .$date_partie ."<br>";
					} */
					$cpt = 1;
					
					while($row = mysql_fetch_assoc($resultat))
					{
						$joueur = $row["joueur"]; 
						$elo = $row["elo"];
						switch ($row["result"]) {
								case 1:
									$score = "Blanc gagne";
									break;
								case 2:
									$score = "Noir gagne";
									break;
								case 3:
									$score = "Match nul";
									break;
							}
						
						
						echo $cpt ."#     " .$row['w_name'] ." (Blanc) - " .$row['b_name'] ." (Noir) : " .$score ."<br>";
						$cpt = $cpt + 1;
			
					}
			}		
			else
			{
				echo "Aucunes parties saisies aujourd'hui";
			}

			echo "<br><br>";
?>			
			
			<!---------->
			
			
			<fieldset style="width: 600px" >
				<legend>Saisir une partie </legend>

<p>
			<label>Date: </label>
			<select name="jour" id="jour">

<?php
			$jour = date("j");
			$mois = date("n");
			$annee = date("Y");
			
			echo "<option>-- Jour --</option>";
			for ($i = 1; $i <= 31; $i++)
			{
				if($i == $jour)
				{
					echo "<option selected='selected' >$i</option>";
				}
				else
				{
					echo "<option >$i</option>";
				}
			}
?>			
			</select>	
	
			<select name="mois" id="mois">
			
<?php
			echo "<option>-- Mois --</option>";
			
			if($mois == 1)
			{
				echo "<option selected='selected'>Janvier</option>";
			}
			else
			{
				echo "<option>Janvier</option>";
			}
			
			if($mois == 2)
			{
				echo "<option selected='selected'>Février</option>";
			}
			else
			{
				echo "<option>Février</option>";
			}
			
			if($mois == 3)
			{
				echo "<option selected='selected'>Mars</option>";
			}
			else
			{
				echo "<option>Mars</option>";
			}
			
			if($mois == 4)
			{
				echo "<option selected='selected'>Avril</option>";
			}
			else
			{
				echo "<option>Avril</option>";
			}
			
			if($mois == 5)
			{
				echo "<option selected='selected'>Mai</option>";
			}
			else
			{
				echo "<option>Mai</option>";
			}
			
			if($mois == 6)
			{
				echo "<option selected='selected'>Juin</option>";
			}
			else
			{
				echo "<option>Juin</option>";
			}
			
			if($mois == 7)
			{
				echo "<option selected='selected'>Juillet</option>";
			}
			else
			{
				echo "<option>Juillet</option>";
			}
			
			if($mois == 8)
			{
				echo "<option selected='selected'>Aôut</option>";
			}
			else
			{
				echo "<option>Aôut</option>";
			}
			
			if($mois == 9)
			{
				echo "<option selected='selected'>Septembre</option>";
			}
			else
			{
				echo "<option>Septembre</option>";
			}
			
			
			if($mois == 10)
			{
				echo "<option selected='selected'>Octobre</option>";
			}
			else
			{
				echo "<option>Octobre</option>";
			}
			
			if($mois == 11)
			{
				echo "<option selected='selected'>Novembre</option>";
			}
			else
			{
				echo "<option>Novembre</option>";
			}
			
			if($mois == 12)
			{
				echo "<option selected='selected'>Décembre</option>";
			}
			else
			{
				echo "<option>Décembre</option>";
			}
			
?>
			</select>
	
			<select name="annee" id="annee" > <!-- onchange='affiche_joueur()' -->
<?php
			echo "<option>-- Année --</option>";

			for ($i = 2015; $i <= 2020; $i++)
			{
				if($i == $annee)
				{
					echo "<option selected='selected' >$i</option>";
				}
				else
				{
					echo "<option >$i</option>";
				}
			}
?>
			</select>
				
</p>
				<div id='masque_joueur' > <!--  style="visibility: hidden;"  -->
				<p>				
				<div class="center">
			
				<label>Blanc: </label>
				<select name='J1' id='J1' onchange='go()'>
					<option value="joueur1">- - - Choisissez le Joueur 1 - - -</option>
					<?php
						
						//$sql = "SELECT * FROM classement";
						$sql = "SELECT * FROM "	.$_SESSION['classement'];
                        
                        $sql = "SELECT * FROM "    .$_SESSION['classement'] . " ORDER BY joueur ASC";
                                                                                                 						
						$res = mysql_query($sql);
						while($val = mysql_fetch_array($res)) 
						{
							echo "<option>".$val["joueur"]."</option>\n";
						}
					?>
				</select>
				</div>		
				</p>		
				</div>
				
				<div id='souscategorie' style="visibility: hidden;"  >  <!-- style='display:inline' -->

				<div class="center">					
				
				</br>
				
				<label>Noir: </label>
					<select name='J2' id='J2'  ">  <!--   -->
					</select>
				</div>					
				</div> 
				
				<img src="loader.gif" style="display: none;" name = "image" id="image">
				
				<br>
				
				<div id="score" style="visibility: hidden; ">
					<label>Résultat :</label>
					<select name='selection_score' id='selection_score' size="1" style='display:inline' onchange='active_sauver()' >
					<option value="resultat">- - - Resultat de la partie - - -</option>

<?php
					$J1_nom = $_SESSION['$J1_nom'];
					$J2_nom = $_SESSION['$J2_nom'];
		
					echo '<option>Blanc gagne</option>';
					echo '<option>Blanc perd</option>';
					echo '<option>Match nul</option>';
?>
				
					</select>
				</div>
				
				<br/>
<?php
				
				//if($_SESSION['logged']!= "no")
				//{
					echo '		
							<div> 
							<input id="send" name="send" type="submit"  disabled value="Enregistrer"  onclick="start()" /> 
							</div>';
				//}
?>				
				<br>
				<br>
				
				<div id='message'></div>
				
				<div id="message_ajout" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				La partie a été ajouté à la base.
				</p>
				</div>
				
				<div id="message_date_non_valide" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				La date n'est pas valide, recommencer !.
				</p>
				
				<div id="message_date_invalide" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				La date choisie n'est pas valide !.
				</p>
				</div>
				
				<div id="message_expiration" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				La session a expiré. Veuillez vous reconnecter.
				</p>
				
				<div id="message_doublon" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				La partie existe déjà dans la database !.
				</p>
				
				<div id="message_probleme" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				Les champs ne sont pas valides !.
				</p>
				
				
				<div id="message_connexion" style="display: none">
				<p>
				<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
				Bienvenue.
				</p>
				</div>
				
			</fieldset>
		<!--	</form> -->
		
			</div>

			<br/>
			<br/>
			<br/>
			<br/>
			<br/>

<!---------------------------------------------------------->	
<?php			
    include('include_pied_page.php');
	include('include_publicite.php');
?>
<!---------------------------------------------------------->	
			</div>  <!-- contenu -->
<!---------------------------------------------------------->	
	</div>   <!-- main -->
<!---------------------------------------------------------->	

	<script type="text/javascript">
		call_update();
	</script>

</body>
</html>


