function animationClick(element, animation) {
    element = $(element);
    element.addClass('animated ' + animation);
    window.setTimeout(function() {
        element.removeClass('animated ' + animation)
    }, 2000)
}

function leveringSetselect(afb) {
    if (afb == "bezorgen") {
        localStorage.setItem("afhaalbezorg", "BEZORGEN")
        leveringSessionwijzig("BEZORGEN")
        $("#contantlabel").show();
    }
    if (afb == "afhalen") {
        localStorage.setItem("afhaalbezorg", "AFHALEN");
        leveringSessionwijzig("AFHALEN")
    }
}

function leveringSessionwijzigcart() {
    var checkedValue = $('#switchsw:checked').val();
    if(checkedValue=='on'){
    levering="BEZORGEN"
    localStorage.setItem("afhaalbezorg", "BEZORGEN");
    }else{
    levering="AFHALEN"
    localStorage.setItem("afhaalbezorg", "AFHALEN");
    }
    //alert($('#switchsw').val())
    $.post("classes/leveringset.php", {
        levering: levering
    }, function(e) {
        $("#cart").load('classes/cart.php');
                        $(".totaalshow").load('classes/totaal.php', {
                            totaal: "inc"
                        });

                        $(".aantam").load('classes/aantal.php');


                        
    })
}


function leveringSessionwijzig(levering) {
    $.post("classes/leveringset.php", {
        levering: levering
    }, function(e) {
        location.reload()
    })
}

function leveringSession(levering, postcode, huisnr, plaats, adres, wat) {
    $.post("classes/leveringset.php", {
        levering: levering,
        postcode: postcode,
        huisno: huisnr,
        plaats: plaats,
        adres: adres
    }, function(e) {
        if (wat == 4) {
            location.reload()
        }
    })
}

function postcodestart(postc, huisn, levering, wat) {
    $.ajax({
        type: 'POST',
        url: 'https://www.dobizzz.nl/doposv2/postcodeapi.php?postcode=' + postc + '&huisnr=' + huisn,
        success: function(data) {
           
            if (data.status == 'ok') {
                var result = data.results[0];
                localStorage.setItem('adres', result.street + " " + huisn);
                localStorage.setItem('plaats', result.city);
                localStorage.setItem('lat', result.lat);
                localStorage.setItem('long', result.lng);
                localStorage.setItem('huisnr', huisn);
                localStorage.setItem('straat', result.street);
                leveringSession(levering, postc, huisn, result.city, result.street + " " + huisn, wat)
            }
        },
        error: function(data) {
            //console.log(data)
        }
    })
}

function postReview() {
    var e = "chksession=true";
    jQuery.ajax({
        type: "POST",
        url: "classes/chk_session.php",
        data: e,
        cache: !1,
        success: function(e) {
            if (e == "0") {
                window.location.replace("postcode.php")
            }
        }
    })
}

function CheckForSession() {
    var e = "chksession=true";
    jQuery.ajax({
        type: "POST",
        url: "classes/chk_session.php",
        data: e,
        cache: !1,
        success: function(e) {
            if (e == "0") {
                window.location.replace("postcode.php")
            }
        }
    })
}
function validateForm(e) {
  let x = document.forms["myForm"]["fname"].value;
  if (x == "") {
    alert("Naam invullen");
    return false;
  }
}

