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

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
****************************************************************************************/

include ("../inc/config.inc.php");
?>
<?php require ("../inc/func.inc.php") ?>
<?php require ("../inc/func_time.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php require ("../inc/func_log.inc.php") ?>
<?php require ("../inc/fic.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
?>
<?php
	$td = strip_tags(trim($_GET['td'])); // Récupération type demande
	$co = strip_tags(trim($_GET['co'])); // Récupération code Contrat
	$cc = strip_tags(trim($_GET['cc'])); // Récupération code Client
	$ca = strip_tags(trim($_GET['ca'])); // Récupération code Agence

	$nbcontrats = 0;
	$pbm_cocl = FALSE;

	// Niveau <=3 ne peut que voir les contrats
	if (@$HTTP_SESSION_VARS["niv_log"] <= 3)
		$td = "view";


	if ($ca<> "")
	{
		// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
		if (((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3)) AND ($ca <> @$HTTP_SESSION_VARS["code_age"]))
			$pbm_cocl = TRUE;
	}

	if (($co <> "") AND ($pbm_cocl == FALSE))
	{
		// Récupération des informations contrat client
		$row = lit_enr_cocl($co);
		if (is_array($row))
		{
			extract ($row);
			
			// Si contrat terminé, force le mode "view"
			if (formatedate($cocl_datefin) <> "")
				$td="view";
			
			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
			if ($td <> "add")
			{
				if (((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3)) AND ($cocl_codeage <> @$HTTP_SESSION_VARS["code_age"]))
					$pbm_cocl = TRUE;
			}
		}
		else
			$pbm_cocl = TRUE;

		if ($pbm_cocl == FALSE)
		{
			// Vérification qu'il existe bien des contrats et nombre de ceux-ci
			$nbcontrats = nb_cocl_in_cli($cocl_codecli);
	
			// Vérification du nombre de ce contrat utilisé dans les interventions
			$signe = cocl_signed($co);
	
			$cocl_datecreaf = formatedate($cocl_datecrea);
			$cocl_datemodf = formatedate($cocl_datemod);
			$cocl_dateenvf = formatedate($cocl_dateenv);
			$cocl_daterecf = formatedate($cocl_daterec);
			$cocl_datesigf = formatedate($cocl_datesig);
			$cocl_datedf = formatedate($cocl_dated);
			$cocl_dateff = formatedate($cocl_datef);
			$cocl_daterelf = formatedate($cocl_daterel);
			$cocl_datefinf = formatedate($cocl_datefin);
	
			if ($td == "edit")
			{
				$cc=$cocl_codecli;
				$ca=$cocl_codeage;
				
				$titre = "MISE A JOUR CONTRAT CLIENT";
			}

			if ($td == "view")
			{
				$cc=$cocl_codecli;
				$ca=$cocl_codeage;
				
				$titre = "VISUALISATION CONTRAT CLIENT";
			}
	
			if ($td == "del")
				$titre = "SUPPRESSION CONTRAT CLIENT";
	
			if ($td == "add")
			{
				$cocl_datecreaf = $datef;
				$cocl_dateenvf = $datef;
				$cocl_daterecf = $datef;
				$cocl_code = $co;
				$cocl_codecli = $cc;
				$cocl_codeage = $ca;
				$cocl_nbs = 3600; // 1 heure...en secondes
				$cocl_preavis = preavis_max();
				$cocl_datedf = $datef;
				$cocl_renouv = 0;
				
				$titre = "CREATION CONTRAT CLIENT";
			}
		}
	}
	else
	{
		if ($td == "add")
		{
			$cocl_datecreaf = $datef;
			$cocl_dateenvf = $datef;
			$cocl_code = crecodesession("RL");
			$cocl_daterecf = $datef;
			$co = $cocl_code;
			$cocl_nbs = 3600; // 1 heure...en secondes
			$cocl_preavis = preavis_max();
			$cocl_datedf = $datef;
			$cocl_renouv = 0;

			$titre = "CREATION CONTRAT CLIENT";
		}

	}

	$nbh = sec_heure($cocl_nbs);
	$soldeh = sec_heure($cocl_soldes);
		
	if (($cc <>"") AND ($pbm_cocl == FALSE))
	{
		$row = lit_enr_cli($cc);
		if (is_array($row))
		{
			extract ($row);

			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
	
			if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
			{
				$row = lit_enr_age($HTTP_SESSION_VARS["code_age"]);
				extract ($row);
				$zc_couverte = $age_zone;
			}
			else
				$zc_couverte = zc_age_actives();

			// Contrôle qu'une agence n'essaye pas de "hacker" un client
			if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
			{
				if (dep_in_zc($cli_dep,$zc_couverte) == TRUE) 
				{
					if (($cli_prop <> @$HTTP_SESSION_VARS["code_age"]) AND ($cli_prop <> NULL))
					{
						$pbm_cocl = TRUE;
					}
				}
				else
				{
					$pbm_cocl = TRUE;
				}
			}
			$cocl_codecli = $cc;
		}
	}
?>

<!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 : Fiche Contrat Client</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../inc/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 validation des formulaires -->
<script language="JavaScript" src="../inc/validator.js"></script>

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

<!-- Scripts Javascript pour vérifications -->
<script language="JavaScript" src="../inc/verif.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>

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

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

</head>

<?PHP if ($td == "del"){ ?>
<body onload=warndel();>
<?PHP } else { ?>
<body>
<?PHP } ?>

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

<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<!-- Affiché uniquement s'il y a un code client -->
<?PHP
if ($pbm_cocl == FALSE)
{
if ($cc <> "")
{
?>
<table width="100%"  border="0">
<tr>
	<td width="33%"><p><strong><em><a href="cocl_lst.php?ta=m&ord=DES&tri=Code">Retour &agrave; la liste<br>
	</a></em></strong></p>
	<?PHP if (($td == "edit") OR ($td=="view") AND ($nbcontrats <> 0))
	{
		$url_coclh = "coclhd_lst.php?or=con&cc=".$cocl_codecli."&search=".$cocl_code;
	?>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $url_coclh; ?>',800,560,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")><img src="../img/view.gif" width="22" height="22" border="0" align="absmiddle"></a> <a href="javascript:;" onclick="fensaisie('<?PHP echo $url_coclh; ?>',800,560,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")> Historique contrat</a><?PHP
	}
	?>
    </td>
	<td width="33%" valign="top"><div align="center"><span class="annot_gras_it">(<img src="../img/oblig.gif" width="10" height="11"> : Saisie obligatoire)</span> </div></td>
	<td width="33%"><div align="right">
		<div align="right">
		<?PHP
			if (($td == "edit") OR ($td == "view"))
			{
			$urlimp = "cocl_pdf.php?code=".$co;
			?>
			<a href="javascript:;" onclick="lanceimp('<?PHP echo $urlimp; ?>');")><img src="../img/print.gif" title="Imprimer le contrat" width="22" height="22" border="0" align="absmiddle"></a> 
			<a href="javascript:;" onclick="lanceimp('<?PHP echo $urlimp; ?>');")> Imprimer le contrat</a></div>
		<?PHP } ?>
		</div>
	</td>
</tr>
</table>

<form name="cocl_maj" method="post" action="cocl_enr.php" onsubmit="return v.exec();">
<input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
<input name="typdde" type="hidden" id="typdde" value="<?PHP echo $td ?>">
<input name="codecli" type="hidden" id="codecli" value="<?PHP echo $cocl_codecli ?>">
<?PHP if ($td == "add") { ?>
	<input name="code" type="hidden" id="code" value="<?PHP echo $cocl_code ?>">
	<input name="renouv" type="hidden" id="renouv" value="<?PHP echo $cocl_renouv ?>">
	<input name="num" type="hidden" id="num" value="">
	<input name="datefinf" type="hidden" id="datefinf" value="">
<?PHP } ?>

<!-- Détails internes de la fiche intervention -->
<table width="100%"  border="0">
<tr>
	<td width="33%">
	<?PHP if (($td == "edit") OR ($td == "view"))
	{
		$url_detcocl = "cocld_maj.php?co=".$co;
	?>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $url_detcocl; ?>',800,300,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');")><img src="../img/detail.gif" width="16" height="16" border="0" align="absmiddle"></a>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $url_detcocl; ?>',800,300,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');")> Détails</a>
	<?PHP
	}
	else
	{
	?>
		&nbsp;
	<?PHP
	}
	?>
    </td>
	<td width="33%" valign="top">&nbsp;</td>
	<td width="33%">
	<div align="right">
	<?PHP 
	 $echeance_ok = TRUE;
	 if ($cocl_dateff <> "")
	 {
		if (echeance_depassee_date($datea,$cocl_datef) == TRUE)
			$echeance_ok = FALSE;
	 }
	if ((($td == "edit") OR ($td == "view")) AND ($cocl_datefinf == "") AND ($cocl_datesigf <> "") AND ($echeance_ok == TRUE))
	{
		$url_int = "int_maj.php?td=add&ci=&cc=".$cc."&ct=&ca=".$ca."&co=".$co;
	?>
	  <a href="javascript:;" onclick="javascript:; window.location=('<?PHP echo $url_int; ?>');"><img src="../img/int.gif" width="16" height="16" border="0" align="absmiddle"></a>
	  <a href="javascript:;" onclick="javascript:; window.location=('<?PHP echo $url_int; ?>');")> Saisir intervention</a>
	<?PHP
	}
	else
	{
	?>
		&nbsp;
	<?PHP
	}
	?>
	</div>
	</td>
