﻿function UpdateProductIdsForShoppingList(checkBoxInstance, productId) {
    if (checkBoxInstance.checked == true) {
        AddToShoppingListEntryList(productId);
    }
    else {
        RemoveFromShoppingListEntryList(productId);
    }
}

function UpdateShoppingListEntryAmount(textBoxInstance, productId) {
    var value = textBoxInstance.value;
    var amount = parseInt(value, 10);
    if (amount < 0
        || isNaN(amount))
        return;
        
    aktionen.webservices.shoppinglistentry_v1.UpdateShoppingListEntryAmount(productId, amount, SucceededCallback, FailedCallback);
}

// adds a product id to the shopping list
function AddToShoppingListEntryList(productId) {
    aktionen.webservices.shoppinglistentry_v1.AddToShoppingListEntryList(productId, SucceededCallback, FailedCallback);
}

// adds a number of ids to a shoppinglist
function AddIdsToShoppingListEntryList(productIds) {
    aktionen.webservices.shoppinglistentry_v1.AddIdsToShoppingListEntryList(productIds, SucceededCallback, FailedCallback);
}

// removes a productid to the shopping list
function RemoveFromShoppingListEntryList(productId)  {
    aktionen.webservices.shoppinglistentry_v1.RemoveFromShoppingListEntryList(productId, SucceededCallback, FailedCallback);
}

// removes a number ids from a shopping list
function RemoveIdsFromShoppingListEntryList(productIds) {
    aktionen.webservices.shoppinglistentry_v1.RemoveIdsFromShoppingListEntryList(productIds, SucceededCallback, FailedCallback);
}

//call back function from web service
function SucceededCallback(result, userContext, methodName) {
    switch (methodName) {
        case ("AddToShoppingListEntryList"):
            {
                //alert("AddToShoppingListEntryList: Done");
                if (result > 0) {
                    CheckUnCheckMarkedProductsCheckBox(true);
                    EnableShoppingListButtons(true);
                }
                break;
            }
        case "AddIdsToShoppingListEntryList":
            {
                //alert("AddIdsToShoppingListEntryList: Done");
                //alert(result);
                if (result > 0) {
                    CheckUnCheckMarkedProductsCheckBox(true);
                    EnableShoppingListButtons(true);
                }
                break;
            }
        case ("RemoveFromShoppingListEntryList"):
            {
                //alert("RemoveFromShoppingListEntryList: Done");
                if (result == 0) {
                    CheckUnCheckMarkedProductsCheckBox(false);
                    EnableShoppingListButtons(false);
                }
                break;
            }
        case ("RemoveIdsFromShoppingListEntryList"):
            {
                //alert("RemoveIdsFromShoppingListEntryList: Done");
                if (result == 0) {
                    CheckUnCheckMarkedProductsCheckBox(false);
                    EnableShoppingListButtons(false);
                }
            }
        case ("UpdateShoppingListEntryAmount"):
            {
                //alert("UpdateShoppingListEntryAmount: Done");
            }
        default:
            {
                break;
            }
    }
}

//if the call to web service was unsuccessfull then this function is executed
function FailedCallback(error, userContext, methodName) {
    if (error !== null) {
        //alert(methodName + ":" + error.get_message());
    }
}

// check or uncheck the checkbox "marked products"
function CheckUnCheckMarkedProductsCheckBox(checked) {
    var markedProductsCheckBox = document.getElementById(markedProductsCheckBoxId);
    if (markedProductsCheckBox == null) {
        return;
    }

    markedProductsCheckBox.checked = checked;
}

function EnableShoppingListButtons(enabled) {
    // sentAsEmailButtonId - defined in ResultList.ascx
    var sentAsEmailButton = $('#' + sentAsEmailButtonId);
    if (sentAsEmailButton != null) {
        sentAsEmailButton.attr('disabled', !enabled);
    }

    // gotoShoppingListButtonId - defined in ResultList.ascx
    var gotoShoppingListButton = $('#' + gotoShoppingListButtonId);
    if (gotoShoppingListButton != null) {
        gotoShoppingListButton.attr('disabled', !enabled);
    }

    // deleteShoppingListEntryButtonId - defined in ResultList.ascx
    var deleteShoppingListEntryButton = $('#' + deleteShoppingListEntryButtonId);
    if (deleteShoppingListEntryButton != null) {
        deleteShoppingListEntryButton.attr('disabled', !enabled);
    }
}

// event handler for maked products checkbox
function MarkedProductsClick() {
    var markedProductsCheckBox = document.getElementById(markedProductsCheckBoxId);
    if (markedProductsCheckBox == null) {
        return;
    }

    // checkbox is checked --> check all checkboxes in the actual list
    if (markedProductsCheckBox.checked == true) {
        var idsToAdd = '';
        $("#ResultTable input:checkbox").not("#" + markedProductsCheckBoxId).each(function() {
            if ($(this).is(':checked') === false) {
                // hiddenfield next to checkbox
                var value = $(this).next().attr('value');
                if (idsToAdd == '') {
                    idsToAdd = value;
                }
                else {
                    idsToAdd += ',' + value;
                }
                $(this).attr('checked', true);
            }
        });

        AddIdsToShoppingListEntryList(idsToAdd);
    }
    // checkbox is unchecked --> uncheck all checkboxes in the actual list
    else {
        var idsToRemove = '';
        $("#ResultTable input:checkbox").not("#" + markedProductsCheckBoxId).each(function() {
            if ($(this).is(':checked') === true) {
                // hiddenfield next to checkbox
                var value = $(this).next().attr('value');
                if (idsToRemove == '') {
                    idsToRemove = value;
                }
                else {
                    idsToRemove += ',' + value;
                }
                $(this).attr('checked', false);
            }
        });

        RemoveIdsFromShoppingListEntryList(idsToRemove);
    }
}

function CheckAmounts() {
    var isOkay = true;
    var checkBoxes = $("#ResultTable input:checkbox").not("#" + markedProductsCheckBoxId);

    for(var index = 0; index < checkBoxes.length; index++)
    {
        var checkBox = checkBoxes[index];
        if ($(checkBox).is(':checked') === true) {
            // hiddenfield next to checkbox
            var productId = $(checkBox).next().attr('value');
            var amountTextBoxId = $(checkBox).next().next().attr('value');
            var textBoxInstance = $('#' + amountTextBoxId);
            if ($(textBoxInstance) != null) {
                var amount = $(textBoxInstance).attr('value');
                var amountCustValDiv = $('#ShoppingListEntryAmountCustValDiv_' + productId);

                if (parseInt(amount, 10) <= 0) {
                    $(amountCustValDiv).css('display', 'block');
                    isOkay = false;
                }
                else {
                    $(amountCustValDiv).css('display', 'none');
                }
            }
        }
    }
    
    return isOkay;
}