pizzacart = {

 doBounce: function(element, times, distance, speed) {
    for(i = 0; i < times; i++) {
        element.animate({marginTop: '-='+distance},speed)
            .animate({marginTop: '+='+distance},speed);
    }        
},


    besteloverzichtx: function(e) {
        $.post("bestelling_overzicht.php", {
            bestellingno: e
        }, function(e) {
            $("#besteloverzicht").html(e)
        })
    },

  aantaltonen: function() {

$.getJSON("classes/artikelenaantal.php", function(response) {
$('.aantinb').html('');
for(var i in response) {
//console.log(response[i]['aantal']+" x "+response[i]['artikel']);
$('.artclass_'+response[i]['artikel']).html('<div class="aantinb">'+response[i]['aantal']+'</div>');
    
}
    });


    },







/////////////////////////////////


    toevoegenoptietotaal: function(e, prijsexopties, artid, opm, deoptie) { 
       // alert(artid)

                    $.post("classes/basketopties.php", $("#" + e).serialize(), function(a) {

                         acode = JSON.parse(a)
                        //console.log(a)

                        var prijs= acode[0];
                        var statiegeld= acode[1];
                        var sup= acode[2];
                        var prijsactie= acode[3];
                        var statiegeldactie= acode[4];
                        var supactie= acode[5];


                       // console.log(prijs)
                        //console.log(statiegeld)
                       // console.log(sup)
                       // console.log(prijsactie)
                       // console.log(statiegeldactie)
                       // console.log(supactie)
                       // console.log(prijsexopties)
                       // console.log(deoptie)
                        
                        if(opm==1)
                        prijsextraopm(prijs, statiegeld, sup, prijsactie, statiegeldactie, supactie, prijsexopties,artid);
                        else
                        prijsextra(prijs, statiegeld, sup, prijsactie, statiegeldactie, supactie, prijsexopties,artid);
                       
                        
                    })
                },


    ///////////////////////////////























    toevoegen: function(e) { 
        
        var text = "";
        var texttemp = "";
        var checked;  
      // $('.panel-details').find(".collapse.show").each(function(){
       // $(this).collapse("hide")
      //  });
        $('.panel-details').each(function(){
          checked = 0;  
            aa = $(this).find('.optietitels');
            link = $(this).find('h5');
       
        $(this).find('input').each(function(){
            
            if(!$(this).prop('required')){
            checked = 1;
            
            } else {
                if ($(this).prop('checked')) {
                    checked++;

                }

            }


        });

                    if(checked<1){
                    $(this).find(".collapse").each(function(){
                        $(this).collapse("show")
       texttemp += "<div style='background:red; color:#fff; width:100%; border-radius:5px; padding:5px; margin-top:5px;'>"+ aa.text() + "</div>";                 
                         
                    });

                   }
               
        });
        if(texttemp>"" ){
            notie.alert(4, texttemp, 2);
        }else{
        var t = "chksession=true";
        jQuery.ajax({
            type: "POST",
            url: "classes/chk_session.php",
            data: t,
            cache: !1,
            success: function(t) {
                if (t == "1") {
                    $.post("classes/basket.php", $("#" + e).serialize(), function(a) {
                        $("#cart").load('classes/cart.php');
                        $(".totaalshow").load('classes/totaal.php', {
                            totaal: "inc"
                        });
   
                        $(".aantam").load('classes/aantal.php');
                        pizzacart.doBounce($(".aantam"), 4, '5px', 100);
                      
                    })
                } else {
                    $.post("classes/basket.php", $("#" + e).serialize(), function(a) {
                        $("#cart").load('classes/cart.php');
                        $(".totaalshow").load('classes/totaal.php', {
                            totaal: "inc"
                        });

                        $(".aantam").load('classes/aantal.php');
                        pizzacart.doBounce($(".aantam"), 4, '5px', 100);
                        
                    })
                }

                if (localStorage.getItem('postcode') === null) {
                    postcodecheck('', 4)
                }
                pcode = e.split("artikelform");
                var ret = e.replace('artikelform','');
                pizzacart.aantaltonen();
                pizzacart.doBounce($(".artclass_"+ret), 4, '5px', 100); 
            }
        })
        bkShow();
      $('.bsluiten').trigger( "click" );
      $('.bsluitenprod').trigger( "click" );
   
  }
    },
    toevoegenplus: function(e) {
        $.post("classes/basket.php", {
            plusproduct: e
        }, function(a) {
            $("#cart").load('classes/cart.php');
            $(".totaalshow").load('classes/totaal.php', {
                totaal: "inc"
            });

            pcode = e.split(":");
            pizzacart.aantaltonen();
            pizzacart.doBounce($(".artclass_"+pcode[0]), 4, '5px', 100);
            $(".aantam").load('classes/aantal.php');
            pizzacart.doBounce($(".aantam"), 4, '5px', 100);
            $("#gratiscontainer").load('classes/gratisfirst.php');
            $("#gratiscontainerkorting").load('classes/gratis.php');
            $("#gratiscontainermin").load('classes/totaal_gratis.php');
        })
          bkShow();
    },
    toevoegenmin: function(e) {
        $.post("classes/basket.php", {
            minproduct: e
        }, function(a) {
            $("#cart").load('classes/cart.php');
            $(".totaalshow").load('classes/totaal.php', {
                totaal: "inc"
            });
            pcode = e.split(":");
            pizzacart.aantaltonen();
            pizzacart.doBounce($(".artclass_"+pcode[0]), 4, '5px', 100);
            $(".aantam").load('classes/aantal.php');
            pizzacart.doBounce($(".aantam"), 4, '5px', 100);
            $("#gratiscontainer").load('classes/gratisfirst.php');
            $("#gratiscontainerkorting").load('classes/gratis.php');
            $("#gratiscontainermin").load('classes/totaal_gratis.php');
        })
          bkShow();
    },
    artikelopties: function(a, p, t, korting, kortingbereken, kortingid, voucherart) {
          

        $.post("classes/optiesclass.php", {
            artikel: a,
            prijs: p,
            prijstoon: t,
            prijskorting: korting,
            kortingtoon: kortingbereken,
            kortingid: kortingid,
            voucherart: voucherart
        }).done(function(data) {
        	$('#optiecontainer').html("");
            $('#optiecontainer').append(data);
            $("#productModal").modal("show");
            $("#postcodeModal").css("left", "0");
            $('body').removeAttr('style')
        
        })
        setTimeout('$("#btncart").removeAttr("disabled")', 1500)
    },



    toevoegenopties: function(e) {
        var t = "chksession=true";
        jQuery.ajax({
            type: "POST",
            url: "classes/chk_session.php",
            data: t,
            cache: !1,
            success: function(t) {}
        })
    },
    levering: function(e, t, n) {
        $.post("basketinner.php", {
            geselecteerd: e,
            postcode: t,
            huisno: n
        }, function(t) {
            sessi(e);
            $("#cart").html(t)
        })
    },
    bankkosten: function(e) {
        $.post("classes/basketinner.php", {
            transactie: e
        }, function(e) {
            $("#cart").html(e)
            $(".totaalshow").load('classes/totaal.php', {
                totaal: "inc"
            });

        })
    },
    verwijderen: function(e) {
        $.post("classes/basket.php", {
            artikeldel: e
        }, function(a) {
            $("#cart").load('classes/cart.php');
            $(".totaalshow").load('classes/totaal.php', {
                totaal: "inc"
            });
            pcode = e.split(":");
            pizzacart.aantaltonen();
            $(".aantam").load('classes/aantal.php');
            $("#gratiscontainer").load('classes/gratisfirst.php');
            $("#gratiscontainerkorting").load('classes/gratis.php');
            $("#gratiscontainermin").load('classes/totaal_gratis.php');
        })

    },

    voucherverwijderen: function(e, a) {
        $.post("classes/basket.php", {
            voucherdel: e,
            vaantal: a
        }, function(e) {
            $("#cart").load('classes/cart.php');
            $(".totaalshow").load('classes/totaal.php', {
                totaal: "inc"
            });

            $(".aantam").load('classes/aantal.php');
            $("#spaarpunt").load('classes/aantalpunten.php');
            $("#spaarpuntm").load('classes/aantalpunten.php')
        })
    },
    voucher: function(id,punten) {

        $.post("classes/basket.php", {
            voucher_id: $("#vouchercode").val(),
            voucher_art: id,
            voucher_punten: punten
        }, function(e) {
            //console.log(e)
            if (e == 0) pizzacart.message("Ingevoerde code is niet juist of niet geldig meer!")
            if (e == 1) pizzacart.message("Coupon " + $("#vouchercode").val() + " toegevoegd!")
            if (e.length > 5){

            acode = JSON.parse(e)
            //console.log(acode)
            hidePanelCart();
            pizzacart.artikelopties(acode[0],'0','0','0','0','0',acode[0]);
         }


            //else pizzacart.message("Coupon " + $("#vouchercode").val() + " toegevoegd!")
            $("#cart").load('classes/cart.php');
            $(".totaalshow").load('classes/totaal.php', {
                totaal: "inc"
            });

            $(".aantam").load('classes/aantal.php');
            //$("#aantaw").load('classes/aantal.php');
            $("#spaarpunt").load('classes/aantalpunten.php');
            $("#spaarpuntm").load('classes/aantalpunten.php');
            $('#sb-search-submit').removeClass('sb-search-submit-open');
            $('#sb-search').removeClass('sb-search-open');
            $('#vouchercode').removeClass('sb-input-open');
            $('#sb-icon-search').html("COUPONCODE")
        })
    },
    /*
    vouchercheck: function(id, punten, li) {
        $.post("classes/aantalpunten.php", {}, function(e) {
            if (li == 1) {
                if (e >= punten) pizzacart.voucher(id,punten)
                else pizzacart.message('Je hebt onvoldoende punten!')
            } else {
                pizzacart.message('Je bent niet ingelogd!')
            }
        })
    },
*/
    vouchercheck: function(id, punten, li) {
        $.post("classes/aantalpunten.php", {}, function(e) {
            if (li == 1) {
                if (punten < 0 ) {
                    pizzacart.message('Er is iets mis met deze Voucher.<br>Neem contact met ons op.')
                }
                else{
                if (e >= punten) 
                    pizzacart.voucher(id, punten)
                else 
                    pizzacart.message(geenpunten)
            }
            } else {
                pizzacart.message(nietingelogd)
            }
        })
    },



    driveinntijd: function(e) {
     
           $("#bezorgtijd").load('classes/functions.php', {
                soort: e
            });

       
    },
    groeplist: function() {
    $("#groepModal").modal("show");
    },
    message: function(msg) {
        notie.alert(4, '<img src="dontcopy/logo.png" width="100px;"><br>' + msg, 2)
    },
    voucherart: function(id) {
        $("#voucherid").val(id);
        pizzacart.voucher()
    },
    reserveren: function(e) {
        $("#defaultform").submit(e)
    },
    infotoon: function() {
        $('#infomodalinner').load("classes/info.php");
        $("#infoModal").modal("show")
    },
    logintoon: function() {
        $("#panel-mobile").removeClass("show");
        $("#logininner").load('login/index.php?url=bestellen.php');
        $("#loginModal").modal("show")
    },
    resettoon: function() {
        $("#logininner").load('login/password_reset.php');
        $("#loginModal").modal("show")
    },
    logout: function() {
        var currentURL = window.location.href.split('#')[0];
        currentURL = window.location.href.split('?')[0];
        window.location.href = "index.php?logout"
    },
    driveinlinktoon: function() {
        $("#panel-mobile").removeClass("show");
        $("#driveinlinkinner").load('classes/driveinnlinkstuur.php');
        $("#driveinlinktoon").modal("show")
    },
    toonkentekenbox: function() {
        $("#panel-mobile").removeClass("show");
       // $("#driveinlinkinner").load('classes/driveinnlinkstuur.php');
        $("#driveinboxtoon").modal("show")
    },
}