</tr>
</table>

<!-- Date de création -->
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_datecreaf"><div align="right">Date Cr&eacute;ation </div></td>
	<td>
		<input name="datecreaf" type="text" id="datecreaf" value="<?PHP echo $cocl_datecreaf ?>" size="10" maxlength="10" readonly="1">
		<?PHP if ($td == "add") { ?>
		<img src="../img/calendar.png" id="f_img_datecreaf" title="Choisir votre date" width="16" height="16">
		<img src="../img/oblig.gif" width="10" height="11">
		<?PHP } ?>
	</td>
</tr>
</table>

<!-- Code du contrat client (affiché uniquement si édition ou suppression) -->
<?PHP if ($td <> "add") {?>
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_codecocl"><div align="right">Code </div></td>
	<td>
		<input name="code" type="text" id="code" value="<?PHP echo $cocl_code ?>" size="20" maxlength="20" readonly="1">
		<?PHP
		 $echeance_ok = TRUE;
		 if ($cocl_dateff <> "")
		 {
			if (echeance_depassee_date($datea,$cocl_datef) == TRUE)
				$echeance_ok = FALSE;
		 }
		if (($cocl_datesigf == "") OR ($cocl_datefinf <> "") OR ($echeance_ok == FALSE))
		{
			if ($cocl_datesigf == "")
			{
				echo "<span class=\"titre_std_gras_jaune_frouge\" style=\"padding: 2px;\">";
				echo "Non Signé";
				echo "</span>";
				if (($cocl_datefinf <> "") OR ($echeance_ok == FALSE))
				{
					echo "<strong><em>";
					echo " / ";
					echo "</strong></em>";
				}
			}
			if (($cocl_datefinf <> "") OR ($echeance_ok == FALSE))
			{
				echo "<span class=\"titre_std_gras_jaune_frouge\" style=\"padding: 2px;\">";
				echo "Terminé";
				echo "</span>";
			}
		}
		?>
	</td>
</tr>
</table>
<?PHP }  ?>

