﻿/*----------------------此功能实现购物车功能--------------------------------*/
function setPropertyVal(objVal) {
    $("#txtProperty").val(objVal);
    //给productmingxi中的隐藏控件：txtProperty 赋选中属性值
}
//加入购物车时在商品列表页判断是否有属性
function panduanproductshuxing(productid, intShuLiang, maxbuyshu, ispropertys, defaulttpe) {
    //判断是否促销商品,是:判断当前日期是否是促销结束日期的前一天,不是:继续执行
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/PromoExpiredDate",
        data: "{ProductID:" + productid + "}",
        dataType: "json",
        success: function(result) {
            if (result != null && result.d) {
                popBox.show('/pop/popmsg.htm', function() {
                    $('#products_sidles').cycle({
                        fx: '',
                        pause: 1,
                        timeout: 4000
                    });
                    $('#divMsg').html('此商品已售完!');
                });
                return;
            }
            else {
                //得到购物车中商品的数量
                TempShop(productid, intShuLiang, maxbuyshu, ispropertys, defaulttpe);
            }
        }
    });
}
function TempShop(productid, intShuLiang, maxbuyshu, ispropertys, defaulttpe) {
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/GetShoppingCartProductShu",
        data: "{Product_ID:" + productid + "}",
        dataType: "json",
        success: function(result) {
            var productshu = parseInt(result.d) + parseInt(intShuLiang);
            if (productshu > parseInt(maxbuyshu)) {
                //alert('购买失败，此商品最大购买数不能超过'+maxbuyshu);
                var content = "购买失败，此商品最大购买数不能超过" + maxbuyshu;
                popBox.show('/pop/popmsg.htm', function() {
                    $('#products_sidles').cycle({
                        fx: '',
                        pause: 1,
                        timeout: 4000
                    });
                    $('#divMsg').html(content);
                });
            }
            else {
                //有商品属性信息并且是商品列表页
                if (ispropertys.length > 0) {
                    var content = "请选择商品详细类别信息再加入购物车！";
                    popBox.show('/pop/popmsgtrue.htm', function() {
                        $('#divMsg').html(content);
                        $('#hidProductID').val(productid);
                        $('#hidShuLiang').val(intShuLiang);
                        $('#hidProtype').val(defaulttpe);
                        $('#hidComment').val("0");

                    });
                }
                else {
                    btnShow2(productid, intShuLiang, maxbuyshu, 1);
                }
            }
        }
    });
}
function TranWin() {
    var productid = $('#hidProductID').val();
    var intShuLiang = $('#hidShuLiang').val();
    var defaulttpe = $('#hidProtype').val();
    window.open("/sales/productmingxi.aspx?product_id=" + productid + "&num=" + intShuLiang + "&protype=" + defaulttpe);
    //        if($('#hidComment').val()=="0")
    //        {
    //            window.open("/sales/productmingxi.aspx?product_id=" + productid + "&num=" + intShuLiang+"&protype="+defaulttpe);
    //        }
    //        else
    //        {
    //            window.open("/sales/productmingxieng.aspx?product_id=" + productid + "&num=" + intShuLiang+"&protype="+defaulttpe);
    //        }
}

