// clignotement texte
//function clignotement(){
//    if (document.getElementById("a_promotions").style.display=="block") {
//    document.getElementById("a_promotions").style.display="none";
//    } else {
//        document.getElementById("a_promotions").style.display="block";
//    }
//}
//// mise en place de l appel régulier de la fonction toutes les 0.5 secondes
//setInterval("clignotement()", 500);



// accès compte client
$(function(){
	$("#form_compte_client").submit(function(){
		
		mail = $("input.mail").val();
		password = $("input.pass").val();

		$.ajax({
			type: 'POST',
			url: "ajax/acces_compte_client.php",
			data: "&mail="+mail+"&pass="+password+"",
			success: function(data) {
				if(data != 1){
					alert("Ce compte client n'existe pas");
				}else{
					window.location="vente-espace-client.html";
				}
			}
		});
		return false;
		
	});
});


// retourne une taille selon la hauteur d'un enfant
function afficherPanierCommande(){
	$(".loader").show();
	$.ajax({
            type: 'POST',
            url: "ajax/vente-afficher-panier.php",
            data: "",
            success: function(data) {
                $(".loader").remove();
                if(data != 0){
                    $(".panier p").html(data);
                }else{
                    $(".panier p").html("Votre panier est vide");
                }
            }
	});
}


// function vider champs sur mesure
function viderChampsSurMesure(){
	$(".input_surmesure").each(function(){
		$(this).val("");
	});
}


// retourne une taille selon la hauteur d'un enfant
function returnInfosTaillePrixFromHauteur(hauteur_enfant,idArticle){
	$.ajax({
		type: 'POST',
		url: "ajax/vente-return-taille.php",
		data: "&hauteur="+hauteur_enfant+"&idArticle="+idArticle+"",
		
		success: function(data) {
			var objIdTaille = jQuery.parseJSON(data);
			if(objIdTaille.id_modele != null){
				returnInfosTaillePrix(objIdTaille.id_modele,idArticle);
				$(".taille").each(function(){
					$(this).removeClass("tailleSelect");
				});
			}else{
				$(".etiketPrix span.prix").html("");
				$("table.submit td.submit").hide();
				alert("Pas de taille disponible selon vos mesures");
			}
		}
	});
}


// fonction qui récupère les infos taille et prix
function returnInfosTaillePrix(idTaille,idArticle){
	
	// on vide le formulaire
	$("form.addCmd").val();
	
	var indisponible = "Indisponible pour le moment";
	$(".loader").show();
	
	$(".taille").each(function(){
		$(this).removeClass("tailleSelect");
	});
	$("#"+idTaille+"").addClass("tailleSelect");
	
	$.ajax({
		type: 'POST',
		url: "ajax/vente-return-prix.php",
		data: "&idTaille="+idTaille+"&idArticle="+idArticle+"",
		beforeSend : function() { // traitements JS à faire AVANT l'envoi
			$("span.prix").html('<img src="imagesFront/loader.gif" alt="" class="loader"/>'); // ajout d'un loader pour signifier l'action
		},

		success: function(returnInfosTaille) {
			
			$(".loader").remove(); // on cache le loader
			var objTaille = jQuery.parseJSON(returnInfosTaille); // on transforme la chaine de retour Json en objet
			// alert(objTaille.stock);
	
			// article avec promo
			if(objTaille.prixPromo != "undefined"){
				// on affiche les prix, prix promo et réduc (selon les cas)
				$(".etiketPrix span.prix").html(objTaille.prixPromo+" €");
				$(".etiketPrix span.reduc").html(objTaille.reduc+" % de remise");
				$(".etiketPrix span.prixBarre").html(objTaille.prixSsPromo+" €");
				// on renseigne les champs hidden
				$(".article_prix").val(objTaille.prixSsPromo);
				$(".article_prix_promo").val(objTaille.prixPromo);
				$(".article_reduc").val(objTaille.reduc);
			}
			
			// article sans promo
			if(objTaille.prixPromo == ""){
				$(".etiketPrix span.prix").html(objTaille.prixSsPromo+" €");
				$(".etiketPrix span.reduc").html("");
				$(".etiketPrix span.prixBarre").html("");
				// on renseigne les champs hidden
				$(".article_prix").val(objTaille.prixSsPromo);
				$(".article_prix_promo").val();
				$(".article_reduc").val();
			}
			
			// renseignement des champs hidden communs
			$(".article_id").val(idArticle);
			$(".article_stock").val(objTaille.stock);
			$(".article_modele").val(objTaille.idModele);
			
			// affichage du stock
			$("span.stockDispo").html("[ "+objTaille.stock+" en stock ]");

			// gestion du bouton submit
			if(objTaille.stock == 0){
				$("table.submit td.submit").hide();
				$("table.submit td.indisponible").html(indisponible+" en "+objTaille.age);
				$(".qte").hide();
			}else{
				$("table.submit td.submit").show();
				$("table.submit td.indisponible").html("");
				$(".qte").show();
			}
			
		}
	
	});
	
	return false;
	
}

