<?php

	session_start();

	
	
	if(!isset($_SESSION['panier']))
	  {
    			/* Initialisation du panier */
    				$_SESSION['panier'] = array();
    				/* Subdivision du panier */
    				$_SESSION['panier']['id'] = array();
    				$_SESSION['panier']['qte'] = array();
    				$_SESSION['panier']['designation'] = array();
    				$_SESSION['panier']['prix_ttc'] = array();
    				$_SESSION['panier']['prix_ht'] = array();
    				 
   	  }

	

	if(isset($_GET['action']))
	  {
	     if($_GET['action'] == "charge" ) 
               {  
        		$indice = $_GET['elem'];
			
			$connexion = mysql_connect("localhost", "root", "");
	                mysql_select_db("boutique", $connexion);

			
			$requete = "SELECT * FROM panier WHERE id='$indice' ";
     			$result = mysql_query($requete);
			$panier_liste=mysql_fetch_array($result);
			
			//print_r($panier_liste);
			
	    		$articles = explode("," , $panier_liste['panier']);
	  	     	
	  	     	//echo "+++++";
	  	     	//print_r($articles);
	  	     	
	  	     	$nb_articles = count($articles)/4; 
			
			//echo $nb_articles;
			
			 // 0 3 6 
			 
			$connexion = mysql_connect("localhost", "root", "");
       		   	mysql_select_db("boutique", $connexion);
			 
			$ptr = 0;
			 
			//NTH82K, lave_linge, 3, 1438.5, X28Z-22, 3, 2164.5 
			// 0      1    2      3       4   5 
			for($i=0;$i<$nb_articles;$i++)
			{ 
				$ref = $articles[$ptr];
				$ref = trim($ref);
				 $requete = "SELECT * FROM lave_linge WHERE modele='$ref'";
			         $result = mysql_query($requete);
				 $data  = mysql_fetch_assoc($result);
		
				 $select['id'] = $data['id'];
	      			 $select['designation'] = $data['modele'];	
				 $select['qte'] = $articles[2+$ptr];
				 $select['prix_ttc'] = $data['prix_ttc'];
				 $select['prix_ht'] = $data['prix_ht'];
				 
				 
				 if(in_array($ref, $_SESSION['panier']['designation']))
	  	                   { $position = array_search($ref, $_SESSION['panier']['designation']);
 	                    	    // modifie qte
 	                  	    $_SESSION['panier']['qte'][$position] = $_SESSION['panier']['qte'][$position] + $articles[2+$ptr];;
		       	           }  		
				 else
				      {
				       ajout($select);
				      }
				 $ptr = $ptr + 4;
				 
				 
		        }
			
			header('Location: rafraichir_panier.php');
			
			
	  }	

	}

	
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml" lang="fr"><head>
  

<title>Panier virtuel - Achat en ligne  </title>
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">

<link rel="stylesheet" href="gb_styles.css" media="screen,projection" type="text/css">
<link rel="stylesheet" href="gabarit_gps.css" type="text/css">
<link rel="stylesheet" href="style_new.css" type="text/css">
<link rel="stylesheet" href="top_gps.css" type="text/css">
<link rel="stylesheet" href="top.css" type="text/css">
<link rel="stylesheet" href="shopping_cart.css" type="text/css">
<link rel="SHORTCUT ICON" href="http://www.webdistrib.com/favicon.ico">

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


</head>

<body>

		

<script type="text/javascript">

function request(parametre) {
	
	var XHR = null;

		if(window.XMLHttpRequest) // Firefox
			XHR = new XMLHttpRequest();
		else if(window.ActiveXObject) // Internet Explorer
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
		else { // XMLHttpRequest non supporté par le navigateur
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
		}

		if(XHR != null){
		document.getElementById('div1').innerHTML  ="Patientez...";
		}

		// envoie de la requête, methode plus url
		XHR.open('POST','panier.php', true);
		// header et des paramètres
		XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		
		//XHR.send('var=123');
		XHR.send(parametre);

		// on guette les changements d'état de l'objet
		XHR.onreadystatechange = function attente() 
		                           {
						// l'état est à 4, requête reçu !
						if(XHR.readyState == 4)
						  {
							if(XHR.status == 200)
							  { // ecriture de la réponse
							    document.getElementById('div1').innerHTML = XHR.responseText;
			                                  }
		                                  }
		                           }

		// le travail est terminé	
		return;
}