<!-- Affichage du client -->
<table width="800"  border="0">
<tr>
	<td width="400" class="tfvNormal" colspan="2"  id="f_codecli">
		<div align="center">Client</div>
	</td>
	</tr>
</table>

<!-- Détails client -->
<table width="800"  border="0">
<tr>
	<td width="370" valign="top" bgcolor="#CCFFCC">
	<?PHP
	if (($cli_ste <>"") OR ($cli_nom <> ""))
		echo "<img src=\"../img/".$ico_cpta[$cli_cpta]."\" title =\"".$mess_cpta[$cli_cpta]."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
	echo "<b>";
	if ($cli_ste <>"") 
	{
		echo $cli_ste."<br>";
		echo "<span class=\"annot_gras_it\">";
	}
	echo $cli_civilite." ".$cli_prenom." ".$cli_nom."<br>";
	if ($cli_ste <>"") 
	{
		echo "</span>";
	}
	echo "</b>";
	echo $cli_adr1."<br>";
	if ($cli_adr2 <>"") 
		echo $cli_adr2."<br>";
	echo Retournecp($cli_ville)." ".Retourneville($cli_ville)."<br>";
	echo "Tel : ".$cli_telf."<br>";
	?>
	</td>
	<td width="30" valign="top">
	<?PHP 
	if (@$HTTP_SESSION_VARS["niv_log"] >= 3)
	{
	  $url_affnotesc = "aff_notesc.php?cc=".$cc;
	?>
	  <a href="javascript:;" onclick="fensaisie('<?PHP echo $url_affnotesc; ?>',800,400,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');")><img src="../img/detail.gif" width="16" height="16" border="0" align="absmiddle"></a>
	  <a href="javascript:;" onclick="fensaisie('<?PHP echo $url_affnotesc; ?>',800,400,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');")> Notes</a><?PHP
	}
	else
		echo "&nbsp;";
	?>
	</td>
	</tr>
</table>

