﻿/*****************
Actions on Basket
*****************/
top.processingUpdate = false;

function addProductToBasket(jItemIdentifier, originType) {
    var productColorID = jQuery(jItemIdentifier).find('.typeAndSize select.type').val();
    var productSizeID = jQuery(jItemIdentifier).find('.typeAndSize select.size').val();
    var quantity = jQuery(jItemIdentifier).find('.quantityWrap > .quantity').val()
    
    if (!top.processingUpdate && !isNaN(quantity) && quantity > 0) {
        top.processingUpdate = true;
        jQuery.post(window.url_AddToBasket, { productColorID: productColorID, productSizeID: productSizeID, quantity: quantity }, function (data) {
            handleAddProductToBasketResult(originType, data);
        }, 'json');
    }
    return false;
}

function UpdateProductInBasket(productColorID, productSizeID, quantity, employeeID, jItemIdentifier, originType) {
    if (!top.processingUpdate && !isNaN(quantity) && quantity > 0) {
        top.processingUpdate = true;
        jQuery.post(window.url_UpdateBasket, { productColorID: productColorID, productSizeID: productSizeID, quantity: quantity, employeeID: employeeID }, function (data) { handleUpdateProductInBasketResult(originType, jItemIdentifier, data) }, 'json');

    }

    return false;
}

function CheckBasket(jItemIdentifier, originType) {
    jQuery.post(window.url_CheckBasket, { }, function (data) { handleCheckBasketResult(originType, jItemIdentifier, data) }, 'json');

    return false;
}

function DeleteProductFromBasket(productColorID, productSizeID, employeeID, jItemIdentifier, originType, confirmText) {
    displayPopup({
        'message': confirmText + ' <br /><div class="approveBtnsWrap clearfix"><a href="#" class="btnSimple left" onclick="jQuery.fancybox.close(); return false;">NEJ</a><a href="#" class="btnSimple right" onclick="DeleteProductFromBasketConfirmed(' + productColorID + ', ' + productSizeID + ', ' + employeeID + ', \'' + jItemIdentifier + '\', \'' + originType + '\'); return false;">JA</a></div>',
        'type': 'error',
        "ondisplay": function (popup) {
            popup.addClass("deleteFromBasket");
        }
    });
    return false;
}

function DeleteProductFromBasketConfirmed(productColorID, productSizeID, employeeID, jItemIdentifier, originType) {
    if (!top.processingUpdate) {
        top.processingUpdate = true;
        jQuery.post(window.url_DeleteFromBasket, { productColorID: productColorID, productSizeID: productSizeID, employeeID: employeeID }, function (data) { handleDeleteProductFromBasketResult(originType, jItemIdentifier, data) }, 'json');
    }
    smallBasketScrollbar();
    displayPopup({
        "message": jQuery("#basketUpdateText .delete").html(),
        "autohide": 1500,
        "type": "info"
    });
    return false;
}

function RequestPaymentAccept(confirmText, deliveryUrl) {
    displayPopup({
        'message': confirmText + ' <br /><div class="clearfix"><a href="#" class="btnSimple left" onclick="jQuery.fancybox.close(); return false;">NEJ</a><a class="btnSimple right" href="' + deliveryUrl + '">JA</a></div>',
        'type': 'error'
    });
}

/*****************
Helpers
*****************/
function handleAddProductToBasketResult(originType, data) {

    var htmBasketUpdateMsg = jQuery("#basketUpdateText .add").html();
    var strMessageType = "info";
    var intAutoHideTime = 1500;
    
    if (data.returnStr != undefined) {
        if (data.returnStr == "-1") {
            htmBasketUpdateMsg = jQuery("#basketUpdateText .notenoughcredits").html()
            strMessageType = "error";
            intAutoHideTime = 3000;
        }
        if (data.returnStr == "-2") {
            htmBasketUpdateMsg = jQuery("#basketUpdateText .PayRestAmount").html()
            strMessageType = "info";
            intAutoHideTime = 5000;
        }
    }

    if (!hLib.booIE6) {
        displayPopup({
            "message": htmBasketUpdateMsg,
            "autohide": intAutoHideTime,
            "type": strMessageType
        });
    }
    else { alert(htmBasketUpdateMsg) }

    if (strMessageType != "error") {
        updateBasketMini();
        updateBasketExpanded();
        jQuery('.quantityWrap > .quantity').val('');
    }
    top.processingUpdate = false;
}

