<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.6
Copyright (C) 2006  Sylvain FATOME

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

session_start(); 
?>
<?php if ((@$HTTP_SESSION_VARS["etat_log"] <> "connecte") or ((@$HTTP_SESSION_VARS["niv_log"] <> 9) AND (@$HTTP_SESSION_VARS["niv_log"] <> 5))) header("Location: ../index.php") ?>
<?php include ("../inc/config.inc.php") ?>
<?php require ("../inc/func.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php require ("../inc/func_log.inc.php") ?>
<?php require ("../inc/func_txt.inc.php") ?>
<?php require ("../inc/fic.inc.php") ?>
<?php require ("../inc/lst.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);

	$txt_stat_age = array(
		"--- Merci de choisir un type d'analyse statistique ---",
		"Nombre de créations de clients sur une période donnée",
		"Répartition des catégories d'agences actives");

 	if (strip_tags(trim(@$_POST["Submit"])) <> "Calculer") // On a peut-être choisi un type de statistique
	{
		if (strip_tags(trim(@$_POST["Submit"])) == "OK")
		{
			$typstat = strip_tags(trim($_POST['ts']));
		}
		else
			$typstat = 0;
	}
	else
		$typstat = strip_tags(trim($_POST['ts']));
	
	$stat_ok = TRUE;
	$aff_stat = FALSE;
	$pbm_stat = FALSE;

	// Vérification du mode du connecté (pour éviter les hacks)
	if (@$HTTP_SESSION_VARS["niv_log"] < 5)
		$stat_ok = FALSE;

	switch ($typstat)
	{
		/* Nombre de créations de clients sur une période donnée */
		case 1 :
		
			if (strip_tags(trim(@$_POST["Submit"])) == "Calculer")
			{
				$date1f = strip_tags(trim(@$_POST["date1f"]));
				if ($date1f <> "")
					$date1 = datefrang($date1f);
				else
				{
					$stat_ok = FALSE;
					$msg = "Problème sur les dates...retour au choix";
					$pbm_stat = TRUE;
				}
				
				if ($stat_ok == TRUE)
				{
					$date2f = strip_tags(trim(@$_POST["date2f"]));
					if ($date2f <> "")
						$date2 = datefrang($date2f);
					else
					{
						$stat_ok = FALSE;
						$msg = "Problème sur les dates...retour au choix";
						$pbm_stat = TRUE;
					}
				}
				
				if ($stat_ok == TRUE)
				{
					if (count($_POST["age"]) > 0)
					{
						$age_select = "";
						for ($i = 0; $i < count($_POST["age"]); $i++)
						{
							$age_select.=$_POST["age"][$i]."-";
						}
						$age_select_tab = explode("-",substr($age_select,0,strlen($age_select)-1));
						if ((in_array("toutes",$age_select_tab)) AND (count($age_select_tab) > 1))
						{
							$stat_ok = FALSE;
							$msg = "\"TOUTES\" doit être choisi seul...retour au choix";
							$pbm_stat = TRUE;
						}
					}					
					else
					{
						$stat_ok = FALSE;
						$msg = "Vous n'avez pas sélectionné d'agence...retour au choix";
						$pbm_stat = TRUE;
					}
				}
				
				if ($stat_ok == TRUE)
				{
					$cat = strip_tags(trim(@$_POST["cat"]));
					if ($cat == "")
					{
						$stat_ok = FALSE;
						$msg = "Vous n'avez pas sélectionné de catégorie...retour au choix";
						$pbm_stat = TRUE;
					}
				}

				if ($stat_ok == TRUE)
				{
					// Analyse des données et traitements statistiques
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
				
					/* Initialisation des totaux généraux */
				
					$nb_cli_crees_total = 0;
					$nb_cli_prospects_crees_total = 0;
					$nb_cli_ddeint_crees_total = 0;
					$nb_cli_inter_crees_total = 0;
					
					/* Calcul des totaux généraux */

					$rech = "SELECT age_code FROM ".TBAGE;
					$rech.= " ORDER BY age_code";
				
					$rs = mysql_query($rech) or die(mysql_error());
						
					if (mysql_num_rows($rs) >0)
					{
						while ($row = mysql_fetch_assoc($rs)) 
						{
							extract($row);

							// Détermination du nombre de clients créés par les agences sur la période
							$rech2 = "SELECT * FROM ".TBCLI;
							$rech2.= " WHERE cli_codecrea='".$age_code."'";
							$rech2.= " AND cli_datecrea>='".$date1."' AND cli_datecrea<='".$date2."'";
		
							$rs2 = mysql_query($rech2) or die(mysql_error());
							$nb_cli_crees_total = $nb_cli_crees_total + mysql_num_rows($rs2);
							
							mysql_free_result($rs2);

							// Détermination du nombre de clients encore prospects et créés par les agences sur la période
							$rech2 = "SELECT * FROM ".TBCLI;
							$rech2.= " WHERE cli_codecrea='".$age_code."'";
							$rech2.= " AND cli_type=0";
							$rech2.= " AND cli_datecrea>='".$date1."' AND cli_datecrea<='".$date2."'";
						
							$rs2 = mysql_query($rech2) or die(mysql_error());
							$nb_cli_prospects_crees_total = $nb_cli_prospects_crees_total + mysql_num_rows($rs2);

							mysql_free_result($rs2);
						
							// Détermination du nombre de client ayant demandé une intervention (non effectuée) et créés par les agences sur la période
							$rech2 = "SELECT * FROM ".TBCLI;
							$rech2.= " WHERE cli_codecrea='".$age_code."'";
							$rech2.= " AND cli_type=1";
							$rech2.= " AND cli_datecrea>='".$date1."' AND cli_datecrea<='".$date2."'";
						
							$rs2 = mysql_query($rech2) or die(mysql_error());						
							$nb_cli_ddeint_crees_total = $nb_cli_ddeint_crees_total + mysql_num_rows($rs2);
							
							mysql_free_result($rs2);

							// Détermination du nombre total de clients ayant demandé une intervention (effectuée) et créés par les agences sur la période
							$nb_cli_inter_crees_total = $nb_cli_crees_total - $nb_cli_prospects_crees_total - $nb_cli_ddeint_crees_total;
						}
					}

					/* Traitement par agence */
				
					$rech = "SELECT * FROM ".TBAGE;
					
					// Traitement de multiples
					if (!in_array("toutes",$age_select_tab))
					{
						for ($i=0; $i<count($age_select_tab); $i++)
						{
							if(!stristr($rech,"WHERE "))
								$rech.= " WHERE age_code='".$age_select_tab[$i]."'";
							else
								$rech.= " OR age_code='".$age_select_tab[$i]."'";
						}
					}
					if ($cat <> "tcat")
					{
						if(!stristr($rech,"WHERE "))
							$rech.= " WHERE age_cat='".$cat."'";
						else
							$rech.= " AND age_cat='".$cat."'";
					}
					$rech.= " ORDER BY age_nom";
				
					$rs = mysql_query($rech) or die(mysql_error());
					$nb_age_select = mysql_num_rows($rs);

					$i = 0;
						
					if ($nb_age_select >0)
					{
						while ($row = mysql_fetch_assoc($rs)) 
						{
							extract($row);

							// Récupération du code agence
							$code_agence[$i] = $age_code;

							// Détermination du nombre de clients créés par l'agence sur la période
							$rech2 = "SELECT * FROM ".TBCLI;
							$rech2.= " WHERE cli_codecrea='".$age_code."'";
							$rech2.= " AND cli_datecrea>='".$date1."' AND cli_datecrea<='".$date2."'";
		
							$rs2 = mysql_query($rech2) or die(mysql_error());
							$nb_cli_crees_age[$i] = mysql_num_rows($rs2);
							mysql_free_result($rs2);

							// Détermination du nombre de clients encore prospects et créés par l'agence sur la période
							$rech2 = "SELECT * FROM ".TBCLI;
							$rech2.= " WHERE cli_codecrea='".$age_code."'";
							$rech2.= " AND cli_type=0";
							$rech2.= " AND cli_datecrea>='".$date1."' AND cli_datecrea<='".$date2."'";
						
							$rs2 = mysql_query($rech2) or die(mysql_error());
							$nb_cli_prospects_crees_age[$i] = mysql_num_rows($rs2);
							mysql_free_result($rs2);
						
							// Détermination du nombre de client ayant demandé une intervention (non effectuée) et créés par les agences sur la période
							$rech2 = "SELECT * FROM ".TBCLI;
							$rech2.= " WHERE cli_codecrea='".$age_code."'";
							$rech2.= " AND cli_type=1";
							$rech2.= " AND cli_datecrea>='".$date1."' AND cli_datecrea<='".$date2."'";
						
							$rs2 = mysql_query($rech2) or die(mysql_error());
							$nb_cli_ddeint_crees_age[$i] = mysql_num_rows($rs2);
							mysql_free_result($rs2);

							// Détermination du nombre total de clients ayant demandé une intervention (effectuée) et créés par les agences sur la période
							$nb_cli_inter_crees_age[$i] = $nb_cli_crees_age[$i] - $nb_cli_prospects_crees_age[$i] - $nb_cli_ddeint_crees_age[$i];

							$i++;
						}
					}
					mysql_close($conn);  
					
					$aff_stat = TRUE;
				}
			}
			
			break;
		

		/* Répartition des catégories d'agences actives */
		case 2 :
			if (strip_tags(trim(@$_POST["Submit"])) == "OK")
			{
				if ($stat_ok == TRUE)
				{
					// Analyse des données et traitements statistiques
					$nb_total_agences = nb_age();
					
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
				
					/* Traitement par agence */
				
					$rech = "SELECT * FROM ".TBACAT;			
					$rech.= " ORDER BY acat_desc";
				
					$rs = mysql_query($rech) or die(mysql_error());
					$nb_acat_select = mysql_num_rows($rs);

					$nb_age_select = 0;
					$i = 0;
						
					if ($nb_acat_select >0)
					{
						while ($row = mysql_fetch_assoc($rs)) 
						{
							extract($row);

							// Récupération du code agence
							$code_acat[$i] = $acat_code;

							// Détermination du nombre d'agences actives pour la catégorie examinée
							$rech2 = "SELECT * FROM ".TBAGE;
							$rech2.= " WHERE age_cat='".$acat_code."'";
							$rech2.= " AND age_ouvert='1'";
		
							$rs2 = mysql_query($rech2) or die(mysql_error());
							$nb_acat_age[$i] = mysql_num_rows($rs2);
							$nb_age_select = $nb_age_select + $nb_acat_age[$i];
							mysql_free_result($rs2);

							$i++;
						}
					}
					mysql_close($conn); 
					
					$aff_stat = TRUE;
				}
			}
			break;

		default : 
			break;
	}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Statistiques Agences</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

<!-- Définition Javascript pour calendrier popup -->

  <!-- Feuille de Style -->
  <link rel="stylesheet" type="text/css" media="all" href="../jscal/skins/aqua/theme.css" title="Aqua" />

  <!-- Script Javascript Principal -->
  <script type="text/javascript" src="../jscal/calendar.js"></script>

  <!-- Choix du langage -->
  <script type="text/javascript" src="../jscal/lang/calendar-fr.js"></script>

  <!-- the following script defines the Calendar.setup helper function, which makes
       adding a calendar a matter of 1 or 2 lines of code. -->
  <script type="text/javascript" src="../jscal/calendar-setup.js"></script>

<!-- Affichage nouvelle fenêtre pour impression -->
<script type="text/javascript">
	function lanceimp(page)
	{
	  window.open(page,'impression','width='+screen.width+' height='+screen.height);
	}
</script>

<!-- Fin de la définition Javascript pour calendrier popup -->

</head>

<body>

<?php 
	include_once ("../inc/entete.php"); 
?>

<p align="center"> <span class="titrepage">STATISTIQUES AGENCES </span></p>
<?PHP
if (nb_age() >0)
{
?>
	<form name="typstat" method="post" action="age_stat.php">
	<br>
	<div align="center">
	<?PHP
	if ($typstat == 0)
	{
	?>
		<select name="ts" id="ts">
		<?PHP
		for ($i=0; $i<count($txt_stat_age); $i++)
		{
			if ($typstat == $i)
			{
			?>
				<option value="<?PHP echo $i; ?>" selected="selected"><?PHP echo $txt_stat_age[$i]; ?></option>
			<?PHP
			}
			else
			{
			?>
				<option value="<?PHP echo $i; ?>"><?PHP echo $txt_stat_age[$i]; ?></option>
			<?PHP
			}
		}
		?>					
		</select>
		<input type="submit" name="Submit" value="OK">
	<?PHP
	}
	else
	{
	?>
		<br>
		<span class="titre_gras_soul">
		<?PHP echo $txt_stat_age[$typstat]; ?>
		</span>
		<br><br><br>
	<?PHP
		if (count($age_select_tab) <> "")
		{
			switch ($typstat)
			{
			case 1 :
				echo "<span class=\"annot_std_it\">";
				if ((in_array("toutes",$age_select_tab)) AND (count($age_select_tab) == 1))
				{
					echo "Toutes les agences";
				}
				else
				{
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					if (!in_array("toutes",$age_select_tab))
						if (count($age_select_tab) == 1) // Une seule agence choisie
							$liste_age = "Pour l'agence : ";
						else
							$liste_age = "Pour les agences : ";
					for ($i=0; $i<count($age_select_tab); $i++)
					{
						$rs = mysql_query("SELECT * FROM ".TBAGE." WHERE `age_code` = '".$age_select_tab[$i]."'");
						if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
						{
							if ($row = mysql_fetch_array($rs))
							{
								extract($row);
								$liste_age .=$age_nom.", ";
							}
						}
					}
					echo substr($liste_age,0,strlen($liste_age)-2);
					mysql_free_result($rs);
					mysql_close($conn);
				}
				if ($cat <> "tcat")
				{
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs = mysql_query("SELECT * FROM ".TBACAT." WHERE `acat_code` = '".$cat."'");
					if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
					{
						if ($row = mysql_fetch_array($rs))
						{
							extract($row);
							echo "<br>Filtre sur la catégorie : ".$acat_desc;
						}
					}
					mysql_free_result($rs);
					mysql_close($conn);
				}
				echo "<br>";
				echo "Période du ".formatedate($date1)." au ".formatedate($date2)."<br>";
				echo "</span>";
				break;
				
			default : 
				break;
			}
		}
	}
	?>
	</div>
	<br>
	</form>
	<?PHP
	if (($typstat <> 0) AND ($stat_ok == TRUE))
	{
		if ($aff_stat == FALSE)
		{
	?>
			<a href="javascript:;" onclick="self.location.href=('age_stat.php');")><img src="../img/stat.png" title="Autre statistique" width="22" height="22" border="0" align="abddle"></a>
			<a href="javascript:;" onclick="self.location.href=('age_stat.php');")> Autre statistique </a>
			<br>
	<?PHP
		}
		else
		{
	?>
			<a href="javascript:;" onclick="self.location.href=('age_stat.php');")><img src="../img/stat.png" title="Nouvelle statistique" width="22" height="22" border="0" align="abddle"></a>
			<a href="javascript:;" onclick="self.location.href=('age_stat.php');")> Nouvelle statistique </a>
			<br>
	<?PHP
		}
	}

	if ($stat_ok == TRUE)
	{
		switch ($typstat)
		{
			/* Nombre de créations de clients sur une période donnée */
			case 1 :
				if ($aff_stat == FALSE) // On est en mode "paramètres"
				{
	?>
				<form name="clients" class="form_pointilles" method="post" action="age_stat.php">
				<input name="ts" type="hidden" id="ts" value="<?PHP echo $typstat ?>">
				<table width="100%" border="0">
				<tr>
                    <td width="50%" align="center">
						<br>
						Choisissez une agence (s&eacute;lections multiples possibles).
						<br>
						<span class="annot_petit_it">(Si 'Toutes', les agences ferm&eacute;es seront aussi analys&eacute;es)</span>
					</td>
                    <td align="center">
						<br>
						Choisissez une cat&eacute;gorie d'agence.
						<br>
						<br>
					</td>
				</tr>
				<tr>
                    <td width="50%" align="center">
						<select name="age[]" id="age" size="5" multiple="multiple">
						<option value="toutes" selected="selected">---------- Toutes ---------- </option>
		
						<?PHP
						// Création de la liste des agences actives
						
						$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);
						$rs = mysql_query("SELECT * FROM ".TBAGE." WHERE `age_ouvert` = '1' ORDER BY age_nom");
						while ($resultat = mysql_fetch_array($rs)) 
						{
							extract($resultat);
							$txt_age = $age_nom." (".$age_code.")";
							echo '<option value="'.$age_code.'">'.$txt_age.'</option>';
						}
						mysql_free_result($rs);
						mysql_close($conn);
						?>
						</select>
					</td>
                    <td align="center">
						<select name="cat" size="5" id="cat">
						<option value="tcat" selected="selected">---------- Toutes ---------- </option>
						<?PHP
						
						// Création de la liste des catégories
						
						$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);
						$rs = mysql_query("SELECT * FROM ".TBACAT." ORDER BY acat_desc");
						
						while ($resultat = mysql_fetch_array($rs)) 
						{
							extract($resultat);
							echo '<option value="'.$acat_code.'">'.$acat_desc.'</option>';
						}
						mysql_free_result($rs);
						mysql_close($conn);
						?>
						</select>
					</td>
                  </tr>
                </table>
				<br>
				<br>
				<br>
				<table width="400"  border="0" align="center">
				<tr>
					<td width="30" class="rubfrm" id="f_date1"><div align="right">Du...</div></td>
					<td>
						<input name="date1f" type="text" id="date1f" size="10" maxlength="10" readonly="1">
						<img src="../img/calendar.png" id="f_img_date1f" title="Choisir votre date" width="16" height="16">
						<img src="../img/oblig.gif" width="10" height="11">
					</td>
					<td width="30" class="rubfrm" id="f_date2f"><div align="right">Au...</div></td>
					<td>
						<input name="date2f" type="text" id="date2f" size="10" maxlength="10" readonly="1">
						<img src="../img/calendar.png" id="f_img_date2f" title="Choisir votre date" width="16" height="16">
						<img src="../img/oblig.gif" width="10" height="11">
					</td>
				</tr>
				</table>
				<p align="center">
				    <input type="submit" name="Submit" value="Calculer">
					<input name="init" type="reset" id="init" value="R&eacute;initialiser">
				</p>
			</form>
			
			  <script type="text/javascript">
				Calendar.setup({
					inputField     :    "date1f",     // id of the input field
					ifFormat       :    "%d-%m-%Y",      // format of the input field
					button         :    "f_img_date1f",  // trigger for the calendar (button ID)
					align          :    "Ll",           // alignment (defaults to "Bl")
					singleClick    :    true
				});
				Calendar.setup({
					inputField     :    "date2f",     // id of the input field
					ifFormat       :    "%d-%m-%Y",      // format of the input field
					button         :    "f_img_date2f",  // trigger for the calendar (button ID)
					align          :    "Ll",           // alignment (defaults to "Bl")
					singleClick    :    true
				});
			  </script>
			  
	<?PHP
			}
			else // On est en mode "affichage de stats"
			{
				// Définition et entête de la table
				echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
	
				echo "<th>";
				echo "Agence";
				echo "</th>\n";
				echo "<th>";
				echo "Total créés";
				echo "</th>\n";
				echo "<th>";
				echo "Encore prospects";
				echo "</th>\n";
				echo "<th>";
				echo "Attente intervention";
				echo "</th>\n";
				echo "<th>";
				echo "Intervention";
				echo "</th>\n";

				$i = 0;

				/* Données pour toutes les agences */

				// Création de l'entête de ligne (TR)
				creetrlst($i);

				echo "<td>";
				echo "<span class=\"annot_std_it\">";
				echo "Toutes les agences";
				echo "</span>";
				echo "</td>\n";
				echo "<td align=\"center\">";
				if ($nb_cli_crees_total <> 0)
				{
					echo "<span class=\"annot_std_it\">";
					echo $nb_cli_crees_total;
					echo "</span>";
				}
				else
					echo "-";
				echo "</td>\n";
				echo "<td align=\"center\">";
				if ($nb_cli_prospects_crees_total <> 0)
				{
					echo "<span class=\"annot_std_it\">";
					echo $nb_cli_prospects_crees_total;
					echo "</span>";
					if ($nb_cli_crees_total <> 0)
					{
						echo"<span class=\"annot_petit_it\">";
						echo " (".round((($nb_cli_prospects_crees_total/$nb_cli_crees_total)*100),2)."%)";
						echo "</span>";
					}
				}
				else
					echo "-";
				echo "</td>\n";
				echo "<td align=\"center\">";
				if ($nb_cli_ddeint_crees_total <> 0)
				{
					echo "<span class=\"annot_std_it\">";
					echo $nb_cli_ddeint_crees_total;
					echo "</span>";
					if ($nb_cli_crees_total <> 0)
					{
						echo"<span class=\"annot_petit_it\">";
						echo " (".round((($nb_cli_ddeint_crees_total/$nb_cli_crees_total)*100),2)."%)";
						echo "</span>";
					}
				}
				else
					echo "-";
				echo "</td>\n";
				echo "<td align=\"center\">";
				if ($nb_cli_inter_crees_total <> 0)
				{
					echo "<span class=\"annot_std_it\">";
					echo $nb_cli_inter_crees_total;
					echo "</span>";
					if ($nb_cli_crees_total <> 0)
					{
						echo"<span class=\"annot_petit_it\">";
						echo " (".round((($nb_cli_inter_crees_total/$nb_cli_crees_total)*100),2)."%)";
						echo "</span>";
					}
				}
				else
					echo "-";
				echo "</td>\n";

				echo "</tr>\n";
				
				$i++;

				/* Saut de ligne pour lisibilité */
				
				// Création de l'entête de ligne (TR)
				creetrlst($i);

				echo "<td>";
				echo "&nbsp;";
				echo "</td>\n";
				echo "<td>";
				echo "&nbsp;";
				echo "</td>\n";
				echo "<td>";
				echo "&nbsp;";
				echo "</td>\n";
				echo "<td>";
				echo "&nbsp;";
				echo "</td>\n";
				echo "<td>";
				echo "&nbsp;";
				echo "</td>\n";

				echo "</tr>\n";
				
				$i++;

				if ($nb_age_select > 0)
				{
					for ($j=0; $j<$nb_age_select; $j++)
					{
						// Création de l'entête de ligne (TR)
						creetrlst($i);
		
						echo "<td>";
						$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);
						$rs = mysql_query("SELECT * FROM ".TBAGE." WHERE `age_code` = '".$code_agence[$j]."'");
						if ($row = mysql_fetch_array($rs))
						{
							extract($row);
							$txt_age = $age_nom;
						}
						mysql_free_result($rs);
						mysql_close($conn);
						echo "<span class=\"annot_std_gras\">";
						echo $txt_age." (".$code_agence[$j].")";
						echo "</span>";
						if ($age_ouvert == 0)
						{
							echo "<span class=\"annot_petit_it\">";
							echo " Fermée";
							echo "</span>";
						}
						echo "<br>";
						if ($nb_cli_crees_age[$j] <> 0)
						{
							echo "<span class=\"annot_petit_it\">";
							echo "<div align=\"right\">";
							echo "Par rapport agence : ";
							echo "<br>Par rapport total : ";
							echo "</div>";
							echo "</span>";
						}
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($nb_cli_crees_age[$j] <> 0)
						{
							echo "<span class=\"annot_std_gras\">";
							echo $nb_cli_crees_age[$j];
							echo "</span>";
							if ($nb_cli_crees_total <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo "<br>-<br>(".round((($nb_cli_crees_age[$j]/$nb_cli_crees_total)*100),2)."%)";
								echo "</span>";
							}							
						}
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($nb_cli_prospects_crees_age[$j] <> 0)
						{
							echo "<span class=\"annot_std_gras\">";
							echo $nb_cli_prospects_crees_age[$j];
							echo "</span>";
							echo "<br>";
							if ($nb_cli_crees_age[$j] <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo "(".round((($nb_cli_prospects_crees_age[$j]/$nb_cli_crees_age[$j])*100),2)."%)";
								echo "</span>";
							}
							else
								echo "-";
							echo "<br>";
							if ($nb_cli_prospects_crees_total <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo "(".round((($nb_cli_prospects_crees_age[$j]/$nb_cli_prospects_crees_total)*100),2)."%)";
								echo "</span>";
							}							
							else
								echo "-";
						}
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($nb_cli_ddeint_crees_age[$j] <> 0)
						{
							echo "<span class=\"annot_std_gras\">";
							echo $nb_cli_ddeint_crees_age[$j];
							echo "</span>";
							echo "<br>";
							if ($nb_cli_crees_age[$j] <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo " (".round((($nb_cli_ddeint_crees_age[$j]/$nb_cli_crees_age[$j])*100),2)."%)";
								echo "</span>";
							}
							else
								echo "-";
							echo "<br>";
							if ($nb_cli_ddeint_crees_total <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo "(".round((($nb_cli_ddeint_crees_age[$j]/$nb_cli_ddeint_crees_total)*100),2)."%)";
								echo "</span>";
							}							
							else
								echo "-";
						}
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($nb_cli_inter_crees_age[$j] <> 0)
						{
							echo "<span class=\"annot_std_gras\">";
							echo $nb_cli_inter_crees_age[$j];
							echo "</span>";
							echo "<br>";
							if ($nb_cli_crees_age[$j] <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo " (".round((($nb_cli_inter_crees_age[$j]/$nb_cli_crees_age[$j])*100),2)."%)";
								echo "</span>";
							}							
							echo "<br>";
							if ($nb_cli_inter_crees_total <> 0)
							{
								echo"<span class=\"annot_petit_it\">";
								echo "(".round((($nb_cli_inter_crees_age[$j]/$nb_cli_inter_crees_total)*100),2)."%)";
								echo "</span>";
							}							
							else
								echo "-";
						}
						else
							echo "-";
						echo "</td>\n";
		
						echo "</tr>\n";
						
						$i++;
					}
				}
				else
				{
					// Création de l'entête de ligne (TR)
					creetrlst($i);
	
					echo "<td>";
					echo"<span class=\"annot_petit_it\">";
					echo "Aucun résultat à cette demande.";
					echo "</span>";
					echo "</td>\n";
					echo "<td>";
					echo "&nbsp;";
					echo "</td>\n";
					echo "<td>";
					echo "&nbsp;";
					echo "</td>\n";
					echo "<td>";
					echo "&nbsp;";
					echo "</td>\n";
					echo "<td>";
					echo "&nbsp;";
					echo "</td>\n";
	
					echo "</tr>\n";
				}
				
				echo "</table>\n";
				// *** Fin affichage de la table
		}
		
			break;

		/* Répartition des catégories d'agences actives */
		case 2 :
			// Définition et entête de la table
			echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

			echo "<th>";
			echo "Catégories";
			echo "</th>\n";
			echo "<th>";
			echo "Quantité";
			echo "</th>\n";
			echo "<th>";
			echo "Pourcentage";
			echo "</th>\n";

			if ($nb_acat_select > 0)
			{
				for ($j=0; $j<$nb_acat_select; $j++)
				{
					// Création de l'entête de ligne (TR)
					creetrlst($j);
	
					echo "<td>";
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rech = "SELECT * FROM ".TBACAT." WHERE `acat_code` = '".$code_acat[$j]."'";
					$rs = mysql_query($rech);
					if ($row = mysql_fetch_array($rs))
					{
						extract($row);
					}
					mysql_free_result($rs);
					mysql_close($conn);
					echo "<span class=\"annot_std_gras\">";
					echo $acat_desc;
					echo "</span>";
					echo "<br>";
					echo "</td>\n";
					echo "<td align=\"center\">";
					if ($nb_acat_age[$j] <> 0)
					{
						echo "<span class=\"annot_std_gras\">";
						echo $nb_acat_age[$j];
						echo "</span>";
					}
					else
						echo "-";
					echo "</td>\n";
					echo "<td align=\"center\">";
					if ($nb_acat_age[$j] <> 0)
					{
						if ($nb_total_agences <> 0)
						{
							echo"<span class=\"annot_petit_it\">";
							echo round((($nb_acat_age[$j]/$nb_total_agences)*100),2)."%";
							echo "</span>";
						}
						else
							echo "-";
					}
					else
						echo "-";
					echo "</td>\n";
	
					echo "</tr>\n";
				}

				creetrlst($nb_acat_select);
				echo "<td align=\"right\">";
				echo"<span class=\"annot_std_it\">";
				echo "TOTAL : ";
				echo "</span>";
				echo "</td>\n";
				echo "<td align=\"center\">";
				echo"<span class=\"annot_std_it\">";
				echo $nb_age_select;
				echo "</span>";
				echo "</td>\n";
				echo "<td align=\"center\">";
				echo"<span class=\"annot_std_it\">";
				echo "100%";
				echo "</span>";
				echo "</td>\n";
				echo "</tr>\n";
	
			}

			echo "</table>\n";
			// *** Fin affichage de la table

			break;

		default : 
			break;
		}
	}
	else
	{
		if ($pbm_stat == TRUE)
		{
		?>
			<p align="center" class="annot_std_it"><?PHP echo $msg; ?></p>
			<script type="text/javascript">
			window.setTimeout("location=('age_stat.php');",5000)
			</script>
		<?PHP
		}
	}
}
else
{
?>
	<p align="center" class="annot_std_it">Vous n'avez cr&eacute;&eacute; aucune agence...</p>
	<p align="center" class="annot_std_it">Retour à l'accueil dans 2 secondes...</p>
	<script type="text/javascript">
	window.setTimeout("location=('index.php');",2000)
	</script>
<?PHP
}
?>


<?php 
	include_once ("pied.php")
?>