<!-- Code de l'agence (choisi uniquement si création, affiché uniquement si édition ou suppression) -->
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_agence"><div align="right">Agence</div></td>
	<td>
		<?PHP
		if ($td =="add")  // Ajout = zone modifiable (on peut choisir une agence)
		{ 
			if ((@$HTTP_SESSION_VARS["niv_log"] <> 4) AND (@$HTTP_SESSION_VARS["niv_log"] <> 3))
			{
				if ($cli_prop == NULL) // On affiche la liste uniquement si le client est non affecté
				{
				
					// Création de la liste des agences ouvertes et couvrant le client
					
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rech = "SELECT * FROM ".TBAGE." WHERE `age_ouvert` = '1' ORDER BY age_nom";
					$rs = mysql_query($rech);
		
					if (mysql_num_rows($rs) <> 0)
					{
					?>
					
						<select name="codeage" id="codeage">
			
						<?PHP
							if ($cocl_codeage <> "")
								echo "<option value=\"\">-- Choisissez une agence --</option>";
							else
								echo "<option value=\"\" selected>-- Choisissez une agence --</option>";
			
							while ($resultat = mysql_fetch_array($rs)) 
							{
								extract($resultat);
				
								if (dep_in_zc($cli_dep,$age_zone) == TRUE)
								{
									$age_nomcode = $age_nom." (".$age_code.")";
									if ($cocl_codeage == $age_code)
										echo '<option value="'.$age_code.'" selected>'.$age_nomcode.'</option>';
									else
										echo '<option value="'.$age_code.'">'.$age_nomcode.'</option>';
								}
							}
						?>
						</select>
					<?PHP
					}
					else
					{
						echo "<span class=\"annot_gras_it\">";
						echo "Pas d'agence ouverte";
						echo "</span>";
					}
					mysql_free_result($rs);
					mysql_close($conn);
					?>
					<img src="../img/oblig.gif" width="10" height="11">
				<?PHP
				}
				else
				{
					$row = lit_enr_age($cli_prop);
					extract($row);
					$age_nomcode = $age_nom." (".$age_code.")";
				?>
					<input name="codeage" type="hidden" id="codeage" value="<?PHP echo $age_code ?>">
					<input name="nomage" type="text" id="nomage" value="<?PHP echo $age_nomcode ?>" size="50" maxlength="50" readonly="1">
		<?PHP
				}
			}
			else
			{
				$row = lit_enr_age($HTTP_SESSION_VARS["code_age"]);
				extract($row);
				$age_nomcode = $age_nom." (".$age_code.")";
		?>
				<input name="codeage" type="hidden" id="codeage" value="<?PHP echo $age_code ?>">
				<input name="nomage" type="text" id="nomage" value="<?PHP echo $age_nomcode ?>" size="50" maxlength="50" readonly="1">
		<?PHP
			}
		} 
		else  // Edition ou suppression = zone non modifiable (affichage seul)
		{
			$row = lit_enr_age($cocl_codeage);
			extract($row);
			$age_nomcode = $age_nom." (".$age_code.")";
		?>
		<input name="codeage" type="hidden" id="codeage" value="<?PHP echo $age_code ?>">
		<input name="nomage" type="text" id="nomage" value="<?PHP echo $age_nomcode ?>" size="50" maxlength="50" readonly="1">
		<?PHP 
		} 
		?>
	</td>
</tr>
</table>

<!-- Type de contrat -->
<table width="800"  border="0">
  <tr>
    <td width="160" class="tfvNormal" id="f_co"><div align="right">Contrat Type </div></td>
    <td><?PHP
		if ($td =="add")  // Ajout = zone modifiable (on peut choisir un type de contrat)
		{ 
		?>
        <?PHP
			
			// Création de la liste des types de contrats actifs
			
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM ".TBCO."  WHERE co_actif = '1' ORDER BY co_nom";
			$rs = mysql_query($rech) or die(mysql_error());

			if (mysql_num_rows($rs) <> 0)
			{
			?>
        <select name="co" id="co">
          <?PHP
				if ($cocl_codeco <> "")
					echo "<option value=\"\">-- Choisissez un type de contrat --</option>";
				else
					echo "<option value=\"\" selected>-- Choisissez un type de contrat --</option>";

				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					if ($cocl_codeco == $co_code)
						echo '<option value="'.$co_code.'" selected>'.$co_nom.'</option>';
					else
						echo '<option value="'.$co_code.'">'.$co_nom.'</option>';
				}
			?>
        </select>
        <?PHP
			}
			else
			{
		?>
				<select name="co" id="co">
				<option value="" selected>-- Pas de contrat type actif --</option>
				</select>
		<?PHP
			}
			mysql_free_result($rs);
			mysql_close($conn);
			?>
        <img src="../img/oblig.gif" width="10" height="11">
        <?PHP 
		} 
		else  // Edition ou suppression = zone non modifiable (affichage seul)
		{
			$row = lit_enr_co($cocl_codeco);
			extract($row);
		?>
        <input name="co" type="hidden" id="co" value="<?PHP echo $cocl_codeco ?>" size="20">
        <input name="nomco" type="text" id="nomco" value="<?PHP echo $co_nom ?>" size="50" maxlength="50" readonly="1">
        <?PHP 
		} 
		?>
    </td>
  </tr>
</table>

