$("#itemjian").livequery("click", function () {
    var txtNumber = $(this).next();
    var num = $.trim(txtNumber.val());
    var Qty = txtNumber.attr("Qty");
    if (!/^[1-9]\d*$/.test(num)) {
        txtNumber.val(Qty);
        return false;
    }
    else {
        var qnum = (num * 1) - 1;
        if(qnum > 0) {
            txtNumber.val(qnum).change();
        } else {
            qnum = 1;
            txtNumber.val(qnum).change();
        }
    }
});
$("#itemjia").livequery("click", function () {
    var txtNumber = $(this).prev();
    var num = $.trim(txtNumber.val());
    var Qty = txtNumber.attr("Qty");
    if (!/^[1-9]\d*$/.test(num)) {
        txtNumber.val(Qty);
    }
    else {
        txtNumber.val((num * 1) + 1).change();
    }
    return false;
});
$("button.itemjian").livequery("click", function () {
    var txtNumber = $(this).next();
    var num = $.trim(txtNumber.val());
    var Qty = txtNumber.attr("Qty");
    if (!/^[1-9]\d*$/.test(num)) {
        txtNumber.val(Qty);
        return false;
    }
    else {
        var qnum = (num * 1) - 1;
        if(qnum > 0) {
            txtNumber.val(qnum).change();
        } else {
            qnum = 1;
            txtNumber.val(qnum).change();
            return false;
        }
        ajax_chg_qty ($(this), qnum);
    }
});
$("button.itemjia").livequery("click", function () {
    var txtNumber = $(this).prev();
    var num = $.trim(txtNumber.val());
    var Qty = txtNumber.attr("Qty");
    if (!/^[1-9]\d*$/.test(num)) {
        txtNumber.val(Qty);
        return false;
    }
    else {
        txtNumber.val((num * 1) + 1).change();
        ajax_chg_qty ($(this), (num * 1) + 1);
    }
});
$("#updateCartQtyBtn").livequery('click', function(){
    var cartQty = [];
    $(".cart_product_line").each(function(i, e){
        var product_qty = $(e).find('input.qty').val();
        var product_key = $(e).attr('data-product-key');
        var obj         = {product_qty: product_qty, product_key: product_key};
        cartQty.push(obj);
    });
    $.post('index.php?controller=cart&action=ajax_batch_chg_cart_qty', {cart_qty: cartQty}, function(data){
        window.location.reload();
    });
});
function ajax_chg_qty (that, qty) {
    var productLine = that.parents('.cart_product_line');
    var productKey  = productLine.attr('data-product-key');
    $('#loading-center').show();
    $.get('index.php?controller=cart&action=ajax_chg_cart_qty&product_key=' + productKey + '&product_qty=' + qty, {}, function(jsonData){
        var data = eval("(" + jsonData + ")");
        if(data.errcode == 0) {
            var product = data.product;
            var price = product.price;
            var discount_price = product.discount_price;
            var total = product.total;
            productLine.find('span.price_old').text(price);
            if(discount_price) {
                productLine.find('span.price_new').text(discount_price);
            }
            productLine.find('.crt-product_subtotal .cart-price .price').text(total);
            $("#subtotal_value_id").text(data.sub_total);
            $("#grandtotal_value_id").text(data.calculate);
            $('#grand_total font').text(data.calculate);
        }
        reloadCartHeader();
        $('#loading-center').hide();
        // window.location.reload();
    }, 'JSON');
}
function reloadCartHeader() {
    $.get('index.php?controller=cart&action=ajax_cart_header_block', {}, function(data){
        $("#cartHeaderBlock").html(data);
    });
}
/*
$(".deleteOrderLine").livequery("click", function () {
    var warecode = $(this).attr("warecode");
    var kitorder = $(this).attr("kitorder");
    $.getJSON("/", { 'WareCode': warecode, 'KitOrder': kitorder }, function (shoppingCart) {
        bindShoppingCart(shoppingCart);
        GetCartCount();
    });
});

$(".updateOrderLine").livequery("change", function () {
    var warecode = $(this).attr("warecode");
    var num = $.trim($(this).val());
    var Qty = $(this).attr("Qty");
    var kitorder = $(this).attr("kitorder");

    if (num == "") {
        $(this).val(Qty);
        return false;
    }
    if (!/^[1-9]\d*$/.test(num)) {
        alert("数量不正确");
        $(this).val(Qty);
        return false;
    }
    num = parseInt(num);
    if (num <= 1 || num > 99) {
        alert("数量大于1并且小于100！");
        $(this).val(Qty);
        return false;
    }
    $.getJSON("/", { 'WareCode': warecode, 'Qty': num, 'KitOrder': kitorder }, function (shoppingCart) {
        if (shoppingCart.MSG) {
            alert(shoppingCart.MSG);
            bindShoppingCart(shoppingCart.ShoppingCart);
        }
        else {
            bindShoppingCart(shoppingCart);
            GetCartCount();
        }

    });
});
*/