function btnShow2(productid, intShuLiang, maxbuyshu, isfavororderpropertys) {
    var content = "";
    // //得到购物车中商品的数量
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/GetShoppingCartProductShu",
        data: "{Product_ID:" + productid + "}",
        dataType: "json",
        success: function(result) {
            var productshu = parseInt(result.d) + parseInt(intShuLiang);
            if (productshu > parseInt(maxbuyshu)) {
                //alert('购买失败，此商品最大购买数不能超过'+maxbuyshu);
                content = "购买失败，此商品最大购买数不能超过" + maxbuyshu;
                popBox.show('/pop/popmsg.htm', function() {
                    $('#divMsg').html(content);
                });
                return;
            }
            else {
                var propertys;
                //(商品列表页或商品详细页)
                if (isfavororderpropertys == 1) {
                    propertys = $("#txtProperty").val();
                    if (typeof propertys == "undefined") {
                        propertys = "";
                    }
                    else {
                        if (propertys.length == 0) {
                            //alert("请选择商品类别信息！");
                            content = "请选择商品类别信息！";
                            popBox.show('/pop/popmsg.htm', function() {
                                $('#divMsg').html(content);
                            });
                            return;
                        }
                    }
                }
                else   //我的收藏和我的订单的加入购物车
                {
                    propertys = isfavororderpropertys;
                }
                $("#shopcart_content").hide();
                var isflat = false;
                var panduanshu = 0;
                //判断购物车中大小五条的购物车中是是否存在此商品
                $.ajax({
                    type: "POST",
                    contentType: "application/json;utf-8",
                    url: "../webservice/ShoppingCarts.asmx/PanDuanCunZaiProduct",
                    data: "{Product_ID:" + productid + ",Propertys:'" + propertys + "'}",
                    dataType: "json",
                    success: function(result) {
                        if (result.d == "-1")//管理员有没有删除商品表中的此商品信息或下架
                        {
                            //alert('此商品已经下架！');
                            content = "此商品已经下架！";
                            popBox.show('/pop/popmsg.htm', function() {
                                $('#divMsg').html(content);
                            });
                            return;
                        }
                        else {
                            if (result.d == "-2")//判断当前加入购物车的商品是否下架
                            {
                                //alert('此商品已缺货！');
                                content = "此商品已售完!";
                                popBox.show('/pop/popmsg.htm', function() {
                                    $('#divMsg').html(content);
                                });
                                return;
                            }
                            else if (result.d == "-3") {
                                content = "此商品不是此城市的商品，不能加入购物车！";
                                popBox.show('/pop/popmsg.htm', function() {
                                    $('#divMsg').html(content);
                                });
                                return;
                            }
                            else {
                                if (result.d == "1" || result.d == 1)//购物车中是否存在此商品
                                {
                                    isflat = true;
                                    panduanshu = parseInt(maxbuyshu) - parseInt(intShuLiang);
                                }
                                shifoucunzai(isflat, panduanshu, maxbuyshu, productid, intShuLiang, propertys);
                            }
                        }
                    }
                });
            }
        }
    });
}