<!-- Périodicité du contrat -->
<table width="800"  border="0">
  <tr>
    <td width="160" class="tfvNormal" id="f_cope"><div align="right">Fr&eacute;quence</div></td>
    <td>
		<?PHP
		if (($signe == FALSE) AND ($td <> "view")) // Affichage du choix de périodicité uniquement si pas encore signé
		{ 
			// Création de la liste des types des périodicités
			
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM ".TBPER." ORDER BY cope_nbj";
			$rs = mysql_query($rech);

			if (mysql_num_rows($rs) <> 0)
			{
		?>
        		<select name="cope" id="cope" onChange="verif_delai();">
		<?PHP
				if ($cocl_codecope <> "")
					echo "<option value=\"\">-- Choisissez une fréquence --</option>";
				else
					echo "<option value=\"\" selected>-- Choisissez une fréquence --</option>";

				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					if ($cope_nbj <> 0)
						$txt_nbj = " (tous les ".$cope_nbj." jours)";
					else
						$txt_nbj = " (à la demande)";

					if ($cocl_codecope == $cope_code)
						echo '<option value="'.$cope_code.'" selected>'.$cope_desc.$txt_nbj.'</option>';
					else
						echo '<option value="'.$cope_code.'">'.$cope_desc.$txt_nbj.'</option>';
				}
		?>
        		</select>
        <?PHP
			}
			else
			{
				echo "<span class=\"annot_gras_it\">";
				echo "Pas de périodicité";
				echo "</span>";
			}
			mysql_free_result($rs);
			mysql_close($conn);
			?>
        	<img src="../img/oblig.gif" width="10" height="11">
		<?PHP
		} 
		else 
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM ".TBPER." WHERE `cope_code` = '".$cocl_codecope."'";
			$rs = mysql_query($rech);
			if ($row = mysql_fetch_array($rs))
				extract ($row);
			mysql_free_result($rs);
			mysql_close($conn);
		?>
        <input name="cope" type="hidden" id="cope" value="<?PHP echo $cocl_codecope ?>">
        <input name="tcope" type="text" id="tcope" value="<?PHP echo $cope_desc ?>" size="20" maxlength="20" readonly="1">
		<?PHP 
		} 
		?>
	 </td>
  </tr>
</table>

<!-- Délai d'intervention -->
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_delai">
		<div align="right">D&eacute;lai Interv.
		</div>
		<div align="right"></div>
	</td>
    <td>
		<?PHP
		if (($signe == FALSE) AND ($td <> "view")) // Affichage du choix de périodicité uniquement si pas encore signé
		{
		?>
			<input name="delai" type="text" id="delai" value="<?PHP echo $cocl_delai ?>" size="2" maxlength="2" align="right">
			<span class="annot_petit_it">heures ouvr&eacute;es(obligatoire seulement si &agrave; la demande) </span>
		<?PHP
		}
		else // Si c'est signé, on se contente d'afficher le délai d'intervention
		{
		?>
	        <input name="delai" type="text" id="delai" value="<?PHP echo $cocl_delai ?>" size="2" maxlength="2" align="right" readonly="1">
			<span class="annot_petit_it">heures ouvr&eacute;es </span>
		<?PHP
		}
		?> 
	</td>
</tr>
</table>

<!-- Nombre d'heures par contrat -->
<table width="800"  border="0">
  <tr>
    <td width="160" class="tfvNormal" id="f_nbheures"><div align="right">Nb heures</div></td>
    <td>
		<?PHP
		if (($signe == FALSE) AND ($td <> "view")) // Affichage du choix de périodicité uniquement si pas encore signé
		{
		?>
			<input name="nbh" type="text" id="nbh" value="<?PHP echo $nbh ?>" size="8" maxlength="8">
			<span class="annot_petit_it"> (format hh:mm:ss. 0 = sans limite de temps)</span>
			<span class="annot_std_gras">,</span>
			<?PHP
			if ($cocl_typh == 0)
			{
			?>
				<select name="typh" id="typh">
				<option value="0" selected>Par intervention</option>
				<option value="1">Quota d'heures</option>
				</select>
			<?PHP
			}
			else
			{
			?>
				<select name="typh" id="typh">
				<option value="0">Par intervention</option>
				<option value="1" selected>Quota d'heures</option>
				</select>
			<?PHP
			}
		}
		else // Si c'est signé, on se contente d'afficher le détail horaire
		{
		?>
	        <input name="nbh" type="text" id="nbh" value="<?PHP echo $nbh ?>" size="8" maxlength="8" readonly="1">
			<span class="annot_std_gras">,</span>
	        <input name="typh" type="text" id="typh" value="<?PHP echo $typ_horaire[$cocl_typh]; ?>" size="20" maxlength="20" readonly="1">
		<?PHP
			if ((($td == "edit") OR ($td == "del")) AND ($cocl_typh == 1))
			{
		?> 
			<br>
			<span class="annot_std_it">Solde :</span>		
			<input name="soldeh" type="text" id="soldeh" value="<?PHP echo $soldeh ?>" size="8" maxlength="8">
		<?PHP
			}
		}
		?>
  </tr>
</table>