function handleUpdateProductInBasketResult(originType, jItemIdentifier, data) {
    if (data.Success) {
        displayPopup({
            "message": jQuery("#basketUpdateText .update").html(),
            "autohide": 1500,
            "type": "info"
        });

        updateBasketMini();
        updateBasketExpanded();
        updateBasketDataOnly(data, jItemIdentifier);

    }
    else {
        displayPopup({
            "message": jQuery("#basketUpdateText .notenoughcredits").html(),
            "autohide": 1500,
            "type": "error"
        });
    }
    setTimeout("location.href = location.href.replace('#', '');", 1500);
    top.processingUpdate = false;
}

function handleCheckBasketResult(originType, jItemIdentifier, data) {
    if (data.Success) {
        jQuery('#basketform').submit();
    }
    else {
        displayPopup({
            "message": jQuery("#basketUpdateText .notenoughcreditsnext").html(),
            "autohide": 1500,
            "type": "error"
        });
    }
    setTimeout("location.href = location.href.replace('#', '');", 1500);
    top.processingUpdate = false;
}

function handleDeleteProductFromBasketResult(originType, jItemIdentifier, data) {
    //remove all list items similar to the jItemIdentifier
    updateBasketMini();
    updateBasketExpanded();

    if (jQuery(jItemIdentifier).siblings("tr[class*='jBasketItem-']").length == 0) {
        jQuery('.emp' + data.EmployeeID).remove();
    }
    else {
        jQuery(jItemIdentifier).remove();
    }
    

    top.processingUpdate = false;
    jQuery.fancybox.close();


    if (jQuery("#UpdatePage").val()) {        
        setTimeout("location.href = location.href.replace('#', '');", 500);
    }
}

function updateBasketMini() {
    jQuery('#mvc_basketMini').load(url_LoadBasketMini, {}, function () { });
}

function updateBasketExpanded() {
    jQuery('#mvc_basketExpanded').load(url_LoadBasketExpanded, {}, function () { InitFoldBasket(); });
}

function updateBasketDataOnly(data, jItemIdentifier) {
    //Update LinePriceTotal, LinePrice
    //if (data.LinePriceTotal) { jQuery(jItemIdentifier).find(".totalLinePrice").html(data.CurrencyCode + "&nbsp;" + data.LinePriceTotal); }
    //if (data.LinePrice) { jQuery(jItemIdentifier).find(".price").html(data.CurrencyCode + "&nbsp;" + data.LinePrice); }

    //Update TotalPrice, TotalPriceNoFreight
    //BasketList
    //if (data.Freight) { jQuery(".productlistBottom > .totalFreight > .total").html(data.CurrencyCode + "&nbsp;" + data.Freight); }
    //if (data.TotalPrice) { jQuery(".productlistBottom > .totalPrice > .total").html(data.CurrencyCode + "&nbsp;" + data.TotalPrice); }
    //if (data.TotalPriceNoFreight) { jQuery(".productlistBottom > .totalMomsDelivery > .total").html(data.CurrencyCode + "&nbsp;" + data.TotalPriceNoFreight); }

    //Update TotalPrice, Freight
    //Expanded
    //if (data.Freight) { jQuery(".listBottom  > .miniTotal > .totalFreight > .totalPrice").html(data.Freight + "&nbsp;" + data.CurrencyCode); }
    //if (data.TotalPrice) { jQuery(".listBottom > .miniTotal > .totalEnd > .totalPrice").html(data.TotalPrice + "&nbsp;" + data.CurrencyCode); }
}