//加入购物车时在商品详细页判断是否有属性
function btnShow(productid, intShuLiang, maxbuyshu, isfavororderpropertys) {
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/PromoExpiredDate",
        data: "{ProductID:" + productid + "}",
        dataType: "json",
        success: function(result) {
            if (result.d) {
                popBox.show('/pop/popmsg.htm', function() {
                    $('#products_sidles').cycle({
                        fx: '',
                        pause: 1,
                        timeout: 4000
                    });
                    $('#divMsg').html('此商品已售完!');
                });
                return;
            }
            else {
                var content = "";
                // //得到购物车中商品的数量
                $.ajax({
                    type: "POST",
                    contentType: "application/json;utf-8",
                    url: "../webservice/ShoppingCarts.asmx/GetShoppingCartProductShu",
                    data: "{Product_ID:" + productid + "}",
                    dataType: "json",
                    success: function(result) {
                        var productshu = parseInt(result.d) + parseInt(intShuLiang);
                        if (productshu > parseInt(maxbuyshu)) {
                            //alert('购买失败，此商品最大购买数不能超过'+maxbuyshu);
                            content = "购买失败，此商品最大购买数不能超过" + maxbuyshu;
                            popBox.show('/pop/popmsg.htm', function() {
                                $('#divMsg').html(content);
                            });
                            return;
                        }
                        else {
                            var propertys;
                            //(商品列表页或商品详细页)
                            if (isfavororderpropertys == 1) {
                                propertys = $("#txtProperty").val();
                                if (typeof propertys == "undefined") {
                                    propertys = "";
                                }
                                else {
                                    if (propertys.length == 0) {
                                        //alert("请选择商品类别信息！");
                                        content = "请选择商品类别信息！";
                                        popBox.show('/pop/popmsg.htm', function() {
                                            $('#divMsg').html(content);
                                        });
                                        return;
                                    }
                                }
                            }
                            else   //我的收藏和我的订单的加入购物车
                            {
                                propertys = isfavororderpropertys;
                            }
                            $("#shopcart_content").hide();
                            var isflat = false;
                            var panduanshu = 0;
                            //判断购物车中大小五条的购物车中是是否存在此商品
                            $.ajax({
                                type: "POST",
                                contentType: "application/json;utf-8",
                                url: "../webservice/ShoppingCarts.asmx/PanDuanCunZaiProduct",
                                data: "{Product_ID:" + productid + ",Propertys:'" + propertys + "'}",
                                dataType: "json",
                                success: function(result) {
                                    if (result.d == "-1")//管理员有没有删除商品表中的此商品信息或下架
                                    {
                                        //alert('此商品已经下架！');
                                        content = "此商品已经下架！";
                                        popBox.show('/pop/popmsg.htm', function() {
                                            $('#divMsg').html(content);
                                        });
                                        return;
                                    }
                                    else {
                                        if (result.d == "-2")//判断当前加入购物车的商品是否下架
                                        {
                                            //alert('此商品已缺货！');
                                            content = "此商品已缺货！";
                                            popBox.show('/pop/popmsg.htm', function() {
                                                $('#divMsg').html(content);
                                            });
                                            return;
                                        }
                                        else if (result.d == "-3") {
                                            content = "此商品不是此城市的商品，不能加入购物车！";
                                            popBox.show('/pop/popmsg.htm', function() {
                                                $('#divMsg').html(content);
                                            });
                                            return;
                                        }
                                        else {
                                            if (result.d == "1" || result.d == 1)//购物车中是否存在此商品
                                            {
                                                isflat = true;
                                                panduanshu = parseInt(maxbuyshu) - parseInt(intShuLiang);
                                            }
                                            shifoucunzai(isflat, panduanshu, maxbuyshu, productid, intShuLiang, propertys);
                                        }
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }
    });

}

//判断购物车中是否有相同商品
function shifoucunzai(isflat, panduanshu, maxbuyshu, productid, intShuLiang, propertys) {
    if (isflat) {
        //判断购物车中大小五条的购物车中是是否存在此商品
        if (panduanshu > maxbuyshu) {
            //alert('您最多购买此商品数量为'+maxbuyshu);
            var content = "您最多购买此商品数量为" + maxbuyshu;
            popBox.show('/pop/popmsg.htm', function() {
                $('#divMsg').html(content);
            });
        }
        else {
            AddShoppingCart(productid, intShuLiang, propertys);
        }
    }
    else//不存在直接加入
    {
        AddShoppingCart(productid, intShuLiang, propertys);
    }
}

//购物车中所有信息及所有事件的获取和显示
function getBottomShoppingCart() {
    //给最底下的购物车赋值
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/GetShoppingCart",
        data: "{}",
        dataType: "json",
        success: function(result) {
            $("#cartcontent").html(result.d);
            //给最底下的购物车外边的数量和金额赋值
            $.ajax({
                type: "POST",
                contentType: "application/json;utf-8",
                url: "../webservice/ShoppingCarts.asmx/GetShoppingCartSumCountAndSumPrice",
                data: "{}",
                dataType: "json",
                success: function(result) {
                    var val = result.d;
                    if (val.length > 0) {
                        $("#ctl00_Bottom_UserPannel1_lblShuLiang").text(val.split(',')[0]);
                        $("#ctl00_Bottom_UserPannel1_lblZongE").text(parseFloat(val.split(',')[1]).toFixed(2));
                        GetNewShoppingCartContent(); //在重新得一下购物车中的内容
                    }
                    else {
                        $("#ctl00_Bottom_UserPannel1_lblShuLiang").text("0");
                        $("#ctl00_Bottom_UserPannel1_lblZongE").text("0.0");
                    }
                }
            });
            cartshopkuang();
        }
    });
}
//控制购物车中四个圆角
function cartshopkuang() {
    $(".shopcart_ctop").show();
    $(".shopcart_cbt").show();
}
//在重新得一下购物车中的内容
function GetNewShoppingCartContent() {
    //给最底下的购物车赋值
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/GetShoppingCart",
        data: "{}",
        dataType: "json",
        success: function(result) {
            $("#cartcontent").html(result.d);
            //给最底下的购物车外边的数量和金额赋值
            $.ajax({
                type: "POST",
                contentType: "application/json;utf-8",
                url: "../webservice/ShoppingCarts.asmx/GetShoppingCartSumCountAndSumPrice",
                data: "{}",
                dataType: "json",
                success: function(result) {
                    var val = result.d;
                    if (val.length > 0) {
                        $("#ctl00_Bottom_UserPannel1_lblShuLiang").text(val.split(',')[0]);
                        $("#ctl00_Bottom_UserPannel1_lblZongE").text(parseFloat(val.split(',')[1]).toFixed(2));
                    }
                    else {
                        $("#ctl00_Bottom_UserPannel1_lblShuLiang").text("0");
                        $("#ctl00_Bottom_UserPannel1_lblZongE").text("0.0");
                    }
                }
            });
        }
    });
}

//加入购物车（加入COOKIES）
function AddShoppingCart(productid, intShuLiang, propertys) {
    if (typeof propertys == "undefined") {
        propertys = "";
    }
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/AddProductShoppingcart",
        data: "{Product_ID:" + productid + ",ShuLiang:" + intShuLiang + ",Propertys:'" + propertys + "'}",
        dataType: "json",
        success: function(result) {
            if (result.d == "0") {
                //alert('添加购物车失败！');
                var content = "添加购物车失败！";
                popBox.show('/pop/popmsg.htm', function() {
                    $('#divMsg').html(content);
                });
            }
            else {

            }
        }
    });
    window.setTimeout('getBottomShoppingCart()', 300);
    $("#shopcart_content").show();
}

//删除购物车（COOKIES）
function DelProductShopCart(productid, userid, propertys) {
    //            var content="确定要删除购物车中的此商品吗? ";
    //            popBox.show('/pop/popmsgalter.htm', function() {
    //                $('#hidProductID').val(productid);
    //                $('#hidProperty').val(propertys);
    //                $('#divMsg').html(content);
    //            });
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/DelShoppingCart",
        data: "{ProductID:" + productid + ",Propertys:'" + propertys + "'}",
        dataType: "json",
        success: function(result) {
            if (result.d == "0") {
                var content = "删除失败！";
                popBox.show('/pop/popmsg.htm', function() {
                    $('#divMsg').html(content);
                });
            }
            else {
                getBottomShoppingCart();
                $("#shopcart_content").show();
            }
        }
    });
}

