// ---------------------------------------------------------------------------------------------------- //
// Panier
// ---------------------------------------------------------------------------------------------------- //

// ---------------------------------------------------------------------------------------------------- //
// Bouton ajouter au panier
// ---------------------------------------------------------------------------------------------------- //
$j(document).ready(function() {
	$j(".btn_ajout_panier").fancybox({
		hideOnContentClick:false,
		showCloseButton:false,
		frameWidth:600,
		frameHeight:120,
		overlayColor:'#000',
		overlayOpacity:0.8
	});
});

// ---------------------------------------------------------------------------------------------------- //
// Ajouter au panier
// ---------------------------------------------------------------------------------------------------- //
function ajoutPanier(article, quantite,location) {
	var method="GET";
	xhr_object = Navigateur();
	xhr_object.open(method, "/global/includes/ajax/ajout-panier.php?article=" + article + "&qte=" + quantite); 
	xhr_object.onreadystatechange = 
		function() 
		{ 
			if(xhr_object.readyState == 4) {
				var retour = xhr_object.responseText;
				if(retour =="ok") MAJPanierHaut();
			}
		}

	// Header 
	if(method == "POST")
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xhr_object.send(null);
}

// ---------------------------------------------------------------------------------------------------- //
// Modification du panier
// ---------------------------------------------------------------------------------------------------- //
function modifPanier(article,type) {
	
	var method = "GET";
	
	xhr_object = Navigateur();
	// Objet
	xhr_object.open(method, "/global/includes/ajax/modif-panier.php?article=" + article + "&type=" + type); 
	xhr_object.onreadystatechange = 
		function() 
		{ 
			if(xhr_object.readyState == 4) {
				var retour = xhr_object.responseText;
				document.getElementById("panier").innerHTML = retour;
				MAJPanierHaut();
			}
		}
	
	// Header 
	if(method == "POST")
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xhr_object.send(null);
	
}

// ---------------------------------------------------------------------------------------------------- //
// Mise à jour du panier
// ---------------------------------------------------------------------------------------------------- //
function MAJPanierHaut(){
	var method = "GET";
	
	xhr_object = Navigateur();
	// Objet
	xhr_object.open(method, "/global/includes/ajax/maj-panier.php"); 
	xhr_object.onreadystatechange = 
		function() 
		{
			if(xhr_object.readyState == 4) {
				var retour = xhr_object.responseText;
				var newQ = retour.split("#")[0];
				var newPrix = retour.split("#")[1];
				document.getElementById("spPanier").innerHTML = retour;				
				//if(newQ==1)
					//document.getElementById("spPanier").innerHTML = newQ+' article : '+newPrix+' €';
				//else
					//document.getElementById("spPanier").innerHTML = newQ+' articles : '+newPrix+' €';
			}
		}

	// Header 
	if(method == "POST")
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xhr_object.send(null);
}

// ---------------------------------------------------------------------------------------------------- //
// Application d'un bon de réduction
// ---------------------------------------------------------------------------------------------------- //
function valid_bon(code){
	var method = "GET";
	
	xhr_object = Navigateur();
	// Objet
	xhr_object.open(method, "/global/includes/ajax/bon-reduction.php?code=" + code); 
	xhr_object.onreadystatechange = 
		function() 
		{
			if(xhr_object.readyState == 4) {
				var retour = xhr_object.responseText;;
				document.getElementById("panier").innerHTML = retour;	
				MAJPanierHaut();
			}
		}

	// Header 
	if(method == "POST")
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xhr_object.send(null);
}

// ---------------------------------------------------------------------------------------------------- //
// Vérification de la quantité*
// ---------------------------------------------------------------------------------------------------- //
function verifQuantite(){
	var txtquantite = document.getElementById('quantiteProd');
	var btnAddPanier = document.getElementById('btn_ajout_panier');
	if(isNaN(txtquantite.value) || txtquantite.value < 1){
		txtquantite.value="1";
		//btnAddPanier.disabled='true';
		//btnAddPanier.src="/global/img/bt/bt-ajout-panier-inactif.jpg";
	}
	else{
		btnAddPanier.enabled = 'true';
		btnAddPanier.src = "/global/img/bt/bt-ajout-panier-actif.jpg";
	}
}
function Qmoins() {
	if(parseInt(document.getElementById('quantiteProd').value) < 2)
		document.getElementById('quantiteProd').value = "1";
	else
		document.getElementById('quantiteProd').value =	parseInt(document.getElementById('quantiteProd').value) - 1;
}
function Qplus() {
	document.getElementById('quantiteProd').value =	parseInt(document.getElementById('quantiteProd').value) + 1;
}
