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

Version 0.9.7

Copyright (C) 2006-2007  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

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_rel.inc.php");
include_once("../inc/fic_zc.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("geoip.inc.php");
Archivelog(@$_SESSION["code_log"]);

$td = strip_tags(trim($_GET['td']));
$coderech = strip_tags(trim($_GET['cc']));
$nbcontacts = 0;
$nbcontrats = 0;
$nbinterventions = 0;
$ca ="";
$pbm_cli = FALSE;

$msg1_err_type="Pas d'agence active";
$msg2_err_type="Aucune ville dans ce pays ou département/région/état";

switch (@$_SESSION["niv_log"])
{
	// Un client ne peux qu'éditer son propre compte
	case 1 :
		$td="edit";
		$coderech=@$_SESSION["code_log"];
		$cli_code=@$_SESSION["code_log"];
		break;
	case 3 :
	case 4 :
		$row = lit_enr_age($_SESSION["code_age"]);
		if (is_array($row))
		{
			extract ($row);
			$cli_codepays=$age_codepays;
		}
		else
			$pbm_cli=TRUE;
		break;
	default :
		break;
}

if ($coderech <> "")
{
	// Vérification qu'il existe bien des contacts et nombre de ceux-ci
	$nbcontacts = nb_ccli_in_cli($coderech);

	// Vérification qu'il existe bien des contrats et nombre de ceux-ci
	$nbcontrats = nb_cocl_in_cli($coderech);

	// Vérification qu'il existe bien des interventions et nombre de celles-ci
	switch ($_SESSION["niv_log"])
	{
		case 3 :
		case 4 :
			$nbinterventions = nb_int_cli($coderech,$_SESSION["niv_log"],$_SESSION["code_age"]);
			break;
		case 6 :
		case 7 :
			$nbinterventions = nb_int_cli($coderech,$_SESSION["niv_log"],$_SESSION["code_do"]);
			break;
		case 5 :
		case 9 :
			$nbinterventions = nb_int_cli($coderech,$_SESSION["niv_log"],"");
			break;
	}

	// Contrôle qu'une agence n'essaye pas de "hacker" un client
	if ($td <> "add")
	{
		// Récupération des informations clients
		$row = lit_enr_cli($coderech);
		if (is_array($row)==TRUE)
		{
			extract ($row);
	
			switch(@$_SESSION["niv_log"])
			{
				case 3 :
				case 4 :
					if (client_age_ok($cli_code,@$_SESSION["code_age"],TRUE)==FALSE)
						$pbm_cli = TRUE;
					break;
				default :
					break;
			}
		}
		else
			$pbm_cli=TRUE;
	}
	else
		$pbm_cli=TRUE;
}
else
{
	if ($td == "add")
	{
		$cli_code = crecodesession("C");
		$cli_cpta = 0;
		$cli_prev =0;
	}
	else
		$pbm_cli=TRUE;
}

switch ($td)
{
	case "add" :
		$titre = "CREATION COMPTE CLIENT";
		break;
	case "edit" :
		$titre = "MISE A JOUR COMPTE CLIENT";
		break;
	case "del" :
		$titre = "SUPPRESSION COMPTE CLIENT";
		break;
	default :
		$pbm_cli=TRUE;
		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 : Fiche Client</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 -->
<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/verif_form.js"></script>

<!-- Scripts Javascript pour contrôle de champs -->
<script	type="text/javascript" src="../jscript/verif_field.js"></script>

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<script language="javascript">
var tab1_champ=new Array(
<?PHP
if (@$_SESSION["niv_log"] >= 2)
{
	if ($td=="add")
	{
?>
	"nom:typ ; format:liste ; req:Y ; txt:Type; vmn:1 ; stok:frmok ; stbad:frmbad",
<?PHP
	}
	else
	{
?>
	"nom:typ ; format:txt ; req:Y ; txt:Type; stok:frmok ; stbad:frmbad",
<?PHP
	}
?>
	"nom:cat ; format:liste ; req:Y ; txt:Catégorie ; vmn:1; stok:frmok ; stbad:frmbad",
<?PHP
}
?>
	"nom:ste ; format:txt ; req:N ; txt:Société; lmx:50 ; stok:frmok ; stbad:frmbad",
	"nom:rcs ; format:txt ; req:N ; txt:RCS; lmx:50 ; stok:frmok ; stbad:frmbad",
	"nom:civil ; format:liste ; req:Y ; txt:Civilité ; vmn:1; stok:frmok ; stbad:frmbad",
	"nom:prenom ; format:txt ; req:Y ; txt:Civilité ; lmx:50; stok:frmok ; stbad:frmbad",
	"nom:nom ; format:txt ; req:Y ; txt:Civilité ; lmx:50; stok:frmok ; stbad:frmbad",
	"nom:adr1 ; format:txt ; req:Y ; txt:Adresse(1); lmx:50; stok:frmok ; stbad:frmbad",
	"nom:adr2 ; format:txt ; req:N ; txt:Adresse(2); lmx:50; stok:frmok ; stbad:frmbad",
	"nom:ville ; format:liste ; req:Y ; txt:Ville; vmn:1; stok:frmok ; stbad:frmbad",
	"nom:pays ; format:liste ; req:Y ; txt:Pays; vmn:1; stok:frmok ; stbad:frmbad",
	"nom:telf ; format:tel ; req:Y ; txt:Téléphone Fixe; stok:frmok ; stbad:frmbad",
	"nom:fax ; format:tel ; req:N ; txt:Télécopieur; stok:frmok ; stbad:frmbad",
	"nom:telp ; format:telp ; req:N ; txt:Téléphone portable; stok:frmok ; stbad:frmbad",
	"nom:email ; format:mail ; req:N ; txt:Email ; lmx:50; stok:frmok ; stbad:frmbad",
<?PHP if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9))
{ 
?>
	"nom:notaa ; format:txt ; req:N ; txt:Notes administration; stok:frmok ; stbad:frmbad",
<?PHP
}
if (@$_SESSION["niv_log"] >= 3)
{ 
?>
	"nom:notat ; format:txt ; req:N ; txt:Notes techniques; stok:frmok ; stbad:frmbad",
<?PHP
}
?>
	"nom:mess ; format:mail ; req:N ; txt:Messagerie ; lmx:50; stok:frmok ; stbad:frmbad");