function fnTiShi() {
    var productid = $('#hidProductID').val();
    var propertys = $('#hidProperty').val();
    popBox.removeAll();
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/DelShoppingCart",
        data: "{ProductID:" + productid + ",Propertys:'" + propertys + "'}",
        dataType: "json",
        success: function(result) {
            if (result.d == "0") {
                var content = "删除失败！";
                popBox.show('/pop/popmsg.htm', function() {
                    $('#divMsg').html(content);
                });
            }
            else {
                getBottomShoppingCart();
                $("#shopcart_content").show();
            }
        }
    });
}

function DelTrueShoppingCart() {
    var productid = $('#hidProductID').val();
    popBox.removeAll();
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/DelMoreShoppingCart",
        data: "{ProductID:'" + productid + "'}",
        dataType: "json",
        success: function(result) {
            if (result.d == "0") {
                var content = "删除失败！";
                popBox.show('/pop/popmsg.htm', function() {
                    $('#divMsg').html(content);
                });
            }
            else {
                getBottomShoppingCart();
                $("#shopcart_content").show();
            }
        }
    });
}

//更新购物车
function UpdateShoppingCart(productid, shuliang, propertys) {
    $.ajax({
        type: "POST",
        contentType: "application/json;utf-8",
        url: "../webservice/ShoppingCarts.asmx/UpdateShoppingCart",
        data: "{Product_ID:" + productid + ",ShuLiang:" + shuliang + ",Propertys:'" + propertys + "'}",
        dataType: "json",
        success: function(result) {
            if (result.d == "0") {
                //                        var content="修改失败！";
                //                        popBox.show('/pop/popmsg.htm', function() {
                //                            $('#divMsg').html(content);
                //                        });
            }
            else {
            }
        }
    });
    window.setTimeout('getBottomShoppingCart()', 100);
}
//购物车中针对商品数量的加减操作
function GetShopShuLiang(rowindex, shu, maxbuyshu, productid, propertys) {
    if ($("#txtShuLiang" + rowindex).val().length == 0) {
        $("#txtShuLiang" + rowindex).val("1")
    }
    //修改单个商品的数量
    if (shu > 0) {
        if (parseInt($("#txtShuLiang" + rowindex).val()) < maxbuyshu) {
            $("#txtShuLiang" + rowindex).val(parseInt($("#txtShuLiang" + rowindex).val()) + 1);
        }
        else {
            var content = "此商品最大购买数不能大于" + maxbuyshu + ",您的购物车中已经" + maxbuyshu + "个。";
            popBox.show('/pop/popmsg.htm', function() {
                $("#txtShuLiang" + rowindex).val(maxbuyshu);
                $('#divMsg').html(content);
            });
            return;
        }
    }
    else {
        if (parseInt($("#txtShuLiang" + rowindex).val()) > 1) {
            $("#txtShuLiang" + rowindex).val(parseInt($("#txtShuLiang" + rowindex).val()) - 1);
        }
    }
    UpdateShoppingCart(productid, parseInt($("#txtShuLiang" + rowindex).val()), propertys);

}
//个人详细页中的加减和首页中的促销加减操作
function GetShopShuLiangList(rowindex, shu, maxbuyshu, productid) {
    if ($("#txtShuLiangs" + rowindex).val().length == 0) {
        $("#txtShuLiangs" + rowindex).val("1")
    }
    //修改单个商品的数量
    if (shu > 0) {
        if (parseInt($("#txtShuLiangs" + rowindex).val()) < maxbuyshu) {
            $("#txtShuLiangs" + rowindex).val(parseInt($("#txtShuLiangs" + rowindex).val()) + 1);
        }
        else {
            //alert('此类型的商品最大购买数量不能大于'+maxbuyshu);
            var content = "此类型的商品最大购买数量不能大于" + maxbuyshu;
            popBox.show('/pop/popmsg.htm', function() {
                $('#products_sidles').cycle({
                    fx: '',
                    pause: 1,
                    timeout: 4000
                });
                $('#divMsg').html(content);
            });
            $("#txtShuLiangs" + rowindex).val(maxbuyshu);
        }
    }
    else {
        if (parseInt($("#txtShuLiangs" + rowindex).val()) > 1) {
            $("#txtShuLiangs" + rowindex).val(parseInt($("#txtShuLiangs" + rowindex).val()) - 1);
        }
    }
}

