<?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_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_loc.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_prm.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_tac.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_zc.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.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("../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);

$td = $_GET['td']; // Récupération type demande
$tc = intval($_GET['tc']); // Récupération type client
$co = $_GET['co']; // Récupération code Contrat
$cc = $_GET['cc']; // Récupération code Client
$ca = $_GET['ca']; // Récupération code Agence

$nbcontrats = 0;
$pbm_coloc = FALSE;
$coloc_idsite = 0;
$nbpdtloc = 0;
$chx_site = TRUE;
$select_ctact = TRUE;
$select_age = TRUE;
$add_ctact = TRUE;
$typ_ctact = '';

if (($tc < 0) OR ($tc > 4))
    $pbm_coloc = TRUE;

$codeadev = give_dev_codea_defaut($_SESSION["lng"], TRUE);

if ($pbm_coloc == FALSE) {
    // Niveau <=2 ne peut que voir les contrats
    if (@$_SESSION["niv_log"] <= 2)
        $td = "view";

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

    if ($co <> "") {
        // Récupération des informations contrat client
        $row = lit_enr_coloc($co);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            if ($coloc_codecli <> $cc)
                $pbm_coloc = TRUE;

            if ($pbm_coloc == FALSE)
            {
                $nbpdtloc = nb_pdtloc_in_coloc($co);

                if (trim($cocl_codeadev) == '')
                    $codeadev = give_dev_codea_defaut_coloc($co, $_SESSION["lng"]);

                // Contrôle si contrat soldé (si tous les produits sont revenus, on force la clôture)
                if (datetolocal($coloc_datefin) == "") {
                    if (all_pdtloc_ret_in_coloc($co, TRUE) == TRUE) {
                        $coloc_dateclos = lastdate_ret_in_coloc($co, TRUE);
                        clot_coloc($co, $coloc_dateclos, TRUE);
                    }
                }

                // Si contrat terminé, force le mode "view"
                if (datetolocal($coloc_dateclos) <> "")
                    $td = "view";

                if ($td <> "add") {
                    // Contrôle qu'on n'essaye pas de "hacker" un contrat
                    switch (@$_SESSION["niv_log"]) {
                        case 1 :
                            if ($coloc_codecli <> $_SESSION["code_log"])
                                $pbm_coloc = TRUE;
                            break;
                        case 3 :
                            if ($coloc_codeage <> $_SESSION["code_age"])
                                $pbm_coloc = TRUE;
                            break;
                        case 4 :
                            if ($coloc_codeage <> $_SESSION["code_log"])
                                $pbm_coloc = TRUE;
                            break;
                        default :
                            break;
                    }
                }

                if ($pbm_coloc == FALSE) {
                    $coloc_datecreaf = datetolocal($coloc_datecrea);
                    $coloc_datedebf = datetolocal($coloc_datedeb);
                    $coloc_datefinf = datetolocal($coloc_datefin);
                    $coloc_dateclosf = datetolocal($coloc_dateclos);

                    if ($td == "edit") {
                        $cc = $coloc_codecli;
                        $ca = $coloc_codeage;
                    }

                    if ($td == "view") {
                        $cc = $coloc_codecli;
                        $ca = $coloc_codeage;
                    }

                    if ($td == "add") {
                        $coloc_datecreaf = $datef;
                        $coloc_datedebf = $datef;
                        $coloc_datefinf = "";
                        $coloc_dateclosf = "";
                        $coloc_code = $co;
                        $coloc_codecli = $cc;
                        $coloc_codeage = $ca;
                        $coloc_sitep = 1;
                    }
                }
            }
        }
    } else {
        if ($td == "add") {
            $coloc_datecreaf = $datef;
            $coloc_datedebf = $datef;
            $coloc_datefinf = "";
            $coloc_dateclosf = "";
            $coloc_code = crecodesession("CO");
            $co = $coloc_code;
            $coloc_sitep = 1;
        } else
            $pbm_coloc = TRUE;
    }


    $pbm_cli = FALSE;

    if ($cc <> "") {
        switch ($tc) {
            case 0 :
                $row = lit_enr_cli($cc, TRUE);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);

                    // Contrôle qu'on n'essaye pas de "hacker" un client
                    switch (@$_SESSION["niv_log"]) {
                        case 1 :
                            $tab_cli = give_tab_rcli($_SESSION["code_log"], TRUE);
                            if (exist_in_array($cc, $tab_cli) == FALSE)
                                $pbm_cli = TRUE;
                            $select_age = FALSE;
                            break;
                        case 3 :
                        case 4 :
                            if (client_age_ok($cli_code, @$_SESSION["code_age"], TRUE) == FALSE)
                                $pbm_cli = TRUE;
                            // On force l'agence
                            $select_age = FALSE;
                            $coloc_codeage = @$_SESSION["code_age"];
                            break;
                        default :
                            // Si agence propriétaire, on ne peut pas choisir l'agence
                            if (trim($cli_prop) <> NULL) {
                                $select_age = FALSE;
                                $coloc_codeage = $cli_prop;
                            }
                            break;
                    }

                    if ($pbm_cli == FALSE)
                    {
                        // Si pas de société, pas de demandé par
                        if (trim($cli_ste) == "") {
                            $chx_site = FALSE;
                            $select_ctact = FALSE;
                            $add_ctact = FALSE;
                            $coloc_idsite = 0;
                        } else // On récupère le code maître (si le client fait partie d'un regroupement)
                            $tab_maitre = give_tab_maitre_cli($cli_code, TRUE);
                        $tab_ddeur = give_tab_ccli($cli_code, intval($coloc_idsite), $tab_maitre);
                        $typ_ctact = 'cli';

                        $coloc_codecli = $cc;
                    }
                } else
                    $pbm_cli = TRUE;
                break;
            case 1 :
                $row = lit_enr_age($cc, TRUE);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                    $coloc_codecli = $cc;
                    $tab_ddeur = give_tab_cage($age_code);
                    $typ_ctact = 'age';
                } else
                    $pbm_cli = TRUE;
                break;
            case 2 :
            case 4 :
                $row = lit_enr_tec($cc, TRUE);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                    $coloc_codecli = $cc;
                } else
                    $pbm_cli = TRUE;
                break;
            case 3 :
                $row = lit_enr_st($cc, TRUE);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);

                    // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
                    switch (@$_SESSION["niv_log"]) {
                        case 3 :
                        case 4 :
                            // On force l'agence
                            $select_age = FALSE;
                            $coloc_codeage = @$_SESSION["code_age"];
                            break;
                        default :
                            break;
                    }

                    $coloc_codecli = $cc;
                    $tab_ddeur = give_tab_cst($st_code);
                    $typ_ctact = 'st';
                } else
                    $pbm_cli = TRUE;
                break;
            default :
                $pbm_cli = TRUE;
                break;
        }
    }
}

