<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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://smi.no-ip.org
 ****************************************************************************************/


// 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_cli.inc.php");
include_once("../inc/fic_dos.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_loc.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_tra.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../classes/phpmailer/class.smtp.php");
include_once("../inc/mail.inc.php");
include_once("../inc/lst.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);
$_POST = clean_str($_POST);

$pbm_cli = FALSE;
$form_ok = FALSE;
$totalccli = 0;
$td = '';
$codecli = '';
$idccli = 0;
$action_to_do = '';
$retourliste = FALSE;

if (isset($_POST['nbutton']))
{
    if (trim(@$_POST["nbutton"]) == 'enr') // On a cliqué sur valider
        $action_to_do = 'valid';
    if (trim(@$_POST["nbutton"]) == 'del') // On a cliqué sur supprimer
        $action_to_do = 'delete';
}

if (isset($_GET['td']))
    $td = @$_GET['td'];
else {
    if ($action_to_do <> '')
        $td = @$_POST['td'];
}

if (isset($_GET['cc']))
    $codecli = @$_GET['cc'];
else {
    if ($action_to_do <> '')
        $codecli = @$_POST['cc'];
}

if (isset($_GET['icc']))
    $idccli = intval(@$_GET['icc']);
else {
    if ($action_to_do <> '')
        $idccli = intval(@$_POST['icc']);
}

$tx = '';
if (isset($_GET['tx']))
    $tx = @$_GET['tx'];
else {
    if (isset($_POST['tx']))
        $tx = @$_POST['tx'];
    else
        $pbm_cli = TRUE;
}

if (isset($_GET['is']))
    $is = @$_GET['is'];
else {
    if (isset($_POST['is']))
        $is = @$_POST['is'];
}
if (($td <> "add") AND (($tx == "int") OR ($tx == "coloc") OR ($tx == "ot")))
    $pbm_cli = TRUE;

if ($codecli <> "") {
    // Contrôle qu'un client n'essaye pas de "hacker" un autre compte
    if (@$_SESSION["niv_log"] == 1) {
        $tab_cli = give_tab_rcli($_SESSION["code_log"], TRUE);
        if (exist_in_array($codecli, $tab_cli) == FALSE)
            $pbm_cli = TRUE;
    }

    if ($pbm_cli == FALSE) {
        $row = lit_enr_cli($codecli, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            $totalccli = nb_ccli_in_cli($codecli);
            if ($totalccli == 0)
                $td = "add";
            $retourliste = FALSE;
            if ($action_to_do <> '') {
                $titre = @$_POST["titre"];
                $idccli = @$_POST["icc"];
                $code = @$_POST["cc"];
                $civil = str_majuscules(@$_POST["civil"]);
                $prenom = ucname(@$_POST["prenom"]);
                $nom = str_majuscules(@$_POST["nom"]);
                $fonction = str_majuscules(@$_POST["fonction"]);
                $adr1 = str_majuscules(@$_POST["adr1"]);
                $adr2 = str_majuscules(@$_POST["adr2"]);
                $pays = str_majuscules(@$_POST["pays"]);
                $dep = str_majuscules(@$_POST["dep"]);
                $ville = str_majuscules(@$_POST["ville"]);
                $telf = @$_POST["telf"];
                $fax = @$_POST["fax"];
                $telp = @$_POST["telp"];
                $email = strtolower(@$_POST["email"]);
                $mess = strtolower(@$_POST["mess"]);
                $nota = @$_POST["nota"];
                $mailc = 0;
                if (strtolower(@$_POST["cpta"]) == "cpta")
                    $mailc = 1;
                $maild = 0;
                if (strtolower(@$_POST["dir"]) == "dir")
                    $maild = 1;
                $mailt = 0;
                if (strtolower(@$_POST["tec"]) == "tec")
                    $mailt = 1;

                $pass = cremdp(FALSE);
                $passcrypt = md5($pass);

                $rech = "";
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

                switch ($td) {
                    case "add" :
                        // données traçabilité
                        $titre_tra = _("Création du contact client");
                        $desc_tra_default = $prenom;
                        $desc_tra_default .= chr(13) . chr(10);
                        $desc_tra_default .= $nom;

                        // On ajoute la fiche
                        $rech = "INSERT INTO `" . TBCCLI . "`";
                        $rech .= " (ccli_datecrea,ccli_datemod,ccli_code,ccli_pass,ccli_codecli,ccli_codefonc,ccli_civilite,ccli_prenom,ccli_nom,ccli_telf,";
                        $rech .= "ccli_adr1,ccli_adr2,ccli_dep,ccli_ville,ccli_codepays,";
                        $rech .= "ccli_fax,ccli_telp,ccli_email,ccli_mess,ccli_nota,ccli_mailc,ccli_maild,ccli_mailt) VALUES";
                        $rech .= " ('$datea','$datea','$code','$passcrypt','$code','$fonction','$civil','" . $db->escape($prenom) . "','" . $db->escape($nom) . "','$telf',";
                        $rech .= " '" . $db->escape($adr1) . "','" . $db->escape($adr2) . "','$dep','$ville','$pays',";
                        $rech .= "'$fax','$telp','$email','$mess','" . $db->escape($nota) . "','$mailc','$maild','$mailt')";
                        break;
                    case "edit" :
                        // On récupère l'enregistrement précédent (traçabilité)
                        $rech = "SELECT * FROM `" . TBCCLI . "` WHERE ccli_id = '$idccli'";
                        $ex_enrccli = $db->get_row($rech, ARRAY_A);

                        if (@$_SESSION["niv_log"] >= 5) {
                            $rech = "UPDATE `" . TBCCLI . "` SET `ccli_datemod`='" . $datea . "', `ccli_codecli`='" . $code . "', `ccli_codefonc`='" . $fonction . "',";
                            $rech .= " `ccli_civilite`='" . $civil . "', `ccli_prenom`='" . $db->escape($prenom) . "', `ccli_nom`='" . $db->escape($nom) . "', `ccli_telf`='" . $telf . "', ";
                            $rech .= " `ccli_adr1`='" . $db->escape($adr1) . "', `ccli_adr2`='" . $db->escape($adr2) . "', `ccli_dep`='" . $dep . "', `ccli_ville`='" . $ville . "', `ccli_codepays`='" . $pays . "', ";
                            $rech .= " `ccli_fax`='" . $fax . "', `ccli_telp`='" . $telp . "', `ccli_email`='" . $email . "', `ccli_mess`='" . $mess . "', ";
                            $rech .= " `ccli_nota`='" . $db->escape($nota) . "', `ccli_maild`='" . $maild . "', `ccli_mailc`='" . $mailc . "', `ccli_mailt`='" . $mailt . "'";
                            $rech .= "  WHERE `ccli_id` = '" . $idccli . "'";
                        } else {
                            $rech = "UPDATE `" . TBCCLI . "` SET `ccli_datemod`='" . $datea . "',";
                            $rech .= " `ccli_codecli`='" . $code . "', `ccli_codefonc`='" . $fonction . "',";
                            $rech .= " `ccli_civilite`='" . $civil . "', `ccli_prenom`='" . $db->escape($prenom) . "', `ccli_nom`='" . $db->escape($nom) . "', `ccli_telf`='" . $telf . "', ";
                            $rech .= " `ccli_adr1`='" . $db->escape($adr1) . "', `ccli_adr2`='" . $db->escape($adr2) . "', `ccli_dep`='" . $dep . "', `ccli_ville`='" . $ville . "', `ccli_codepays`='" . $pays . "', ";
                            $rech .= " `ccli_fax`='" . $fax . "', `ccli_telp`='" . $telp . "', `ccli_email`='" . $email . "', `ccli_mess`='" . $mess . "', ";
                            $rech .= " `ccli_maild`='" . $maild . "', `ccli_mailc`='" . $mailc . "', `ccli_mailt`='" . $mailt . "'";
                            $rech .= "  WHERE `ccli_id` = '" . $idccli . "'";
                        }
                        break;
                    case "del" :
                        $rech = "DELETE FROM `" . TBCCLI . "` WHERE `ccli_id` = '$idccli'";
                        break;
                    default :
                        break;
                }

                if ($rech <> "") {
                    $db->query($rech);

                    switch ($td) {
                        case "add" :
                            // Si en ajout, on doit mettre à jour le code de connnexion
                            $last_id = $db->insert_id;
                            $new_code = $code . "_" . $last_id;
                            $rech = "UPDATE `" . TBCCLI . "` SET `ccli_code`='" . $db->escape($new_code) . "'";
                            $rech .= "  WHERE `ccli_id` = '" . $last_id . "'";
                            $db->query($rech);

                            // Ajout traçabilité
                            $desc_tra = $desc_tra_default;
                            add_trace(2, $code, "CRE", $titre_tra, $desc_tra, FALSE);

                            // Ajout du compte pour connexion
                            // Niveau par défaut : 12
                            // Actif par défaut
                            $log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
                            $rech = "INSERT INTO `" . TBPW . "` (pw_code,pw_pass,pw_level,pw_lastsuccess,pw_lastip,pw_actif) VALUES ('" . $db->escape($new_code) . "','$passcrypt','12','$dateheurea','$log_ip','1')";
                            $db->query($rech);
                            break;
                        case "edit" :
                            // On récupère l'enregistrement actuel (traçabilité)
                            $rech = "SELECT * FROM `" . TBCCLI . "` WHERE ccli_id = '$idccli'";
                            $nouv_enrccli = $db->get_row($rech, ARRAY_A);

                            // On compare les éléments d'enregistrement pour traçabilité
                            $trace_added = analyse_trace("ccli", $ex_enrccli, $nouv_enrccli, '', FALSE);

                            $new_code = $code . "_" . $idccli;
                            // On vérifie si le mot de passe a bien été généré
                            $rech = "SELECT COUNT(*) FROM `" . TBPW . "` WHERE `pw_code` = '" . $db->escape($new_code) . "'";
                            $nbenr = $db->get_var($rech);
                            if ($nbenr == 0) // N'existe pas...on crée
                            {
                                // Mise à jour du compte contact
                                $rech2 = "UPDATE `" . TBCCLI . "` SET `ccli_code`='" . $db->escape($new_code) . "'";
                                $rech2 .= "  WHERE `ccli_id` = '" . $idccli . "'";
                                $db->query($rech2);

                                // Ajout du compte pour connexion
                                // Niveau par défaut : 12
                                // Actif par défaut
                                $log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
                                $rech2 = "INSERT INTO `" . TBPW . "` (pw_code,pw_pass,pw_level,pw_lastsuccess,pw_lastip,pw_actif) VALUES ('" . $db->escape($new_code) . "','$passcrypt','12','$dateheurea','$log_ip','1')";
                                $db->query($rech2);
                            }
                            break;
                        case "del" :
                            // Suppression du mot de passe rattaché
                            $old_code = $code . "_" . $idccli;
                            $rech = "DELETE FROM `" . TBPW . "` WHERE `pw_code` = '" . $db->escape($old_code) . "'";
                            $db->query($rech);
                            break;
                        default :
                            break;
                    }
                    $retourliste = TRUE;
                }
                $db->disconnect();

                switch ($td) {
                    case "add" :
                        $titre = _("CREATION CONTACT");
                        // Envoi d'email vers le client
                        envoi_mail("", _("Ouverture de votre compte client"), "ccli", "cre", "xxx", "ccli", $new_code, $pass, "", TRUE);
                        // Envoi d'email vers le centre de gestion
                        envoi_mail("", _("Création de contact client"), "ccli", "cre", "xxx", "cdg", $new_code, "", "", TRUE);
                        break;
                    case "edit" :
                    case "del" :
                        if ($td == "edit")
                            $titre = _("EDITION CONTACT");
                        else
                            $titre = _("SUPPRESSION CONTACT");
                        break;
                    case "lst" :
                        $titre = _("LISTE CONTACTS");
                        break;
                    default :
                        $pbm_cli = TRUE;
                        break;
                }
            } else {
                switch ($td) {
                    case "add" :
                        $titre = _("CREATION CONTACT");
                        $ccli_adr1 = $cli_adr1;
                        $ccli_adr2 = $cli_adr2;
                        $ccli_dep = $cli_dep;
                        $ccli_ville = $cli_ville;
                        $ccli_codepays = $cli_codepays;
                        $ccli_telf = $cli_telf;
                        $ccli_fax = $cli_fax;
                        break;
                    case "edit" :
                    case "del" :
                        if ($td == "edit")
                            $titre = _("EDITION CONTACT");
                        else
                            $titre = _("SUPPRESSION CONTACT");
                        $row = lit_enr_ccli_id_in_cli($idccli, $codecli);
                        if (is_array($row)) {
                            $row = encode_str($row);
                            extract($row);
                        } else
                            $pbm_cli = TRUE;
                        break;
                    case "lst" :
                        $titre = _("LISTE CONTACTS");
                        break;
                    default :
                        $pbm_cli = TRUE;
                        break;
                }
            }
            // On recalcule le total contacts
            $totalccli = nb_ccli_in_cli($codecli);
            if ($totalccli == 0)
                $td ="add";

        } else
            $pbm_cli = TRUE;
    }
} else
    $pbm_cli = TRUE;
?>
<!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=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Contact Client"); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = ["nom:civil ; format:liste ; req:Y ; txt:<?PHP echo _("Civilité"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:prenom; format:txt ; req:Y ; txt:<?PHP echo _("Prénom"); ?>; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:nom; format:txt ; req:Y ; txt:<?PHP echo _("Nom"); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:fonction ; format:liste ; req:Y ; txt:<?PHP echo _("Fonction"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:adr1 ; format:txt ; req:Y ; txt:<?PHP echo _("Adresse"); ?> ; lmn:1 ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:adr2 ; format:txt ; req:N ; txt:<?PHP echo _("Adresse (2)"); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:ville ; format:liste ; req:Y ; txt:<?PHP echo _("Ville"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:dep ; format:liste ; req:Y ; txt:<?PHP echo _("Département"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:pays ; format:liste ; req:Y ; txt:<?PHP echo _("Pays"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:telf; format:tel ; req:Y ; txt:<?PHP echo _("Téléphone Fixe"); ?>; stok:frmok ; stbad:frmbad",
            "nom:fax; format:tel ; req:N ; txt:<?PHP echo _("Télécopieur"); ?>; stok:frmok ; stbad:frmbad",
            "nom:telp; format:telp ; req:N ; txt:<?PHP echo _("Téléphone portable"); ?>; stok:frmok ; stbad:frmbad",
            "nom:email; format:mail ; req:Y ; txt:<?PHP echo _("Email"); ?>; stok:frmok ; stbad:frmbad",
            "nom:mess; format:mail ; req:N ; txt:<?PHP echo _("Messagerie"); ?> ; stok:frmok ; stbad:frmbad",
            <?PHP if (@$_SESSION["niv_log"] >= 5) { ?>
            "nom:nota; format:txt ; req:N ; txt:<?PHP echo _("Notes"); ?>; stok:frmok ; stbad:frmbad",
            <?PHP } ?>
            "nom:mess; format:mail ; req:N ; txt:<?PHP echo _("Messagerie"); ?> ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <?PHP if ($pbm_cli == FALSE) { ?>
        <script type="text/javascript">
            <!--
            <?PHP
            if ($retourliste == TRUE)
            {
                switch ($tx) {
                    case "coloc" :
                    case "ot" :
                    case "int" :
                    case "loc" :
                        $tab_maitre = give_tab_maitre_cli($codecli,TRUE);
                        cre_sel_ddeurs_cli($codecli, $is, $tab_maitre, 'window.opener.','ddeur');
                        break;
                    case "dosd_ccli" :
                        $tab_maitre = give_tab_maitre_cli($codecli,TRUE);
                        cre_sel_ddeurs_cli($codecli, $is, $tab_maitre, 'window.opener.','ccli');
                        break;
                    case "dosd_cmque" :
                        $tab_maitre = give_tab_maitre_cli($codecli,TRUE);
                        cre_sel_ddeurs_cli($codecli, $is, $tab_maitre, 'window.opener.','cmque');
                        break;
                    default :
                        break;
                }
            }
            ?>
            $(function () {
                $("#nom").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#adr1").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#adr2").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#email").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#mess").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#prenom").Setcase({caseValue: 'allfirst', changeonFocusout: true});
            });

            function init_all() {
                init_Body();
                init_autohref_classe('falselnkimg','');
                <?php
                if ($retourliste == FALSE)
                {
                    switch ($td)
                    {
                        case "add" :
                            echo "init_valid_form('enr','ccli_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                            break;
                        case "edit" :
                            echo "init_valid_form('enr','ccli_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                            break;
                        case "view" :
                            echo 'readonly_all();';
                            break;
                        case "del" :
                            echo "init_valid_form('del','ccli_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                            echo 'readonly_all();';
                            echo 'warn_del();';
                            break;
                    }
                    echo 'init_ajax_ville();';
                    echo 'init_ajax_dep();';
                    echo "chk_contacts('1','ctact','".$totalccli."');";
                    echo "add_func_click('quit','sclose');";
                    echo "init_autohref('lst','click', 'ccli_maj.php?cc=".$codecli."&td=lst&tx=".$tx."',false);";
                    echo "init_autohref('add','click', 'ccli_maj.php?cc=".$codecli."&td=add&tx=".$tx."',false);";
                    echo "Focus_first();";
                }
                else
                {
                    switch ($tx)
                    {
                        case "cli" :
                            echo "self.location.href=('ccli_maj.php?cc=".$codecli."&td=lst&tx=".$tx."');";
                            break;
                        case "coloc" :
                        case "dosd_ccli" :
                        case "dosd_cmque" :
                        case "ot" :
                        case "int" :
                        case "loc" :
                            echo 'update_sel_ddeurs();';
                            echo "self.close();";
                            break;
                        default :
                            break;

                    }
                }
                ?>
            }

            $(document).ready(function () {
                init_all();
            });
            //-->
        </script>

    <?PHP } ?>
</head>
<body>

<?PHP
if ($pbm_cli == FALSE)
{
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p align="center"><span class="titre_gras"><?PHP echo _("Pour le client"); ?> : <?php echo $cli_ste; ?> </span></p>
<table width="890" border="0">
    <tr>
        <td width="33%">&nbsp;</td>
        <td width="33%" align="center" class="annot_petit_it">
            <div id="oblig">(<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                : <?PHP echo _("Saisie obligatoire"); ?>)
            </div>
        </td>
        <td width="33%" align="right">
            <input name="quit" type="button" class="bton_fgris_petit" id="quit" value="<?PHP echo _("Quitter"); ?>">
            <?PHP
            if (($totalccli > 0) AND ($td <> "lst") AND ($tx == "cli"))
                echo '<input name="lst" type="button" class="bton_fgris_petit" id="lst" value="' . _("Revenir à la liste") . '">';
            ?>
        </td>
    </tr>
</table>
<?PHP
if ($retourliste == FALSE)
{
switch ($td)
{
case "add" :
case "edit" :
case "del" :
?>
<form name="ccli_maj" id="ccli_maj" method="post" action="ccli_maj.php">
    <input name="icc" type="hidden" id="icc" value="<?PHP echo $ccli_id ?>">
    <input name="cc" type="hidden" id="cc" value="<?PHP echo $codecli ?>">
    <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
    <input name="tx" type="hidden" id="tx" value="<?PHP echo $tx ?>">
    <input name="is" type="hidden" id="is" value="<?PHP echo $is ?>">
    <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
    <input name="nbutton" type="hidden" id="nbutton" value="">
    <table width="890" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_civil" align="right"><?PHP echo _("Civilité"); ?></td>
            <td width="84%">
                <?PHP
                // Création de la liste des civilités
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBCIV . "` ORDER BY civ_desc";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('civil', 'civil', $rows, 'civ_code', 'civ_desc', '', $ccli_civilite, '', _("-- Faites votre choix --"));
                ?>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_prenom" align="right"><?PHP echo _("Prénom"); ?></td>
            <td><input name="prenom" type="text" id="prenom" value="<?PHP echo $ccli_prenom ?>" size="50"
                       maxlength="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>"></td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_nom" align="right"><?PHP echo _("Nom"); ?></td>
            <td><input name="nom" type="text" id="nom" value="<?PHP echo $ccli_nom ?>" size="50" maxlength="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>"></td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_func" align="right"><?PHP echo _("Fonction"); ?></td>
            <td width="84%">
                <?PHP
                // création de la liste des fonctions
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBFONC . "` ORDER BY fonc_desc";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('fonction', 'fonction', $rows, 'fonc_code', 'fonc_desc', '', $ccli_codefonc, '', _("-- Faites votre choix --"));
                ?>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_adr1" align="right"><?PHP echo _("Adresse"); ?></td>
            <td>
                <input name="adr1" type="text" id="adr1" value="<?PHP echo $ccli_adr1; ?>" size="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_adr2" align="right"><?PHP echo _("Adresse (2)"); ?></td>
            <td>
                <input name="adr2" type="text" id="adr2" value="<?PHP echo $ccli_adr2; ?>" size="50">
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_pays" align="right"><?PHP echo _("Pays"); ?></td>
            <td>
                <?PHP
                // Création de la liste des pays
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBPAYS . "` ORDER BY pays_nom";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('pays', 'pays', $rows, 'pays_code', 'pays_nom', '', $ccli_codepays, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                ?>
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_dep" align="right"><?PHP echo _("Département"); ?></td>
            <td>
                <?PHP
                // Création de la liste des départements
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBDEP . "` WHERE `dep_codepays`='" . $ccli_codepays . "' ORDER BY dep_code";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('dep', 'dep', $rows, 'dep_code', 'dep_code', 'dep_nom', $ccli_dep, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                ?>
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_ville" align="right"><?PHP echo _("Ville"); ?></td>
            <td>
                <?PHP
                // Création de la liste des villes
                $rech = "";
                if ($ccli_dep <> "") {
                    if ($ccli_dep <> "-1") {
                        $filtre = $ccli_dep;
                        if (($filtre == "2A") OR ($filtre == "2B"))
                            $filtre = "20";
                        $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_dep` = '" . str_majuscules($filtre) . "' AND `villes_codepays`='" . str_majuscules($ccli_codepays) . "' ORDER BY villes_nom";
                    } else {
                        $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_codepays`='" . str_majuscules($ccli_codepays) . "' ORDER BY villes_nom";
                    }
                }
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                if ($rech <> "")
                    $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('ville', 'ville', $rows, 'villes_id', 'villes_nom', 'villes_cp', $ccli_ville, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                ?>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_telf" align="right"><?PHP echo _("Tél. Fixe"); ?></td>
            <td><input name="telf" type="text" id="telf" value="<?PHP echo $ccli_telf ?>" size="20" maxlength="20">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>"></td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_fax" align="right"><?PHP echo _("Télécopieur"); ?></td>
            <td><input name="fax" type="text" id="fax" value="<?PHP echo $ccli_fax ?>" size="20" maxlength="20"></td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_telp" align="right"><?PHP echo _("Téléphone portable"); ?></td>
            <td><input name="telp" type="text" id="telp" value="<?PHP echo $ccli_telp ?>" size="20" maxlength="20">
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_email" align="right"><?PHP echo _("Email"); ?></td>
            <td><input name="email" type="text" id="email" value="<?PHP echo $ccli_email ?>" size="50" maxlength="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                <?PHP if ($td == "edit") { ?>
                <a href="mailto:<?PHP echo $ccli_email ?>"><img src="../img/mail.png"
                                                                title="<?PHP echo _("Envoyer e-mail"); ?>" border="0"
                                                                align="absmiddle"></a></td>
            <?PHP } ?>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_mess" align="right"><?PHP echo _("Messagerie"); ?></td>
            <td><input name="mess" type="text" id="mess" value="<?PHP echo $ccli_mess ?>" size="50" maxlength="50"></td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_mess" align="right"><?PHP echo _("Accepte les emails"); ?>...</td>
            <td>
                <?PHP
                foreach ($typmail_client as $key => $value) {
                    echo '<input type="checkbox"';
                    echo ' name="' . $key . '" id="' . $key . '"';
                    echo ' style="background-color : #FFFFFF;" value="' . $key . '"';
                    switch ($key) {
                        // Comptabilité
                        case "cpta" :
                            if (intval($ccli_mailc) == 1)
                                echo ' checked';
                            break;
                        // Direction
                        case "dir" :
                            if (intval($ccli_maild) == 1)
                                echo ' checked';
                            break;
                        // Technique
                        case "tec" :
                            if (intval($ccli_mailt) == 1)
                                echo ' checked';
                            break;
                    }
                    echo '>';
                    echo $value . ' ';
                }
                ?>
                <div class="annot_std_it">
                    (<?PHP echo _("Vous pouvez choisir plusieurs possibilités pour recevoir les emails...ou aucune"); ?>
                    )
                </div>
            </td>
        </tr>
        <?PHP if (@$_SESSION["niv_log"] >= 5) {
            ?>
            <tr>
                <td width="160" class="rubfrm" id="f_nota" align="right"><?PHP echo _("Notes"); ?></td>
                <td><textarea name="nota" id="nota"><?PHP echo $ccli_nota ?></textarea></td>
            </tr>
        <?PHP } ?>
    </table>
    <p align="center">
        <?PHP
        switch ($td)
        {
            case "add" :
            case "edit" :
                echo '<input name="enr" type="button" class="bton_std" id="enr" value="'._("Valider").'">';
                break;
            case "del" :
                echo '<input name="del" type="button" class="bton_std" id="del" value="'._("Supprimer").'">';
                break;
        }
        echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';

        if ($td == "edit")
        {
            echo '<br />';
            echo '<br />';
            echo '<input name="add" type="button" class="bton_std" id="add" value="'. _("Saisir un nouveau contact").'">';
        }
        ?>
    </p>
</form>
<?PHP
break;
case "lst" :
    // création de la liste des contacts clients
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBCCLI . "` WHERE `ccli_codecli` = '" . $db->escape($codecli) . "' ORDER BY 'ccli_nom'";

    $rows = $db->get_results($rech, ARRAY_A);

    if (($rows) AND (count($rows) <> 0)) {
        echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";

        // création de l'entête de la table contacts clients
        echo "<th>";
        echo _("Nom, Prénom");
        echo "</th>\n";
        echo "<th>";
        echo _("Fonction");
        echo "</th>\n";
        echo "<th>";
        echo _("Téléphone");
        echo "</th>\n";
        echo "<th>";
        echo _("Email");
        echo "</th>\n";
        echo "<th>";
        echo _("Choisir");
        echo "</th>\n";

        $j = 0;

        foreach ($rows as $row) {
            $row = encode_str($row);
            // création de l'entête de ligne (TR)
            creetrlstdet($j);

            echo "<td>\n";
            echo $row["ccli_nom"] . " " . $row["ccli_prenom"];
            echo "</td>\n";
            echo "<td>\n";
            $txtfonc = "&nbsp;";
            $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $row["ccli_codefonc"] . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2)
                $txtfonc = $row2["fonc_desc"];
            echo $txtfonc;
            echo "</td>\n";
            echo "<td align=\"center\">\n";
            echo $row["ccli_telf"];
            echo "</td>\n";
            echo "<td align=\"center\">\n";
            echo "<a href=\"mailto:" . $row["ccli_email"] . "\">" . $row["ccli_email"] . "</a>";
            echo "</td>\n";

            echo "<td align=\"center\">\n";
            $lst_maj = TRUE;
            cre_falselnk_img('', 'ccli_edit_'.$row["ccli_id"], 'span', 'center', _("Edition"), 'lnk', 'ccli_maj.php?td=edit&cc=' . $row["ccli_codecli"] . '&icc=' . $row["ccli_id"] . '&tx=' . $tx , '../img/edit.png');
            $lst_del = TRUE;
            cre_falselnk_img('', 'ccli_del_'.$row["ccli_id"], 'span', 'center', _("Suppression"), 'lnk', 'ccli_maj.php?td=del&cc=' . $row["ccli_codecli"] . '&icc=' . $row["ccli_id"] . '&tx=' . $tx , '../img/del.png');
            echo "</td>\n";
            echo "</tr>\n";

            $j++;
        }

        echo "</table>\n";
        // Fin affichage table liste contacts clients
        $taille_piedla = "";
        include_once("piedla.inc.php");
        ?>
        <p align="center">
            <input name="add" type="button" class="bton_std" id="add"
                   value="<?PHP echo _("Saisir un nouveau contact"); ?>">
        </p>
    <?PHP
    }
    $db->disconnect();
    break;

default :
    break;
}
}
/*
else
{
	switch ($tx)
	{
		case "cli" :
			pop_ret_auto(_("Fiche enregistrée"),'','ccli_maj.php?cc='.$codecli.'&td=lst&tx='.$tx);
			break;
		default :
			break;
	}
}
*/
}
    /*
else
    close_fen_auto('', 'fscomp');
    */
?>
</body>
</html>