<!-- Dates de "vie" du contrat -->
<table width="800"  border="0">
  <tr>
    <td width="160" class="tfvNormal" id="f_validite"><div align="right">Validit&eacute;</div></td>
    <td>
		<span class="annot_petit_gras">Du</span>      
		<input name="datedf" type="text" id="datedf" value="<?PHP echo $cocl_datedf ?>" size="10" maxlength="10" readonly="1">
		<?PHP if (($signe == FALSE) AND ($td <> "view")) { // On peut modifier la date de départ uniquement si pas signé ?>
        <img src="../img/calendar.png" id="f_img_datedf" title="Choisir votre date" width="16" height="16"> 
		<img src="../img/oblig.gif" width="10" height="11">
		<?PHP } ?>
		 <span class="annot_petit_gras">au</span>	    
		 <input name="dateff" type="text" id="dateff" value="<?PHP echo $cocl_dateff ?>" size="10" maxlength="10" readonly="1">
		<?PHP if (($signe == FALSE) AND ($td <> "view")) { // On peut modifier la date de fin uniquement si pas signé ?>
        <img src="../img/calendar.png" id="f_img_dateff" title="Choisir votre date" width="16" height="16"> 
		<a href="javascript:;" onclick="document.cocl_maj.dateff.value=''"><img src="../img/raz.gif" width="12" height="12" border="0" align="absmiddle"></a> 
		<img src="../img/oblig.gif" width="10" height="11">
        <span class="annot_petit_it">(ne pas mettre de date de fin pour un contrat 'illimit&eacute;') </span>
		<?PHP } ?>
	</td>
  </tr>
</table>

<!-- Nombre de jours de préavis -->
<table width="800"  border="0">
  <tr>
    <td width="160" class="tfvNormal" id="f_preavis"><div align="right">Pr&eacute;avis</div></td>
    <td>
		<?PHP if (($signe == FALSE) AND ($td <> "view")) { // On peut modifier le préavis uniquement si pas signé ?>
		<input name="preavis" type="text" id="preavis" value="<?PHP echo $cocl_preavis ?>" size="3" maxlength="3" align="right"> 
		<span class="annot_std_gras">jours avant date d'&eacute;ch&eacute;ance</span> 
		<?PHP 
			echo "<span class=\"annot_petit_it\">";
			echo "(La valeur doit se trouver entre ".preavis_max()." et ".preavis_min()." jours)";
			echo "</span>";
		} 
		else 
		{?>
		<input name="preavis" type="text" id="preavis" value="<?PHP echo $cocl_preavis ?>" size="3" maxlength="3" align="right" readonly="1"> 
		<?PHP } ?>
	</td>
  </tr>
</table>

<!-- Détail matériel concerné -->
<table width="800"  border="0">
  <tr>
    <td width="160" valign="top" class="tfvNormal" id="f_matos"><div align="right">Mat&eacute;riel Concern&eacute; </div></td>
    <td>
	<?PHP if (($signe == FALSE) AND ($td <> "view")) { // On peut modifier le matériel uniquement si pas signé ?>
		<textarea name="matos" cols="80" rows="20" id="matos"><?PHP echo $cocl_matos ?></textarea>
		<img src="../img/oblig.gif" width="10" height="11">
	<?PHP } else { ?>
		<textarea name="matos" cols="80" rows="20" id="matos" readonly><?PHP echo $cocl_matos ?></textarea>
	<?PHP } ?>
	</td>
  </tr>
</table>

