﻿cst.apply('cart', {
    initPage: function () {
        var quantityEls = $('.cart-quantity input');
        cst.makeInputNumericOnly(quantityEls);
        quantityEls.keydown(function () {
            return cst.cart.updateQuantity($(this).attr('id').split('quantity-')[1]);
        });

        $('#apply-coupon-link').click(function () {
            cst.cart.applyCoupon(true);
            return false;
        });

        cst.preppurchase.prepPurchaseQuote();
        cst.preppurchase.prepPurchaseInvoice();
    },
    updateCart: function (cart) {
        for (var i = 0; i < cart.items.length; i++) {
            var item = cart.items[i];
            $('#total-price-' + item.id).html(item.totalPrice);
            $('#total-price-' + item.id).formatCurrency();
            $('#discount-' + item.id).html((item.discountRate * 100) + '%');
        }

        $('#subtotal').html(cart.subTotal);
        $('#subtotal').formatCurrency();
        $('#taxamount').html(cart.taxAmount);
        $('#taxamount').formatCurrency();

        if (cart.discountAmount > 0) {
            $('#discount-amount').html(cart.discountAmount);
            $('#discount-amount').formatCurrency();
            $('#discount-amount').prepend('-');
            $('#discount-row').show();
        } else {
            $('#discount-row').hide();
        }

        $('#total-price').html(cart.totalPrice);
        $('#total-price').formatCurrency();
        $('#Coupon').val(cart.couponCode);
        if ($.isArray(cart.messages) && cart.messages.length > 0) {
            $('#infobox').html(cart.messages.join(''));
            $('#infobox').show();
        } else
            $('#infobox').hide();
    },
    quanityId: null,
    updateQuantity: function (itemId) {
        if (cst.cart.quanityId != null) {
            clearTimeout(cst.cart.quanityId);
        }
        cst.cart.quanityId = setTimeout(function () { cst.cart.updatePrice(itemId); }, 200);
    },
    updatePrice: function (id) {
        var quantity = $('#quantity-' + id).val();
        $.ajax({
            url: '/shoppingcart/updatequantity',
            type: 'POST',
            data: {
                cartId: id,
                quantity: quantity == '' ? 0 : quantity
            },
            success: function (data) {
                $('#coupon-error').html('');
                cst.cart.updateCart(data);
            },
            error: function (xhr, textStatus, errorThrown) {
                cst.showErrorMessage('Error updating quantity, please try again.');
            }
        });

        return false;
    },
    applyCoupon: function (force) {
        var value = $('#Coupon').val();

        if (!force && (!value || value.trim() == '')) {
            return;
        }

        $.ajax({
            url: '/shoppingcart/applycoupon',
            type: 'POST',
            data: { couponCode: value },
            success: function (data) {
                cst.cart.updateCart(data);
            },
            error: function (xhr, textStatus, errorThrown) {
                cst.showErrorMessage('Error applying coupon, please try again.');
            }
        });

        return false;
    }
});
