<?php
	session_start();
	include('database.php');
	DB_connexion(); 
	
	/*if(!isset($_SESSION['logged']) )
	{		header ('Location: interdiction.php');
	}
	
	if($_SESSION['logged']== "no")
	{
		header ('Location: interdiction.php');
	} */
	
	/************* pour s'affranchir de la multiple validation + rafraissisement **********************************/		
	/*if(!empty($_GET) )
	{
		//$_SESSION['sauvegarde_formulaire'] = $_GET ;
		//$page_actuelle = $_SERVER['PHP_SELF'] ;
		//if(!empty($_SERVER['QUERY_STRING']))
		//{
		//	$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		//}
     
	    $page_actuelle = "tchat_site_work.php"; 
	 
		header('Location: ' . $page_actuelle);
		exit;
	}

	if(isset($_SESSION['sauvegarde_formulaire']))
	{
				$_GET = $_SESSION['sauvegarde_formulaire'] ;
				unset($_SESSION['sauvegarde_formulaire']);
	}	
	*/
	$chat_msg = '';
	$chat_destinataires= '';
	//$_SESSION['login'] = "plouf";
	
    //vérifications des données (éviter les attaques SQL)
            
    if (isset($_GET['msg'])) {
         $chat_msg = htmlspecialchars($_GET['msg'], ENT_QUOTES); //protection du message
    }

	if (isset($_GET['destinataire'])) {
         $chat_destinataires = htmlspecialchars($_GET['destinataire'], ENT_QUOTES); //protection des destinataires
    }
	
	if( isset($_GET['msg']) && $_GET['msg'] != '' && isset($_GET['action']) && $_GET['action'] == "chat" )
	  {
					//$_SESSION['login'] = "lolo";
					//echo $_GET["membres[]"];
					//var_dump($_GET); 
					//echo $chat_destinataires;
					mysql_query("INSERT INTO tchat VALUES('', '".$_SESSION['login']."', '".$chat_msg."' ,'".$chat_destinataires."', NOW() , '' )");
					header('Location:tchat_reload.php');
	}
	

?>

<!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">
<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>
<!---------------------------------------------------------->	
<?php
		include('include_css_template_commun.php');
		include('include_css.php');
		include('css_tools_skill.php');
		include('include_javascript.php');
?> 

<style type="text/css">
			 
</style>

<link href="css/tchat_style.css" rel="stylesheet" type="text/css">	

<script type="text/javascript">

function selectAll(selectname)
{
	var selectbox = document.forms[0].elements[selectname];
	for (var i = 0; i < selectbox.length; i++)
	{
		selectbox.options[i].selected = true;
	}
}

</script>

<script type="text/javascript">

function select_all_connected() 
{

	/*document.getElementById('valide_form').value="ok";*/
	
	
	//var aSelect = document.test_multiple_select_form.test_multiple_select[];
	var aSelect = document.formulaire_chat["membres[]"]; 
	var aSelectLen = aSelect.length;
	for(i = 0; i < aSelectLen; i++) 
	{
		aSelect.options[i].selected = true;
	}
	
	
}

function unselect_all_connected() 
{

	/*document.getElementById('valide_form').value="ok";*/
	
	
	//var aSelect = document.test_multiple_select_form.test_multiple_select[];
	var aSelect = document.formulaire_chat["membres[]"]; 
	var aSelectLen = aSelect.length;
	for(i = 0; i < aSelectLen; i++) 
	{
		aSelect.options[i].selected = false;
	}
	
	
}

</script>

<script type="text/javascript">
function call_update()
{
	 ajax_routine(); 
	 setTimeout("call_update()", 1000); //appelle la fonction 1 fois par  seconde
}
</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
	
	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 id = "box_message">		
		<h2 class="retrait">Tchat du site.</h2>
	</div>
		
	<div class="centerblock">
	
				<div class="gauche">  
					<br/> 
						<div class="contenthead">Messages:</div>
							<div class="contentblock">
					
								<div id= "rafraichir">
<?php		
									$msgs=mysql_query('SELECT * FROM tchat ORDER BY id DESC LIMIT 0,50');
									$cpt = 0;
																		
									while($infos = mysql_fetch_array($msgs)) 
									{
										$champ = date('d-m-Y ', strtotime($infos["date_mess"])) ."  à  " .date(' H:i:s', strtotime($infos["date_mess"])); 
										$utilisateur = $infos["login"];
										$txt = $infos["mess"];
										$id = $infos["id"];
										$destinataires = $infos["destinataires"];
									
										$pieces = explode(",", $destinataires);
										$nb_elements = count($pieces);
										
										$flag_login = false;
										
										for($i = 0; $i < $nb_elements; $i++)
										{	$element = $pieces[$i];
											if($element == $_SESSION['login'])	
											{  $flag_login = true;
											    echo '<div class="pseudo"><img src="images/puce_tchat_login.jpg" />&nbsp;'.$utilisateur.'</div> ';
												if($cpt%2 == 1) /* impair */
													echo '<div class="affiche_message_pair">'.$txt.'</div>';
												else /* pair */
													echo '<div class="affiche_message_impair">'.$txt.'</div>';
												echo '<div class="date">'.$champ.'<img src="images/puce_tchat_date.jpg" /></div>';
												$cpt = $cpt + 1;
											}
										}
								    }             
?>					
								</div>
								
							</div>
				</div>
				
				
				<div class="droite">  
                <br/> 				

				<form  action='<?=$_SERVER["PHP_SELF"];?>' method="get" name="formulaire_chat" onsubmit="return bravo(this)"> 

				<div class="droitehead">Membres connectés:</div>
					<div class="droiteblock">
                    
                    <select name="membres[]" id="membres" style="width: 200px; height:300px;"  multiple="multiple" > <!--onchange="recupSelected(this.value);"> -->
<?php
					$query = "SELECT * FROM user_online WHERE connexion = 'online' ";
					$result = mysql_query($query);
					while( $data = mysql_fetch_assoc($result) )
					{	$var = $data["login"];
						echo '<option>'.$var.'</option>';
					}						
?>
                    
					 </select>
					</div> 
                </div>
				
				
				<div class="saisir_message">  
					<textarea rows="4" cols="50" name="msg" id="msg" ></textarea> 
					<input type="hidden" name="destinataire" id="destinataire" value="" />	
					<input type="hidden"  name="action" id="action"  value="" />
					<input type="submit" value="Envoyer" onclick="traitement();" /> 
					<!--<input type="checkbox" name="check_selected" value="" onclick="select_all_connected()" >Selection
					<input type="checkbox" name="check_selected" value="" onclick="unselect_all_connected()" >Déselection
					-->
					<input type="radio" name="check_selected" value="select" onclick="select_all_connected()">Selection 
					<input type="radio" name="check_selected" value="unselect" onclick="unselect_all_connected()">Déselection
					
					<br>
					
					<!--<a href="javascript:select_all_connected()" title="select all">select all</a> -->
				</div> 

				</form> 
			
	
	</div>		
	
	<br/>
	<br/>
	
           

<!---------------------------------------------------------->	
<?php			
    include('include_pied_page.php');
	include('include_publicite.php');
?>
<!---------------------------------------------------------->	
			</div>  <!-- contenu -->
<!---------------------------------------------------------->	
	</div>   <!-- main -->
<!---------------------------------------------------------->	
	<script type="text/javascript">
		call_update()
	</script>

	<script type="text/javascript">
		callUpdateChat() 
    </script>

</body>
</html>