<?PHP if ($td <> "add") { ?>  <!-- Début de zone exclue si création -->
<!-- Signataire du contrat -->
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_datesig"><div align="right">Sign&eacute; le </div></td>
	<td>
		<input name="datesig" type="text" id="datesig" value="<?PHP echo $cocl_datesigf ?>" size="10" maxlength="10" readonly="1">
	</td>
</tr>
<tr>
	<td width="160" class="tfvNormal" id="f_signe"><div align="right">Sign&eacute; par </div></td>
	<td>
		<input name="signe" type="text" id="signe" value="<?PHP echo $cocl_signe ?>" size="70" maxlength="70" readonly="1">
	</td>
</tr>
</table>

<br>

<!-- Affichage uniquement si le client a été relancé -->
<?PHP if ($cocl_daterelf <> "") { ?>  
<!-- Date de relance du contrat -->
<table width="800"  border="0">
  <tr>
    <td width="160" class="tfvNormal" id="f_daterelf"><div align="right">Relanc&eacute; le </div></td>
    <td>
		<input name="daterelf" type="text" id="daterelf" value="<?PHP echo $cocl_daterelf ?>" size="10" maxlength="10" readonly="1">
        <span class="annot_petit_it">
		<?PHP 
			echo "(".$cocl_nivrel." fois)";
		?>
		</span>
	</td>
  </tr>
</table>
<?PHP } ?>

<!-- Affiché uniquement si contrat signé -->
<?PHP if ($cocl_datesigf <> "") { ?>  

<!-- Contrat renouvelé ou pas ? -->
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_renouv"><div align="right">Renouvel&eacute;</div></td>
	<td>
		<?PHP
		if (($cocl_datefinf == "") AND ($td <> "view")) // Si ce n'est pas clôt, on peut saisir
		{
		?>
				<select name="renouv" id="renouv" onChange="verif_renouv(this.value);">
			<?PHP
			if ($cocl_renouv == 0)
			{
			?>
				<option value="0" selected>Non</option>
				<option value="1">Oui</option>
			<?PHP
			}
			else
			{
			?>
				<option value="0">Non</option>
				<option value="1" selected>Oui</option>
			<?PHP
			}
			?>
				</select>
				<span class="annot_std_gras">Nouveau contrat :</span>
	        	<input name="num" type="text" id="num" value="<?PHP echo $cocl_rnum; ?>" size="20" maxlength="20" onChange="verif_num(this.value);">
				<img src="../img/oblig.gif" width="10" height="11" align="top">
				<span class="annot_petit_it">(Si oui)</span>	
	  <?PHP
		}
		else // Si c'est clot, on se contente d'afficher le statut
		{
		?>
	        <input name="renouv" type="text" id="renouv" value="<?PHP echo $niv_renouv[$cocl_renouv]; ?>" size="3" maxlength="3" readonly="1">
			<span class="annot_std_gras">Nouveau contrat :</span>
	        <input name="num" type="text" id="num" value="<?PHP echo $cocl_rnum; ?>" size="20" maxlength="20" readonly="1">
		<?PHP
		}
		?>
	</td>
</tr>
</table>

<!-- Date de clôture du contrat -->
<table width="800"  border="0">
<tr>
	<td width="160" class="tfvNormal" id="f_datefinf"><div align="right">Contrat cl&ocirc;t le </div></td>
	<td>
		<input name="datefinf" type="text" id="datefinf" value="<?PHP echo $cocl_datefinf ?>" size="10" maxlength="10" readonly="1">
	<?PHP
		if ($td <> "view") // Affichage du choix de la date de clôture uniquement si pas signé
		{
	?>
		<img src="../img/calendar.png" id="f_img_datefinf" title="Choisir votre date" width="16" height="16">
		<a href="javascript:;" onclick="document.cocl_maj.datefinf.value=''"><img src="../img/raz.gif" width="12" height="12" border="0" align="absmiddle"></a> 
		<span class="annot_petit_it">(Si vous saisissez une date, le contrat sera d&eacute;finitivement cl&ocirc;t) </span>
	<?PHP 
		} 
	?>
	</td>
</tr>
<!-- Fin affiché uniquement si contrat signé -->
<?PHP 
} 
?>  
</table>


<?PHP } ?>  <!-- Fin de zone exclue si création -->

<!-- Montant du contrat -->
<table width="800"  border="0">
<tr>
    <tr>
      <td width="20%" class="tfvNormal" id="f_pu"><div align="right">PV HT </div></td>
      <td>
	<?PHP if (($signe == FALSE) AND ($td <> "view")) { // On peut modifier le prix uniquement si pas signé ?>
          <input name="pu" type="text" id="pu" align="right" value="<?PHP echo $cocl_pvht ?>" size="10" maxlength="10"> 
      <img src="../img/oblig.gif" width="10" height="11"><br>
      <span class="annot_std_it">(la d&eacute;cimale doit &ecirc;tre '</span><span class="annot_gras_it">.</span><span class="annot_std_it">' et pas la virgule !)</span> </p>
	<?PHP } else { ?>
          <input name="pu" type="text" id="pu" align="right" value="<?PHP echo $cocl_pvht ?>" size="10" maxlength="10" readonly="1"> 
	<?PHP } ?>
      </td>
    </tr>
</table>


<p align="center">
<?PHP 
if (($td == "edit") or ($td == "add")) // Boutons si en édition ou création
{ 
?>
	<input name="RAZ" type="reset" id="RAZ" value="R&eacute;initialiser">
	<input name="enr" type="submit" id="enr" value="Enregistrer">
	<input name="enrimp" type="submit" id="enrimp" value="Enregistrer &amp; Imprimer">
<?PHP 
} 
?>

<?PHP 
if ($td == "del") // Bouton si en mode suppression
{ 
?>
	<input name="del" type="submit" id="del" value="Supprimer">
<?PHP 
} 
?>
</p>
</form>
	
