<html>

<head>
  <meta http-equiv='content-type' content='text/html;charset=utf-8'>
  <title>WebBike V1.0</title>
  <SCRIPT language=JavaScript src='valideformulaire.js' type=text/javascript></SCRIPT>
  <SCRIPT language=JavaScript>
    function return_ok_client(no) {
      if (event.keyCode == 10 || event.keyCode == 13 || event.keyCode == 32) {
        ok_client(no);
      }
    }
  </SCRIPT>
  <link href="style.css" rel="stylesheet" media="screen">
</head>

<body>
  <?php
  require 'database.inc.php';
  //session_start();
  //session_register("lg_user");
  //session_register("lg_password");
  //session_register("lg_admin");
  //session_register("lg_menu1");
  //session_register("lg_menu2");
  //session_register("lg_menu3");
  //session_register("lg_menu4");
  //session_register("lg_menu5");
  function auto_client($Nom, $Prenom, $Mode)
  {
    global $NoVisu, $Type, $Sous_Type;
    //echo "$Nom\n";
    if (empty($Nom) && empty($Prenom))
      $requette = "SELECT * FROM clients ORDER BY Nom, Prenom";
    else if (empty($Nom))
      $requette = "SELECT * FROM clients WHERE " . Compare_Sans_Accents("Prenom", addslashes($Prenom)) . " ORDER BY Nom, Prenom";
    else if (empty($Prenom))
      $requette = "SELECT * FROM clients WHERE " . Compare_Sans_Accents("Nom", $Nom) . " ORDER BY Nom, Prenom";
    //$requette="SELECT * FROM clients WHERE ".Compare_Sans_Accents("Nom",addslashes($Nom))." ORDER BY Nom, Prenom";
    else
      $requette = "SELECT * FROM clients WHERE " . Compare_Sans_Accents("Nom", $Nom) . " AND " . Compare_Sans_Accents("Prenom", $Prenom) . "ORDER BY Nom, Prenom";
    //$requette="SELECT * FROM clients WHERE ".Compare_Sans_Accents("Nom",addslashes($Nom))." AND ".Compare_Sans_Accents("Prenom",addslashes($Prenom))."ORDER BY Nom, Prenom";
    //echo $requette."\n";
    echo "<script language=JavaScript>\n";
    if ($resultat = Connection::executeRawQueryWithfetchAll($requette)) {
      $nb = count($resultat);
      if ($nb == 1) {
        if ($ligne = $resultat[0]) {
          echo "if(confirm('" . addslashes($ligne['Nom']) . " " . addslashes($ligne['Prenom']) . " " . addslashes($ligne['Adresse']) . " " . addslashes($ligne['Localite']) . "'))\n";
          echo "{\n";
          if ($Mode == 5) {
            echo "window.opener.document.info.NoClient2.value = '" . $ligne['NoClient'] . "';\n";
            echo "window.opener.document.info.Nom2.value = '" . addslashes($ligne['Nom']) . "';\n";
            echo "window.opener.document.info.Prenom2.value = '" . addslashes($ligne['Prenom']) . "';\n";
          } else {
            echo "window.opener.document.info.NoClient.value = '" . $ligne['NoClient'] . "';\n";
            echo "window.opener.document.info.Nom.value = '" . addslashes($ligne['Nom']) . "';\n";
            echo "window.opener.document.info.Prenom.value = '" . addslashes($ligne['Prenom']) . "';\n";
          }
          if ($Mode == 1) {
            //echo "window.opener.document.info.cc.value = '".$ligne['CarteCredit']."';\n";
            //echo "window.opener.document.info.ncc.value = '".$ligne['NoCarteCredit']."';\n";
            //echo "window.opener.document.info.ecc1.value = '".substr($ligne['ExpirationCarteCredit'],5,2)."';\n";
            //echo "window.opener.document.info.ecc2.value = '".substr($ligne['ExpirationCarteCredit'],0,4)."';\n";
          }
          if ($Mode == 2) {
            if ($resultat2 = Vehicule::getVehiculeByClient($ligne['NoClient'])) {
              if ($ligne2 = $resultat2[0]) {
                echo "window.opener.document.info.NoChasis.value = '" . $ligne2['NoChasis'] . "';\n";
                echo "window.opener.document.info.Vehicule.value = '" . $ligne2['MarqueModel'] . " " . $ligne2['Model'] . "';\n";
                echo "window.opener.document.info.Km.value = '" . $ligne2['Km'] . "';\n";
                $resultat3 = ChassisModel::getChassisModelByNoChassis($ligne2['NoChasis']);
                //echo "alert('no chassis ".$ligne2["NoChasis"]."');\n";
                echo "window.opener.document.forms['info'].service.options.length = 1;\n";
                echo "window.opener.document.forms['info'].service.selectedIndex = 0;\n";
                if ($ligne3 = $resultat3) {
                  echo "window.opener.document.info.vhc.value = '" . $ligne3['Id'] . "';\n";
                  //echo "alert('vhc ".$ligne3["Model"]."');\n";
                  $resultat4 = ServiceVHC::getServiceVHCByVhc($ligne3["Id"]);
                  $found_next_service = false;
                  $pos = 1;
                  foreach ($resultat4 as $ligne4) {
                    echo "window.opener.add_service_option('" . $ligne4["Id"] . "','Service " . $ligne4["Km"] . "','" . $ligne4["Duree"] . "');\n";
                    if (!$found_next_service) {
                      if ($ligne4["Km"] > $ligne2['Km']) {
                        echo "if(confirm('Service ?'))\n";
                        echo "{\n";
                        $found_next_service = true;
                        echo "window.opener.document.forms['info'].service.selectedIndex=$pos;\n";
                        echo "window.opener.duree_service();\n";
                        echo "}\n";
                      }
                    }
                    $pos++;
                  }
                } else {
                  echo "window.opener.document.info.vhc.value = '0';\n";
                }
                if ($ligne2 = $resultat2[1]) {
                  echo "alert('attention plusieurs vhc');\n";
                }
              }
            }
          }
          if ($Mode == 3) {
            //echo "alert('av');\n";
            //echo "window.opener.document.info.Valider.focus();\n";        	
            //echo "alert('ap');\n";
          }
          if ($Mode == 4) {
            echo "window.opener.document.info.Km.select();\n";
            if ($resultat2 = Vehicule::getVehiculeByClient($ligne['NoClient'])) {
              $first = 1;
              foreach ($resultat2 as $ligne2) {
                if ($first == 0) {
                  echo "else ";
                }
                $first = 0;
                echo "if(confirm('" . $ligne2['NoChasis'] . " " . $ligne2['MarqueModel'] . " " . $ligne2['Model'] . " " . $ligne2['NoPlaque'] . "'))\n";
                echo "{\n";
                echo "window.opener.document.info.NoChasis.value = '" . $ligne2['NoChasis'] . "';\n";
                echo "window.opener.document.info.Marque.value = '" . $ligne2['MarqueModel'] . "';\n";
                echo "window.opener.document.info.Model.value = '" . $ligne2['Model'] . "';\n";
                echo "window.opener.document.info.Km.value = '" . $ligne2['Km'] . "';\n";
                echo "window.opener.document.info.Km.select();\n";
                echo "window.opener.document.info.NoPlaque.value = '" . $ligne2['NoPlaque'] . "';\n";
                echo "}\n";
              }
            }
          }
          if ($Mode < 3) {
            if (!empty($ligne['Natel'])) {
              echo "window.opener.document.info.Telephone.value = '" . $ligne['Natel'] . "';\n";
            } else if (!empty($ligne['TelPriv'])) {
              echo "window.opener.document.info.Telephone.value = '" . $ligne['TelPriv'] . "';\n";
            } else if (!empty($ligne['TelProf'])) {
              echo "window.opener.document.info.Telephone.value = '" . $ligne['TelProf'] . "';\n";
            } else {
              echo "window.opener.document.info.Telephone.value = '';\n";
            }
          }
          echo "window.close();\n";
          echo "}\n";
          echo "else\n";
          echo "{\n";
          echo "if(confirm('Créer nouveau client?'))\n";
          echo "{\n";
          echo "window.resizeTo(800,600);\n";
          echo "}\n";
          echo "else\n";
          echo "{\n";
          echo "window.close();\n";
          echo "}\n";
          echo "}\n";
          echo "</script>\n";
        }
      } else if ($nb == 0) {
        echo "if(confirm('Voulez-vous créer ce client?'))\n";
        echo "{\n";
        echo "window.resizeTo(800,600);\n";
        echo "}\n";
        echo "else\n";
        echo "{\n";
        echo "window.close();\n";
        echo "}\n";
        echo "</script>\n";
        $Type = 3;
        $Sous_Type = 3;
        //Affiche_formulaire(3,NOUVEAU,1,'',0,$Nom,$Prenom,$Mode);
        Affiche_formulaire(3, NOUVEAU, 1, '', $NoVisu, $Nom, $Prenom, $Mode);
      } else {
        echo "window.resizeTo(800,600);\n";
        echo "var tabNoClient=new Array;\n";
        echo "var tabNom=new Array;\n";
        echo "var tabPrenom=new Array;\n";
        echo "var tabNoTel=new Array;\n";
        if ($Mode == 1) {
          //echo "var tabCC=new Array;\n";
          //echo "var tabNCC=new Array;\n";
          //echo "var tabECC=new Array;\n";
        }
        if ($Mode == 2) {
          echo "var tabNC=new Array;\n";
          echo "var tabVhc=new Array;\n";
          echo "var tabKm=new Array;\n";
          echo "var tabMulti=new Array;\n";
          echo "var tabIdVhc=new Array;\n";
          echo "var tabService=new Array;\n";
        }
        if ($Mode == 4) {
          echo "var tabNC=new Array;\n";
          echo "var tabMarque=new Array;\n";
          echo "var tabModel=new Array;\n";
          echo "var tabKm=new Array;\n";
          echo "var tabNoPlaque=new Array;\n";
        }
        echo "function ok_client(pos)\n";
        echo "{\n";
        //echo "var pos=this.document.info.NoClient.selectedIndex;\n";
        if ($Mode == 5) {
          echo "window.opener.document.info.NoClient2.value = tabNoClient[pos];\n";
          echo "window.opener.document.info.Nom2.value = tabNom[pos];\n";
          echo "window.opener.document.info.Prenom2.value = tabPrenom[pos];\n";
        } else {
          echo "window.opener.document.info.NoClient.value = tabNoClient[pos];\n";
          echo "window.opener.document.info.Nom.value = tabNom[pos];\n";
          echo "window.opener.document.info.Prenom.value = tabPrenom[pos];\n";
        }
        if ($Mode < 3) {
          echo "window.opener.document.info.Telephone.value = tabNoTel[pos];\n";
        }
        if ($Mode == 1) {
          //echo "window.opener.document.info.cc.value = tabCC[pos];\n";
          //echo "window.opener.document.info.ncc.value = tabNCC[pos];\n";
          //echo "window.opener.document.info.ecc1.value = tabECC[pos].substr(5,2);\n";
          //echo "window.opener.document.info.ecc2.value = tabECC[pos].substr(0,4);\n";
        }
        if ($Mode == 2) {
          //echo "window.opener.document.info.NoChasis.value = tabNC[pos];\n";
          //echo "window.opener.document.info.Vehicule.value = tabVhc[pos];\n";
          //echo "window.opener.document.info.Km.value = tabKm[pos];\n";
          //echo "if(tabMulti[pos]==1)\n";
          //echo "{\n";
          //echo "alert('attention plusieurs vhc');\n";
          //echo "}\n";
          echo "window.opener.recherche_vhc();\n";
        }
        if ($Mode == 3) {
          //echo "window.opener.document.info.Valider.focus();\n";        	
        }
        if ($Mode == 4) {
          echo "window.opener.document.info.Km.select();\n";
          echo "if(confirm(tabNC[pos]+' '+tabMarque[pos]+' '+tabModel[pos]+' '+tabNoPlaque[pos]))\n";
          echo "{\n";
          echo "window.opener.document.info.NoChasis.value = tabNC[pos];\n";
          echo "window.opener.document.info.Marque.value = tabMarque[pos];\n";
          echo "window.opener.document.info.Model.value = tabModel[pos];\n";
          echo "window.opener.document.info.Km.value = tabKm[pos];\n";
          echo "window.opener.document.info.Km.select();\n";
          echo "window.opener.document.info.NoPlaque.value = tabNoPlaque[pos];\n";
          echo "}\n";
        }
        echo "window.close();\n";
        echo "}\n";
        echo "</script>\n";
        $cpt = 0;
        if ($nb > 10) {
          $nbe = 10;
        } else {
          $nbe = $nb;
        }
        echo "<form name='info'><select name='NoClient' size='$nbe' OnKeyPress='return_ok_client(this.document.info.NoClient.selectedIndex);' OnDblClick='ok_client(this.document.info.NoClient.selectedIndex);'>\n";
        $first = true;
        foreach ($resultat as $ligne) {
          echo "<option value='$cpt'";
          if ($first) {
            $first = false;
            echo " selected";
          }
          echo ">" . $ligne['Nom'] . " " . $ligne['Prenom'] . " " . $ligne['Adresse'] . " " . $ligne['Localite'] . "</option>\n";
          echo "<script language=JavaScript>\n";
          echo "this.document.info.NoClient.focus();\n";
          echo "tabNoClient.push(" . '"' . $ligne['NoClient'] . '"' . ")\n";
          echo "tabNom.push(" . '"' . addslashes($ligne['Nom']) . '"' . ")\n";
          echo "tabPrenom.push(" . '"' . addslashes($ligne['Prenom']) . '"' . ")\n";
          if ($Mode == 1) {
            //echo "tabCC.push(".'"'.$ligne['CarteCredit'].'"'.")\n";
            //echo "tabNCC.push(".'"'.$ligne['NoCarteCredit'].'"'.")\n";
            //echo "tabECC.push(".'"'.$ligne['ExpirationCarteCredit'].'"'.")\n";
          }
          if ($Mode == 2) {
            //$requette2="SELECT * FROM vehicules WHERE Client=".$ligne['NoClient'];
            //if ($resultat2 = mysql_query($requette2))
            //{
            //if($ligne2 = mysql_fetch_array( $resultat2 ))
            //{
            //echo "tabNC.push(".'"'.$ligne2['NoChasis'].'"'.")\n";
            //echo "tabVhc.push(".'"'.$ligne2['MarqueModel']." ".$ligne2['Model'].'"'.")\n";
            //echo "tabKm.push(".'"'.$ligne2['Km'].'"'.")\n";
            //if($ligne2 = mysql_fetch_array( $resultat2 ))
            //{
            //echo "tabMulti.push(".'"1"'.")\n";
            //}
            //else
            //{
            //echo "tabMulti.push(".'"0"'.")\n";
            //}			  
            //}
            //else
            //{
            //echo "tabNC.push(".'""'.")\n";
            //echo "tabVhc.push(".'""'.")\n";
            //echo "tabKm.push(".'""'.")\n";
            //}
            //}
          }
          if ($Mode == 4) {
            if ($resultat2 = Vehicule::getVehiculeByClient($ligne['NoClient'])) {
              if ($ligne2 = $resultat2[0]) {
                echo "tabNC.push(" . '"' . $ligne2['NoChasis'] . '"' . ")\n";
                echo "tabMarque.push(" . '"' . $ligne2['MarqueModel'] . '"' . ")\n";
                echo "tabModel.push(" . '"' . $ligne2['Model'] . '"' . ")\n";
                echo "tabKm.push(" . '"' . $ligne2['Km'] . '"' . ")\n";
                echo "tabNoPlaque.push(" . '"' . $ligne2['NoPlaque'] . '"' . ")\n\n";
              } else {
                echo "tabNC.push(" . '""' . ")\n";
                echo "tabMarque.push(" . '""' . ")\n";
                echo "tabModel.push(" . '""' . ")\n";
                echo "tabKm.push(" . '""' . ")\n";
                echo "tabNoPlaque.push(" . '""' . ")\n";
              }
            }
          }
          if (!empty($ligne['Natel'])) {
            echo "tabNoTel.push(" . '"' . $ligne['Natel'] . '"' . ")\n";
          } else if (!empty($ligne['TelPriv'])) {
            echo "tabNoTel.push(" . '"' . $ligne['TelPriv'] . '"' . ")\n";
          } else if (!empty($ligne['TelProf'])) {
            echo "tabNoTel.push(" . '"' . $ligne['TelProf'] . '"' . ")\n";
          } else {
            echo "tabNoTel.push(" . '""' . ")\n";
          }
          echo "</script>\n";
          $cpt++;
        }
        echo "</select><BR>\n";
        echo "<BUTTON onClick='ok_client(this.document.info.NoClient.selectedIndex);'>Valide</BUTTON></form>\n";
      }
    }
    return $nb;
  }
  if (Test_User($lg_user, $lg_password) == TRUE) {
    if (!isset($Type)) {
      $Type = 0;
    }
    if (!isset($param1)) {
      $param1 = "";
    }
    if (!isset($param2)) {
      $param2 = "";
    }
    if (!isset($Mode)) {
      $Mode = 0;
    }
    if (!isset($NoVisu)) {
      $NoVisu = 0;
    }
    //echo "$Mode<BR>";
    if ($Type == 3) {
      switch ($Sous_Type) {
        case 3:
          switch ($Etape) {
            case 0:
              echo "1234<BR>";
              Affiche_formulaire(3, NOUVEAU, 1, '', $NoVisu, $param1, $param2, $Mode);
              break;
            case 1:
              Valide_Modif(3, NOUVEAU, 1, '', $NoVisu, ", DateModif, PersonneModif", ", '" . date("Y-m-d") . "', '" . addslashes($lg_user) . "'");
              $nb = auto_client($param1, $param2, $Mode);
              break;
          }
          break;
      }
    } else {
      $param1 = $Nom;
      $param2 = $Prenom;
      $nb = auto_client($Nom, $Prenom, $Mode);
    }
    if ($nb != 0) {
      echo "<form>\n";
      echo "<input type='hidden' name='Type' value='3'>\n";
      echo "<input type='hidden' name='Sous_Type' value='3'>\n";
      echo "<input type='hidden' name='Etape' value='0'>\n";
      echo "<input type='hidden' name='NoVisu' value='0'>\n";
      echo "<input type='hidden' name='param1' value='$param1'>\n";
      echo "<input type='hidden' name='param2' value='$param2'>\n";
      echo "<input type='hidden' name='Mode' value='$Mode'>\n";
      echo "<BUTTON type='submit'>Nouveau client</BUTTON>\n";
      echo "</form>\n";
    }
  }
  ?>
</body>

</html>