function update_qte(id, val)
 {
 	
 	var valeur = document.getElementById(id).value;
	//alert("Contenu: " + valeur + " item");
 	quantite = parseInt(valeur) + parseInt(val);
	if (quantite < 1) { quantite = 1; }
	if (quantite > 99) { quantite = 99; }
 	document.getElementById(id).value=quantite;
 	

	var element = "prix0";
	prix = document.getElementById(element).value
	//alert("Prix: " + prix + " item");
	
	somme = prix * quantite;
	
	//alert("Tarif: " + somme + " euros");
	
	// rafraichir
	
	//header("Location: dev_panfffier.php");
	//location.reload() ; 
	//window.location.replace("dev_panier.php");
	parametre = 'var=quantite';
	//xhr.send("var="+quantite);
	//request("qte="+quantite);
	//request("index="+id);
	
	var contenu = "update";
	
	data="index="+id+"&qte="+quantite+"&operation="+contenu;
	request(data);
	//data="val_1="+arg1+"&val_2="+arg2+"&val_3="+arg3;



}


function changer_qte(id)
 {
	
	var quantite = document.getElementById(id).value;
	if(quantite < 1) 
	  { quantite = 1; }
 	document.getElementById(id).value=quantite;
	
	//alert("Contenu: " + quantite );
	
	var contenu = "update";
	
	data="index="+id+"&qte="+quantite+"&operation="+contenu;
	
	request(data);
}


</script>



			<!-- page -->
		<div style="z-index: 1000; position: relative;" id="page">

			<!-- header -->
			<div id="header">
				<h1><a name="top" href="http://www.materiel.net/">Materiel.net</a></h1>
			</div>


			<!-- nav -->

					<div id="nav">
				<div class="GHeadMenu">

<ul class="nav-line">

<li class=""><a href="../acceuil.php" style="" title="Accueil"><span>Accueil</span></a></li>

<li class="Bonnes affaires"><a href="../catalogue/affaires.php" style="background-image:url(/live/97759.gif);" title="Bonnes affaires"><span>affaires</span></a></li>

<li class="Gros menager"><a href="../catalogue/gros_menager.php" style="background-image:url(/live/97760.gif);" title="Gros menager"><span>Gros Ménager</span></a></li>

<li class="Petit menager"><a href="../catalogue/petit_menager.php" style="background-image:url(/live/97761.gif);" title=" Petit ménager"><span>PEtit Menager</span></a></li>

<li class="Images"><a href="../catalogue/image.php" style="background-image:url(/live/97762.gif);" title="Images"><span>Images</span></a></li>

<li class="Son"><a href="../catalogue/son.php" style="background-image:url(/live/97763.gif);" title="SON"><span>SON</span></a></li>

<li class="Video"><a href="../catalogue/video.php" style="background-image:url(/live/97764.gif);" title="Video"><span>Video</span></a></li>

<li class="Photo et camescope"><a href="../catalogue/photo_camescope.php" style="background-image:url(/live/97769.gif);" title="Photo / cam&eacute;scopes">Photo / cam&eacute;scopes</a></li>

<li class="Telephonie"><a href="../catalogue/telephonie.php" style="background-image:url(/live/97770.gif);" title="T&eacute;l&eacute;phonie">T&eacute;l&eacute;phonie </a></li>

</ul>
<ul class="nav-line">

<li class="art de la table"><a href="../catalogue/art_de_la_table.php" style="background-image:url(/live/97766.gif);" title="Art de la table"><span>Art de la table</span></a></li>

<li class="Accessoires"><a href="../catalogue/accessoires.php" style="background-image:url(/live/97767.gif);" title="Accessoires &amp;Consommables"><span>accessoires &amp;consommables</span></a></li>

<li class="Chauffage & clim"><a href="../catalogue/chauffage_clim.php" style="background-image:url(/live/97768.gif);" title="Chauffage &amp; Clim"><span>chauffage &amp; clim</span></a></li>

<li class="Automobiles"><a href="../catalogue/automobile.php" style="background-image:url(/live/97769.gif);" title="Automobiles"><span>Automobiles</span></a></li>

<li class="GPS"><a href="../catalogue/gps.php" style="background-image:url(/live/97770.gif);" title="GPS"><span> GPS</span></a></li>

<li class="MP3"><a href="../catalogue/mp3.php" style="background-image:url(/live/97771.gif);" title="AUDIO / MP3 / MP4"><span>AUDIO / MP3 / MP4</span></a></li></ul>
			  </div>
				<!--cached-->			</div>


<div id="panier" style="overflow: auto;">

	<div style="padding: 0pt 0pt 0pt 5px; float: left; width: 745px;">
		<table class="BskTitle" width="100%" border="0" cellpadding="0" cellspacing="0">
			<tbody><tr>
				<th width="689" height="40">
				Mon panier <img src="panier_fichiers/PuceBleue.gif" alt="" width="8" height="11">
					<hr>
				</th>
				<td rowspan="2" width="56"><img src="panier_fichiers/BskHeaderIco.jpg" alt="" width="57" height="66"></td>
			</tr>
			<tr>
				<td style="vertical-align: top;" height="26"><a href="http://www.materiel.net/home.html">Accueil &gt;</a></td>
			</tr>
		</tbody></table>
		
		</div>
