﻿function RefreshCart() {

    $.post("/home/UpdateSCart/" + getLang(), { pid: 0, m: 23 },
           function (data) {
               $("#scart").html(data.Quantity + " " + getCartTopSmallItems() + ": &euro;" + data.Subtotal.toFixed(2));
               $("#scartsubtotaal").html('&euro;' + data.Total.toFixed(2));

               $('#scartshippingcosts').html('&euro;' + data.ShippingCosts.toFixed(2));
               $('#scartprods li').remove();
               if ($("#mand") != null) {
                   $('#mand li').remove();

                   $("#mandsub li").remove();

                   $("#mandsub").append('<li><span >&euro;' + data.Subtotal.toFixed(2) + '</span>' + getCartCount() + ' (' + data.Quantity + ')</li>');
                   //  $("#mandsub").append('<li class="gap"><span>&euro;' + data.Subtotal.toFixed(2) + '</span>Subtotaal</li>');
                   $("#mandsub").append('<li><span>&euro;' + data.ShippingCosts.toFixed(2) + '</span>' + getCartShippingName() + '</li>');

                   if (data.btwLow + data.btwHigh > 0) {
                       $("#mandsub").append('<li><span>&euro;' + data.btwLow.toFixed(2) + '</span>' + 'BTW 6%' + '</li>');
                       $("#mandsub").append('<li><span>&euro;' + data.btwHigh.toFixed(2) + '</span>' + 'BTW 19%' + '</li>');
                   }

                   $("#mandsub").append('<li class="last"><span>&euro;' + data.Total.toFixed(2) + '</span> ' + getCartTotalPrice() + '</li>');

               }



               if ($("#carloginsub") != null) {
                   $('#carloginsub li').remove();

                   $("#cartLogin li").remove();

                   $("#carloginsub").append('<li><span >&euro;' + data.Subtotal.toFixed(2) + '</span>' + getCartCount() + ' (' + data.Quantity + ')</li>');
                   // $("#carloginsub").append('<li class="gap"><span>&euro;' + data.Subtotal.toFixed(2) + '</span>Subtotaal</li>');
                   $("#carloginsub").append('<li><span>&euro;' + data.ShippingCosts.toFixed(2) + '</span>' + getCartShippingName() + '</li>');
                   if (data.btwLow + data.btwHigh > 0) {
                       $("#carloginsub").append('<li><span>&euro;' + data.btwLow.toFixed(2) + '</span>' + 'BTW 6%' + '</li>');
                       $("#carloginsub").append('<li><span>&euro;' + data.btwHigh.toFixed(2) + '</span>' + 'BTW 19%' + '</li>');
                   }
                   $("#carloginsub").append('<li class="last"><span>&euro;' + data.Total.toFixed(2) + '</span>' + getCartTotalPrice() + '</li>');

               }

               $("#orderscartempty").hide();
               $("#orderscart").show();

               if (data.Products.length < 1) {

                   if ($("#maincart") != null) {
                       $("#maincart").hide();
                       $("#orderscartempty").show();

                   }

                   if ($("#orderscart") != null) {
                       $("#orderscart").hide();
                       $("#maincartempty").show();

                   }
               }


               $.each(data.Products, function (i, item) {

                   $("#scartprods").append('<li class="bg"><div class="left"><a href="' + item.Url + '"><img width="85px" src="' + item.Imgsrc + '" alt="" /></a></div><div class="center"><h3>' + item.Name + '</h3><p>' + item.Quantity + ' stuk</p></div><div class="right"><a href="javascript:itemdel(' + item.ID + ')" class="close">close</a><span><strong>&euro;' + item.SubTotalEx.toFixed(2) + '</strong></span></div></li>');


                   if ($("#cartLogin") != null) {
                       $("#cartLogin").append('<li><div class="left"><img width="85px"  src="' + item.Imgsrc + '" alt="" /></div><div class="center"><h3>' + item.Name + '</h3><p>' + item.Quantity + ' stuk</p></div><div class="right"><a href="javascript:itemdel(' + item.ID + ')"><img src="/Content/' + getwapp() + '/i/delete.png" class="delete" alt="" /></a><span>&euro;' + item.SubTotalEx.toFixed(2) + '</span></div></li>');
                   }





                   if ($("#mand") != null) {

                       /*
                       var selectoptions = '';

                       for (i = 1; i < 100; i++) {

                       if (i == item.Quantity) {
                       selectoptions += '<option selected="selected" value="' + item.ID + '_' + i + '">' + i + '</option>';
                       } else {
                       selectoptions += '<option value="' + item.ID + '_' + i + '">' + i + '</option>';
                       }
                       }
                       */

                       $("#mand").append('<li><div class="mid"><div class="top"><div class="bottom"><div class="first"><a href="' + item.Url + '"><img width="72px" alt="" src="' + item.Imgsrc + '"/></a></div><div class="link"><a href="' + item.Url + '"><strong>' + item.Name + '</strong>' + item.Description + '</a></div><div class="form"><input type="text" class="quantity" style="margin: 40px 0; width: 50px;" name="' + item.ID + '" value="' + item.Quantity + '"/></div><span>&euro;' + item.Price.toFixed(2) + '</span><span class="price">&euro;' + item.SubTotal.toFixed(2) + '</span><a class="close" href="javascript:itemdel(' + item.ID + ')">close</a></div></div></div></li>');

                       $("input[name='" + item.ID + "']").keydown(function (event) {
                           // Allow only backspace and delete
                           if (event.keyCode == 46 || event.keyCode == 8) {
                               // let it happen, don't do anything
                           } else {
                               // Ensure that it is a number and stop the keypress
                               if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                                   event.preventDefault();
                               }
                           }
                       });
                   }

               });
               if (typeof Custom != 'undefined') {
                   Custom.init();
               }

           }, 'json');

       }



       function ResetPwd() {
           var x = $("#remail").val();
           var y = getrpwd(x);

           $('#pemail').hide();
           $('#pstatemail').show();
       }


       $(document).ready(function () {
           $("#qpfield").keydown(function (event) {
               // Allow only backspace and delete
               if (event.keyCode == 46 || event.keyCode == 8) {
                   // let it happen, don't do anything
               } else {
                   // Ensure that it is a number and stop the keypress
                   if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                       event.preventDefault();
                   }
               }
           });

           $("input.quantity").live('keyup', function () {
               itemchange($(this).attr('name'), parseInt($(this).val()));
           });
       });


