<?php

	session_start();
	
	error_reporting(0);
	
	if(!empty($_POST) )
	{
		$_SESSION['sauvegarde_formulaire'] = $_POST ;
     
		$page_actuelle = $_SERVER['PHP_SELF'] ;
		if(!empty($_SERVER['QUERY_STRING']))
		{
			$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		}
     
		header('Location: ' . $page_actuelle);
		exit;
	}

	if(isset($_SESSION['sauvegarde_formulaire']))
	{
				$_POST = $_SESSION['sauvegarde_formulaire'] ;
				unset($_SESSION['sauvegarde_formulaire']);
	}


	$nom_erreur = "";
	$prenom_erreur = "";
	$email_erreur = "";
	$sujet_erreur = "";
	$message_erreur = "";
	$captcha_erreur = "";
	
	$correct = "1";
	$affiche_message = "0";

	$choix1_erreur = "";
	$choix2_erreur = "";
	$note_erreur = "";
	
	
	$champ_nom = "";
	$champ_prenom = "";
	$champ_email = "";
	$champ_sujet = "";
	$champ_message = "";
	
	$val1 = "";
	$val2 = "";
	
	/*if(!empty($_POST)){
    //
    // Debug
    //
    echo '<b>Variables</b> :<br />';
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
}*/

	//error_reporting(0);
	
	if(isset($_POST['valide'])) 
	{
		//$val1 =  $_SESSION['captcha'] ;
		//$val2 =  $_POST["code"];
	
		$champ_nom = $_POST['nom'];
		$champ_prenom = $_POST['prenom'];
		$champ_email = $_POST['email'];
		$champ_sujet = $_POST['sujet'];
		$champ_message = $_POST['message'];
		
		$val1 = $_POST['choix1'];
		$val2 = $_POST['choix2'];
		$note = $_POST['note'];
	
		if(empty($_POST["code"]))
		 {
			$captcha_erreur = "Code obligatoire";
			$correct = "0";
		 } 
		else {
				if( $_SESSION['captcha'] != $_POST['code'])
				//if( $val1 != $val2)
				{		
				  $captcha_erreur = "Code erroné";
				 $correct = "0";
				}
		}
	
		 if(empty($_POST["nom"]))
		 {
			$nom_erreur = "Nom obligatoire";
			$correct = "0";
		 } 
		 else {
				$nom = test_input($_POST["nom"]);
				if (!preg_match("/^[a-zA-Z ]*$/",$nom)) 
				{
					$nom_erreur = "Seules les lettres et les espaces sont autorisés";
					$correct = "0";
				}
		}
	
		 if(empty($_POST["prenom"]))
		 {
			$prenom_erreur = "Prénom non renseigné";
			$correct = "0";
		 } 
		 else {
				$prenom = test_input($_POST["prenom"]);
				if (!preg_match("/^[a-zA-Z ]*$/",$prenom)) 
				{
					$prenom_erreur = "Seules les lettres et les espaces sont autorisés";
					$correct = "0";
				}
		}
	
		if (empty($_POST["email"]))
		{
			$email_erreur = "Email non renseigné ";
			$correct = "0";
		} 
		else {
				$email = test_input($_POST["email"]);
    			if (!filter_var($email, FILTER_VALIDATE_EMAIL))
				{
					$email_erreur = "Email invalide !";
					$correct = "0";
				}
		}
	
		if (empty($_POST["sujet"]))
		{
			$sujet_erreur = "Sujet non renseigné ";
			$correct = "0";
		} 
		else {
				$sujet = test_input($_POST["sujet"]);
    			if (!preg_match("/^[a-zA-Z ]*$/",$sujet)) 
				{
					$sujet_erreur = "Seules les lettres et les espaces sont autorisés";
					$correct = "0";
				}
		}
	
		if (empty($_POST["message"])) 
		{
			$message_erreur = "Vide !";
			$correct = "0";
		} 
		
		if(isset($_POST['choix1']))
		{
			$choix1_erreur = "";
		
		}
		else
		{
			$choix1_erreur = "Merci de faire un choix";
			$correct = "0";
		}

		if(isset($_POST['choix2']))
		{
			$choix2_erreur = "";
		}
		else
		{
			$choix2_erreur = "Merci de faire un choix";
			$correct = "0";
		}
		
		if(isset($_POST['note']))
		{
			if($_POST['note'] == "Choix")
			{	$note_erreur = "Veuillez donner une note.";
				$correct = "0";
			}
		}
		
		
		
		if($correct == "1")
		{
			$user = "root";
			$password = "";
			$host = "localhost";
			$db = "test";
		
			$db_connection = mysql_connect($host, $user, $password);
			mysql_select_db($db, $db_connection); 
	
			if(!$db_connection)
			{
				die('Could not connect: ' . mysql_error());
			}

			$nom = $_POST['nom'];
			$prenom = $_POST['prenom'];
			$email = $_POST['email'];
			$sujet = $_POST['sujet'];
			$message = $_POST['message'];
			$date = date("d-m-Y");
			$heure = date("H:i:s");
			$concate = $val1 ."-" .$val2;
			$note =  $_POST['note'];
			
			$sql = "INSERT INTO avis VALUES ('', '$nom', '$prenom' , '$email', '$sujet', '$message', '$date', '$heure', '$concate', '$note')";
			//echo $sql;

			mysql_query($sql);
			mysql_close();
			
			$affiche_message = "1";
			
	    }	

	}	
	
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}	
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head>
<title>Votre avis sur la location ...</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="style.css" type="text/css">
<!--
<link href="http://www.mediavacances.com/favicon.ico" rel="shortcut icon">
-->


