﻿//Functie om de bestelling van het winkelmandje uit te voeren
function OrderShoppingCart()
{
    $.post("/ShoppingCart/OrderShoppingCart", null, function (result)
    {
        //Indien de bestelling gelukt is, wordt de functie OnOrderShoppingCartSuccess uitgevoerd (als deze functie bestaat).
        if (result == "")
        {
            //Indien de bestelling gelukt is het winkelmandje opnieuw laden, dat is dan leeg gemaakt
            ReloadShoppingCart();

            if (typeof OnOrderShoppingCartSuccess == 'function')
                OnOrderShoppingCartSuccess();



        }

        //Indien er niemand is ingelogd, wordt de functie OnOrderShoppingCartLoginError uitgevoerd (als deze functie bestaat).
        if (result == "loginerror")
        {
            if (typeof OnOrderShoppingCartLoginError == 'function')
                OnOrderShoppingCartLoginError();

        }

        //Indien er geen producten in het winkelmandje zitten, wordt de functie OnOrderShoppingCartAmountError uitgevoerd (als deze functie bestaat).
        if (result == "amounterror")
        {
            if (typeof OnOrderShoppingAmountError == 'function')
                OnOrderShoppingAmountError();

        }

    });
}



/*Functie om een bepaald product toe te voegen in het winkelmandje. Als het er al in zit wordt het aantal +1 gedaan*/
function AddEntityToShoppingCart(entityId)
{
    var oParams = {};
    oParams["entityId"] = entityId;

    $.post("/ShoppingCart/AddEntity", oParams, function (result)
    {

        ReloadShoppingCart();

    });
}

/*Functie om een bepaald product toe te voegen in de favorieten.*/
function AddEntityToFavorites(entityId)
{
    var oParams = {};
    oParams["entityId"] = entityId;

    $.post("/ShoppingCart/AddToFavorites", oParams, function (result)
    {



    });
}

/*Functie om een bepaald product te verwijderen uit de favorieten.*/
function DeleteEntityFromFavorites(entityId)
{
    var oParams = {};
    oParams["entityId"] = entityId;

    $.post("/ShoppingCart/RemoveFromFavorites", oParams, function (result)
    {



    });
}


/*Functie om het aantal van een bepaald product in het winkelmandje met 1 te verminderen*/
function MinusEntityToShoppingCart(entityId)
{
    var oParams = {};
    oParams["entityId"] = entityId;

    $.post("/ShoppingCart/MinusEntity", oParams, function (result)
    {

        ReloadShoppingCart();

    });
}

/*Functie om het aantal van een bepaald product in het winkelmandje met 1 te vermeerderen*/
function PlusEntityToShoppingCart(entityId)
{
    AddEntityToShoppingCart(entityId);
}


/*Functie om een bepaald product helemaal uit het winkelmandje te vewijderen*/
function DeleteEntityFromShoppingCart(entityId)
{
    var oParams = {};
    oParams["entityId"] = entityId;

    $.post("/ShoppingCart/DeleteEntity", oParams, function (result)
    {

        ReloadShoppingCart();

    });
}

/*Functie om het aantal van een bepaald product in te stellen*/
function UpdateEntityAmountInShoppingCart(entityId, amount)
{
    var oParams = {};
    oParams["entityId"] = entityId;
    oParams["amount"] = amount;

    $.post("/ShoppingCart/UpdateEntityAmount", oParams, function (result)
    {

        ReloadShoppingCart();

    });
}


/*Functie om de onderdelen die aangevinkt zijn als "Winkelmand refresh?" opnieuw te laden*/
function ReloadShoppingCart()
{
    $("div[shoppingCartReload=1]").each(function ()
    {

        var theObject = $(this);

        var oParams = {}
        oParams["contentpartgroupid"] = $(this).attr("shoppingCartReloadContentPartGroupId");

        $.post("/ContentPartGroup/GetContentPartGroupHtmlByContentPartGroupId", oParams, function (result)
        {
            $(theObject).replaceWith(result);
        });


    });
}