</script>

<!-- Scripts Javascript pour alertes -->
<script language="JavaScript" src="../jscript/alertes.js"></script>

<!-- Scripts Ajax -->
<script type="text/javascript" src="../ajax/util.js"></script>
<script type="text/javascript" src="../ajax/SelectUpdater.js"></script>

<?PHP if ($pbm_cli==FALSE) { ?>
<script type="text/javascript">
function updateDep()
{
	ZCUpdater2 = new SelectUpdater("dep", "<?PHP echo $rep_appli.'/ajax/'; ?>recupdepzc.php?pays=", "attn1", "msg1", "<?PHP echo $msg1_err_type; ?>");
	anciencodepays = "";
	anciendep = "";
	document.getElementById("pays").onchange = function() {
		var listepays = document.getElementById("pays");
		var codepays = listepays.options[listepays.selectedIndex].value;
		if (codepays.length > 0)
		{
			if (codepays != anciencodepays)
			{
				// Faire l'appel Ajax
				ZCUpdater2.run(codepays);
				var txthtml = "<option>-- Faites votre choix --</option>\n";
				document.getElementById("ville").innerHTML=txthtml;
				document.getElementById("attn2").innerHTML = "";
				document.getElementById("msg2").innerHTML = "";
				anciencodepays = codepays;
			}
		}
		else
		{
			// Remettre la liste a vide et annuler l'appel eventuel
			ZCUpdater2.reset();
			anciencodepays = "";
		}
	}
}

function updateVille()
{
	villeUpdater = new SelectUpdater("ville", "<?PHP echo $rep_appli.'/ajax/'; ?>recupville.php?dep=", "attn2", "msg2", "<?PHP echo $msg2_err_type; ?>");
	anciendep = "";
	document.getElementById("dep").onchange = function() {
		var listedep = document.getElementById("dep");
		var dep = listedep.options[listedep.selectedIndex].value;
		var listepays = document.getElementById("pays");
		var codepays = listepays.options[listepays.selectedIndex].value;
		if (dep.length > 0)
		{
			if (dep != anciendep)
			{
				// Faire l'appel Ajax
				var prmget=dep+"_"+codepays;
				villeUpdater.run(prmget);
				anciendep = dep;
			}
		}
		else
		{
			// Remettre la liste a vide et annuler l'appel eventuel
			villeUpdater.reset();
			anciendep = "";
		}
	}
}
</script>
<?PHP } ?>
</head>

<?PHP
switch (@$_SESSION["niv_log"])
{
	case 1 :
		echo '<body onLoad="metFocus();tst_sites_client(document.cli_maj.ste.value);">';
		break;
	case 2 :
		if ($td <> "add") 
			echo '<body onLoad="metFocus();tst_sites_client(document.cli_maj.ste.value);updateDep(document.cli_maj.pays.value);updateVille(document.cli_maj.dep.value);">';
		else
			echo '<body onLoad="metFocus();updateDep(document.cli_maj.pays.value);updateVille(document.cli_maj.dep.value);">';
		break;
	case 3 :
	case 4 :
	case 5 :
	case 6 :
	case 7 :
	case 9 :
		switch($td)
		{
			case "del" :
				echo '<body onLoad="metReadonly();warndelcli();">';
				break;
			case "view" :
				echo '<body onLoad="metReadonly();">';
				break;
			case "add" :
				echo '<body onLoad="metFocus();updateDep(document.cli_maj.pays.value);updateVille(document.cli_maj.dep.value);">';
				break;
			default :
				echo '<body onLoad="metFocus();tst_sites_client(document.cli_maj.ste.value);updateDep(document.cli_maj.pays.value);updateVille(document.cli_maj.dep.value);">';
				break;
		}
		break;
	default :
		break;
}