function getrpwd(id) {
    $.post("/home/ResetPwd/" + getLang(), { email: id }, function (data) {
        return data;
    });
}

function subLogin() {

    $.post("/home/CustomerLogin/" + getLang(), $("#customerloginform").serializeArray(),
    function (data) {
        if (data) {
            $('#Password').addClass("error");
            $('#PasswordError').show();
        }
        else {
            $('#Password').removeClass("error");
            $('#PasswordError').hide();
            window.location.href = "/home/Adres/" + getLang();
        }

    }, 'json');
}

function ResellerLogin() {

    $.post("/home/CustomerLogin/" + getLang(), $("#customerloginform").serializeArray(),
    function (data) {
        if (data) {
            $('#Password').addClass("error");
            $('#PasswordError').show();
        }
        else {
            $('#Password').removeClass("error");
            $('#PasswordError').hide();
            window.location.href = "/Home/index/" + getLang();
        }

    }, 'json');
}

function addemailnewsletter() {
    $("#Formnewsletteremailadd").hide();
    $("#thankyouforyouremail").show();
    $.post("/home/addEmail/" + getLang(), $("#Formnewsletteremailadd").serializeArray(),
    function (data) {

    }, 'json');
}

function AddReseller() {

    $.post("/home/AddReseller/" + getLang(), $("#customeraddform").serializeArray(),
           function (data) {

               if (!data.isValid) {

                   if (data.Initials) {
                       $('#Initials').addClass("error");
                       $('#InitialsError').show();
                   }
                   else {
                       $('#Initials').removeClass("error");
                       $('#InitialsError').hide();
                   }

                   if (data.Lastname) {
                       $('#Lastname').addClass("error");
                       $('#LastnameError').show();
                   }
                   else {
                       $('#Lastname').removeClass("error");
                       $('#LastnameError').hide();
                   }

                   if (data.Email) {
                       $('#Email').addClass("error");
                       $('#EmailError').show();
                   }
                   else {
                       $('#Email').removeClass("error");
                       $('#EmailError').hide();
                   }


                   if (data.InvoiceStreet) {
                       $('#InvoiceStreet').addClass("error");
                       $('#InvoiceStreetError').show();
                   }
                   else {
                       $('#InvoiceStreet').removeClass("error");
                       $('#InvoiceStreetError').hide();
                   }


                   if (data.InvoiceZipcode) {
                       $('#InvoiceZipcode').addClass("error");
                       $('#InvoiceZipcodeError').show();
                   }
                   else {
                       $('#InvoiceZipcode').removeClass("error");
                       $('#InvoiceZipcodeError').hide();
                   }


                   if (data.InvoiceCity) {
                       $('#InvoiceCity').addClass("error");
                       $('#InvoiceCityError').show();
                   }
                   else {
                       $('#InvoiceCity').removeClass("error");
                       $('#InvoiceCityError').hide();
                   }


                   if (data.InvoiceCountry) {
                       $('#InvoiceCountry').addClass("error");
                       $('#InvoiceCountryError').show();
                   }
                   else {
                       $('#InvoiceCountry').removeClass("error");
                       $('#InvoiceCountryError').hide();
                   }


                   if (data.Phone) {
                       $('#Phone').addClass("error");
                       $('#PhoneError').show();
                   }
                   else {
                       $('#Phone').removeClass("error");
                       $('#PhoneError').hide();
                   }


                   if (data.Kvk) {
                       $('#Kvk').addClass("error");
                       $('#KvkError').show();
                   }
                   else {
                       $('#Kvk').removeClass("error");
                       $('#KvkError').hide();
                   }


                   if (data.Tax) {
                       $('#Tax').addClass("error");
                       $('#TaxError').show();
                   }
                   else {
                       $('#Tax').removeClass("error");
                       $('#TaxError').hide();
                   }

                   /*
                   if (data.Fax) {
                       $('#Fax').addClass("error");
                       $('#FaxError').show();
                   }
                   else {
                       $('#Fax').removeClass("error");
                       $('#FaxError').hide();
                   }



                   if (data.IBAN) {
                       $('#IBAN').addClass("error");
                       $('#IBANError').show();
                   }
                   else {
                       $('#IBAN').removeClass("error");
                       $('#IBANError').hide();
                   }


                   if (data.BIC) {
                       $('#BIC').addClass("error");
                       $('#BICError').show();
                   }
                   else {
                       $('#BIC').removeClass("error");
                       $('#BICError').hide();
                   }

                   */

                   if (data.CompanyName) {
                       $('#CompanyName').addClass("error");
                       $('#CompanyNameError').show();
                   }
                   else {
                       $('#CompanyName').removeClass("error");
                       $('#CompanyNameError').hide();
                   }

                   if (data.DeliveryStreet) {
                       $('#DeliveryStreet').addClass("error");
                       $('#DeliveryStreetError').show();
                   }
                   else {
                       $('#DeliveryStreet').removeClass("error");
                       $('#DeliveryStreetError').hide();
                   }

                   if (data.DeliveryZipcode) {
                       $('#DeliveryZipcode').addClass("error");
                       $('#DeliveryZipcodeError').show();
                   }
                   else {
                       $('#DeliveryZipcode').removeClass("error");
                       $('#DeliveryZipcodeError').hide();
                   }

                   if (data.DeliveryCity) {
                       $('#DeliveryCity').addClass("error");
                       $('#DeliveryCityError').show();
                   }
                   else {
                       $('#DeliveryCity').removeClass("error");
                       $('#DeliveryCityError').hide();
                   }

                   if (data.DeliveryCountry) {
                       $('#DeliveryCountry').addClass("error");
                       $('#DeliveryCountryError').show();
                   }
                   else {
                       $('#DeliveryCountry').removeClass("error");
                       $('#DeliveryCountryError').hide();
                   }

               }
               else {

                   window.location.href = "/home/requestedReseller/" + getLang();

               }

           }, 'json');


}

