﻿
ShoppingCart = {

    sEStoresAccessName: '',
    sAddToCartPageInVietMua: '/Ajaxloading/EStores/AddProductToShoppingCartInVietMua.aspx',
    sAddToCartPage: '/Ajaxloading/EStores/AddProductToShoppingCart.aspx',
    sRemoveFromCartPage: '/Ajaxloading/EStores/RemoveProductFromShoppingCart.aspx',
    sUpdateShoppingCartViewPage: '/Ajaxloading/EStores/UpdateShoppingCartView.aspx',
    sReCalculateCartPage: '/Ajaxloading/EStores/ReCalculateShoppingCart.aspx',
    
    AddProductToCartInVietMua: function(eStoresId, productId) {
    
        beforeAjaxLoading();

        $.ajax({
            type: "POST",
            url: this.sAddToCartPageInVietMua,
            data: 'eStoresId=' + eStoresId + '&productId=' + productId + '&randCode=' + Math.random(),
            dataType: "html",
            success: function(msg){
                msg = msg.replace('\r\n', '');
                msg = msg.replace('\r\n', '');
                afterAjaxLoading();
                if(msg.indexOf('Duplicate:') != -1) {
                    var message = 'Bạn đang mua sản phẩm tại cửa hàng ' + msg.substring(10, msg.length) + '.\r\nVui lòng thanh toán trước khi mua sản phẩm tại cửa hàng khác.';
                    alert(message);
                }
                else {
                    alert('Sản phẩm được được thêm vào Giỏ hàng.');
                    $('#lnkShoppingCart').html(msg);
                    $('#lnkShoppingCart').attr("href", "javascript:ShoppingCart.ViewShoppingCart()");
                }
            }
        });
    },
    
    AddProductToCart: function(eStoresId, productId) {
    
        beforeAjaxLoading();

        $.ajax({
            type: "POST",
            url: this.sAddToCartPage,
            data: 'eStoresId=' + eStoresId + '&productId=' + productId + '&randCode=' + Math.random(),
            dataType: "html",
            success: function(msg){
                msg = msg.replace('\r\n', '');
                msg = msg.replace('\r\n', '');
                afterAjaxLoading();
                if(msg.indexOf('Duplicate:') != -1) {
                    var message = 'Bạn đang mua sản phẩm tại cửa hàng ' + msg.substring(10, msg.length) + '.\r\nVui lòng thanh toán trước khi mua sản phẩm tại cửa hàng khác.';
                    alert(message);
                }
                else {
                    alert('Sản phẩm được được thêm vào Giỏ hàng.');
                    $('#divShoppingCart').html(msg);
                }
            }
        });
    },
    
    RemoveProductFromCart: function(eStoresAccessName, productId) {
    
        beforeAjaxLoading();

        $.ajax({
            type: "POST",
            url: this.sRemoveFromCartPage,
            data: 'estoresAccessName=' + eStoresAccessName + '&productId=' + productId + '&randCode=' + Math.random(),
            dataType: "html",
            success: function(msg){
                msg = msg.replace('\r\n', '');
                msg = msg.replace('\r\n', '');
                $('#divViewShoppingCart').html(msg);
                afterAjaxLoading();
            }
        });
        
        UpdateShoppingCartView(this.sUpdateShoppingCartViewPage);
    },
    
    ReCalculateCart: function(eStoresAccessName) {
    
        if(CheckUnsignAmount() == true) {
        
            beforeAjaxLoading();
            
            var amountList = '';
            $('.lstAmount').each(function(index) {
                amountList += $(this).val() + '-';
            });

            $.ajax({
                type: "POST",
                url: this.sReCalculateCartPage,
                data: 'estoresAccessName=' + eStoresAccessName + '&productAmountList=' + amountList + '&randCode=' + Math.random(),
                dataType: "html",
                success: function(msg){
                    msg = msg.replace('\r\n', '');
                    msg = msg.replace('\r\n', '');
                    $('#divViewShoppingCart').html(msg);
                    afterAjaxLoading();
                }
            });
            
            UpdateShoppingCartView(this.sUpdateShoppingCartViewPage);
        }
    },
    
    ViewShoppingCart: function() {
        
        window.location = '/Cua-hang/' + this.sEStoresAccessName + '/Xem-thong-tin-gio-hang/';
    }
}

function UpdateShoppingCartView(sPage) {

    $.ajax({
        type: "POST",
        url: sPage,
        dataType: "html",
        success: function(msg){
            msg = msg.replace('\r\n', '');
            msg = msg.replace('\r\n', '');
            afterAjaxLoading();
            $('#divShoppingCart').html(msg);
        }
    });
}

function CheckUnsignAmount() {
    
    $('.lstAmount').each(function(index) {
        var amountVal = $(this).val();
        if(isInteger(amountVal)) {
            var amountInt = parseInt(amountVal);
            if(amountInt <= 0) {
                alert('Số lượng phải là số Nguyên dương > 0');
                return false;
            }
        }
        else {
            alert('Số lượng phải là số Nguyên dương > 0');
            return false;
        }
    });
    
    return true;
}