include_once("entete.inc.php"); 
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<?PHP
if ($pbm_cli == FALSE)
{
	if ((nb_ccat() <> 0) AND (nb_civ() <> 0) AND (nb_fonc() <> 0) AND (nb_age_actives()<>0))
	{
?>
<table width="100%"  border="0">
<tr>
    <td width="33%">
	<?PHP if (@$_SESSION["niv_log"] >= 3) { ?>
	<a href="cli_lst.php">Retour &agrave; la liste</a></div>
	<?PHP } else {
	if (@$_SESSION["niv_log"] >= 1) { ?>
	<a href="index.php">Retour &agrave; l'accueil</a></div>
	<?PHP } }?>
	</td>
    <td width="33%" bgcolor="#FF0000"><div align="center"><span class="titrerubrique1">Coordonn&eacute;es</span></div></td>
    <td width="33%">&nbsp;</td>
  </tr>
</table>
<table width="100%"  border="0">
<tr>
	<td width="31%">
    <?PHP if (($td == "edit") AND ($cli_ste <> "") AND (@$_SESSION["niv_log"]<>2))
	{
		$url_ccli = "ccli_maj.php?td=add&cc=".$cli_code;
	?>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $url_ccli; ?>',950,560,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")><img src="../img/ctacts.gif" width="22" height="22" border="0" align="absmiddle"></a>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $url_ccli; ?>',950,560,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")> Ajouter un contact </a>
	<?PHP
		if (($nbcontacts <> 0) AND (@$_SESSION["niv_log"]<>2))
		{
			$url_ccli = "ccli_maj.php?td=lst&cc=".$cli_code;
    ?>
		/
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $url_ccli; ?>',950,560,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")> Edition contacts (<?PHP echo sprintf ("%'03s",$nbcontacts);?>)</a>
	<?PHP
		}
	}
	?>
	</td>
	<td width="39%" valign="top">
		<div id="oblig" align="center" class="annot_gras_it">
		(<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire"> : Saisie obligatoire)
		</div>
	</td>
	<td width="30%" align="right">
		<?PHP
		if (@$_SESSION["niv_log"]<>2)
		{
			if (($td == "edit") OR ($td == "view"))
			{
		?>
		<select name="etats" size="1" id="etats" onChange="javascript:; lanceimp(this.value); this.value='';" class="annot_petit">
		<option value="" selected>&raquo; Etats &laquo;</option>
		<?PHP
			$urlimp = "cli_maji.php?cc=".$coderech;
			echo "<option value=\"".$urlimp."\" class=\"icone\" style=\"background-image: url(../img/sel_fiche.gif);\">Fiche Client</option>\n";

			if ($nbinterventions <> 0)
			{
				echo "<optgroup label=\"Interventions\" class=\"icone\" style=\"background-image: url(../img/sel_int.gif);\">\n";
				if (@$_SESSION["niv_log"]<>1)
					$url_inter = "int_lsti.php?ord=DES&tri=Code&search=".$cli_code."&rech=Rechercher";
				else
					$url_inter = "int_lsti.php?ord=DES&tri=";
				echo "<option value=\"".$url_inter."\">Liste</option>\n";
				echo "</optgroup>\n";
			}

			if ($nbcontrats <> 0)
			{
				echo "<optgroup label=\"Contrats\" class=\"icone\" style=\"background-image: url(../img/sel_con.gif);\">\n";
				if (@$_SESSION["niv_log"]<>1)
					$url_cocl = "cocl_lsti.php?ord=DES&tri=Code&search=".$cli_code."&rech=Rechercher";
				else
					$url_cocl = "cocl_lsti.php?ord=DES&tri=Code";
				echo "<option value=\"".$url_cocl."\">Liste</option>\n";
				$url_coclsm = "coclsm_pdf.php?cc=".$cli_code;
				echo "<option value=\"".$url_coclsm."\">Synthèse complète</option>\n";
				echo "<option value=\"".$url_coclsm."&tco=o\">Synthèse ouverts</option>\n";
				echo "<option value=\"".$url_coclsm."&tco=c\">Synthèse clots</option>\n";
				echo "</optgroup>\n";
			}

			if (exist_rel_cli($cli_code) == TRUE)
			{
				echo "<optgroup label=\"Relevé de parc\" class=\"icone\" style=\"background-image: url(../img/sel_rel.gif);\">\n";
				$url_rel = "rel_pdf.php?cr=".num_rel_cli($cli_code)."&pbm=0";
				echo "<option value=\"".$url_rel."\">Relevé complet</option>\n";
				$url_relp = "rel_pdf.php?cr=".num_rel_cli($cli_code)."&pbm=1";
				echo "<option value=\"".$url_relp."\">Relevé avec problèmes</option>\n";
				echo "</optgroup>\n";
			}
		}
		?>		
		</select>
		<?PHP } else { ?>
		&nbsp;
		<?PHP } ?>
	</td>
</tr>
</table>

<table width="100%"  border="0">
<tr>
	<td width="40%">
	<?PHP if (($td == "edit") AND ($nbcontrats <> 0) AND (@$_SESSION["niv_log"]<>2) AND (@$_SESSION["niv_log"]<>7) AND (@$_SESSION["niv_log"]<>6))
	{
		$url_coclh = "coclhd_lst.php?or=cli&cc=".$cli_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');")> Historiques contrats (<?PHP echo sprintf ("%'03s",$nbcontrats);?>) </a>
	<?PHP
	}
	?>
	</td>
	<td width="20%" valign="top">&nbsp;</td>
	<td width="40%" align="right">
	<?PHP if (($td == "edit") AND (@$_SESSION["niv_log"]>=3))
	{
		if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
		{
			$url_addint = "int_maj.php?td=add&cc=".$cli_code."&ca=".@$_SESSION["code_age"];
		}
		else
		{
			$url_addint = "int_maj.php?td=add&cc=".$cli_code."&ca=".$cli_prop;
		}
	?>
		<img src="../img/int.gif" width="22" height="22" align="absmiddle">
		<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_addint; ?>');")> Saisir une intervention </a>
	<?PHP
		if (($nbinterventions <> 0)  AND (@$_SESSION["niv_log"]<>2))
		{
			$url_inter = "int_lst.php?ord=DES&tri=Code&search=".$cli_code."&rech=Rechercher";
	?>
		/
		<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_inter; ?>');")> Voir les interventions (<?PHP echo sprintf ("%'03s",$nbinterventions);?>)</a>
	<?PHP
		}
	}
	?>	</td>
</tr>
</table>

<table width="100%"  border="0">
<tr>
	<td width="31%" align="left" valign="top">
	<?PHP if (($td == "edit") AND (@$_SESSION["niv_log"]>=3) AND (@$_SESSION["niv_log"]<>7) AND (@$_SESSION["niv_log"]<>6))
	{
		if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
		{
			$url_addcocl ="cocl_maj.php?td=add&co=&cc=".$cli_code."&ca=".@$_SESSION["code_age"];
		}
		else
		{
			$url_addcocl ="cocl_maj.php?td=add&co=&cc=".$cli_code."&ca=".$cli_prop;
		}
	?>
		<img src="../img/contrat.gif" width="22" height="22" align="absmiddle">
		<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_addcocl; ?>');")> Saisir un contrat </a>
	<?PHP
		if (($nbcontrats <> 0) AND (@$_SESSION["niv_log"]<>2))
		{
			$url_cocl = "cocl_lst.php?ord=DES&tri=Code&search=".$cli_code."&rech=Rechercher";
	?>
		/
		<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_cocl; ?>');")> Voir les contrats (<?PHP echo sprintf ("%'03s",$nbcontrats); ?>)</a>
	<?PHP
		}
	} 
	else
		echo "&nbsp;";
	?>
	</td>
	<td width="39%" valign="top" align="center">
	<?PHP 
	if ((@$_SESSION["niv_log"] >= 3) AND ($td == "edit") AND (@$_SESSION["niv_log"]<>7) AND (@$_SESSION["niv_log"]<>6))
	{
	  $url_repcli = "fic_down.php?dir=clients/".strtolower($cli_code);
	?>
		 <a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_repcli; ?>');")><img src="../img/rep_priv.gif" title="Aller dans le r&eacute;pertoire priv&eacute;" width="22" height="22" border="0" align="absmiddle"></a>
		<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_repcli; ?>');")> Aller dans le r&eacute;pertoire priv&eacute;</a>
	<?PHP
	}
	else
		echo "&nbsp;";
	?>
	</td>
	<td width="30%" align="right">
	<?PHP if (($td == "edit") AND (@$_SESSION["niv_log"]<>2) AND (@$_SESSION["niv_log"]<>7) AND (@$_SESSION["niv_log"]<>6))
	{
		if (exist_rel_cli($coderech) == TRUE)
		{
			if (@$_SESSION["niv_log"]>=3)
				$url_rel = "rel_maj.php?td=edit&cr=".num_rel_cli($cli_code);
			else
				$url_rel = "rel_maj.php";
	?>
			<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_rel; ?>');")><img src="../img/relexist.gif" title="Voir le relev&eacute; de parc" width="22" height="22" border="0" align="absmiddle"></a>
			<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_rel; ?>');")> Voir le relev&eacute; de parc </a>
	<?PHP
		}
		else
		{
			if (@$_SESSION["niv_log"]>=3)
			{
				$url_rel = "rel_maj.php?td=add&cc=".$cli_code;
	?>
			<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_rel; ?>');")><img src="../img/rel.gif" title="Saisir le relev&eacute; de parc" width="22" height="22" border="0" align="absmiddle"></a>
			<a href="javascript:;" onclick="self.location.href=('<?PHP echo $url_rel; ?>');")> Saisir le relev&eacute; de parc </a>
	<?PHP
			}
		}
	}
	else
		echo "&nbsp;";
	?>
	</td>
</tr>
</table>

<form name="cli_maj" method="post" action="cli_enr.php" onSubmit="javascript:; return (verif_cli(this,tab1_champ,document.cli_maj.pays.value,''));">
<input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
<input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
<input name="prev" type="hidden" id="prev" value="<?PHP echo $cli_prev ?>">
<?PHP if ($td == "add") {?>
<input name="codecli" type="hidden" id="codecli" value="<?PHP echo $cli_code ?>">
<?PHP }
if ($td <> "add") {?>
<table width="800"  border="0">
<tr>
	<td width="16%" class="rubfrm" id="f_codecli"><div align="right">Code Client</div></td>
	<td width="84%">
		<input name="codecli" type="text" id="codecli" value="<?PHP echo $cli_code ?>" readonly="1">
	</td>
</tr>
</table>
<?PHP }  ?>

<table width="800"  border="0">
<?PHP
if (@$_SESSION["niv_log"] >= 2)
{
?>
<tr>
	<td class="rubfrm" id="f_typ"><div align="right">Type</div></td>
	<td>
		<?PHP 
		if ($td == "add")
		{ 
			echo "<select name=\"typ\" size=\"1\" id=\"typ\">";
			echo "<option value=\"\" selected>-- Choisissez pourquoi vous créez ce client --</option>";
			for ($i=0; $i<3; $i++)
			{
				echo "<option value=\"".$i."\">".$typ_client[$i]."</option>";
			}
			echo "</select>";
		?>
			<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
		<?PHP
		}

		if (($td == "edit") OR ($td == "del"))
		{ 
			if ($cli_type <> 2)
			{
				echo "<select name=\"typ\" size=\"1\" id=\"typ\">";
				echo "<option value=\"\">-- Choisissez pourquoi vous créez ce client --</option>";
				for ($i=0; $i<3; $i++)
				{
					if ($i == $cli_type)
						echo "<option value=\"".$i."\" selected>".$typ_client[$i]."</option>";
					else
						echo "<option value=\"".$i."\">".$typ_client[$i]."</option>";
				}
				echo "</select>";
			?>
				<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
			<?PHP
			}
			else
			{
			?>
				<input name="typ" type="hidden" id="type" value="<?PHP echo $cli_type; ?>">
				<input name="ntyp" type="text" id="ntype" value="<?PHP echo $typ_client[$cli_type]; ?>" readonly="1">		
			<?PHP
			}
		}
		?>
	</td>
</tr>
<tr>
	<td width="16%" class="rubfrm" id="f_cat"><div align="right">Cat&eacute;gorie</span></div></td>
	<td width="84%">
		<select name="cat" size="1" id="cat">
		<?PHP
		echo "<option>-- Choisissez une catégorie --</option>";

		// Création de la liste des catégories
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs = mysql_query("SELECT * FROM ".TBCCAT." ORDER BY cat_desc");

		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			if ($cli_cat == $cat_code)
			{
				echo '<option value="'.$cat_code.'" selected>'.$cat_desc.'</option>';
			}
			else
			{
				echo '<option value="'.$cat_code.'">'.$cat_desc.'</option>';
			}
		}
		mysql_free_result($rs);
		mysql_close($conn);
		?>
		</select>
		<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
</tr>
<?PHP
}
?>
<tr>
	<td width="16%" class="rubfrm" id="f_ste"><div align="right">Soci&eacute;t&eacute;</div></td>
	<td width="84%">
		<table width="100%" border="0">
		<tr>
		<td width="200">
		<?PHP 
		if ($td <> "add") 
		{
			if ((@$_SESSION["niv_log"] <> 2) AND ($td=="edit"))
			{ 
		?>
		<input name="ste" type="text" id="ste" value="<?PHP echo htmlspecialchars($cli_ste) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase(); tst_sites_client(this.value);">
		<?PHP
			}
			else
			{
		?>
		<input name="ste" type="text" id="ste" value="<?PHP echo htmlspecialchars($cli_ste) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();">
		<?PHP
			}
		}
		else
		{
		?>
		<input name="ste" type="text" id="ste" value="<?PHP echo htmlspecialchars($cli_ste) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();">
		<?PHP
		}
		?>
		</td>
		<td align="left">
		<?PHP 
		if ($td <> "add") 
		{
		?>
			<div id="site" style="display:none" align="left">
				<a href="javascript:;" onclick="javascript:; fen_sites_client(document.cli_maj.codecli.value);")><img src="../img/sites.gif" title="G&eacute;rer les sites" width="16" height="16" border="0" align="bottom"></a>
				<a href="javascript:;" onclick="javascript:; fen_sites_client(document.cli_maj.codecli.value);")> G&eacute;rer les sites secondaires</a>
			</div>
			<div id="pas_site"  style="display:block" align="left">
				&nbsp;
			</div>
		<?PHP
		}
		else
		{
		?>
			&nbsp;
		<?PHP
		}
		?>
		</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
  <td class="rubfrm" id="f_rcs"><div align="right">RCS</div></td>
  <td><input name="rcs" type="text" id="rcs" value="<?PHP echo $cli_rcs ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();"></td>