function AddCustomer() {

    $.post("/home/AddCustomer/" + getLang(), $("#customeraddform").serializeArray(),
           function (data) {

               if (!data.isValid) {

                   if (data.Initials) {
                       $('#Initials').addClass("error");
                       $('#InitialsError').show();
                   }
                   else {
                       $('#Initials').removeClass("error");
                       $('#InitialsError').hide();
                   }

                   if (data.Lastname) {
                       $('#Lastname').addClass("error");
                       $('#LastnameError').show();
                   }
                   else {
                       $('#Lastname').removeClass("error");
                       $('#LastnameError').hide();
                   }

                   if (data.Email) {
                       $('#Email').addClass("error");
                       $('#EmailError').show();
                   }
                   else {
                       $('#Email').removeClass("error");
                       $('#EmailError').hide();
                   }


                   if (data.InvoiceStreet) {
                       $('#InvoiceStreet').addClass("error");
                       $('#InvoiceStreetError').show();
                   }
                   else {
                       $('#InvoiceStreet').removeClass("error");
                       $('#InvoiceStreetError').hide();
                   }


                   if (data.InvoiceZipcode) {
                       $('#InvoiceZipcode').addClass("error");
                       $('#InvoiceZipcodeError').show();
                   }
                   else {
                       $('#InvoiceZipcode').removeClass("error");
                       $('#InvoiceZipcodeError').hide();
                   }


                   if (data.InvoiceCity) {
                       $('#InvoiceCity').addClass("error");
                       $('#InvoiceCityError').show();
                   }
                   else {
                       $('#InvoiceCity').removeClass("error");
                       $('#InvoiceCityError').hide();
                   }


                   if (data.InvoiceCountry) {
                       $('#InvoiceCountry').addClass("error");
                       $('#InvoiceCountryError').show();
                   }
                   else {
                       $('#InvoiceCountry').removeClass("error");
                       $('#InvoiceCountryError').hide();
                   }



                   if (data.DeliveryStreet) {
                       $('#DeliveryStreet').addClass("error");
                       $('#DeliveryStreetError').show();
                   }
                   else {
                       $('#DeliveryStreet').removeClass("error");
                       $('#DeliveryStreetError').hide();
                   }

                   if (data.DeliveryZipcode) {
                       $('#DeliveryZipcode').addClass("error");
                       $('#DeliveryZipcodeError').show();
                   }
                   else {
                       $('#DeliveryZipcode').removeClass("error");
                       $('#DeliveryZipcodeError').hide();
                   }

                   if (data.DeliveryCity) {
                       $('#DeliveryCity').addClass("error");
                       $('#DeliveryCityError').show();
                   }
                   else {
                       $('#DeliveryCity').removeClass("error");
                       $('#DeliveryCityError').hide();
                   }


                   if (data.DeliveryCountry) {
                       $('#DeliveryCountry').addClass("error");
                       $('#DeliveryCountryError').show();
                   }
                   else {
                       $('#DeliveryCountry').removeClass("error");
                       $('#DeliveryCountryError').hide();
                   }

               }
               else {

                   window.location.href = "/home/Overzicht/" + getLang();

               }

           }, 'json');


}