<style>
.error {color: #FF0000;}
</style>


</head>

<body>


<div id="page">
<!--
		<div id="menu_navigation">

			    <div class="section imp1">

        <form id="connexion-zone" method="post" action="renter-login.php">
            <input class="moyen" id="cz-email" name="login" placeholder="Adresse e-mail" type="text">
            <input class="moyen" id="cz-password" name="password" placeholder="Mot de passe" type="password">
            <input class="bouton_action3" name="connexion" value="Connexion" type="submit">
            <p><a href="http://www.mediavacances.com/renter-forgot-password.php">Mot de passe oublié ?</a></p>
        </form>
    </div>


<div id="menu_loc_informations" class="section imp2">

	<p class="titresection">Informations</p>

	<ul class="lienssection">
        <li id="zl_advantages" class="lienmenupuce"><a href="http://www.mediavacances.com/renter-features.php">Les avantages MediaVacances.com</a></li>
		<li id="zl_iphone" class="lienmenupuce"><a href="http://www.mediavacances.com/iphone-app-renters.php">Application iPhone gratuite</a></li>
		<li id="zl_who" class="lienmenupuce"><a href="http://www.mediavacances.com/who.php" title="Qui sommes-nous ?">Qui sommes-nous ?</a></li>
	</ul>

</div>
		</div>
        -->

		<div id="contenu">

			<div id="cartouche_titre_listing">
				<div id="intitule">
                <h1>Laissez- nous votre avis</h1>
				</div>
			</div>
			<div class="bloc_principal_texte">
			<h2>Message</h2>
				
	    	<br>
	
			<p><span class="error">* Champs obligatoires.</span></p>
	
	        <form name="form_commentaires" method="POST" action="avis.php" style="margin:0px;">
<!--
	<table style="border: 1px dashed rgb(121, 119, 119);" align="center" border="0" width="500" cellpadding="3" cellspacing="0">	
-->    
    		<table align="center" border="0" width="500" cellpadding="3" cellspacing="0">	
	        <tbody>
 
			<tr>
				<td class="cellule1" align="right"><b>Nom : </b></td>
				<td class="cellule1" align="left"><input type="text" name="nom" maxlength="50"  value = "<?php echo $champ_nom; ?>">
				<span class="error">* <?php echo $nom_erreur;?></span></td>
			</tr>
			
			<tr>
				<td class="cellule1" align="right"><b>Prénom : </b></td>
				<td class="cellule1" align="left"><input type="text" name="prenom" maxlength="50" value = "<?php echo $champ_prenom; ?>" >
				<span class="error">* <?php echo $prenom_erreur;?></span></td>
			
			</tr>
			
            <tr>
				<td class="cellule1" align="right"><b>Adresse e-mail :</b></td>
				<td class="cellule1" align="left"><input  type="text" name="email" maxlength="70" value = "<?php echo $champ_email; ?>" >   <!-- &nbsp;<i>Facultatif</i> -->
				<span class="error">* <?php echo $email_erreur;?></span></td>
			</tr>
			
            <tr>
				<td class="cellule1" align="right" width="100"><b>Sujet : </b></td>
				<td class="cellule1" align="left"><input  type="text" name="sujet" maxlength="70" value = "<?php echo $champ_sujet; ?>" > 
				<span class="error">* <?php echo $sujet_erreur;?></span></td>
		    </tr>
				
			<tr>
				<td class="cellule1" align="right" valign="top"><b>Message : </b></td>
				<td class="cellule1" align="left"><textarea rows="10"  name="message" style="width:525px;resize:none" ><?php echo $champ_message; ?></textarea>
				<span class="error">* <?php echo $message_erreur;?></span></td> <!-- style="width:325px;" -->
			</tr>
			
			<tr>
				<td class="cellule1" align="right" valign="top"><b>Questions : </b></td>
				<td class="cellule1" align="left">
				Le locataire a trouvé la location conforme au descriptif de l'annonce ?.
				<span class="error">* <?php echo $choix1_erreur;?></span>
				<br>
<?php

				if($val1 == "oui")
				{
					echo '<input type="radio" name="choix1" value="oui" checked >Oui';
					echo '<br>';
					echo '<input type="radio" name="choix1" value="non" >Non';
				}
				if($val1 == "non")
				{
					echo '<input type="radio" name="choix1" value="oui" >Oui';
					echo '<br>';
					echo '<input type="radio" name="choix1" value="non" checked >Non';
				}
				
				if($val1 == "")
				{
					echo '<input type="radio" name="choix1" value="oui" >Oui';
					echo '<br>';
					echo '<input type="radio" name="choix1" value="non" >Non';
				}
?>		

				<br>
				Le locataire recommandera éventuellement cette location à un ami ?.
				<span class="error">* <?php echo $choix2_erreur;?></span>
				<br>

<?php
				if($val2 == "oui")
				{
					echo '<input type="radio" name="choix2" value="oui" checked >Oui';
					echo '<br>';
					echo '<input type="radio" name="choix2" value="non" >Non';
				}
				if($val2 == "non")
				{
					echo '<input type="radio" name="choix2" value="oui" >Oui';
					echo '<br>';
					echo '<input type="radio" name="choix2" value="non" checked >Non';
				}
				
				if($val2 == "")
				{
					echo '<input type="radio" name="choix2" value="oui" >Oui';
					echo '<br>';
					echo '<input type="radio" name="choix2" value="non" >Non';
				}

				echo $note;
?>			
				
				<br>
				Donner une note pour evaluer votre degré de satisfaction  ?.
				<span class="error">* <?php echo $note_erreur;?></span>
				<br>
				<select name="note" size="1">
<?php
				if($note == "Choix")
				{   echo '<option value= "Choix" selected="selected">Choix </option>';
				}
				else
				{
					 echo '<option value= "Choix">Choix </option>';
				}
				
				if($note == "Insuffisant")
				{   echo '<option value= "Insuffisant" selected="selected">Insuffisant</option>';
				}
				else
				{
					echo '<option value= "Insuffisant">Insuffisant</option>';
				}
				
				if($note == "Peu satisfait")
				{   echo '<option value= "Peu satisfait" selected="selected">Peu satisfait</option>';
				}
				else
				{
					echo '<option value= "Peu satisfait">Peu satisfait</option>';
				}
				
				if($note == "Assez satisfait")
				{   echo '<option value= "Assez satisfait" selected="selected">Assez satisfait</option>';
				}
				else
				{
					 echo '<option value= "Assez satisfait">Assez satisfait</option>';
				}
				
				if($note == "Satisfait")
				{   echo '<option value= "Satisfait" selected="selected">Satisfait</option>';
				}
				else
				{
					echo '<option value= "Satisfait">Satisfait</option>';
				}
				
				if($note == "Trés Satisfait")
				{   echo '<option value= "Trés Satisfait" selected="selected">Trés Satisfait</option>';
				}
				else
				{
					echo '<option value= "Trés Satisfait">Trés Satisfait</option>';
				}
?>				
<!--				<option>Choix
				<option>1 non satisfait
				<option>2 
				<option>3 
				<option>4 Satisfait
				<option>5 Trés satisfait -->
				</select>
				<br>
				
<!--				
				<br>
				Le locataire recommandera éventuellement cette location à un ami ?.
				<span class="error">* <?php echo $choix2_erreur;?></span>
				<br>
				<input type="radio" name="choix2" value="oui">Oui
				<br>
				<input type="radio" name="choix2" value="non">Non
-->				
				
				</td> 
			</tr>
			
			
			
			
            <tr>
				<td class="cellule1" align="right"></td>
				<td class="cellule1" align="left">
                    <div id="captcha_sous">
						<img src="captcha.php" alt="Captcha" />
						<p id="explain">Recopiez le code :</p>
						<input id="print" name="print" value="" type="hidden">
						<input type="text" id="code" name="code" maxlength="6" 	type="text">
						<span class="error">* <?php echo $captcha_erreur;?></span>		
					</div>   
						
				</td>
			</tr>
<!--			
			<tr>
            	<td class="cellule1" colspan="2" align="left">
                <input name="d[reponse_mail]" value="y" type="checkbox">&nbsp;Etre prévenu de la réponse par email            </td>
			</tr>
-->		
        	<tr>
            	<td class="cellule1" colspan="2" align="center" height="60">
                <!-- <input name="send" class="bouton_action3" value="Envoyer votre message" type="submit"> -->
				<INPUT TYPE="submit" NAME="valide" VALUE="Valider">
            </td>
			</tr>

<?php
			if($affiche_message == "1")
			{
			   echo '<tr>
            	     <td class="cellule1" colspan="2" align="center" height="60">
                     Votre avis vient d\' être pris comptes
					 <a href="mapage.htm"    >        Retour au site</a>
                     </td>
			         </tr>';
			}		 
?>			
			
		</tbody>
        </table>
	</form>
	

            </div>

		</div>

	</div>

<link rel="stylesheet" href="css.css" type="text/css">

</body>
</html>


<?php
					 
					 
//http://www.w3schools.com/php/php_form_complete.asp
//http://openclassrooms.com/courses/eviter-le-renvoi-repetitif-d-un-formulaire-en-rafraichissant
?>