﻿var ShoppingCart = '#iequus-cart';
var EmptyCartPart = '#iequus-cart.emptyCartPart';
var FullCartPart = '#iequus-cart.fullCartPart';
var SubTotalCost = '#iequus-cart.subtotal-cost';
var TotalQuantity = '#iequus-cart.total-quantity';

//////////////////////////////////////////////////////////////////////////////////////////
//shopping cart helper function
//////////////////////////////////////////////////////////////////////////////////////////
function ShowEmptyCart() {
    $(FullCartPart).each(function() { this.style.display = "none"; });
    $(EmptyCartPart).each(function() { this.style.display = "block"; });
}

function ShowFullCartPart(quantity, SubTotal) {
    $(FullCartPart).each(function() { this.style.display = "block"; });
    $(EmptyCartPart).each(function() { this.style.display = "none"; });
    $(SubTotalCost).each(function() { this.innerHTML = SubTotal; });
    $(TotalQuantity).each(function() { this.innerHTML = quantity; });
}

function HandleError(costResult) {
    if (costResult.HasError) {
        showDialog('Cart Information', 'Error: ' + costResult.ErrorMessage, 'd_error', '1.5');        
        return true;
    }
    return false;
}
//////////////////////////////////////////////////////////////////////////////////////////
//handle add product to cart
//////////////////////////////////////////////////////////////////////////////////////////
function AddToCart(productId, quantity) {
    var url = "/Shop/AddToCart?productId=" + productId + "&quantity=" + quantity+ "&key=" + Math.random();
    $('#ModalContent').modal();
    xhr = $.ajax({
        type: "POST",
        url: url,
        data: "",
        dataType: "json",
        error: function() { /*could not load ajax*/
            $.modal.close();
            alert("Could not add the item to cart.");
            window.location.reload();
        },
        success: function(costResult) {
            $.modal.close();
            if (HandleError(costResult)) {
                return;
            }
            if (costResult.HasExceedQuota) {
                showDialog('Cart Information', 'The product has exceeded its maximum quantity to buy.', 'd_error', '1.5');
            }
            else {
                if (costResult.TotalQuantity > 0) {
                    ShowFullCartPart(costResult.TotalQuantity, costResult.SubTotal);
                }
                showDialog('Cart Information', 'The item has been added to cart.', 'd_success', '1.5');
                window.location.href = "/Shop/ViewCart";
            }
        }
    });
}
//////////////////////////////////////////////////////////////////////////////////////////
///update cart
//////////////////////////////////////////////////////////////////////////////////////////
function UpdateCart() {
    var els = $('.product-quantity');
    var form = $("#cart-detail-form");
    var p = true;
    for (var i = 0; i < els.length; i++) {
        p = p && $(form).validate().element(els[i]);
    }
    if(p) form.submit(); 
}

function SwitchPanel(addressType, IsShownSummary) {
    var summaryDiv = '#' + addressType + '_Summary';
    var editDiv = '#' + addressType + '_Edit';
    if (IsShownSummary) {
        $(summaryDiv).css("display", "block");
        $(editDiv).css("display", "none");
        if ("BillingAddress" == addressType) {
            UpdateWhenCountryChange_BillingAddress();
        } else {
            UpdateWhenCountryChange_ShippingAddress();
        }
    }
    else {
        $(summaryDiv).css("display", "none");
        $(editDiv).css("display", "block");
    }
}
//////////////////////////////////////////////////////////////////////////////////////////
///remove product from cart
//////////////////////////////////////////////////////////////////////////////////////////
function RemoveFromCart(productId) {
    var url = "/Shop/RemoveFromCart?productId=" + productId + "&key=" + Math.random();
    //$('#ModalContent').modal();
    xhr = $.ajax({
        type: "POST",
        url: url,
        data: "",
        dataType: "json",
        error: function() { /*could not load ajax*/
            $.modal.close();
            alert("Could not remove the item from cart.");
            window.location.reload();
        },
        success: function(costResult) {
            $.modal.close();
            if (costResult.TotalQuantity > 0) {
                

            }
            showDialog('Cart Information', 'The item has been removed from cart.', 'd_success', '1.5');
        }
    });
}
//////////////////////////////////////////////////////////////////////////////////////////
///clear cart
//////////////////////////////////////////////////////////////////////////////////////////
function ClearCart() {
    var url = "/Shop/ClearCart?key=" + Math.random();
    $('#ModalContent').modal();
    xhr = $.ajax({
        type: "POST",
        url: url,
        data: "",
        dataType: "json",
        error: function() { /*could not load ajax*/
            $.modal.close();
            alert("Could not clear the cart.");
            window.location.reload();
        },
        success: function(costResult) {
            $.modal.close();
            if (costResult.TotalQuantity > 0) {

            }
            showDialog('Cart Information', 'Your cart has been clear.', 'd_success', '1.5');
        }
    });
}
//////////////////////////////////////////////////////////////////////////////////////////
///apply discount code
//////////////////////////////////////////////////////////////////////////////////////////
function ApplyDiscountCode(discountCode) {
    var url = "/Shop/ApplyDiscountCode?code=" + discountCode + "&key=" + Math.random();
    //$('#ModalContent').modal();
    xhr = $.ajax({
        type: "POST",
        url: url,
        data: "",
        dataType: "json",
        error: function() { /*could not load ajax*/
            $.modal.close();
            alert("Could not apply discount code to your cart.");
            window.location.reload();
        },
        success: function(costResult) {
            $.modal.close();
            if (costResult.TotalQuantity > 0) {

                //$(".fullCartPart").each(function() { this.style.display = "block"; });
                //$(".emptyCartPart").each(function() { this.style.display = "none"; });

                //$(document).find(".subtotalCost").each(function() { this.innerHTML = costResult.SubTotal; });
                //$("#orderCartProductQuantity").html(costResult.TotalQuantity);
            }
            showDialog('Cart Information', 'Your cart has been clear.', 'd_success', '1.5');
        }
    });
}
