﻿function UpdateCart(ID, Quantity) {
    UpdateCart(ID, Quantity, 0, '', true, '');
}
//Updated:2010.03.17 By:XiaoLong
function UpdateSpecialCart(ID, Quantity, buttonid) {
    
    UpdateCart(ID, Quantity, 0, '', false, buttonid);
}


//购物车的数量
function CartCount() {

   
    $.getJSON("/Cart/GetCartCount?cc=" + new Date().getTime(), "", function(data) {
       $("#cartCount").text(data);
      $("#productCartCount").text(data);
    });

}


///将商品加入购物车，
///ID，商品主键或促销套装主键或疗程用药主键
///Quantity,购买商品数量或套装数量。
///hgExp，前换购商品所需要的总积分
///isredirect，加入到购物车之后是否跳转
///carturl：跳转的购物车地址，一般情况下为购物车网站的前缀
///barcode，商品条形码
///isexp:是否积分，0，积分      1，积分
///loginurl  登录网站地址
///mainurl   主网站地址
///weight    重量，目前暂时不用，传0
///hand_in   戒指手寸，无用 传 0
///remarks   标记 传空字符
function UpdateCart(ID, Quantity, hgExp, url, isredirect, buttonid, carturl, barcode, isexp, loginurl, mainurl, weight, hand_in, remarks) {

    var singleProduct =true; //单个产品的变量
 
    if (barcode == "1") {
        barcode = 0;
       singleProduct=false;
    }
     
    if (Quantity == "0") {
        alert("商品数量不能为零");


        if (singleProduct) {
                tocart(); //加载添加购物车窗口
                $("#info").html("商品数量不能为零");
           
        } 
        
        if (buttonid != "") {//判断列表的窗口
            tocart();
            $("#jiashoucang2").css({ top: $("#" + buttonid).position().top - 120, left: $("#" + buttonid).position().left - 150 });
            $("#info").html("商品数量不能为零!");

        }
        return;
    }
   
//    buynum = buynum + 1;
//    var currentbuynum = buynum; //当前购买次数
    
//    if (!isredirect) {
//  
//        $("#shoppingcar").css({ top: $("#" + buttonid).position().top - 50, left: $("#" + buttonid).position().left - 160 });
//        $("#shoppingcartitle").text("正在添加商品...");
//        $("#shoppingcarproductnum").html("<img src=/images/Foreground/Products/1/loading.gif />");
//        $("#shoppingcar").fadeIn(300);
//    }



    $.ajax({

        type: "GET",
        dataType: "html",
        url: mainurl + "/Cart/AddCart/" + parseInt(ID),
        data: "Quantity=" + parseInt(Quantity) + "&hgExp=" + hgExp + "&barcode=" + barcode + "&isexp=" + isexp + "&weight=" + weight + "&hand_in=" + encodeURIComponent(hand_in) + "&remarks=" + encodeURIComponent(remarks) + "&timeStamp=" + new Date().getTime(),
        success: function(result) {
            var resultgroup = result.split(',');

            if (resultgroup[0] == "1") {

                if (isredirect == true) {
                    if (typeof (carturl) == 'undefined' || carturl == "") {
                        window.location = "/cart.aspx?returnurl=" + window.location.href;
                        return;
                    }
                    else {
                        window.location = carturl + "/cart.aspx?returnurl=" + window.location.href;
                        return;
                    }
                    return;
                }
                else {

                    if (singleProduct) {

                        tocart(); //显示添加购物车成功的方法
                        $("#info").html("商品已成功添加到购物车");
                        CartCount();

                    }
                    if (buttonid != "") {


                        tocart();
                        $("#jiashoucang2").css({ top: $("#" + buttonid).position().top - 120, left: $("#" + buttonid).position().left - 160 });

                        $("#info").html("商品已成功添加到购物车");
                        CartCount(); //加载购物车的数量

                    }

                    return;
                }
            }
            else if (resultgroup[0] == "2") {
                window.location = loginurl + "/account/login?returnurl=" + window.location.href;
                return;
            }
            else if (resultgroup[0] == "0") {
                if (singleProduct) {

                    tocart();
                    $("#info").html("很抱歉，您的积分不足，不能购买!!");
                }
                if (buttonid != "") {
                    tocart();
                    $("#jiashoucang2").css({ top: $("#" + buttonid).position().top - 120, left: $("#" + buttonid).position().left - 150 });
                    $("#info").html("很抱歉，您的积分不足，不能购买!!");


                }

                return;
            }
            else if (resultgroup[0] == "3") {

                if (singleProduct) {

                    tocart();
                    $("#info").html("很抱歉，无此商品信息!!");
                }
                if (buttonid != "") {
                    tocart();
                    $("#jiashoucang2").css({ top: $("#" + buttonid).position().top - 120, left: $("#" + buttonid).position().left - 150 });
                    $("#info").html("很抱歉，无此商品信息!");
                }
                return;
            }

            else if (resultgroup[0] == "9") {
                if (isredirect == true) {
                    if (typeof (carturl) == 'undefined' || carturl == "") {
                        window.location = "/cart.aspx?returnurl=" + window.location.href;
                        return;
                    }
                    else {
                        window.location = carturl + "/cart.aspx?returnurl=" + window.location.href;
                        return;
                    }
                    return;
                }
                if (singleProduct) {

                    tocart();

                    $("#info").html("此商品购物车已存在!");
                }

                if (buttonid != "") {

                    tocart();
                    $("#jiashoucang2").css({ top: $("#" + buttonid).position().top - 120, left: $("#" + buttonid).position().left - 150 });
                    $("#info").html("此商品购物车已存在!");
                }

            }
            else {


                if (singleProduct) {

                    tocart();
                    $("#info").html("很抱歉，操作失败，请重新操作!!");
                }

                if (buttonid != "") {
                    tocart();
                    $("#jiashoucang2").css({ top: $("#" + buttonid).position().top - 120, left: $("#" + buttonid).position().left - 150 });
                    $("#info").html("很抱歉，操作失败，请重新操作!!");

                    return false;

                }

                return;
            }
        }
    })
}

//var buynum = 0; //当前页点击购买总次数

//更新页眉迷你购物车状态

function UpdateShoppingHeadState(carturl) {
    $.get(carturl + "/cart/minicart.aspx?t=" + new Date(),
            function(data) {
                if ($("#miniCart") != null) {
                    $("#miniCart").html(data);
                }
            });
}

//批量添加商品
function BatchAddProductCart(id) {
    window.open("/cart.batchaddproductcart.aspx?id=" + id + "&timeStamp=" + new Date().getTime());
}

