(function(){
var elmProduits=$("#produits");
var baseProduit=$('
\
| \
| \
€ | \
| \
€
| \
');
$.getJSON("/json.php?id=bdc&lang=fr",function(data,status){
var produit=data.produit;
var conditionnement=data.conditionnement;
var elmTotal=$();
var elmErreurs=$();
var form=$();
function calculTotal(livraison){
var total=0;
var nbr_btl=0;
var fdp=0;
var remise=0;
var promo=0;
promo = $(".promo .valeur").text();
if(promo==""){promo=0;}
$.each(produit,function(k,v){
nbr_btl+=v.conditionnement.conditionnement*v.quantite;
if(v.prix_remise!="0.00"){
if (promo>0){
total+=v.quantite*v.prix;
} else {
total+=v.quantite*v.prix_remise;
}
} else {
total+=v.quantite*v.prix;
}
});
if(typeof livraison == 'undefined'){
livraison=0;
}
if(nbr_btl>0 && nbr_btl <7){
fdp=10;
}
if(nbr_btl>6 && nbr_btl <13){
fdp=15;
}
if(nbr_btl>12 && nbr_btl <25){
fdp=20;
}
if(nbr_btl>24 && nbr_btl <42){
fdp=35;
}
if(nbr_btl>41 || livraison>=3){
fdp=0;
}
if(nbr_btl>59 && nbr_btl<150){
remise=3;
}
if(nbr_btl>149){
remise=5;
}
$(".fdp .valeur").text(aff_prix(fdp));
$(".remise .valeur").text(aff_prix(remise));
elmTotal.text(aff_prix(total));
if (remise>0 && promo>0){
$("#remise").css('display', '');
var calcul = total-(total*10/100);
var cumul = total-(total*remise/100)-((total-(total*remise/100))*promo/100);
if(cumul < calcul){
var calculremise = calcul;
//totalttc=total-(calculremise*total/100);
$(".totalttc .valeur").text(aff_prix(calculremise+fdp));
} else {
var calculremise = cumul;
//totalttc=total-(calculremise*total/100);
$(".totalttc .valeur").text(aff_prix(calculremise+fdp));
}
} else if(remise>0 && promo==0){
$("#remise").css('display', '');
totalttc=total-(remise*total/100);
$(".totalttc .valeur").text(aff_prix(totalttc+fdp));
} else if(remise==0 && promo>0){
$("#remise").css('display', 'none');
totalttc=total-(promo*total/100);
$(".totalttc .valeur").text(aff_prix(totalttc+fdp));
}
/*if (typeof $(".remise .valeur") != 'undefined') {
//alert('remise');
totalttc=total-(remise*total/100);
$(".totalttc .valeur").text(aff_prix(totalttc+fdp));
}*/
else {
//alert('sans remise');
$("#remise").css('display', 'none');
$(".totalttc .valeur").text(aff_prix(total+fdp));
}
}
$.each(conditionnement,function(k,v){
v.produit=[];
});
$.each(produit,function(k,v){
var _conditionnement=conditionnement[v.conditionnement];
_conditionnement.produit.push(v);
v.conditionnement=_conditionnement;
v.elm=baseProduit.clone();
//$(".selection",v.elm).append(v.ordre);
var image=$.creeLightbox(v.image, v.image_mini,{minWidth:49,minHeight:175});
$(".image",v.elm).append(image);
/*$(".cadre",v.elm).click(function(){
image.triggerHandler('click');
});*/
image.attr({title:v.titre+" - "+v.conditionnement.titre_bdc+" - "+aff_prix(v.prix)+" €"});
$(".titre",v.elm).text(v.titre);
$(".conditionnement",v.elm).text((function(texte){return texte.substr(0,1).toUpperCase()+texte.substr(1);})(v.conditionnement.titre_bdc));
var promo=0;
promo = $(".promo .valeur").text();
if(promo==""){promo=0;}
if(v.prix_remise!="0.00"){
if (promo>0){
v.prix=parseFloat(v.prix);
$.fancybox.dialog.alert("Un tarif déjà remisé n'est pas cumulable avec un code promo !");
} else {
v.prix=parseFloat(v.prix_remise);
}
} else {
v.prix=parseFloat(v.prix);
}
$(".prix>.valeur",v.elm).text(aff_prix(v.prix));
//v.quantite=0;
var input=$("").val(v.quantite);
$(".quantite",v.elm).append(input);
//initialisation sstot
$(".sstot",v.elm).text(aff_prix(v.prix*v.quantite));
$(".supprimer",v.elm).append('supprimer');
var rad = document.bdc.addr_livr;
var prev = null;
for(var i = 0; i < rad.length; i++) {
rad[i].onclick = function () {
//(prev)? console.log(prev.value):null;
if(this !== prev) {
prev = this;
}
var livraison = this.value;
calculTotal(livraison);
};
}
input.inputNumber({max:v.conditionnement.max||999});
input.change(function(){
v.quantite=toInt(this.value);
calculTotal();
v.elm.toggleClass("selected",v.quantite>0);
//$(".selection",v.elm)[v.quantite>0?"fadeIn":"fadeOut"]();
//mise à jour des sstot
$(".sstot",v.elm).text(aff_prix(v.prix*v.quantite));
});
v.elm.toggleClass("selected",v.quantite>0);
//$(".selection",v.elm).toggle(v.quantite>0);
$(document).ready(function(){
elmProduits.append(v.elm);
});
/*dataLayer.push({
'event': 'begin_checkout',
'ecommerce': {
'items': [{
"id": v.ordre,
"name": v.titre,
"list_name": "Champagne",
"brand": "GONET SULCOVA",
"category": "Champagne",
"variant": v.conditionnement.titre_bdc,
"list_position": v.ordre,
"quantity": v.quantite,
"price": v.prix
}]
}
});*/
});
var titres=$(">.produit>.titre",elmProduits);
titres.height(titres.heighter());
/*
if(!$.browser.msie||$.browser.version<=9)
$(">.produit",elmProduits).each(function(k,v){
$(this)
.css({scale:0})
.hide()
.delay(k*50)
.show()
.animate({scale:1},200);
});
*/
$(function(){
elmTotal=$(".total .valeur");
var panneau_erreur=$("#panneau_erreur");
form=$("#bdc");
form.submit(function(){//test erreurs
var erreurs=[];
var total=0;
$.each(conditionnement,function(k1,v1){
var quantite=0;
$.each(v1.produit,function(k2,v2){
quantite+=v2.quantite;
});
total+=quantite;
if(v1.max!==null && quantite>v1.max)
{
erreurs.push("La commande est limitée à "+accord(v1.max,v1.titre)+".");
return true;
}
var modulo=quantite%v1.quantite;
if(modulo>0){
var complement=(v1.quantite-modulo);
erreurs.push("L'expédition se fait par cartons de "+accord(v1.quantite,v1.titre)+",\nvous avez sélectionné "+accord(quantite,v1.titre)+",\najoutez "+accord(complement,v1.titre)+" pour compléter le carton.");
}
});
if(total==0){
erreurs.push("Vous n'avez sélectionné aucun produit");
}
var elmErreurs=$('');
if(erreurs.length<=0)
return true;
$.each(erreurs,function(k,v){
elmErreurs.append($("- ").text(v));
});
$.fancybox.dialog(elmErreurs);
return false;
});
});
calculTotal();
});
function accord(quantite,nom,nomPluriel){
return quantite+" "+((quantite>1)?(nomPluriel||nom)+"s":nom);
};
//$(document).ready(function() {
$("#addr_livr select[name=addr_livr_salon]").click(function() {
document.getElementById('salon').checked=true;
/*$('#addr_livr_salon').change(function(){
table_fact.animate({height:"hide",opacity:"hide",marginBottom:0},900,'easeInOutCubic',function(){$()});
});*/
});
$("#addr_livr input[name=addr_livr]").click(function() {
//if($("#addr_livr input[name=addr_livr]").val()!=4){
$('#addr_livr_salon option[value=""]').attr('selected', true);
//}
});
//});
$(".suite").click(function(){
//var formOk=true;
if(document.getElementById('salon')!=null){
if(document.getElementById('salon').checked==true && $("#addr_livr select[name=addr_livr_salon]").val()=="")
{
$.fancybox.dialog.alert("Vous n'avez pas choisi de salon !");
return false;
}
else{
$("#bdc").submit();
}
} else {
$("#bdc").submit();
}
});
})();