//首页热卖
function GetHotShuLiangList(rowindex, shu, maxbuyshu, productid) {
    if ($("#txtShuLiangHot" + rowindex).val().length == 0) {
        $("#txtShuLiangHot" + rowindex).val("1")
    }
    //修改单个商品的数量
    if (shu > 0) {
        if (parseInt($("#txtShuLiangHot" + rowindex).val()) < maxbuyshu) {
            $("#txtShuLiangHot" + rowindex).val(parseInt($("#txtShuLiangHot" + rowindex).val()) + 1);
        }
        else {
            //alert('此类型的商品最大购买数量不能大于'+maxbuyshu);
            var content = "此类型的商品最大购买数量不能大于" + maxbuyshu;
            popBox.show('/pop/popmsg.htm', function() {
                $('#divMsg').html(content);
            });
            $("#txtShuLiangHot" + rowindex).val(maxbuyshu);
        }
    }
    else {
        if (parseInt($("#txtShuLiangHot" + rowindex).val()) > 1) {
            $("#txtShuLiangHot" + rowindex).val(parseInt($("#txtShuLiangHot" + rowindex).val()) - 1);
        }
    }
}


//首页最新商品
function GetNewShuLiangList(rowindex, shu, maxbuyshu, productid) {
    if ($("#txtShuLiangNew" + rowindex).val().length == 0) {
        $("#txtShuLiangNew" + rowindex).val("1")
    }
    //修改单个商品的数量
    if (shu > 0) {
        if (parseInt($("#txtShuLiangNew" + rowindex).val()) < maxbuyshu) {
            $("#txtShuLiangNew" + rowindex).val(parseInt($("#txtShuLiangNew" + rowindex).val()) + 1);
        }
        else {
            //alert('此类型的商品最大购买数量不能大于'+maxbuyshu);
            var content = "此类型的商品最大购买数量不能大于" + maxbuyshu;
            popBox.show('/pop/popmsg.htm', function() {
                $('#divMsg').html(content);
            });
            $("#txtShuLiangNew" + rowindex).val(maxbuyshu);
        }
    }
    else {
        if (parseInt($("#txtShuLiangNew" + rowindex).val()) > 1) {
            $("#txtShuLiangNew" + rowindex).val(parseInt($("#txtShuLiangNew" + rowindex).val()) - 1);
        }
    }
}