</tr>
<tr>
	<td width="16%" class="rubfrm" id="f_civil"><div align="right">Civilit&eacute;</div></td>
	<td width="84%">
		<select name="civil" size="1" id="civil">
		<?PHP
		echo "<option>-- Choisissez une civilité --</option>";

		// Création de la liste des civilités
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs = mysql_query("SELECT * FROM ".TBCIV." ORDER BY civ_desc");
		
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			if ($cli_civilite == $civ_code)
				echo '<option value="'.$civ_code.'" selected>'.$civ_desc.'</option>';
			else
				echo '<option value="'.$civ_code.'">'.$civ_desc.'</option>';
		}
		mysql_free_result($rs);
		mysql_close($conn);
		?>
		</select>
		<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_prenom"><div align="right">Pr&eacute;nom</div></td>
	<td>
		<input name="prenom" type="text" id="prenom" value="<?PHP echo htmlspecialchars($cli_prenom) ?>" size="50"  onChange="javascript:this.value=ucWords(this.value,true);">
		<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_nom"><div align="right">Nom</div></td>
	<td>
		<input name="nom" type="text" id="nom" value="<?PHP echo htmlspecialchars($cli_nom) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();">
		<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_adr1"><div align="right">Adresse(1)</div></td>
	<td>
		<input name="adr1" type="text" id="adr1" value="<?PHP echo htmlspecialchars($cli_adr1) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();">
		<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_adr2"><div align="right">Adresse(2)</div></td>
	<td>
		<input name="adr2" type="text" id="adr2" value="<?PHP echo htmlspecialchars($cli_adr2) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_pays"><div align="right">Pays</div></td>
	<td>
	<?PHP
	if (@$_SESSION["niv_log"] <> 1) // Un client ne peut pas "se" déménager
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs = mysql_query("SELECT * FROM ".TBPAYS." ORDER BY pays_nom");
	?>			
	<select name="pays" id="pays" onchange="updateDep(this.value);">
	<?PHP
		echo "<option>-- Choisissez un pays --</option>";
	
		// Création de la liste des pays
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			if ($cli_codepays == $pays_code)
				echo '<option value="'.$pays_code.'" selected>'.$pays_nom.'</option>\n';
			else
				echo '<option value="'.$pays_code.'">'.$pays_nom.'</option>\n';
		}
	?>
	</select>
	<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	<?PHP
		mysql_close($conn);
	}
	else
	{
	?>
	<input name="pays" type="hidden" id="pays" value="<?PHP echo $cli_codepays ?>">
	<input name="npays" type="text" id="npays" value="<?PHP echo str_majuscules(Retournepays_open($cli_codepays,TRUE)); ?>" size="50" readonly="1">
	<?PHP
	}
	?>
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_dep"><div align="right">D&eacute;partement ou R&eacute;gion/Etat</div></td>
	<td>
	<?PHP
	if (@$_SESSION["niv_log"] <> 1) // Un client ne peut pas "se" déménager
	{
		// Création de la liste des départements
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		$tab_zone = array();
		$lst_zone = array();
		switch(@$_SESSION["niv_log"])
		{
			case 2 :
			case 5 :
			case 6 :
			case 7 :
			case 9 :
				/* On construit d'abord la liste des départements couverts pour ce pays */
				$tab_zone=tab_zcc_pays_open(str_majuscules($cli_codepays),FALSE);
				break;
				
			case 3 :
			case 4 :
				/* On construit d'abord la liste des départements couverts par cette agence */
				$tab_zone=tab_zcc_age_open(@$_SESSION["code_log"],FALSE);
				break;
			default :
				break;
		}
			
		if (count($tab_zone)>0)
		{
			/* On construit ensuite (pour le select) la liste des départements trouvés */
			$rech="";
			if (count($tab_zone)>0)
			{
				$rech="SELECT * FROM ".TBDEP." WHERE ";
				for ($i=0; $i<count($tab_zone); $i++)
				{
					$rech .= " (dep_code='".str_majuscules($tab_zone[$i])."' AND dep_codepays='".str_majuscules($cli_codepays)."') OR";
				}
				// Suppression du dernier 'OR'
				$rech = substr($rech,0,strlen($rech)-3);
				$rech .=" ORDER BY dep_code";
			}
			// On cree le resultat
			if ($rech<>"")
			{
				$rs = mysql_query($rech) or die(mysql_error());
				while($row = mysql_fetch_array($rs))
				{
					$lst_zone[] = $row;
				}
			}
		}

	?>
	<select name="dep" size="1" id="dep"  onchange="updateVille(this.value)">
	<?PHP
		echo "<option>-- Faites votre choix --</option>";
	
		foreach ($lst_zone as $row)
		{
			extract($row);
			if ($dep_code<>$dep_nom)
				$dep=$dep_code." (".$dep_nom.")";
			else
				$dep=$dep_nom;
			if ($cli_dep == $dep_code)
				echo "<option value=\"".$dep_code."\" selected>".$dep."</option>\n";
			else
				echo "<option value=\"".$dep_code."\">".$dep."</option>\n";
		}
	?>
	</select>
	<span class="annot_petit_it">
	<?PHP
		if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
			echo "(D&eacute;partements couverts par votre agence)";
		else
			echo "(D&eacute;partements couverts par les agences)"; 
	?>
	</span>
	<br>
	 <span id="attn1"></span><span id="msg1" style="background-color:#FF0000; color:#FFFF00; font-weight:bold"><?PHP if ((count($tab_zone)==0) AND ($td<>"del")) echo htmlspecialchars($msg1_err_type)?></span>
	<?PHP
		mysql_close($conn);
	}
	else
	{
		$dep_nom=Retournedep_open($cli_dep,$cli_codepays,TRUE);
		if ($dep_code<>$dep_nom)
			$dep=$cli_dep." (".$dep_nom.")";
		else
			$dep=$dep_nom;
	?>
	<input name="dep" type="hidden" id="dep" value="<?PHP echo $cli_dep ?>">
	<input name="ndep" type="text" id="ndep" value="<?PHP echo str_majuscules($dep); ?>" size="50" readonly="1">
	<?PHP
	}
	?>
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_ville"><div align="right">Ville</div></td>
	<td>
	<?PHP
	if (@$_SESSION["niv_log"] <> 1) // Un client ne peut pas "se déménager"
	{
		// Création de la liste des villes
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	
		$rech="";
		if ($cli_dep<>"")
		{
			if ($cli_dep<>"-1")
			{
				$filtre=$cli_dep;
				if (($filtre == "2A") OR ($filtre == "2B"))
					$filtre = "20";
				$rech = "SELECT * FROM ".TBVILLES." WHERE `villes_dep` = '".str_majuscules($filtre)."' AND `villes_codepays`='".str_majuscules($cli_codepays)."' ORDER BY villes_nom";
			}
			else
			{
				$rech = "SELECT * FROM ".TBVILLES." WHERE `villes_codepays`='".str_majuscules($cli_codepays)."' ORDER BY villes_nom";
			}
		}
		if ($rech<>"")
			$rs = mysql_query($rech);
	?>
	<select name="ville" size="1" id="ville">
	<?PHP
		echo "<option>-- Faites votre choix --</option>";
	
		if ($rech<>"")
		{
			if (mysql_num_rows($rs)>0)
			{
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					if ($cli_ville == $villes_id)
						echo '<option value="'.$villes_id.'" selected>'.str_majuscules($villes_nom).' ('.$villes_cp.')</option>';
					else
						echo '<option value="'.$villes_id.'">'.str_majuscules($villes_nom).' ('.$villes_cp.')</option>';
				}
			}
		}
	?>
	</select>
	 <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	 <span id="attn2"></span><span id="msg2" style="background-color:#FF0000; color:#FFFF00; font-weight:bold"><?PHP if ($rech<>"") {if ((mysql_num_rows($rs)==0) AND ($td<>"del")) echo htmlspecialchars($msg2_err_type);}?></span>
	<?PHP
		mysql_close($conn);
	}
	else
	{
	?>
	<input name="ville" type="hidden" id="ville" value="<?PHP echo $cli_ville ?>">
	<input name="nville" type="text" id="nville" value="<?PHP echo Retourneville_openId($cli_ville,TRUE)." (".Retournecp_openId($cli_ville,TRUE).")"; ?>" size="40" readonly="1">
	<?PHP
	}
	?>
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_telf"><div align="right">T&eacute;l&eacute;phone Fixe </div></td>
	<td>
		<input name="telf" type="text" id="telf" value="<?PHP echo $cli_telf ?>" size="20" maxlength="20">
		<img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_fax"><div align="right">T&eacute;l&eacute;copieur</div></td>
	<td>
		<input name="fax" type="text" id="fax" value="<?PHP echo $cli_fax ?>" size="20" maxlength="20">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_telp"><div align="right">Portable</div></td>
	<td>
		<input name="telp" type="text" id="telp" value="<?PHP echo $cli_telp ?>" size="20" maxlength="20">
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_email"><div align="right">Email</div></td>
	<td>
		<input name="email" type="text" id="email" value="<?PHP echo $cli_email ?>" size="50" maxlength="50" onChange="javascript:this.value=this.value.toLowerCase();">
		<?PHP 
		if (($td == "edit") AND (@$_SESSION["niv_log"] >= 3))
		{ ?>
			<a href="mailto:<?PHP echo $cli_email ?>"><img src="../img/mail.png" width="22" height="22" title="Envoyer e-mail" border="0" align="absmiddle"></a>
		<?PHP } ?>
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_mess"><div align="right">Messagerie</div></td>
	<td>
		<input name="mess" type="text" id="mess" value="<?PHP echo $cli_mess ?>" size="50" maxlength="50" onChange="javascript:this.value=this.value.toLowerCase();">
	</td>