function itemaddCart(id, quantity) {


    if ($("#qpfield") != null) {
        var y = $("#qpfield").val();
        var x = getitempost(id, y, true);

    }
    else {
        var x = getitempost(id, quantity, true);
    }

    setTimeout(function () {
   window.location.href = $("#urlcart").attr("href");
}, 200);
}

function itemadd(id,quantity) {

    var x = getitempost(id, quantity,true);

    setTimeout(function () {
        RefreshCart();
    }, 1000);

    var hide = "#prodAdded_" + id;

    if (hide != null) {
        setTimeout(function () {
            $(hide).removeClass('current');
        }, 4000);

    }
}

function itemchange(id, quantity) {

    var x = getitempost(id, quantity,false);

    setTimeout(function () {
        RefreshCart();
    }, 1000);
}

function itemdel(id) {

    var x = getdelitempost(id);
    setTimeout(function () {
        RefreshCart();
    }, 1000);
}

function shipmentChanged() {

    var y = ($('#isDeliveryAddress').attr("checked") == "checked");
    var z = getcInvoiceCountry($("#InvoiceCountry option:selected").val());

    if (y) {

        $('#selectDeliveryCountry').val($("#InvoiceCountry option:selected").val());

        var x = getcShipment($("#DeliveryCountry option:selected").val());
        setTimeout(function () {
            RefreshCart();
        }, 1000);
    }
    else {

        $('#selectInvoiceCountry').val($("#InvoiceCountry option:selected").val());

        var x = getcShipment($("#InvoiceCountry option:selected").val());
        setTimeout(function () {
            RefreshCart();
        }, 1000);
    }
}

function getitempost(id, q, cqappend) {
    $.post("/home/AddItem/" + getLang(), { pid: id, quantity: q, append: cqappend }, function (data) {
        return data;
    });

}

function getdelitempost(id) {
    $.post("/home/DelItem/" + getLang(), { pid: id }, function (data) {
        return data;
    });
}

function getcShipment(id) {
    $.post("/home/cShipment/" + getLang(), { pid: id }, function (data) {
        return data;
    });
}

function getcInvoiceCountry(id) {
    $.post("/home/cInvoiceCountry/" + getLang(), { pid: id }, function (data) {
        return data;
    });
}

function manDelAdres(checked) {

    if (checked) {
        $('#DelAdres').show();
        shipmentChanged();
    }
    else {
        $('#DelAdres').hide();
        shipmentChanged();
    }
}



$(document).ready(function () {
    if (typeof Custom != 'undefined') {
        Custom.init();
    }

    RefreshCart();

    var x = $('#isDeliveryAddress');
    if (x != null) {
       
        x.prop("checked", false);

    }


});