switch ($td) {
    case "add" :
        $titre = _("CREATION CONTRAT LOCATION");
        break;
    case "edit" :
        $titre = _("MISE A JOUR CONTRAT LOCATION");
        break;
    case "del" :
        $titre = _("SUPPRESSION CONTRAT LOCATION");
        break;
    case "view" :
        $titre = _("VISUALISATION CONTRAT LOCATION");
        break;
    default :
        $pbm_coloc = 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=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Fiche Contrat Location"); ?></title>

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

    <?PHP
    if (($td == "add") OR ($td == "edit")) {
        ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:datedebf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date début"); ?> ; vmn:<?PHP echo $coloc_datecreaf; ?> ; stok:frmok ; stbad:frmbad",
                "nom:datefinf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date fin"); ?> ; vmn:<?PHP echo $coloc_datecreaf; ?> ; stok:frmok ; stbad:frmbad",
                "nom:codeage ; format:liste ; req:Y ; txt:<?PHP echo _("Agence"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:ddeur ; format:liste ; req:<?PHP if (($tc==0) OR ($tc==1) OR ($tc==3)) echo "Y"; else echo "N"; ?> ; txt:<?PHP echo _("Contact client"); ?> ; <?PHP if (($tc==0) OR ($tc==1) OR ($tc==3)) echo "vmn:1;"; ?> stok:frmok ; stbad:frmbad"];
            //-->
        </script>
    <?PHP
    }
    ?>

    <script type="text/javascript">
        <!--
        <?PHP cre_html_pdtincloc('pdtincloc', $td, $co, ''); ?>

        function init_datepicker() {
            $.datepicker.setDefaults({
                closeText: "<?PHP echo _("Fermer"); ?>",
                prevText: "<?PHP echo _("Précédent"); ?>",
                nextText: "<?PHP echo _("Suivant"); ?>",
                currentText: "<?PHP echo _("Aujourd'hui"); ?>",
                monthNames: ["<?PHP echo implode('","',$lng_tmoisf); ?>"],
                monthNamesShort: ["<?PHP echo implode('","',$lng_tmoism); ?>"],
                dayNames: ["<?PHP echo implode('","',$lng_tjourf); ?>"],
                dayNamesShort: ["<?PHP echo implode('","',$lng_tjourm); ?>"],
                dayNamesMin: ["<?PHP echo implode('","',$lng_tjours); ?>"],
                weekHeader: "<?PHP echo _("Sem."); ?>",
                dateFormat: "<?PHP echo $_SESSION['date_fmtshort']; ?>",
                firstDay: 1,
                isRTL: !1,
                changeMonth: true,
                changeYear: true,
                showMonthAfterYear: !1,
                yearSuffix: "",
                showButtonPanel: true,
                showOn: "both",
                buttonImageOnly: true,
                buttonImage: "../img/calendar.png",
                buttonText: "<?PHP echo _("Calendrier"); ?>"
            });
            <?PHP if ($td == "add") { ?>
            $("#datecreaf").datepicker({
                onSelect: function (datesel) {
                    $("#datecreaf").blur();
                    return chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, '', '', 'datedebf,datefinf',false);
                }
            });
            $("#datedebf").datepicker({
                onSelect: function (datesel) {
                    $("#datedebf").blur();
                    return chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, 'datecreaf', '', 'datefinf',false);
                }
            });
            $("#datefinf").datepicker({
                onSelect: function (datesel) {
                    $("#datefinf").blur();
                    return chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, 'datecreaf,datedebf', '', '',false);
                }
            });
            <?PHP } ?>
        }

        function init_all() {
            init_Body();
            init_datepicker();
            init_autohref('tac', 'change','',false);
            pdtincloc();
            <?PHP
            switch ($td)
            {
                case "view" :
                    if (@$_SESSION["niv_log"] >= 3)
                    {
                        if ($tc == 0)
                            echo 'warn_cpta('.$cli_cpta.',"coloc");';
                    }
                    if ($_SESSION["niv_log"]>2)
                        echo "init_new_fen('etats','impression');";
                    echo "readonly_all();";
                    break;
                case "del":
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
                case "edit" :
                    if (@$_SESSION["niv_log"] >= 3)
                    {
                        if ($tc == 0)
                            echo 'warn_cpta('.$cli_cpta.',"coloc");';
                    }
                    if (@$_SESSION["niv_log"] <> 1)
                        echo "init_fensaisie('chx_pdt',900,500);";
                    if ($_SESSION["niv_log"]>2)
                        echo "init_new_fen('etats','impression');";
                    if ($add_ctact == TRUE)
                        echo "init_fensaisie('addccli',1024,600);";
                    if ($nbpdtloc == 0)
                        echo "readonly_id('enr-enrimp',false);";
                    echo "readonly_id('codeage',false);";
                    echo "init_valid_form('enr','coloc_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "add" :
                    if ($select_ctact == FALSE)
                        echo "readonly_id('ddeur',false);";
                    if ($select_age == FALSE)
                        echo "readonly_id('codeage',false);";
                    if ($add_ctact == TRUE)
                        echo "init_fensaisie('addccli',1024,600);";
                    echo "init_warn('".nb_age_actives()."age','','back','','');";
                    if (@$_SESSION["niv_log"] >= 3)
                    {
                        if ($tc == 0)
                            echo 'warn_cpta('.$cli_cpta.',"coloc");';
                    }
                    echo "init_valid_form('enr','coloc_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                default :
                    break;
            }
            if (@$_SESSION["niv_log"] >= 3)
                echo "init_fensaisie('notes_cli',930,380);";
            ?>
            Focus_first();
        }

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