</tr>
<?PHP if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9))
{ 
?>
<tr>
	<td class="rubfrm" id="f_notaa"><div align="right">Notes Administration</div></td>
	<td>
		<textarea name="notaa" cols="90" rows="5" id="notaa"><?PHP echo htmlspecialchars($cli_notaa) ?></textarea>
	</td>
</tr>
<?PHP
}

if (@$_SESSION["niv_log"] >= 3)
{ 
?>
<tr>
	<td height="37" class="rubfrm" id="f_notat"><div align="right">Notes Techniciens </div></td>
	<td>
		<textarea name="notat" cols="90" rows="5" id="notat"><?PHP echo htmlspecialchars($cli_notat) ?></textarea>
	</td>
</tr>
<?PHP
if ((@$_SESSION["niv_log"] <> 7) AND (@$_SESSION["niv_log"] <> 6))
{ 
?>
<tr>
	<td height="37" class="rubfrm" id="f_cpta"><div align="right">Comptabilit&eacute;</div></td>
	<td>
		<?PHP 
		$aff_lst=TRUE;

		// Seules les agences gérant leurs factures peuvent modifier ce paramètre
		if (((@$_SESSION["niv_log"] ==4) AND (mod_fact_age(@$_SESSION["code_log"]) <> 1)) OR (@$_SESSION["niv_log"] == 3))
			$aff_lst=FALSE;
		
		if ($aff_lst==TRUE)
		{
			echo '<select name="cpta" size="1" id="cpta">';
			foreach ($niv_cpta as $key=>$value)
			{
				echo '<option value="'.$key.'"'; 
				if ($cli_cpta == $key)
					echo ' selected';
				echo '>';
				echo $value."</option>";
			}
			echo "</select>";
			echo ' <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">';
		}
		else
		{
		?>
		<input name="cpta" type="hidden" id="cpta" value="<?PHP echo $cli_cpta ?>" >
		<input name="ncpta" type="text" id="ncpta" value="<?PHP echo $mess_cpta[$cli_cpta]; ?>" size="20" readonly="1">
		<?PHP 
		}
		?>
	</td>
</tr>
<?PHP } else { ?>
<input name="cpta" type="hidden" id="cpta" value="<?PHP echo $cli_cpta ?>" >
<?PHP } ?>
<tr>
	<td height="37" class="rubfrm" id="f_cpta"><div align="right">Pr&eacute;venir par</div></td>
	<td>
		<?php 
		foreach ($prev_client as $key=>$value)
		{
			echo '<input type="radio" name="prev" id="prev" style="background : #FFFFFF;" value="'.$key.'"';
			if ($key==$cli_prev)
				echo ' checked';
			echo '>';
			echo " ".$value." ";
			if ($key<>(count($prev_client)-1))
				echo "/";
		}
		?>
		 <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
		<br>
		<span class="annot_petit_it">Pour cr&eacute;ation des comptes, interventions closes, et cr&eacute;ations contrats</span>
	</td>
</tr>
<?PHP
}
?>
</table>

