﻿function UpdateShopCartInfoBox() {

    $.ajax({

        url: "/Shop/MiniCart",
        global: false,
        cache: false,
        type: "GET",
        dataType: "html",
        success: function(msg) {
            $('#minicart').html(msg);
        }
    });

}


function InsertIntoShopCartFromOverview(button, multiVariant) 
{
    var t = $(button).parents('form').serializeArray();    

    InsertIntoShopCart(t[2]["value"], t[1]["value"], t[0]["value"]);
}

function InsertIntoShopCart(articleId, articleVariantId, amount) {

    var ok = CheckStock($('#amount'), articleVariantId, 1, amount);

    if (ok) {
      
        $.ajax({
            url: "/Shopcart/InsertIntoShopCart",
            global: false,
            cache: false,
            type: "POST",
            dataType: "html",
            data: {
                articleId: articleId,
                articleVariantId: articleVariantId,
                amount: amount
            },
            success: function (msg) {
                UpdateShopCartInfoBox();

                $('.infomessage').fadeToggle();
                window.setTimeout(function () { $('.infomessage').fadeToggle(); }, 4000);

            }
        });
    }
}



function CheckVoucherCode() {

    if ($('#Voucher_Code').val() != "") {

        $.ajax({
            url: "/Shopcart/CheckVoucherCode",
            global: false,
            cache: false,
            type: "POST",
            dataType: "json",
            data: {
                voucherCode: $('#Voucher_Code').val()
            },
            success: function (msg) {

                if (msg.result) {
                    $('#voucherform').submit();
                } else {
                    $('#voucherError').html(msg.message);
                }
            }
        });

    }
}

    

            function zoomimage(obj, dir, id) 
            {

                var file = "";

                switch (dir) {
                    case "enlarge":
                        file = "400-200-" + id;
                        break;

                    case "reduce":
                        file = "200-100-" + id;
                        break;
                }

                $(obj).css("background-image", "url(/images/1/"+file+".png)");               
            
            }



            function changeAmount(obj, value)
            {
                var curr = parseInt($('#' + obj).val());
                var change = parseInt(value);
                
                var new_ = curr + change;

                if (new_ > 0) {
                    $('#' + obj).val(new_);
                   // $('#' + obj).trigger("change"); 
                }
            }




            function CheckStock(obj, articleVariantId, origValue, currValue) {
                //alert(articleVariantId + " - " + origValue + " " + currValue);
                var msg;
                if (!isNaN(parseFloat(currValue))) {

                    msg = $.ajax({

                        url: "/Shop/CheckStock",
                        async: false,
                        global: false,
                        cache: false,
                        type: "POST",
                        data: {
                            articleVariantId: articleVariantId,
                            currValue: currValue
                        },
                        dataType: "html",
                        success: function (msg) {

                        }
                    }).responseText;

                } else {
                    msg = "Es wurde keine Zahl eingegeben";
                }

                if (msg != "") {
                    $(obj).val(origValue);
                    alert(msg);
                    return false;
                } else {
                    return true;
                }

            }
 