//验证只能输入数字并且不能超过最大商品类型数量的限制
function yanzheng(obj, maxValue, yuanshuliang, productid, propertys) {
    obj.value = obj.value.replace(/\D/g, '')
    var shengyushu = maxValue - yuanshuliang;
    if (obj.value > parseInt(maxValue)) {
        //alert('此类型的商品最大购买数量不能大于'+maxValue);
        var content = "此类型的商品最大购买数量不能大于" + maxValue;
        popBox.show('/pop/popmsg.htm', function() {

            $('#divMsg').html(content);
        });
        obj.value = yuanshuliang;
    }
    else {
        if (obj.value != "") {
            if (obj.value == "0") {
                obj.value = "1";
            }
            UpdateShoppingCart(productid, obj.value, propertys);
        }
    }
}
//商品详细页、首页中的促销商品的购买数量输入框的验证处理
function yanzhenglist(obj, maxValue, yuanshuliang) {
    obj.value = obj.value.replace(/\D/g, '')
    var shengyushu = maxValue - yuanshuliang;
    if (obj.value > parseInt(maxValue)) {
        //alert('此类型的商品最大购买数量不能大于'+maxValue);
        var content = "此类型的商品最大购买数量不能大于" + maxValue;
        popBox.show('/pop/popmsg.htm', function() {
            $('#products_sidles').cycle({
                fx: '',
                pause: 1,
                timeout: 4000
            });
            $('#divMsg').html(content);
        });
        obj.value = yuanshuliang;
    }
    else {
        if (obj.value != "") {
            if (obj.value == "0") {
                obj.value = "1";
            }
        }
    }
}
//此方法为添加Google跟踪对象使用
function ClickEvent(event) {
    _gaq.push(['_trackEvent', 'BUT', event]);
}

//点提示层后让首页中的促销商品动
function ChangePromotionAction() {
    $('#products_sidles').cycle({
        fx: 'scrollRight',
        pause: 1,
        timeout: 4000
    });
}

