<?php
require 'database.inc.php';
if (Test_User($lg_user, $lg_password) == TRUE) {
?>
	<html>

	<head>
		<meta http-equiv='content-type' content='text/html;charset=utf-8'>
		<SCRIPT language=JavaScript src='valideformulaire.js' type=text/javascript></SCRIPT>
		<title>WebBike V<?php echo VERSION; ?></title>
		<?php
		$size = 2;
		if ($ligne = Poste::getPostesByIP()) {
			$size = $ligne['Size'];
		}
		echo "<link href='css/style$size.css' rel='stylesheet' media='screen'>\n";

		echo "<link href='" . CSS_FOLDER . "style.css' rel='stylesheet' media='screen'>\n"
		?>
		<script language=javascript>
			var tabEtats = new Array;
			var tabTarifs = new Array;
			var tabKmActuel = new Array;
			// var tabCC=new Array;
			// var tabNCC=new Array;
			// var tabECC=new Array;
			function identifie_tarif() {
				debut = new Date();
				debut.setDate(this.document.info.new_jour_debut.value);
				debut.setMonth(this.document.info.new_mois_debut.value - 1);
				debut.setFullYear(this.document.info.new_annee_debut.value);
				debut.setHours(this.document.info.new_heure_debut.value);
				debut.setMinutes(this.document.info.new_minutes_debut.value);
				debut.setSeconds(0);
				fin = new Date();
				fin.setDate(this.document.info.new_jour_fin.value);
				fin.setMonth(this.document.info.new_mois_fin.value - 1);
				fin.setFullYear(this.document.info.new_annee_fin.value);
				fin.setHours(this.document.info.new_heure_fin.value);
				fin.setMinutes(this.document.info.new_minutes_fin.value);
				fin.setSeconds(0);
				dms = (fin.getTime() - debut.getTime());
				if (dms < 0) {
					alert("date de debut plus grande que date de fin"); // >
					return;
				}
				ds = dms / 1000;
				dh = ds / 3600;
				dj = dh / 24;
				nbj = Math.ceil(dj - (2 / 24));
				switch (nbj) {
					case 1:
						this.document.info.new_tarif_type.value = 1;
						this.document.info.nb_jours.value = nbj;
						break;
					case 2:
						if (debut.getDay() == 6 && debut.getHours() >= 11 && debut.getHours() <= 13 && fin.getDay() == 1 && fin.getHours() >= 8 && fin.getHours() <= 10) {
							this.document.info.new_tarif_type.value = 7;
							this.document.info.nb_jours.value = 1;
						} else {
							this.document.info.new_tarif_type.value = 1;
							this.document.info.nb_jours.value = nbj;
						}
						break;
					case 3:
						if (debut.getDay() == 5 && debut.getHours() >= 16 && debut.getHours() <= 18 && fin.getDay() == 1 && fin.getHours() >= 8 && fin.getHours() <= 10) {
							this.document.info.new_tarif_type.value = 8;
							this.document.info.nb_jours.value = 1;
						} else {
							this.document.info.new_tarif_type.value = 2;
							this.document.info.nb_jours.value = nbj;
						}
						break;
					case 4:
						if (debut.getDay() == 4 && debut.getHours() >= 8 && debut.getHours() <= 10 && fin.getDay() == 1 && fin.getHours() >= 8 && fin.getHours() <= 10) {
							this.document.info.new_tarif_type.value = 9;
							this.document.info.nb_jours.value = 1;
						} else {
							this.document.info.new_tarif_type.value = 2;
							this.document.info.nb_jours.value = nbj;
						}
						break;
					case 5:
					case 6:
						this.document.info.new_tarif_type.value = 2;
						this.document.info.nb_jours.value = nbj;
						break;
					case 7:
					case 8:
					case 9:
					case 10:
					case 11:
					case 12:
						this.document.info.new_tarif_type.value = 3;
						this.document.info.nb_jours.value = nbj;
						break;
					case 13:
					case 14:
					case 15:
					case 16:
					case 17:
					case 18:
					case 19:
					case 20:
						this.document.info.new_tarif_type.value = 4;
						this.document.info.nb_jours.value = nbj;
						break;
						//    	this.document.info.new_tarif_type.value=5;
						// 	    this.document.info.nb_jours.value=nbj;
						//    	break;
					default:
						this.document.info.new_tarif_type.value = 6;
						this.document.info.nb_jours.value = deux_dec(nbj / 30);
						break;
				}
				modif_tarif();
			}

			function modif_vhc() {
				tarif_actuel = this.document.info.new_vhc.selectedIndex;
				this.document.info.etat.value = tabEtats[tarif_actuel];
				identifie_tarif();
			}

			function modif_tarif() {
				this.document.info.tarif.value = tabTarifs[tarif_actuel][this.document.info.new_tarif_type.value - 1];
				modif_prix();
			}

			function modif_prix() {
				modif_nb();
			}

			function modif_nb() {
				this.document.info.total.value = deux_dec(this.document.info.tarif.value * this.document.info.nb_jours.value * ((100.00 + (this.document.info.rabais.value * 1.0)) / 100.00));
			}

			function is_we(jour, mois, annee) {
				return 0;
			}

			function cc_client() {
				this.document.info.cc.value = tabCC[this.document.info.NoClient.selectedIndex];
				this.document.info.ncc.value = tabNCC[this.document.info.NoClient.selectedIndex];
				this.document.info.ecc1.value = tabECC[this.document.info.NoClient.selectedIndex].substr(5, 2);
				this.document.info.ecc2.value = tabECC[this.document.info.NoClient.selectedIndex].substr(0, 4);
				this.document.info.scc.value = tabSCC[this.document.info.NoClient.selectedIndex];
			}

			function change_caution() {
				if (this.document.info.caution.value == 0 || this.document.info.caution.value == 5) {
					this.document.info.ncc.style.visibility = "hidden";
					this.document.info.ecc1.style.visibility = "hidden";
					this.document.info.ecc2.style.visibility = "hidden";
					this.document.info.scc.style.visibility = "hidden";
				} else {
					this.document.info.ncc.style.visibility = "visible";
					this.document.info.ecc1.style.visibility = "visible";
					this.document.info.ecc2.style.visibility = "visible";
					this.document.info.scc.style.visibility = "visible";
				}
			}

			function valide_suppresion() {
				if (confirm("Voulez-vous vraiment supprimer cette location ?")) {
					this.document.infodel.submit();
				}
			}

			function imprimer_contract() {
				no = this.document.info.caution.selectedIndex;
				val = this.document.info.caution.options[no].value;
				if (val == -1) {
					alert("Vous devez selectionner le type caution !");
					return 0;
				}
				if (this.document.info.etape.value == 0) {
					this.document.info.etape.value = 1;
				}
				this.document.info.pri.value = 1;
				this.document.info.fact.value = 0;
				this.document.info.submit();
			}

			function imprimer_contract_facture() {
				if (this.document.info.etape.value == 0) {
					no = this.document.info.caution.selectedIndex;
					val = this.document.info.caution.options[no].value;
					if (val == -1) {
						alert("Vous devez selectionner le type caution !");
						return 0;
					}
					this.document.info.etape.value = 1;
				}
				this.document.info.pri.value = 1;
				this.document.info.fact.value = 1;
				this.document.info.submit();
			}

			function debut_location() {
				if (this.document.info.etape.value == 0) {
					this.document.info.caution.selectedIndex = 5;
					this.document.info.Pret.selectedIndex = 1;
					this.document.info.etape.value = 1;
				}
				this.document.info.submit();
			}

			function verif() {
				if (this.document.info.caution.value > 0 && this.document.info.caution.value != 5) {
					if (this.document.info.ncc.value.length != 16) {
						alert("Le numéro de carte de crédit doit avoir 16 chiffres");
						return 0;
					}
					if (this.document.info.scc.value.length != 3) {
						alert("Le numéro sécurité de carte de crédit doit avoir 3 chiffres");
						return 0;
					}

				}
				this.document.info.submit();
			}

			function valide_retour(facture) {
				if (this.document.info.NoClient.value.length == 0) {
					alert('Vous devez assigner un numéro de client');
				} else {
					if (this.document.info.KmRetour.value == this.document.info.KmDepart.value) {
						if (confirm("vous devez rentrer les kilomètres de retour")) {
							return 0;
						}
					}
					if (facture == 1) {
						this.document.info.etape.value = 2;
					} else {
						this.document.info.etape.value = 3;
					}
					this.document.info.submit();
				}
			}

			function cree_facture() {
				if (this.document.info.NoClient.value.length == 0) {
					alert('vous devez absolument avoir le champ NoClient remplis pour créer un facture location');
				} else {
					this.document.info.etape.value = 3;
					this.document.info.fact.value = 1;
					this.document.info.submit();
				}
			}

			function copie_tot_cont() {
				this.document.info.total_cont.value = this.document.info.total.value;
				calcul_tot();
			}

			function deux_dec(nombre_in) {
				var nombre = new String(nombre_in);
				var out = new String('');
				var Taille = nombre.length;
				var Position = -1;
				if (Taille == 0) {
					out = '0.00';
				} else {
					for (cont = 0; cont < Taille; cont++) {
						if ((nombre.charCodeAt(cont)) == 46) {
							Position = cont;
						} else {}
					}
					var Diff = Taille - Position;
					if (Position == -1) {
						out = nombre + '.00';
					} else {
						if (Diff < 3) {
							out = nombre;
							for (cont = Diff; cont < 3; cont++) {
								out = out + '0';
							}
						} else {
							out = nombre.substring(0, Position + 3);
						}
					}
				}
				return out;
			}

			function calcul_tot() {
				this.document.info.total_fact.value = deux_dec(parseFloat(this.document.info.total_cont.value) + parseFloat(this.document.info.total_km.value) + parseFloat(this.document.info.total_frais.value));
			}

			function calcule_rabais() {
				this.document.info.rabais.value = deux_dec(((-1) + (this.document.info.total.value / (this.document.info.tarif.value * this.document.info.nb_jours.value))) * 100);
			}

			function recherche_client() {
				Nom = this.info.elements['Nom'].value;
				Prenom = this.info.elements['Prenom'].value;
				if (Nom.length == 0 && Prenom.length == 0) {
					alert("Vous devez rentrer un nom ou un prénom");
				} else {
					window.open('recherche_client_old.php?Nom=' + Nom + '&Prenom=' + Prenom + '&Mode=1', '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
				}
			}

			function go_facture(NoCommande, NoClient) {
				this.opener.location = "default.php?Type=4&Sous_Type=1&Etape=3&New=0&NoCommande=" + NoCommande + "&NoClient=" + NoClient;
				window.close();
			}

			function tab_date() {
				if (this.document.info.ecc1.value.length == 2) {
					this.document.info.ecc2.select();
				}
			}
		</script>
	</head>

	<body>
	<?php
	function nom_tarif($no_tarif)
	{
		switch ($no_tarif) {
			case 1:
				return "Location 1 à 2 jours";
				break;
			case 2:
				return "Location 3 à 6 jours";
				break;
			case 3:
				return "Location 7 à 12 jours";
				break;
			case 4:
				return "Location 13 + jours";
				break;
			case 5:
				//  	  return "Location 13 à 15 jours";
				break;
			case 6:
				return "Location mensuelle";
				break;
			case 7:
				return "Location WE Mini";
				break;
			case 8:
				return "Location WE Standard";
				break;
			case 9:
				return "Location WE Maxi";
				break;
		}
	}
	function db_tarif($no_tarif)
	{
		switch ($no_tarif) {
			case 1:
				return "Prix1_2";
				break;
			case 2:
				return "Prix3_5";
				break;
			case 3:
				return "Prix6_9";
				break;
			case 4:
				return "Prix10_12";
				break;
			case 5:
				return "Prix13_15";
				break;
			case 6:
				return "PrixMois";
				break;
			case 7:
				return "PrixWE1";
				break;
			case 8:
				return "PrixWE2";
				break;
			case 9:
				return "PrixWE3";
				break;
		}
	}
	function db_kil($no_tarif)
	{
		switch ($no_tarif) {
			case 1:
				return "Kil1_2";
				break;
			case 2:
				return "Kil3_5";
				break;
			case 3:
				return "Kil6_9";
				break;
			case 4:
				return "Kil10_12";
				break;
			case 5:
				return "Kil13_15";
				break;
			case 6:
				return "KilMois";
				break;
			case 7:
				return "KilWE1";
				break;
			case 8:
				return "KilWE2";
				break;
			case 9:
				return "KilWE3";
				break;
		}
	}
	function db_sup($no_tarif)
	{
		switch ($no_tarif) {
			case 1:
				return "Sup1_2";
				break;
			case 2:
				return "Sup3_5";
				break;
			case 3:
				return "Sup6_9";
				break;
			case 4:
				return "Sup10_12";
				break;
			case 5:
				return "Sup13_15";
				break;
			case 6:
				return "SupMois";
				break;
			case 7:
				return "SupWE1";
				break;
			case 8:
				return "SupWE2";
				break;
			case 9:
				return "SupWE3";
				break;
		}
	}
	if ((!empty($User)) && (!empty($Passwd))) {
		$lg_user = $User;
		$lg_password = $Passwd;
	}
	if (Test_User($lg_user, $lg_password) == TRUE) {
		$annee = date("Y");
		if ($new == 0) {
			if ($ligne = Location::getByNoChassisAndDateDebutExact(addslashes($vhc), $Start)) {
				$requette2 = "SELECT * FROM vehiculeslocation" . ID_GARAGE . " WHERE (Categorie <> 3) OR (NoChasis = '" . $ligne['NoChasis'] . "') ORDER BY Position";
				echo "<form METHOD='GET' name='info' action='ok_location.php'>";
				echo "<input type='hidden' name='vhc' value='$vhc'><BR>\n";
				echo "<input type='hidden' name='Start' value='$Start'><BR>\n";
				echo "<input type='hidden' name='pri' value='0'><BR>\n";
				echo "<input type='hidden' name='fact' value='0'><BR>\n";
				echo "<TABLE border=1><TR><TH>Vehicule</TH><TD><select name='new_vhc' onChange='modif_vhc();'>";
				$tarif_actuel = 0;
				$no_tarif = 0;
				if ($resultat2 = Connection::executeRawQueryWithfetchAll($requette2)) {
					foreach ($resultat2 as $ligne2) {
						echo "\n<script language=javascript>\n";
						echo "var tarifs=new Array;\n";
						echo "tabTarifs.push(tarifs);\n";
						echo "tarifs[0]=" . $ligne2['Prix1_2'] . ";\n";
						echo "tarifs[1]=" . $ligne2['Prix3_5'] . ";\n";
						echo "tarifs[2]=" . $ligne2['Prix6_9'] . ";\n";
						echo "tarifs[3]=" . $ligne2['Prix10_12'] . ";\n";
						echo "tarifs[4]=" . $ligne2['Prix13_15'] . ";\n";
						echo "tarifs[5]=" . $ligne2['PrixMois'] . ";\n";
						echo "tarifs[6]=" . $ligne2['PrixWE1'] . ";\n";
						echo "tarifs[7]=" . $ligne2['PrixWE2'] . ";\n";
						echo "tarifs[8]=" . $ligne2['PrixWE3'] . ";\n";
						echo "tabEtats.push(" . '"' . $ligne2['EtatVhc'] . '"' . ")\n";
						echo "tabKmActuel.push(" . '"' . $ligne2['KmActuel'] . '"' . ")\n";
						echo "</script>\n";
						if ($ligne3 = Vehicule::getVehiculeByNoChassis(addslashes($ligne2['NoChasis']))) {
							echo "<option value='" . addslashes($ligne3['NoChasis']) . "'";
							if (!strcmp($ligne3['NoChasis'], $ligne['NoChasis'])) {
								echo " selected";
								$tarif_actuel = $no_tarif;
								$etat = $ligne2['EtatVhc'];
								$tarif = $ligne2[db_tarif($ligne['TypeTarif'])];
								$kmactuel = $ligne2['KmActuel'];
							}
							echo ">" . str_replace(" ", "&nbsp;", taille_text($ligne3['Model'], 10, 0)) . " " . str_replace(" ", "&nbsp;", taille_text($ligne2['NoPlaque'], 10, 0)) . " " . taille_text($ligne2['NoChasis'], 20, 0) . "</option>";
						}
						$no_tarif++;
					}
				}
				echo "\n<script language=javascript>\n";
				echo "var tarif_actuel=$tarif_actuel;\n";
				echo "</script>\n";
				echo "</select></TD></TR><TR><TH>Etat</TH><TD><input name='etat' value='$etat' readonly></TD></TR>";
				echo "<TR><TH>Type</TH><TD><SELECT name='Pret'><OPTION value='0'";
				if ($ligne['Pret'] == 0) {
					echo " selected";
				}
				echo ">Location</OPTION><OPTION value='1'";
				if ($ligne['Pret'] == 1) {
					echo " selected";
				}
				echo ">Pret</OPTION></SELECT></TD></TR>";
				echo "</TABLE><BR>\n";
				echo "<TABLE border=1><TR><TH>No client</TH><TD>";
				echo "<input type='text' name='NoClient' value='" . $ligne['NoClient'] . "' readonly><BUTTON type='button' onClick='recherche_client_ajax(0);'>Recherche</BUTTON>\n";
				$cc = $ligne['TypeCaution'];
				$ncc = $ligne['NoCC'];
				$ecc = $ligne['ExpCC'];
				$scc = $ligne['SecCC'];
				$nom_client = $ligne['Nom'];
				$prenom_client = $ligne['Prenom'];
				$no_telephone = $ligne['Telephone'];
				echo "</TD></TR>\n";
				echo "<TR><TH>Nom</TH><TD><INPUT name='Nom' value='$nom_client'";
				echo "></TD></TR>\n";
				echo "<script language=JavaScript>\n";
				echo "this.document.forms['info'].elements['Nom'].focus();\n";
				echo "</script>\n";
				echo "<TR><TH>Prenom</TH><TD><INPUT name='Prenom' value='$prenom_client'";
				echo " onchange='recherche_client_ajax(0);'></TD></TR>\n";
				echo "<TR><TH>Telephone</TH><TD><input name='Telephone' value='$no_telephone'></TD></TR>\n";
				echo "<TR><TH>Caution</TH><TD><SELECT name='caution' onchange='change_caution();'>";
				if ($ligne['Etape'] == 0) {
					//		    echo "<OPTION value='-1' selected></OPTION>";
					echo "<OPTION value='0'";
					echo ">Cash</OPTION><OPTION value='1'";
					echo ">Visa</OPTION><OPTION value='2'";
					echo ">Master card</OPTION><OPTION value='3'";
					echo ">Amex</OPTION><OPTION value='4'";
					echo ">Diners card</OPTION><OPTION value='5'";
					echo ">Sans</OPTION></SELECT></TD></TR>\n";
				} else {
					echo "<OPTION value='0'";
					if ($cc == 0)
						echo " selected";
					echo ">Cash</OPTION><OPTION value='1'";
					if ($cc == 1)
						echo " selected";
					echo ">Visa</OPTION><OPTION value='2'";
					if ($cc == 2)
						echo " selected";
					echo ">Master card</OPTION><OPTION value='3'";
					if ($cc == 3)
						echo " selected";
					echo ">Amex</OPTION><OPTION value='4'";
					if ($cc == 4)
						echo " selected";
					echo ">Diners card</OPTION><OPTION value='5'";
					if ($cc == 5)
						echo " selected";
					echo ">Sans</OPTION></SELECT></TD></TR>\n";
				}
				//		  echo "<TR><TH>Carte de crédit</TH><TD><input name='cc' value='$cc'></TD></TR>\n";
				echo "<TR><TH>No de carte de crédit</TH><TD><input name='ncc' value='$ncc' size='16' maxlength='16'></TD></TR>\n";
				if ($ecc == "0000-00-00" || $ecc == "2000-00-01")
					echo "<TR><TH>Expiration</TH><TD><input name='ecc1' size=2 onKeyUp='tab_date();'><input name='ecc2' size=2></TD></TR>\n";
				else
					echo "<TR><TH>Expiration</TH><TD><input name='ecc1' size=2 value='" . substr($ecc, 5, 2) . "' onKeyUp='tab_date();'><input name='ecc2' size=2 value='" . substr($ecc, 2, 2) . "'></TD></TR>\n";
				echo "<TR><TH>No sécurité de carte de crédit</TH><TD><input name='scc' value='$scc' size='3' maxlength='3'></TD></TR>\n";
				echo "</TABLE><BR>\n";
				echo "<TABLE border=1><TR><TH>Début</TH><TD>";
				echo "<b>Date</b><select name='new_jour_debut' onchange='identifie_tarif();'>";
				for ($cpt = 1; $cpt <= 31; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Debut'], 8, 2))
						echo " selected";
					echo ">$cpt</option>";
				}
				echo "</select>-";
				echo "<select name='new_mois_debut' onchange='identifie_tarif();'>";
				for ($cpt = 1; $cpt <= 12; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Debut'], 5, 2))
						echo " selected";
					echo ">$cpt " . nom_mois($cpt) . "</option>";
				}
				echo "</select>-";
				echo "<select name='new_annee_debut' onchange='identifie_tarif();'>";
				for ($cpt = $annee - 2; $cpt <= $annee + 2; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Debut'], 0, 4))
						echo " selected";
					echo ">$cpt</option>";
				}
				echo "</select>";
				echo "<b>Heure</b><select name='new_heure_debut' onchange='identifie_tarif();'>";
				for ($cpt = 0; $cpt <= 23; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Debut'], 11, 2))
						echo " selected";
					echo ">";
					if ($cpt < 10)
						echo "0";
					echo "$cpt</option>";
				}
				echo "</select>:";
				echo "<select name='new_minutes_debut' onchange='identifie_tarif();'>";
				for ($cpt = 0; $cpt <= 45; $cpt += 15) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Debut'], 14, 2))
						echo " selected";
					echo ">";
					if ($cpt < 10)
						echo "0";
					echo "$cpt</option>";
				}
				echo "</select>";
				echo "</TD></TR>\n";
				echo "<TR><TH>Fin</TH><TD>";
				echo "<b>Date</b><select name='new_jour_fin' onchange='identifie_tarif();'>";
				for ($cpt = 1; $cpt <= 31; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Fin'], 8, 2))
						echo " selected";
					echo ">$cpt</option>";
				}
				echo "</select>-";
				echo "<select name='new_mois_fin' onchange='identifie_tarif();'>";
				for ($cpt = 1; $cpt <= 12; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Fin'], 5, 2))
						echo " selected";
					echo ">$cpt " . nom_mois($cpt) . "</option>";
				}
				echo "</select>-";
				echo "<select name='new_annee_fin' onchange='identifie_tarif();'>";
				for ($cpt = $annee - 2; $cpt <= $annee + 2; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Fin'], 0, 4))
						echo " selected";
					echo ">$cpt</option>";
				}
				echo "</select>";
				echo "<b>Heure</b><select name='new_heure_fin' onchange='identifie_tarif();'>";
				for ($cpt = 0; $cpt <= 23; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Fin'], 11, 2))
						echo " selected";
					echo ">";
					if ($cpt < 10)
						echo "0";
					echo "$cpt</option>";
				}
				echo "</select>:";
				echo "<select name='new_minutes_fin' onchange='identifie_tarif();'>";
				for ($cpt = 0; $cpt <= 45; $cpt += 15) {
					echo "<option value='$cpt'";
					if ($cpt == substr($ligne['Fin'], 14, 2))
						echo " selected";
					echo ">";
					if ($cpt < 10)
						echo "0";
					echo "$cpt</option>";
				}
				echo "</select>";
				echo "</TD></TR></TABLE><BR>\n";
				echo "<TABLE border=1><TR><TH>Tarif</TH><TD><select name='new_tarif_type' onChange='modif_tarif();'>";
				for ($cpt = 1; $cpt <= 9; $cpt++) {
					echo "<option value='$cpt'";
					if ($cpt == $ligne['TypeTarif'])
						echo " selected";
					echo ">" . nom_tarif($cpt) . "</option>";
				}
				echo "</select></TD></TR><TR><TH>Prix</TH><TD><input name='tarif' value='" . $ligne['Tarif'] . "' onChange='modif_prix();' readonly></TD></TR>";
				echo "<TR><TH>Nombre d'unités</TH><TD><input name='nb_jours' value='" . $ligne['NbUnites'] . "' onChange='modif_prix();'></TD></TR>";
				echo "<TR><TH>Rabais %</TH><TD><input name='rabais' value='" . $ligne['Rabais'] . "' onChange='modif_prix();'></TD></TR>";
				echo "<TR><TH>Total</TH><TD><input name='total' value='" . $ligne['Total'] . "' onChange='calcule_rabais();'></TD></TR>";
				switch ($ligne['Etape']) {
					case 0:
						if ($ligne['NoCommande'] != 0) {
							$ligne_acompte = DBCash::getByNoFacture($ligne['NoCommande']);
							echo "<TR><TH>Acompte</TH><TD>" . $ligne_acompte["Somme"];
							echo " <BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", 0);' style='width:150px'>Facture " . $ligne['NoCommande'] . "</button><BR>\n";
							echo "</TD></TR>";
							$as_acompte = true;
						} else {
							echo "<TR><TH>Acompte</TH><TD><input name='acompte' value='0'>";
							if (As_Right_Groupe(6)) {
								$resultat2 = ModePayement::getVisibleModePayment();
							} else {
								$no_caisse = 1;
								if ($ligne1 = Poste::getPostesByIP()) {
									$no_caisse = $ligne1['NoCaisse'];
								}
								$resultat2 = ModePayement::getModePayementsByNoCaisse($no_caisse);
							}
							echo " <SELECT name='ModePayement' size='1'>\n";
							foreach ($resultat2 as $ligne2) {
								if (($ligne2['Type'] == 2) && (!Is_Banque())) {
								} else {
									if (As_Right_Groupe(6)) {
										echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . " caisse " . $ligne2['NoCaisse'] . "</option>\n";
									} else {
										echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . "</option>\n";
									}
								}
							}
							echo "</TD></TR>";
							$as_acompte = false;
						}
						break;
					case 1:
						if ($ligne['NoCommande'] != 0) {
							$ligne_acompte = DBCash::getByNoFacture($ligne['NoCommande']);
							echo "<TR><TH>Acompte</TH><TD>" . $ligne_acompte["Somme"];
							echo " <BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", 0);' style='width:150px'>Facture " . $ligne['NoCommande'] . "</button><BR>\n";
							echo "</TD></TR>";
						} else {
							echo "<TR><TH>Acompte</TH><TD><input name='acompte' value='0'>";
							if (As_Right_Groupe(6)) {
								$resultat2 = ModePayement::getVisibleModePayment();
							} else {
								$no_caisse = 1;
								if ($ligne1 = Poste::getPostesByIP()) {
									$no_caisse = $ligne1['NoCaisse'];
								}
								$resultat2 = ModePayement::getModePayementsByNoCaisse($no_caisse);
							}
							echo " <SELECT name='ModePayement' size='1'>\n";
							foreach ($resultat2 as $ligne2) {
								if (($ligne2['Type'] == 2) && (!Is_Banque())) {
								} else {
									if (As_Right_Groupe(6)) {
										echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . " caisse " . $ligne2['NoCaisse'] . "</option>\n";
									} else {
										echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . "</option>\n";
									}
								}
							}
							echo "</TD></TR>";
						}
				}
				echo "</TABLE><BR>\n";
				echo "<TABLE border=1>\n";
				$sortie = "";
				switch ($ligne['Etape']) {
					case 4:
					case 3:
					case 2:
					case 1:
						if (empty($ligne['KmRetour'])) {
							$value = $kmactuel;
						} else {
							$value = $ligne['KmRetour'];
						}
						$sortie = "<TR><TH>Etat Retour</TH><TD><input type='text' name='EtatRetour' value='" . $ligne['RemarqueRetour'] . "'></TD></TR>\n" . $sortie;
						if (empty($ligne['FinEffectif'])) {
							$sortie = "<TR><TH>Retour effectif</TH><TD>" . Input_Date_Heure("re", date("Y"), date("m"), date("j"), date("H"), date("i")) . "</TD></TR>\n" . $sortie;
						} else {
							$dde = $ligne['FinEffectif'];
							$sortie = "<TR><TH>Retour effectif</TH><TD>" . Input_Date_Heure("re", substr($dde, 0, 4), substr($dde, 5, 2), substr($dde, 8, 2), substr($dde, 11, 2), substr($dde, 14, 2)) . "</TD></TR>\n" . $sortie;
						}
						$sortie = "<TR><TH>Km retour</TH><TD><input type='text' name='KmRetour' value='$value'></TD></TR>\n" . $sortie;
					case 0:
						if (empty($ligne['KmDepart'])) {
							$value = $kmactuel;
						} else {
							$value = $ligne['KmDepart'];
						}
						if (empty($ligne['DebutEffectif'])) {
							$sortie = "<TR><TH>Départ effectif</TH><TD>" . Input_Date_Heure("de", date("Y"), date("m"), date("j"), date("H"), date("i")) . "</TD></TR>\n" . $sortie;
						} else {
							$dde = $ligne['DebutEffectif'];
							$sortie = "<TR><TH>Départ effectif</TH><TD>" . Input_Date_Heure("de", substr($dde, 0, 4), substr($dde, 5, 2), substr($dde, 8, 2), substr($dde, 11, 2), substr($dde, 14, 2)) . "</TD></TR>\n" . $sortie;
						}
						$sortie = "<TR><TH>Km départ</TH><TD><input type='text' name='KmDepart' value='$value'></TD></TR>\n" . $sortie;
				}
				echo $sortie;
				echo "</TABLE><BR>\n";
				echo "<input type='hidden' name='etape' value='" . $ligne['Etape'] . "'>\n";
				echo "<input type='hidden' name='upd' value='1'>\n";
				echo "<BUTTON TYPE='button' onClick='verif();'>Valider</button><BR>\n";
				if ($ligne['Etape'] == 0) {
					echo "<BIG>Démarrer Location/Prêt</BIG><BR/>\n";
					echo "<BUTTON TYPE='button' onClick='imprimer_contract();' style='width:150px'>Contract</button>";
					if (!$as_acompte) {
						echo "<BUTTON TYPE='button' onClick='imprimer_contract_facture();' style='width:150px'>Contract et facture</button>";
						echo "<BUTTON TYPE='button' onClick='debut_location();' style='width:150px'>Debut prêt</button>";
					}
				}
				echo "<BR>\n";
				if ($ligne['Etape'] == 1) {
					if ($ligne['NoCommande'] != 0) {
						echo "<BUTTON TYPE='button' onClick='valide_retour(1);' style='width:150px'>Retour</button><BR>\n";
					} else {
						echo "<BUTTON TYPE='button' onClick='imprimer_contract_facture();' style='width:150px'>Contract et Facture</button><BR>";
						echo "<BUTTON TYPE='button' onClick='valide_retour(1);' style='width:150px'>Retour avec facture</button><BR>\n";
						echo "<BUTTON TYPE='button' onClick='valide_retour(0);' style='width:150px'>Retour sans facture</button><BR>\n";
					}
				}
				echo "</form>";
				//	      echo "</form>";
				if ($ligne['Etape'] > 0) {
					echo "<form action='print_contract_loc.php' target='_blank' rel='opener'>\n";
					echo "<input type='hidden' name='NoChasis' value='$vhc'>\n";
					echo "<input type='hidden' name='Debut' value='$Start'>\n";
					echo "<input type='hidden' name='Direct' value='1'>\n";
					echo " <button type='submit' style='width:150px'>Imprimer contrat location</button>";
					echo "</form>\n";
				}
				echo "<form METHOD='GET' name='infodel' action='del_location.php'>";
				echo "<input type='hidden' name='vhc' value='$vhc'>\n";
				echo "<input type='hidden' name='Start' value='$Start'>\n";
				if ($ligne['Etape'] < 2) {
					echo "<BUTTON TYPE='button' onClick='valide_suppresion();'>Supprimer</button><BR>\n";
				}
				echo "</form>";
				if ($ligne['Etape'] == 1) {
					if ($ligne['NoCommande'] != 0) {
						echo "<form>";
						echo "<BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", " . $ligne['NoClient'] . ");'>Facture</button><BR>\n";
						echo "</form>";
					}
				}
				if ($ligne['Etape'] == 2) {
					echo "<form>";
					echo "<BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", " . $ligne['NoClient'] . ");'>Facture</button><BR>\n";
					echo "</form>";
				}
			}
		}
	} else {
		$requette2 = "SELECT * FROM vehiculeslocation" . ID_GARAGE . " WHERE Categorie != 3 ORDER BY Position";
		//      $requette2="SELECT * FROM vehiculeslocation".ID_GARAGE." ORDER BY NoChasis";
		echo "<form METHOD='GET' name='info' action='ok_location.php'>";
		echo "<TABLE border=1><TR><TH>Vehicule</TH><TD><select name='new_vhc' onChange='modif_vhc();'>";
		$tarif_actuel = 0;
		$no_tarif = 0;
		if ($resultat2 = Connection::executeRawQueryWithfetchAll($requette2)) {
			foreach ($resultat2 as $ligne2) {
				echo "\n<script language=javascript>\n";
				echo "var tarifs=new Array;\n";
				echo "tabTarifs.push(tarifs);\n";
				echo "tarifs[0]=" . $ligne2['Prix1_2'] . ";\n";
				echo "tarifs[1]=" . $ligne2['Prix3_5'] . ";\n";
				echo "tarifs[2]=" . $ligne2['Prix6_9'] . ";\n";
				echo "tarifs[3]=" . $ligne2['Prix10_12'] . ";\n";
				echo "tarifs[4]=" . $ligne2['Prix13_15'] . ";\n";
				echo "tarifs[5]=" . $ligne2['PrixMois'] . ";\n";
				echo "tarifs[6]=" . $ligne2['PrixWE1'] . ";\n";
				echo "tarifs[7]=" . $ligne2['PrixWE2'] . ";\n";
				echo "tarifs[8]=" . $ligne2['PrixWE3'] . ";\n";
				echo "tabEtats.push(" . '"' . $ligne2['EtatVhc'] . '"' . ")\n";
				echo "</script>\n";
				if ($ligne3 = Vehicule::getVehiculeByNoChassis(addslashes($ligne2['NoChasis']))) {
					echo "<option value='" . addslashes($ligne3['NoChasis']) . "'";
					if (!strcmp($ligne3['NoChasis'], $vhc)) {
						echo " selected";
						$etat = $ligne2['EtatVhc'];
						$tarif_actuel = $no_tarif;
						$tarif = $ligne2[db_tarif(1)];
					}
					echo ">" . str_replace(" ", "&nbsp;", taille_text($ligne3['Model'], 10, 0)) . " " . str_replace(" ", "&nbsp;", taille_text($ligne2['NoPlaque'], 10, 0)) . " " . taille_text($ligne2['NoChasis'], 20, 0) . "</option>";
				}
				$no_tarif++;
			}
		}
		echo "\n<script language=javascript>\n";
		echo "var tarif_actuel=$tarif_actuel;\n";
		echo "</script>\n";
		echo "</select></TD></TR><TR><TH>Etat</TH><TD><input name='etat' value='$etat' readonly></TD></TR>";
		echo "<TR><TH>Type</TH><TD><SELECT name='Pret'><OPTION value='0'>Location</OPTION><OPTION value='1'>Pret</OPTION></SELECT></TD></TR>";
		echo "</TABLE><BR>\n";
		echo "<TABLE border=1><TR><TH>No Client</TH><TD><Input type='text' name='NoClient' readonly><BUTTON type='button' onClick='recherche_client_ajax(0);'>Recherche</BUTTON></TD></TR><TR><TH>Nom</TH><TD><input name='Nom' value=''></TD></TR><TR><TH>Prenom</TH><TD><input name='Prenom' value='' onchange='recherche_client_ajax(0);'></TD></TR><TR><TH>Telephone</TH><TD><input name='Telephone' value=''></TD></TR>";
		echo "<script language=JavaScript>\n";
		echo "this.document.forms['info'].elements['Nom'].focus();\n";
		echo "</script>\n";
		echo "</TABLE><BR>\n";
		echo "<TABLE border=1><TR><TH>Début</TH><TD>";
		echo "<b>Date</b>\n<select name='new_jour_debut' onchange='identifie_tarif();'>\n";
		for ($cpt = 1; $cpt <= 31; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == $jour)
				echo " selected";
			echo ">$cpt</option>\n";
		}
		echo "</select>-";
		echo "<select name='new_mois_debut' onchange='identifie_tarif();'>";
		for ($cpt = 1; $cpt <= 12; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == $mois)
				echo " selected";
			echo ">$cpt " . nom_mois($cpt) . "</option>";
		}
		echo "</select>-";
		echo "<select name='new_annee_debut' onchange='identifie_tarif();'>";
		for ($cpt = $annee - 2; $cpt <= $annee + 2; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == $anneed)
				echo " selected";
			echo ">$cpt</option>\n";
		}
		echo "</select>";
		echo "<b>Heure</b><select name='new_heure_debut' onchange='identifie_tarif();'>";
		for ($cpt = 0; $cpt <= 23; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == ($periode * 2 + 8))
				echo " selected";
			echo ">";
			if ($cpt < 10)
				echo "0";
			echo "$cpt</option>\n";
		}
		echo "</select>:";
		echo "<select name='new_minutes_debut' onchange='identifie_tarif();'>";
		for ($cpt = 0; $cpt <= 45; $cpt += 15) {
			echo "<option value='$cpt'";
			if ($cpt == 0)
				echo " selected";
			echo ">";
			if ($cpt < 10)
				echo "0";
			echo "$cpt</option>\n";
		}
		echo "</select>";
		//		  echo substr($ligne['Debut'],0,4) . " " . substr($ligne['Debut'],5,2) . " " . substr($ligne['Debut'],8,2) . " " . substr($ligne['Debut'],11,2) . " " . substr($ligne['Debut'],14,2);
		echo "</TD></TR>\n";
		echo "<TR><TH>Fin</TH><TD>";
		echo "<b>Date</b><select name='new_jour_fin' onchange='identifie_tarif();'>";
		$date = mktime(0, 0, 0, $mois, $jour + 2, $anneed);
		$a_jour = date("d", $date);
		$a_mois = date("m", $date);
		$a_annee = date("Y", $date);
		for ($cpt = 1; $cpt <= 31; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == $a_jour)
				echo " selected";
			echo ">$cpt</option>\n";
		}
		echo "</select>-";
		echo "<select name='new_mois_fin' onchange='identifie_tarif();'>";
		for ($cpt = 1; $cpt <= 12; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == $a_mois)
				echo " selected";
			echo ">$cpt " . nom_mois($cpt) . "</option>";
		}
		echo "</select>-";
		echo "<select name='new_annee_fin' onchange='identifie_tarif();'>";
		for ($cpt = $annee - 2; $cpt <= $annee + 2; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == $a_annee)
				echo " selected";
			echo ">$cpt</option>\n";
		}
		echo "</select>";
		echo "<b>Heure</b><select name='new_heure_fin' onchange='identifie_tarif();'>";
		for ($cpt = 0; $cpt <= 23; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == ($periode * 2 + 8))
				echo " selected";
			echo ">";
			if ($cpt < 10)
				echo "0";
			echo "$cpt</option>\n";
		}
		echo "</select>:";
		echo "<select name='new_minutes_fin' onchange='identifie_tarif();'>";
		for ($cpt = 0; $cpt <= 45; $cpt += 15) {
			echo "<option value='$cpt'";
			if ($cpt == 0)
				echo " selected";
			echo ">";
			if ($cpt < 10)
				echo "0";
			echo "$cpt</option>\n";
		}
		echo "</select>";
		echo "</TD></TR></TABLE><BR>\n";
		echo "<TABLE border=1><TR><TH>Tarif</TH><TD><select name='new_tarif_type' onChange='modif_tarif();'>";
		for ($cpt = 1; $cpt <= 9; $cpt++) {
			echo "<option value='$cpt'";
			if ($cpt == 0)
				echo " selected";
			echo ">" . nom_tarif($cpt) . "</option>\n";
		}
		$nb_jours = 2;
		$total = $nb_jours * $tarif;
		echo "</select></TD></TR><TR><TH>Prix</TH><TD><input name='tarif' value='$tarif' readonly></TD></TR>";
		echo "<TR><TH>Nombre d'unités</TH><TD><input name='nb_jours' value='$nb_jours' onChange='modif_prix();'></TD></TR>";
		echo "<TR><TH>Rabais %</TH><TD><input name='rabais' value='0' onChange='modif_prix();'></TD></TR>";
		echo "<TR><TH>Total</TH><TD><input name='total' value='$total' onChange='calcule_rabais();'></TD></TR>";
		echo "<TR><TH>Acompte</TH><TD><input name='acompte' value='0'>";
		if (As_Right_Groupe(6)) {
			$resultat2 = ModePayement::getVisibleModePayment();
		} else {
			$no_caisse = 1;
			if ($ligne1 = Poste::getPostesByIP()) {
				$no_caisse = $ligne1['NoCaisse'];
			}
			$resultat2 = ModePayement::getModePayementsByNoCaisse($no_caisse);
		}
		echo " <SELECT name='ModePayement' size='1'>\n";
		foreach ($resultat2 as $ligne2) {
			if (($ligne2['Type'] == 2) && (!Is_Banque())) {
			} else {
				if (As_Right_Groupe(6)) {
					echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . " caisse " . $ligne2['NoCaisse'] . "</option>\n";
				} else {
					echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . "</option>\n";
				}
			}
		}
		echo "</TD></TR>";
		echo "</TABLE><BR>\n";
		echo "<input type='hidden' name='etape' value='0'>\n";
		echo "<button type='submit'>Valider</button><BR>\n";
		echo "</form>";
	}
}
	?>
	</body>

	</html>