/**
 * 商品数量加减
 * @param k “+”或者“-”
 */
function addnum(k,id){
    var num = parseInt($('#change_num'+id).val());
    var buy_limit = parseInt($('#buy_limit').val());
    switch (k) {
        case "+":
            num = num + 1;
            if(num > buy_limit){
                num = num - 1;
                $.showErr('按拼团价格买每人限购'+buy_limit+'件，不能超过数量限制', 0,'hc');
            }
            break;
        case "-":
            if (num !== 1) {
                num = num - 1;
            } else {
                $.showErr('数量不能小于1件', 0,'hc');
            }
            break;
    }
    if(num>1){
        $('#reducenum'+id).removeClass('no_reduce').addClass('reduce');
        $('#reducenum'+id).removeClass('dis_decrease').addClass('decrease');
    } else{
        $('#reducenum'+id).removeClass('reduce').addClass('no_reduce');
        $('#reducenum'+id).removeClass('decrease').addClass('dis_decrease');
    }
    $('#change_num'+id).val(num);
    $("#product_amount").val(num);
}
/**
 * 加入购物车
 */
function addcart(k){
    //alert(checkarrkey_str);return false;
    var url='';
    var goods_id=$('#goods_id').val();
    var arr=checkarrkey_str;
    var number=parseInt($('#change_num'+goods_id).val());
    var act=parseInt($("#act").val());

    //alert(act);return false;
    //var attr_price=combinationprice[arr]['attr_price'];
    var attrs=new Array();
     attrs=arr.split("_");
    var names='';
    for(i=0;i<attrs.length;i++){
       var name=$("#attr"+attrs[i]+"").html();
       var parent_name= $("#attr"+attrs[i]+"").parent().parent().parent().siblings("dt").html();
        var allname=parent_name+":"+name;
        names+=allname+' ';
    }
    var query = new Object;
    query.goods_id = goods_id;
    if(arr){
        query.goods_attr = arr;
    }else{
        query.goods_attr=0;
    }
    if(k==1){
        query.type=1;
        url=APP+"Goods/buy_now";
    }else if(k==2){
        query.type=2;
    }else if(k==3){
        query.type=6;
        url=APP+"Goods/buy_now";
    }else{
        query.type=0;
        url=APP + "Goods/addcart";
    }
    if(k==0){
        query.money=price;

    }
    query.name=names;
    query.number = number;
    query.act = act;
    // query.score = score;
    $.ajax({
        url:url,
        data:query,
        type:"get",
        success:function(data) {
            if (k > 0) {
                window.location.href=APP+"Order/index/goods_id/"+data.goods_id+"/number/"+data.number+"/goods_attr/"+data.goods_attr+"/act/"+data.act+"/type/"+data.type;
            } else {
                if (data.status == 0) {
                    $.openUrl(data.url, data.msg, 'ID', '800', function () {
                        return false;
                    }, function () {
                        return false;
                    });
                } else if (data.status == 2 && k == 0) {
                    $.openUrl(data.url, data.msg, 'ID', '400', function () {
                        return false;
                    }, function () {
                        return false;
                    });
                } else if (data.status == 2 && k > 0) {
                    window.location.href = data.url;

                } else {
                    $.showErr(data.msg, 0, 'hc');
                }
            }
        }
    })
}
/**
 * 列表页加入购物车
 */
function addlistcart(id){
    var act=$("#act"+id).val();
    var query=new Object();
    query.goods_id=id;
    query.act=act;
    query.number= parseFloat($('#change_num'+id).val());
    $.ajax({
        url:APP + "Goods/add_cartlist",
        data:query,
        type:"post",
        success:function(data){
            if(data.status==0) {
                $.openUrl(data.url, data.msg, 'ID', '800', function () {
                    return false;
                }, function () {
                    return false;
                });
            }else if(data.status==2){
                $.openUrl(data.url, data.msg, 'ID', '400', function () {
                    return false;
                }, function () {
                    return false;
                });
            }else if(data.status==3){
                $.openUrl(data.url, data.msg, 'ID','700', function () {
                    return false;
                }, function () {
                    return false;
                });}else{
                $.showErr(data.msg,0,'hc');
            }
        }
    })
}
/**
 * 积分商品加入购物车
 */
function add_integral_cart(){
    var goods_id=$("#goods_id").val();
    var arr=checkarrkey_str;
    var number=parseInt($('#change_num'+goods_id).val());
    var attrs=new Array();
    attrs=arr.split("_");
    var names='';
    for(i=0;i<attrs.length;i++){
        var name=$("#attr"+attrs[i]+"").html();
        var parent_name= $("#attr"+attrs[i]+"").parent().parent().parent().siblings("dt").html();
        var allname=parent_name+":"+name;
        names+=allname+' ';
    }
    var query = new Object;
    query.goods_id = goods_id;
    if(arr){
        query.goods_attr = arr;
    }else{
        query.goods_attr=0;
    }
    query.number=number;
    query.name=names;
    query.number = number;
    query.integra = integra;
    query.type = 3;
    $.ajax({
        url:APP+"Goods/buy_now",
        data:query,
        type:"get",
        success:function(data){
            window.location.href=APP+"Order/index/goods_id/"+data.goods_id+"/number/"+data.number+"/goods_attr/"+data.goods_attr+"/integra/"+data.integra+"/type/"+data.type;

        }
    })

}