<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>
	<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_vhc($NoClient, $Mode)
	{
		echo "<script language=JavaScript>\n";
		if ($resultat = Vehicule::getVehiculeByClient($NoClient)) {
			$nb = count($resultat);
			if ($nb == 0) {
				//      echo "if(confirm('Créer ou rechercher un vehicule?'))\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";
			} else {
				echo "window.resizeTo(800,600);\n";
				if ($Mode == 1) {
					echo "var tabNoChasis=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";
				} else if ($Mode == 2) {
					echo "var tabNoChasis=new Array;\n";
				} else {
					echo "var tabNoChasis=new Array;\n";
					echo "var tabVehicule=new Array;\n";
					echo "var tabKm=new Array;\n";
					echo "var tabIdVhc=new Array;\n";
					echo "var tabServiceKm=new Array;\n";
					echo "var tabServiceId=new Array;\n";
					echo "var tabServiceDuree=new Array;\n";
				}
				echo "function ok_vhc(pos)\n";
				echo "{\n";
				if ($Mode == 1) {
					echo "window.opener.document.info.NoChasis.value = tabNoChasis[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.NoPlaque.value = tabNoPlaque[pos];\n";
				} else if ($Mode == 2) {
					echo "window.opener.document.info.NoChasisAchat.value = tabNoChasis[pos];\n";
				} else {
					echo "window.opener.document.info.NoChasis.value = tabNoChasis[pos];\n";
					echo "window.opener.document.info.Vehicule.value = tabVehicule[pos];\n";
					echo "window.opener.document.info.Km.value = tabKm[pos];\n";
					echo "window.opener.document.info.vhc.value = tabIdVhc[pos];\n";
					//echo "alert(tabIdVhc[pos]);\n";
					echo "window.opener.document.forms['info'].service.options.length = 1;\n";
					echo "window.opener.document.forms['info'].service.selectedIndex = 0;\n";
					//echo "alert(tabServiceKm[pos].length);\n";
					echo "var nb=tabServiceKm[pos].length;\n";
					echo "var service_found=false;\n";
					echo "for (cpt=0;cpt<nb;cpt++)\n";
					echo "{\n";
					echo "  window.opener.add_service_option(tabServiceId[pos][cpt],'Service ' + tabServiceKm[pos][cpt],tabServiceDuree[pos][cpt]);\n";
					echo "  if ((!service_found)&&(tabKm[pos] < tabServiceKm[pos][cpt]))\n";
					echo "  {\n";
					echo "    service_found=true;\n";
					echo "    window.opener.document.forms['info'].service.selectedIndex = cpt + 1;\n";
					echo "  }\n";
					echo "}\n";
					echo "if (service_found)\n";
					echo "{\n";
					echo "  if (!confirm('Service ?'))\n";
					echo "  {\n";
					echo "    window.opener.document.forms['info'].service.selectedIndex = 0;\n";
					echo "  }\n";
					echo "  else\n";
					echo "  {\n";
					echo "    window.opener.duree_service();\n";
					echo "  }\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='info2'><select name='NoChasis' size='$nbe' OnDblClick='ok_vhc(this.document.info2.NoChasis.selectedIndex);'>\n";
				foreach ($resultat as $ligne) {
					echo "<option value='$cpt'>" . $ligne['NoChasis'] . " " . $ligne['MarqueModel'] . " " . $ligne['Model'] . " " . $ligne['NoPlaque'] . "</option>\n";
					echo "<script language=JavaScript>\n";
					if ($Mode == 1) {
						echo "tabNoChasis.push(" . '"' . $ligne['NoChasis'] . '"' . ")\n";
						echo "tabMarque.push(" . '"' . $ligne['MarqueModel'] . '"' . ")\n";
						echo "tabModel.push(" . '"' . $ligne['Model'] . '"' . ")\n";
						echo "tabKm.push(" . '"' . $ligne['Km'] . '"' . ")\n";
						echo "tabNoPlaque.push(" . '"' . $ligne['NoPlaque'] . '"' . ")\n";
					} else if ($Mode == 2) {
						echo "tabNoChasis.push(" . '"' . $ligne['NoChasis'] . '"' . ")\n";
					} else {
						echo "tabNoChasis.push(" . '"' . $ligne['NoChasis'] . '"' . ");\n";
						echo "tabVehicule.push(" . '"' . $ligne['MarqueModel'] . " " . $ligne['Model'] . '"' . ");\n";
						echo "tabKm.push(" . '"' . $ligne['Km'] . '"' . ");\n";
						echo "tabServiceKm.push(new Array);\n";
						echo "tabServiceDuree.push(new Array);\n";
						echo "tabServiceId.push(new Array);\n";
						//echo "alert('no chassis ".$ligne2["NoChasis"]."');\n";
						if ($ligne3 = ChassisModel::getChassisModelByNoChassis($ligne['NoChasis'])) {
							echo "tabIdVhc.push(" . '"' . $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 "tabServiceKm[$cpt].push(" . '"' . $ligne4['Km'] . '"' . ");\n";
								echo "tabServiceDuree[$cpt].push(" . '"' . $ligne4['Duree'] . '"' . ");\n";
								echo "tabServiceId[$cpt].push(" . '"' . $ligne4['Id'] . '"' . ");\n";
							}
						} else {
							echo "tabIdVhc.push(" . '"0"' . ");\n";
						}
					}
					echo "</script>\n";
					$cpt++;
				}
				echo "</select><BR>\n";
				echo "<BUTTON onClick='ok_vhc(this.document.info2.NoChasis.selectedIndex);'>Valide</BUTTON></form>\n";
			}
		}
	}
	if (Test_User($lg_user, $lg_password) == TRUE) {
		if (!isset($Type)) {
			$Type = 0;
		}
		if (!isset($Mode)) {
			$Mode = 0;
		}
		if ($Type == 2) {
			switch ($Sous_Type) {
				case 3:
					switch ($Etape) {
						case 0:
							//            	echo "$Mode<BR>";
							Affiche_formulaire(2, NOUVEAU, 1, '', $NoVisu, $param1, '', $Mode);
							break;
						case 1:
							//            	echo "$Mode<BR>";
							Valide_Modif(
								2,
								NOUVEAU,
								1,
								'',
								$NoVisu,
								", DateModif, PersonneModif",
								", '" . date("Y-m-d") . "', '" . addslashes($lg_user) . "'"
							);
							auto_vhc($param1, $Mode);
							break;
					}
					break;
				case 2:
					switch ($Etape) {
						case 0:
							if (isset($param2)) {
								echo "ici<BR>";
								$Mode = $param2;
							}
							//            	echo "$Mode<BR>";
							Affiche_No_Visu_recherche(2, VISU_DATA, '', $param1, $Mode);
							echo "  <form name='info'>\n";
							echo "   <input type='hidden' name='Type' value=$Type>\n";
							echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
							echo "   <input type='hidden' name='Etape' value='1'>\n";
							echo "   <input type='hidden' name='param1' value='$param1'>\n";
							echo "   <input type='hidden' name='NoVisu' value='$NoVisu'>\n";
							echo "   <input type='hidden' name='Mode' value='$Mode'>\n";
							Create_Recherche(2, $NoVisu);
							echo "   <button type='submit'>Rechercher</button>\n";
							echo "  </form>\n";
							break;
						case 1:
							//            	echo "$Mode<BR>";
							/*	  	    echo "  <form name='info'>\n";
            echo "   <input type='hidden' name='Type' value=$Type>\n";
            echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
            echo "   <input type='hidden' name='Etape' value='1'>\n";
            echo "   <input type='hidden' name='param1' value='$param1'>\n";
            Create_Recherche(2,$NoVisu);
            echo "   <button type='submit'>Rechercher</button>\n";
            echo "  </form>\n";*/
							echo "  <form name='info'>\n";
							echo "   <input type='hidden' name='Type' value=$Type>\n";
							echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
							echo "   <input type='hidden' name='Etape' value='2'>\n";
							echo "   <input type='hidden' name='NoVisu' value=0>\n";
							echo "   <input type='hidden' name='Mode' value='$Mode'>\n";
							echo "   <input type='hidden' name='param1' value='$param1'>\n";
							Affiche_Resultat_Recherche(2, 7, $NoVisu);
							echo "  </form>\n";
							break;
						case 2:
							//            	echo "$Mode<BR>";
							//            	echo "$param1 <BR>";
							//            	echo "$NoVisu <BR>";
							if (isset($Modif)) {
								Valide_Modif(
									2,
									MODIFICATION,
									1,
									${GetNomClef(2)},
									$NoVisu,
									",DateModif='" . date("Y-m-d") . "',PersonneModif='" . addslashes($lg_user) . "'",
									''
								);
								auto_vhc($param1, $Mode);
							}
							Affiche_formulaire(2, MODIFICATION, $no_division = 1, $clef = ${GetNomClef(2)}, $no_visu = $NoVisu, $param1, '', $Mode);
							break;
						case 3:
							echo "$Mode<BR>";
							Valide_Modif(
								2,
								MODIFICATION,
								1,
								${GetNomClef(2)},
								$NoVisu,
								",DateModif='" . date("Y-m-d") . "',PersonneModif='" . addslashes($lg_user) . "'",
								''
							);
							auto_vhc($param1, $Mode);
							break;
					}
					break;
			}
		} else {
			$param1 = $NoClient;
			auto_vhc($NoClient, $Mode);
		}
		echo "<form>\n";
		echo "<input type='hidden' name='Type' value='2'>\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='Mode' value='$Mode'>\n";
		echo "<BUTTON type='submit'>Nouveau vehicule</BUTTON>\n";
		echo "</form>\n";
		echo "<form>\n";
		echo "<input type='hidden' name='Type' value='2'>\n";
		echo "<input type='hidden' name='Sous_Type' value='2'>\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='Mode' value='$Mode'>\n";
		echo "<BUTTON type='submit'>Recherche vehicule</BUTTON>\n";
		echo "</form>\n";
	}
	?>
</body>

</html>