</head>

<body>

<?PHP include_once("../inc/entete.inc.php"); ?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<!-- Affiché uniquement s'il y a un code client -->
<?PHP
if (($pbm_coloc == FALSE) AND ($pbm_cli == FALSE)) {
    if ($cc <> "") {
        if (nb_pdtloc() > 0) {
            ?>
            <table width="100%" border="0">
                <tr>
                    <td width="33%"
                        class="annot_gras_it"><?PHP aff_lnk_lst_rech('coloc_lst.php?ord=DES&tri=Code', _("Retour à la liste"), $_SERVER['HTTP_REFERER'], _("Retour à la page précédente")); ?></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">&nbsp;</td>
                </tr>
                <tr>
                <td width="33%">&nbsp;</td>
                <td width="33%" valign="top">&nbsp;</td>
                <td width="33%" align="right">
                    <?PHP
                    if ((($td == "edit") OR ($td == "view")) AND ($_SESSION["niv_log"] <> 1)) {
                        if ($nbpdtloc > 0) // On imprime que s'il y a des articles dans la location
                        {
                            echo '<span id="affsel" style="visibility:visible">';
                            echo '<select name="etats" size="1" id="etats" class="annot_petit">';
                            echo '<option value="" selected>&#187; '._("Etats").' &#171;</option>';
                            if (@$_SESSION["niv_log"] <> 2) {
                                $urlimp = "coloc_pdf.php?cl=" . $coloc_code;
                                if (url_ok($urlimp, TRUE) == TRUE)
                                    echo '<option value="' . $urlimp . '"  class="icone" style="background-image: url(../img/sel_coloc.gif);">' . _("Contrat") . '</option>';
                            }
                            echo '</select>';
                            echo '</span>';
                        } else
                            echo '&nbsp;';
                    } else
                        echo '&nbsp;';
                    ?>
                </td>
                </tr>
            </table>

            <form name="coloc_maj" id="coloc_maj" method="post" action="coloc_enr.php">
            <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
            <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
            <input name="tc" type="hidden" id="tc" value="<?PHP echo $tc ?>">
            <input name="codecli" type="hidden" id="codecli" value="<?PHP echo $coloc_codecli ?>">
            <input name="prev" type="hidden" id="prev" value="<?PHP echo $cli_prev ?>">
            <input name="idsite" type="hidden" id="idsite" value="<?PHP echo $coloc_idsite ?>">
            <input name="dateclosf" type="hidden" id="dateclosf" value="<?PHP echo datetolocal($coloc_dateclos); ?>">
            <input name="nbutton" type="hidden" id="nbutton" value="">
            <?PHP if ($td == "add") { ?>
                <input name="code" type="hidden" id="code" value="<?PHP echo $coloc_code ?>">
                <input name="num" type="hidden" id="num" value="">
            <?PHP } ?>

            <!-- Code du contrat client (affiché uniquement si édition ou suppression) -->
            <?PHP if ($td <> "add") { ?>
                <table width="100%" border="0">
                    <tr>
                        <td width="250" class="rubfrm" id="f_codecocl" align="right"><?PHP echo _("Code"); ?></td>
                        <td>
                            <input name="code" type="text" id="code" value="<?PHP echo $coloc_code ?>" size="20"
                                   maxlength="20" readonly>
                            <?PHP
                            if ($coloc_dateclosf <> "") {
                                echo '<span class="alerte1">';
                                echo _("Terminé");
                                echo '</span>';
                            }
                            ?>
                        </td>
                        <td align="right">
                            <?PHP
                            if (((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($td == "edit")) {
                                echo '<span id="strtac" style="visibility:visible">';
                                $nb_tac = nb_tac(3, $coloc_code, TRUE);
                                if ($nb_tac > 0) {
                                    echo "<span class=\"annot_petit_it\">";
                                    echo "( " . $nb_tac . " ";
                                    if ($nb_tac > 1)
                                        echo _("tâches");
                                    else
                                        echo _("tâche");
                                    $nb_tac_actives = nb_tac_actives(3, $coloc_code, TRUE);
                                    if ($nb_tac_actives > 0)
                                        echo ", " . _("dont") . " " . $nb_tac_actives . " ";
                                    if ($nb_tac_actives > 1)
                                        echo _("non terminées");
                                    else
                                        echo _("non terminée");
                                    echo ")";
                                    echo "</span>";
                                } else {
                                    echo "<span class=\"annot_petit_gras\">";
                                    echo _("Tâches") . " : ";
                                    echo "</span>";
                                }
                                echo '</span>';

                                $tab_tac = [];
                                if ($nb_tac > 0) {
                                    $url = 'tac_lst.php?ord=DES&tri=Code&fi=5&search=' . $coloc_code;
                                    if (url_ok($url, TRUE) == TRUE)
                                        $tab_tac[$url] = _("Voir");
                                }
                                $url = 'tac_maj.php?td=add&tr=3&cr=' . $coloc_code;
                                if (url_ok($url, TRUE) == TRUE)
                                    $tab_tac[$url] = _("Ajouter");
                                cre_select('tac', 'tac', $tab_tac, '', '', _("-- Faites votre choix --"), 1, FALSE, '', '', '', TRUE);
                            } else
                                echo "&nbsp;";
                            ?>
                        </td>
                    </tr>
                </table>
            <?PHP } ?>

            <!-- Date de création -->
            <table width="100%" border="0">
                <tr>
                    <td width="250" class="rubfrm" id="f_datecreaf"
                        align="right"><?PHP echo _("Date Création"); ?> </td>
                    <td>
                        <input name="datecreaf" type="text" id="datecreaf" value="<?PHP echo $coloc_datecreaf ?>" size="10" maxlength="10" readonly>
                        <?PHP if ($td == "add")
                            echo ' <img src="../img/oblig.png" align="absmiddle" alt="'._("Champ obligatoire").'">';
                        ?>

                </td>
                </tr>
            </table>

            <?PHP if ($td <> "add") {
                $tab_pn = prenom_nom_compte($coloc_codecrea, TRUE);
                ?>
                <!-- créé par...-->
                <table width="100%" border="0">
                    <tr>
                        <td width="250" class="rubfrm" id="f_codecrea" align="right"><?PHP echo _("Créé par"); ?></td>
                        <td>
                            <input name="codecrea" type="text" id="codecrea"
                                   value="<?PHP echo $tab_pn["prenom"] . " " . $tab_pn["nom"] . " (" . $tab_niv_full[$tab_pn["level"]] . ")" ?>"
                                   size="100" readonly="1">
                        </td>
                    </tr>
                </table>
            <?PHP } ?>
            <!-- Date de début du contrat -->
            <table width="100%" border="0">
                <tr>
                    <td width="250" class="rubfrm" id="f_datefinf" align="right"><?PHP echo _("Début le"); ?></td>
                    <td>
                        <input name="datedebf" type="text" id="datedebf" value="<?PHP echo $coloc_datedebf ?>" size="10" maxlength="10" readonly>
                        <?PHP if ($td == "add")
                        {
                            echo '<span id="affchxdd" style="visibility:visible">';
		                    echo' <img src="../img/oblig.png" align="absmiddle" alt="'._("Champ obligatoire").'">';
		                    echo '<br />';
                            echo '<span class="annot_petit_it">('._("Au moins date de création").'</span>';
		                    echo '</span>';
                        }
                        ?>
                    </td>
                </tr>
            </table>

            <!-- Date de fin du contrat -->
            <table width="100%" border="0">
                <tr>
                    <td width="250" class="rubfrm" id="f_datefinf" align="right"><?PHP echo _("Fin prévue le"); ?></td>
                    <td>
                        <input name="datefinf" type="text" id="datefinf" value="<?PHP echo $coloc_datefinf ?>" size="10" maxlength="10" readonly>
                        <?PHP if ($td == "add")
                        {
                            echo '<span id="affchxdf" style="visibility:visible">';
		                    echo ' <img src="../img/oblig.png" align="absmiddle" alt="'._("Champ obligatoire").'">';
		                    echo '<br />';
                            echo '<span class="annot_petit_it">('._("Au moins date de début").')</span>';
		                    echo '</span>';
                        }
                        ?>
                    </td>
                </tr>
            </table>
            <!-- Affichage du client -->
            <table width="100%" border="0">
                <tr>
                    <td class="rubfrm" id="f_codecli" align="center">
                        <?PHP
                        switch ($tc) {
                            case 0 :
                                echo "Client";
                                if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
                                    echo " <img src=\"../img/" . $ico_cpta[$cli_cpta] . "\" title =\"" . $mess_cpta[$cli_cpta] . "\" border=\"0\" align=\"absmiddle\">";
                                break;
                            case 1 :
                                echo _("Agence");
                                break;
                            case 2 :
                                echo _("Technicien agence");
                                break;
                            case 3 :
                                echo _("Sous-traitant");
                                break;
                            case 4 :
                                echo _("Technicien sous-traitant");
                                break;
                            default :
                                echo "&nbsp;";
                                break;
                        }
                        ?>
                    </td>
                </tr>
            </table>

            <!-- détails client -->
            <table width="100%" border="0">
                <tr>
                    <td class="rubfrm" width="45%" align="center" valign="bottom" class="ent_clisite">
                    <?PHP
                    switch ($tc) {
                        case 0 :
                            echo _("Compte client");
                            break;
                        case 1 :
                            echo _("Compte agence");
                            break;
                        case 2 :
                        case 4 :
                            echo _("Compte technicien");
                            break;
                        case 3 :
                            echo _("Compte sous-traitant");
                            break;
                        default :
                            echo "&nbsp;";
                            break;
                    }
                    ?>
                    </td>
                    <?PHP
                    $tab_td = '<td align="center" valign="bottom" class="ent_clisite"';
                    switch ($tc) {
                        case 0 :
                            if (($td == "add") AND ($cli_ste <> "") AND (@$_SESSION["niv_log"] <> 2)) {
                                if (nb_scli_actifs($coloc_codecli, "", TRUE) > 0) {
                                    $tab_td .= ' onclick="javascript:; fen_sites_coloc(document.coloc_maj.codecli.value,document.coloc_maj.td.value,document.coloc_maj.code.value,document.coloc_maj.idsite.value);"';
                                    $tab_td .= ' onMouseOver="this.style.cursor=\'pointer\'"';
                                }
                            }
                            break;
                        default :
                            break;
                    }
                    $tab_td .= '>';
                    echo $tab_td;
                    switch ($tc) {
                        case 0 :
                            echo _("Site qui loue");
                            if (($td == "add") AND ($cli_ste <> "") AND (@$_SESSION["niv_log"] <> 2)) {
                                if (nb_scli_actifs($coloc_codecli, "", TRUE) > 0) {
                                    echo '<div id="choixsiteoui" style="display:block">';
                                    echo '<div id="choixsitenon" style="display:none">';
                                } else {
                                    echo '<div id="choixsiteoui" style="display:none">';
                                    echo '<div id="choixsitenon" style="display:block">';
                                }
                                echo '<img src="../img/sites.png" title="'.("Choisir un site").'" border="0" align="bottom">';
                                echo '</div>';
                            }
                            break;
                        default :
                            echo "&nbsp;";
                            break;
                    }
                    ?>
                    </td>
                    <td width="30" valign="top">&nbsp;</td>
                </tr>
                <tr>
                    <td width="45%" valign="top" bgcolor="#C5CED7">
                        <div id="nomste">
                        <?PHP
                        switch ($tc) {
                            case 0 :
                                if ($cli_ste <> "")
                                    echo $cli_ste . "<br />";
                                else
                                    echo $cli_civilite . " " . $cli_prenom . " " . $cli_nom . "<br />";
                                echo "</span>";
                                echo $cli_adr1 . "<br />";
                                if ($cli_adr2 <> "")
                                    echo $cli_adr2 . "<br />";
                                echo Retournecp_Id($cli_ville, TRUE) . " " . Retourneville_Id($cli_ville, TRUE) . "<br />";
                                echo _("Tel").' : '. $cli_telf;
                                if ($cli_telp <> '')
                                    echo "  / " . $cli_telp;
                                break;
                            case 1 :
                                echo $age_nom . "<br />";
                                echo "</span>";
                                echo $age_adr1 . "<br />";
                                if ($age_adr2 <> "")
                                    echo $age_adr2 . "<br />";
                                echo Retournecp_Id($age_ville, TRUE) . " " . Retourneville_Id($age_ville, TRUE) . "<br />";
                                echo _("Tel").' : '. $age_telf;
                                if ($age_telp <> '')
                                    echo "  / " . $age_telp;
                                break;
                            case 2 :
                            case 4 :
                                echo $tec_civilite . " " . $tec_prenom . " " . $tec_nom . "<br />";
                                echo "</span>";
                                echo $tec_adr1 . "<br />";
                                if ($tec_adr2 <> "")
                                    echo $tec_adr2 . "<br />";
                                echo Retournecp_Id($tec_ville, TRUE) . " " . Retourneville_Id($tec_ville, TRUE) . "<br />";
                                $chemin_imagesco = $chemin_images . "/telc.png";
                                echo "<img src=\"" . $chemin_imagesco . "\" align=\"bottom\">";
                                echo " " . $tec_telf . "<br />";
                                break;
                            case 3 :
                                echo $st_nom . "<br />";
                                echo "</span>";
                                echo $st_adr1 . "<br />";
                                if ($st_adr2 <> "")
                                    echo $st_adr2 . "<br />";
                                echo Retournecp_Id($st_ville, TRUE) . " " . Retourneville_Id($st_ville, TRUE) . "<br />";
                                echo _("Tel").' : '. $st_telf;
                                if ($st_telp <> '')
                                    echo "  / " . $st_telp;
                                break;
                            default :
                                echo "&nbsp;";
                                break;
                        }
                        ?>
                        </div>
                    </td>
                    <?PHP
                    echo '<td valign="top" bgcolor="#C5CED7"';
                    switch ($tc) {
                        case 0 :
                            if (($td == "add") AND ($cli_ste <> "") AND (@$_SESSION["niv_log"] <> 2)) {
                                if (nb_scli_actifs($coloc_codecli, "", TRUE) > 0) {
                                    echo 'onclick="javascript:; fen_sites_coloc(document.coloc_maj.codecli.value,document.coloc_maj.td.value,document.coloc_maj.code.value,document.coloc_maj.idsite.value);"';
                                    echo 'onMouseOver="this.style.cursor=\'pointer\'"';
                                }
                            }
                            break;
                        default :
                            break;
                    }
                    echo '>';
                    switch ($tc) {
                        case 0 :
                            echo '<div id="nomsite">';
                            echo nom_scli($coloc_idsite, TRUE, FALSE,FALSE);
                            echo '</div>';
                            break;
                        default :
                            echo "&nbsp;";
                            break;
                    }
                    ?>
                    </td>
                    <td width="30" valign="top">
                        <?PHP
                        switch ($tc) {
                            case 0 :
                                if (@$_SESSION["niv_log"] >= 3) {
                                    $url_affnotesc = "aff_notesc.php?cc=" . $cc;
                                    if (url_ok($url_affnotesc, TRUE) == TRUE)
                                        cre_falselnk('', 'notes_cli', 'span', '', _("Notes"), $url_affnotesc, '../img/eye.png', _("Notes"));
                                } else
                                    echo "&nbsp;";
                                break;
                            default :
                                echo "&nbsp;";
                                break;
                        }
                        ?>
                    </td>
                </tr>
            </table>

            <?PHP if (($tc == 0) OR ($tc == 1) OR ($tc == 3)) { ?>
                <!-- Demandeur -->
                <table width="100%" border="0">
                <tr>
                <td width="250" class="rubfrm" id="f_ddeur" align="right">
                    <?PHP
                    switch ($tc) {
                        case 0 :
                            echo _("Contact client");
                            break;
                        case 1 :
                            echo _("Contact agence");
                            break;
                        case 3 :
                            echo _("Contact sous-traitant");
                            break;
                        default :
                            echo "&nbsp;";
                            break;
                    }
                    ?>
                </td>
                <td>
                <?PHP
                if (($td == "add") OR ($td == "edit")) // Ajout ou édition = zone modifiable (on peut choisir un contact)
                {
                    if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
                        cre_select('ddeur', 'ddeur', $tab_ddeur, intval($coloc_ddeur), '', _("-- Faites votre choix --"));
                        if ($add_ctact == TRUE) {
                            switch ($tc) {
                                case 0 :
                                    $url = 'ccli_maj.php?td=add&cc=' . $coloc_codecli . '&tx=coloc';
                                    break;
                                case 1 :
                                    $url = 'cage_maj.php?td=add&ca=' . $coloc_codeage . '&tx=coloc';
                                    break;
                                case 3 :
                                    $url = 'cage_maj.php?td=add&ca=' . $coloc_codeage . '&tx=coloc';
                                    break;
                            }
                            if (url_ok($url, TRUE) == TRUE)
                                cre_falselnk('addccli', 'addccli', 'span', '', _("Ajouter contact"), $url, '../img/ctacts.png', _("Ajouter contact"));
                        }
                    } else {
                        $txt_ccli = _("Pas de contact");
                        switch ($tc) {
                            case 0 :
                                $row = lit_enr_ccli_id_in_cli($coloc_ddeur, $cli_code);
                                if (is_array($row)) {
                                    $row = encode_str($row);
                                    extract($row);
                                    $row2 = lit_enr_fonc($ccli_codefonc, TRUE);
                                    if (is_array($row2)) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                        $txt_fonc = $fonc_desc;
                                    } else
                                        $txt_fonc = $ccli_codefonc;
                                    $txt_ccli = $ccli_nom . " " . $ccli_prenom . " (" . $txt_fonc;
                                    $txt_ccli .= " - " . $ccli_telf . ")";
                                }
                                break;
                            case 1 :
                                $row = lit_enr_cage_id_in_age($coloc_ddeur, $cc);
                                if (is_array($row)) {
                                    $row = encode_str($row);
                                    extract($row);
                                    $row2 = lit_enr_fonc($cage_codefonc, TRUE);
                                    if (is_array($row2)) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                        $txt_fonc = $fonc_desc;
                                    } else
                                        $txt_fonc = $cage_codefonc;
                                    $txt_ccli = $cage_nom . " " . $cage_prenom . " (" . $txt_fonc;
                                    $txt_ccli .= " - " . $cage_telf . ")";
                                }
                                break;
                            case 3 :
                                $row = lit_enr_cst_id_in_st($coloc_ddeur, $cc);
                                if (is_array($row)) {
                                    $row = encode_str($row);
                                    extract($row);
                                    $row2 = lit_enr_fonc($cst_codefonc, TRUE);
                                    if (is_array($row2)) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                        $txt_fonc = $fonc_desc;
                                    } else
                                        $txt_fonc = $cst_codefonc;
                                    $txt_ccli = $cst_nom . " " . $cst_prenom . " (" . $txt_fonc;
                                    $txt_ccli .= " - " . $cst_telf . ")";
                                }
                                break;
                        }
                        ?>
                        <input name="nddeur" type="text" id="nddeur" value="<?PHP echo $txt_ccli; ?>" size="100"
                               maxlength="100" readonly>
                    <?PHP
                    }
                } else // Vue ou suppression = zone non modifiable (affichage seul)
                {
                    $txt_ccli = _("Pas de contact");
                    switch ($tc) {
                        case 0 :
                            $row = lit_enr_ccli_id_in_cli($coloc_ddeur, $cli_code);
                            if (is_array($row)) {
                                $row = encode_str($row);
                                extract($row);
                                $row2 = lit_enr_fonc($ccli_codefonc, TRUE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_fonc = $fonc_desc;
                                } else
                                    $txt_fonc = $ccli_codefonc;
                                $txt_ccli = $ccli_nom . " " . $ccli_prenom . " (" . $txt_fonc;
                                $txt_ccli .= " - " . $ccli_telf . ")";
                            }
                            break;
                        case 1 :
                            $row = lit_enr_cage_id_in_age($coloc_ddeur, $cc);
                            if (is_array($row)) {
                                $row = encode_str($row);
                                extract($row);
                                $row2 = lit_enr_fonc($cage_codefonc, TRUE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_fonc = $fonc_desc;
                                } else
                                    $txt_fonc = $cage_codefonc;
                                $txt_ccli = $cage_nom . " " . $cage_prenom . " (" . $txt_fonc;
                                $txt_ccli .= " - " . $cage_telf . ")";
                            }
                            break;
                        case 3 :
                            $row = lit_enr_cst_id_in_st($coloc_ddeur, $cc);
                            if (is_array($row)) {
                                $row = encode_str($row);
                                extract($row);
                                $row2 = lit_enr_fonc($cst_codefonc, TRUE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_fonc = $fonc_desc;
                                } else
                                    $txt_fonc = $cst_codefonc;
                                $txt_ccli = $cst_nom . " " . $cst_prenom . " (" . $txt_fonc;
                                $txt_ccli .= " - " . $cst_telf . ")";
                            }
                            break;
                    }
                    ?>
                    <input name="nddeur" type="text" id="nddeur" value="<?PHP echo $txt_ccli; ?>" size="100"
                           maxlength="100" readonly>
                <?PHP
                }
                ?>
                <br /><span class="annot_petit_it">(<?PHP echo _("Celui qui prend le matériel"); ?>)</span>
                </td>
                </tr>
                </table>
            <?PHP
            } else
                echo '<input name="ddeur" type="hidden" id="ddeur" value="">';
            ?>

            <!-- Code de l'agence (choisi uniquement si création, affiché uniquement si édition ou suppression) -->
            <table width="100%" border="0">
                <tr>
                    <td width="250" class="rubfrm" id="f_agence" align="right"><?PHP echo _("Agence qui loue"); ?></td>
                    <td>
                        <?PHP
                        switch ($tc) {
                            case 0 :
                                $tab_age = give_tab_age_cli($cli_code);
                                break;
                            case 1 :
                                $tab_age = give_tab_age_cli('',$cc);
                                break;
                            case 2 :
                            case 3 :
                            case 4 :
                                $tab_age = give_tab_age_cli('');
                                break;
                        }
                        cre_select('codeage', 'codeage', $tab_age, $coloc_codeage, '', _("-- Faites votre choix --"), 1, FALSE, '', _("-- Pas d'agence --"));
                        ?>
                    </td>
                </tr>
                <tr>
                    <td width="250" class="rubfrm" id="f_devis" align="right"><?PHP echo _("Devise"); ?></td>
                    <td>
                        <input name="codeadev" type="hidden" id="codeadev" value="<?PHP echo $codeadev; ?>" readonly>
                        <input name="ndevise" type="text" id="ndevise"
                               value="<?PHP echo give_dev_nom($codeadev) . " (" . $codeadev . ")"; ?>" size="60"
                               readonly>
                    </td>
                </tr>
            </table>

            <?PHP if ($td <> "add") { ?>
                <table width="100%" border="0">
                    <tr>
                        <td colspan="2" class="rubfrm">
                            <div align="center"><?PHP echo _("Articles réservés ou loués"); ?></div>
                        </td>
                    </tr>
                </table>
            <?PHP } ?>
            <div id="pdtincloc"></div>

            <!-- Lien d'ajout d'article -->
            <?PHP
            if (($td == "edit") AND (@$_SESSION["niv_log"] <> 1) AND (datetolocal($coloc_dateclos) == "")) {
                $urlchoixart = "pdtloc_lst.php?ta=l&td=" . $td . "&tc=" . $tc . "&cc=" . $coloc_codecli . "&cl=" . $coloc_code . "&ca=" . $coloc_codeage . "&dd=" . $coloc_datedebf . "&df=" . $coloc_datefinf . "&fi=1";

                ?>
                <table border="0" width="100%">
                    <tr>
                        <td width="50%">
                            <?php
                            if (url_ok($urlchoixart, TRUE) == TRUE)
                                cre_falselnk('', 'chx_pdt', 'span', '', _("Ajouter un article"), $urlchoixart, '../img/add.png', _("Ajouter un article"));
                            ?>
                        </td>
                        <td width="50%">&nbsp;

                        </td>
                    </tr>
                </table>
            <?PHP
            }
            ?>

            <p align="center">
                <?PHP
                switch ($td)
                {
                    case "add" :
                        echo '<input name="enr" type="button" class="bton_std" id="enr" class="bton_std" value="'._("Enregistrer").'">';
                        echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" class="bton_std" value="'._("Réinitialiser").'">';
                        break;
                    case "edit" :
                        echo '<input name="enr" type="button" id="enr" class="bton_std" value="'._("Enregistrer").'">';
                        echo '<input name="enrimp" type="button" id="enrimp" class="bton_std" value="'._("Enregistrer & Imprimer").'">';
                        echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
                        echo '<div align="center" class="annot_petit_it">'._("Validation possible si au moins un article choisi").'</div>';
                        break;
                    case "del" :
                        echo '<input name="del" type="submit" class="bton_std" id="del" value="'._("Supprimer").'">';
                        break;
                }
                ?>
            </p>
            </form>
        <?PHP
        } else {
            if (@$_SESSION["niv_log"] == 3)
                pop_ret_auto('', '', "index.php");
            else
                pop_ret_auto(_("Aucun produit à louer"), 'warn', "pdtloc_lst.php?ta=m");
        }
        ?>

        <!-- Fin affiché uniquement s'il y a un code client -->
    <?PHP
    } else
        pop_ret_auto(_("Merci de choisir un client"), 'warn', "cli_lst.php&td=loc");
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "coloc_lst.php?ord=DES&tri=Code");
include_once("pied.php");
?>
</body>
</html>