<?PHP 
if ($td <> "view")
{
	if (($td == "edit") OR ($td == "del"))
	{
		if ($signe <> "")
		{
?>
			<script type="text/javascript">
				Calendar.setup({
					inputField     :    "datefinf",     // id of the input field
					ifFormat       :    "%d-%m-%Y",      // format of the input field
					button         :    "f_img_datefinf",  // trigger for the calendar (button ID)
					align          :    "Ll",           // alignment (defaults to "Bl")
					singleClick    :    true
				});
			</script>
<?PHP
		} 
		else 
		{ 
?>
			<script type="text/javascript">
				Calendar.setup({
					inputField     :    "datedf",     // id of the input field
					ifFormat       :    "%d-%m-%Y",      // format of the input field
					button         :    "f_img_datedf",  // trigger for the calendar (button ID)
					align          :    "Ll",           // alignment (defaults to "Bl")
					singleClick    :    true
				});
				Calendar.setup({
					inputField     :    "dateff",     // id of the input field
					ifFormat       :    "%d-%m-%Y",      // format of the input field
					button         :    "f_img_dateff",  // trigger for the calendar (button ID)
					align          :    "Ll",           // alignment (defaults to "Bl")
					singleClick    :    true
				});
			</script>
<?PHP
		} 
?>
    
	<script>
	// form fields description structure
	var a_fields = {
		'datecreaf':{'l':'Date Création','r':true,'f':'date','t':'f_datecreaf'},
		'codecli':{'l':'Client','r':true,'t':'f_codecli'},
		'codeage':{'l':'Agence','r':true,'t':'f_agence'},
		'co':{'l':'Contrat Type','r':true,'t':'f_co'},
		'cope':{'l':'Périodicité','r':true,'t':'f_cope'},
		'delai':{'l':'Délai Intervention','r':false,'f':'integer','t':'f_delai'},
		'nbh':{'l':'Nb Heures','r':true,'f':'time2','t':'f_nbheures'},
		'datedf':{'l':'Date Début','r':true,'f':'date','t':'f_validite'},
		'preavis':{'l':'Préavis','r':true,'f':'unsigned','t':'f_preavis'},
		'matos':{'l':'Matériel','r':true,'t':'f_matos'},
		'pu':{'l':'PV HT','r':true,'f':'real','t':'f_pu','mx':10}
	},
	
	o_config = {
		'to_disable' : ['enr', 'RAZ'],
		'alert' : 1
	}
	
	// validator constructor call
	var v = new validator('cocl_maj', a_fields, o_config);
	</script>
<?PHP 
	} 
	else 
	{ 
?>
	  <script type="text/javascript">
		Calendar.setup({
			inputField     :    "datecreaf",     // id of the input field
			ifFormat       :    "%d-%m-%Y",      // format of the input field
			button         :    "f_img_datecreaf",  // trigger for the calendar (button ID)
			align          :    "Ll",           // alignment (defaults to "Bl")
			singleClick    :    true
		});
		Calendar.setup({
			inputField     :    "datedf",     // id of the input field
			ifFormat       :    "%d-%m-%Y",      // format of the input field
			button         :    "f_img_datedf",  // trigger for the calendar (button ID)
			align          :    "Ll",           // alignment (defaults to "Bl")
			singleClick    :    true
		});
		Calendar.setup({
			inputField     :    "dateff",     // id of the input field
			ifFormat       :    "%d-%m-%Y",      // format of the input field
			button         :    "f_img_dateff",  // trigger for the calendar (button ID)
			align          :    "Ll",           // alignment (defaults to "Bl")
			singleClick    :    true
		});
	</script>
		
	<script>
	// form fields description structure
	var a_fields = {
		'datecreaf':{'l':'Date Création','r':true,'f':'date','t':'f_datecreaf'},
		'codecli':{'l':'Client','r':true,'t':'f_codecli'},
		'codeage':{'l':'Agence','r':true,'t':'f_agence'},
		'co':{'l':'Contrat Type','r':true,'t':'f_co'},
		'cope':{'l':'Périodicité','r':true,'t':'f_cope'},
		'delai':{'l':'Délai Intervention','r':false,'f':'integer','t':'f_delai'},
		'nbh':{'l':'Nb Heures','r':true,'f':'time2','t':'f_nbheures'},
		'datedf':{'l':'Date Début','r':true,'f':'date','t':'f_validite'},
		'preavis':{'l':'Préavis','r':true,'f':'unsigned','t':'f_preavis'},
		'matos':{'l':'Matériel','r':true,'t':'f_matos'},
		'pu':{'l':'PV HT','r':true,'f':'real','t':'f_pu','mx':10}
	},
	
	o_config = {
		'to_disable' : ['enr', 'RAZ'],
		'alert' : 1
	}
	
	// validator constructor call
	var v = new validator('cocl_maj', a_fields, o_config);
	</script>
<?PHP 
	}
} 
?>

<?PHP if ($td == "add") 
{
?>
	<script type="text/javascript">
	warnnbage(<?PHP echo nb_age_actives(); ?>,"cont");
	warnnbtec(<?PHP echo nb_tec_actifs() ?>);
	</script>
<?PHP
}
else
{
	if ($td == "edit")
	{
?>
	<script type="text/javascript">
	warnedit(<?PHP echo $cli_cpta; ?>,"cont");
	</script>
<?PHP
	}
}
?>  

<!-- Fin Affiché uniquement s'il y a un code client -->
<?PHP
}
else
{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('cli_lst.php?ta=m');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de choisir un client...</p>
<?PHP
}
}
else
{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('cocl_lst.php?ta=m');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de choisir un de vos contrats...</p>
<?PHP
 }
?>