</div>		
		


<div id="pagewidth">
<div id="header">
<div id="infoscontact"></div>
</div>


<div id="wrapper" class="clearfix">
<form name="form_panier" method="post" action="panier.php">
<div class="TxtOrange"><a href="../home.php" class="TxtOrange">Accueil</a> » 
<a href="shopping_cart.php" class="TxtOrange">Panier</a>
</div>

<div class="btntop" align="right">
</div>
<div id="shoppingcartlisting">
<input id="nbProducts" name="nbProducts" value="2" type="hidden">
<input id="nbProducts2" name="nbProducts2" value="2" type="hidden">


<?php


			print_r($_POST);

			
 	
		  
		        
		  	if(isset($_POST['operation']))
		   	  {
		  	    if($_POST['operation'] == "update")	
		  	      {  echo "bilan";
		  	         $pos = $_POST['index'];
		  	         $_SESSION['panier']['qte'][$pos] = $_POST['qte'];
		  	         //header('Location: ajouter_panier.php'); 
		  	      }
		  	  }     
		  
		   	
		   	if(isset($_GET['caddie']))
		   	  {
		   	
		   	      $id = $_GET['id']; 
		   	      $caddie = $_GET['caddie']; 

		   		if( $caddie=="ajout" )
	                  	  {
	                        	//echo '<body onLoad="alert(\'ajouter un produit au panier ...\')">'; 	
	                    		if(in_array($id, $_SESSION['panier']['designation']))
	  	                  	  { $position = array_search($id, $_SESSION['panier']['designation']);
	 	                    	    // modifie qte
	 	                    	    $_SESSION['panier']['qte'][$position] = $_SESSION['panier']['qte'][$position] + $_POST['qte'];
		       	            	    header('Location: rafraichir_panier.php');
		       	            	    //   var_dump($_SESSION['panier']);
		       	          	}  		
	                  	
	                  		else {  $connexion = mysql_connect("localhost", "root", "");
       		   	                	mysql_select_db("boutique", $connexion);

				        	$requete = "SELECT * FROM lave_linge WHERE modele='$id'";
			                	$result = mysql_query($requete);
				        	$data  = mysql_fetch_assoc($result);
		
				        	$select['id'] = $data['id'];
	      			        	$select['qte'] = $_POST['qte'];
				        	$select['designation'] = $data['modele'];	
				        	$select['prix_ttc'] = $data['prix_ttc'];
				        	$select['prix_ht'] = $data['prix_ht'];
				        	ajout($select);
				        	header('Location: rafraichir_panier.php');

		                    	}		  	   
		   	         }
		   	
		   	}	
		   	  
		   	
		   	
		   	if(isset($_GET['action']) && isset($_GET['indice']) )
		   	  {
		   	  	if($_GET['action'] == "plus")
		                  {  
		                    $_SESSION['panier']['qte'][$_GET['indice']] = $_SESSION['panier']['qte'][$_GET['indice']]+1;
			            $_GET['action'] ="";
			            header('Location: rafraichir_panier.php');
			          }
		   	
		   	        if($_GET['action'] == "moins")
		                  { //echo '<body onLoad="alert(\'Plus\')">'; 
		                   $_SESSION['panier']['qte'][$_GET['indice']] = $_SESSION['panier']['qte'][$_GET['indice']]-1;
             		           header('Location: ajouter_panier.php');
             		           if($_SESSION['panier']['qte'][$_GET['indice']] == 0)	
                                     {   
                            	        $action = "efface";
			             }
		                  }
		   	  	
		   	  	
			 	if($action == "efface")
		           	  {    
		                	$indice =  $_GET['indice'];  
		         		array_splice($_SESSION['panier']['id'], $indice, 1);
			 		array_splice($_SESSION['panier']['designation'], $indice, 1);
			 		array_splice($_SESSION['panier']['qte'], $indice, 1);
			 		array_splice($_SESSION['panier']['prix_ttc'], $indice, 1);
				 	array_splice($_SESSION['panier']['prix_ht'], $indice, 1);
				 	print_r($_SESSION['panier']);   
			 		$nb_articles = count($_SESSION['panier']['id']);
			 		header('Location: rafraichir_panier.php');
			           }
		           
			        
			  }
			  
			            //var_dump($_SESSION['panier']);
				
			   	


?>

<?php
function ajout($select)
{
    array_push($_SESSION['panier']['id'],$select['id']);
    array_push($_SESSION['panier']['qte'],$select['qte']);
    array_push($_SESSION['panier']['designation'],$select['designation']);
    array_push($_SESSION['panier']['prix_ttc'],$select['prix_ttc']);
    array_push($_SESSION['panier']['prix_ht'],$select['prix_ht']);
} 

?>	