<p align="center">
<?PHP 
	if (($td == "edit") or ($td == "add")) 
	{ 
?>
		<input name="RAZ" type="reset" class="bton_fmarr_std" id="RAZ" value="R&eacute;initialiser">
		<input name="submit" type="submit" class="bton_fmarr_std" id="submit" value="Enregistrer">
		<?PHP 
		if (($cli_cpta <> 3) AND (@$_SESSION["niv_log"] >= 3))
		{
		?>
			<input name="int" type="submit" class="bton_fmarr_std" id="int" value="Enregistrer & Saisir Intervention">
		<?PHP
		} 
	}
	if (($td == "del") AND (@$_SESSION["niv_log"] >= 3)) { ?>
	<input name="del" type="submit" class="bton_fmarr_std" id="del" value="Supprimer">
	<input name="annul" type="button" class="bton_fmarr_std" id="annul" value="Annuler" onclick="javascript:; history.back();">
	<?PHP } ?>
</p>
</form>
<?PHP
		// On n'alerte sur le client que si on est en niveau "technicien" ou supérieur
		if (@$_SESSION["niv_log"] >= 3)
		{
?>
	<script type="text/javascript">
	warncpta(<?PHP echo $cli_cpta; ?>,"client");
	</script>
<?PHP
			if (($td=="edit") AND (exist_rpbm_actif("add",$cli_code,"")==TRUE))
			{
?>
	<script type="text/javascript">
	alert('Il y a des problèmes non affectés sur ce compte client.\nVoir dans le relevé de parc.');
	</script>
<?PHP
			}
		}
	}
	else
	{
		if (@$_SESSION["niv_log"] >= 5)
		{
			if (nb_ccat() == 0)
			{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('cat_lst.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de cr&eacute;er au moins une cat&eacute;gorie de client...</p>
<?PHP
			}
		
			if (nb_civ() == 0)
			{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('civ_lst.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de cr&eacute;er au moins une civilit&eacute;...</p>
    <?PHP
			}
		
			if (nb_fonc() == 0)
			{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('fonc_lst.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de cr&eacute;er au moins une fonction...</p>
    <?PHP
			}
		
			if (nb_age_actives() == 0)
			{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('age_lst.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de cr&eacute;er au moins une agence...</p>
<?PHP
			}
		}
		else
		{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('index.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Logiciel pas encore param&eacute;tr&eacute;<br>Merci de contacter l'administrateur...</p>
<?PHP
		}
	}
}
else
{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('index.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Probl&ecirc;me sur les param&egrave;tres...</p>
<?PHP
}
include_once("pied.php");
?>