<div><img src="cart.gif" alt=""> <h1 class="TxtOrange">VOTRE PANIER</h1></div>

<?php

	$nb_articles = count($_SESSION['panier']['id']);

	if($nb_articles != 0)
	  { echo "<div>Votre panier contient "; 
	    echo $nb_articles; 
	    echo " article(s)</div> ";
	  }

	if($nb_articles == 0)
	  { echo "<div>Votre panier est vide </div>";
	  }

?>





<div class="cart">
<div class="cartheader clearfix">
<ul class="ListeHorizontale">
<li class="cartheadercol1">Article</li>
<li class="cartheadercol2">Quantité</li>
<li class="cartheadercol3">Prix unitaire TTC<br>(Eco-participation incluse)</li>
<li class="cartheadercol4">Total</li>
<li class="cartheadercol5">Supprimer</li>
</ul>
</div>

	
	
	

<?php

		$total = 0;


	for($i=0;$i<count($_SESSION['panier']['id']);$i++)
	   { $total+=$_SESSION['panier']['prix_ttc'][$i]*$_SESSION['panier']['qte'][$i];
	     $prix = "prix".$i;
	     $boite ="elem".$i;
?>
		<div class="cartlisting clearfix noLast">
		<ul class="ListeHorizontale">
		<li class="cartlistingcol1">
		<img src="shopping_cart.php_fichiers/m684708.jpg" alt="à definir" width="60"></li>	
		<li class="cartlistingcol2 TxtRouge">
		<a href="http://www.webdistrib.com/cat/product_info.php?productId=684708" class="txtLibelle">
		<b><?php echo $_SESSION['panier']['designation'][$i]; ?></b></a>
		</li>
		<li class="cartlistingcol3">
		<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td>
		

		<input id="<?php echo $i;  ?>" name="<?php echo $i;  ?>" value="<?php echo $_SESSION['panier']['qte'][$i]; ?>"
		onchange="javaScript:changer_qte(<?php echo $i; ?>);"	 size="4" type="text">&nbsp;</td>
		<td>
		
		
		
		
		<a href="panier.php?action=plus&indice=<?php echo $i; ?> ">
		<img src="plus.gif" alt="Ajouter"></a><br>
		
		<a href="panier.php?action=moins&indice=<?php echo $i; ?> ">
		<img src="moins.gif" alt="Retirer"></a>
		
		
		</td></tr></tbody></table>
		
		</li>
		<li class="cartlistingcol4 Txt14"><b><?php echo $_SESSION['panier']['prix_ttc'][$i]; ?> &#8364;</b></li>
		
		<input type="hidden" id="<?php echo $prix; ?>" name="<?php echo $prix; ?>" value="<?php echo $_SESSION['panier']['prix_ttc'][$i]; ?>" />

		<li class="cartlistingcol5 Txt14"><b><?php echo $_SESSION['panier']['prix_ttc'][$i]*$_SESSION['panier']['qte'][$i]; ?> &#8364;</b></li>
		<li class="cartlistingcol6">
		
		<a href="panier.php?action=efface&indice=<?php echo $i; ?> ">
		<img src="del.gif" alt="Supprimer ce produit"></a>
		
	
		</li>
		</ul>
		</div>
		
<?php
	}
	
	//echo "<script>window.location.replace(\"panier.php\")</script>";
?>		




<div class="cartfooter clearfix">
<ul class="ListeHorizontale">
 
 	<li class="cartfootercol1"><a href="#" onclick="javascript:window.open('../dynpop/proforma.php','proforma','width=800,height=600');return
 	false;"><img src="shopping_cart.php_fichiers/proforma.gif" alt="Facture proforma"></a></li>
 
 
<?php 

//	<li class="cartfootercol1"><a href="#" onclick="javascript:window.open('../dynpop/proforma.php','proforma','width=800,height=600');return
// 	false;"><img src="shopping_cart.php_fichiers/proforma.gif" alt="Facture proforma"></a></li>
 

	if($nb_articles == 0)
          { $total = 0.0;
          }	 	

?>	
 
 
<li class="cartfootercol2 TxtOrange"><b>TOTAL DE VOTRE COMMANDE : <span 
class="Txt14 TxtRouge"><?php echo $total; ?> &#8364;</span></b></li>
</ul>
</div>
</div></div>
<div align="right">
<br>
<a href="sauver_panier.php">
<img src="../images/savelw1.png" alt="Continuez vos achats" width="64" height="64" />
<img src="btn_addproduct.gif" alt="Continuez vos achats"></a>&nbsp;&nbsp;


<a href="validation_commande.php">
<img src="btn_passorder.gif" alt="Passez votre commande"></a>
</div>
</form>

<div id="div1">affichage du resultat</div>


<br>
</div>


</div>



</body></html>

<?php
// page retour F5
//echo "<script>location.replace('panier.php');</script>";
?>