/**
 * 商品数量加减
 * @param k “+”或者“-”
 */
function addnum(k,id){
    var moq = parseInt($('#moq'+id).val());
    var num = parseInt($('#change_num'+id).val());
    switch (k) {
        case "+":
            num = num + 1;
            break;
        case "-":
            if (num !==1 && num > moq) {
                num = num - 1;
            } else {
                $.showErr('起订量不得低于'+moq,0,'hc');return false;
            }
            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');
    }
    if(num>0){
        $('#change_num'+id).val(num);
        $("#product_amount").val(num);
    }else{
        $.showErr('数量不能小于'+moq+'件',0,'hc');
        $('#change_num'+id).val(moq);
        $("#product_amount").val(moq);
    }

}
/**
 * 加入购物车
 */
function addcart(k){
    var url='';
    var goods_id=$('#goods_id').val();
    var arr=checkarrkey_str;
    var number=parseInt($('#change_num'+goods_id).val());
    var moq = parseInt($('#moq'+goods_id).val());
    var act=parseInt($("#act").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;
    }
    if(query.goods_attr==0 && moq>number){
        $.showErr('数量不能小于'+moq+'件',0,'hc');return false;
    }
    if(k==1){
        query.type=1;
        url=APP+"PurGoods/buy_now";
    }else{
        query.type=0;
        url=APP + "PurGoods/addcart";
    }
    if(k==0){
        query.money=price;
    }
    query.name=names;
    query.number = number;
    query.act = act;
    query.pack_id=parseInt($('#package'+goods_id).val());
    $.ajax({
        url:url,
        data:query,
        type:"get",
        success:function(data) {
            if (k > 0) {
                window.location.href=APP+"PurOrder/index/goods_id/"+data.goods_id+"/number/"+data.number+"/goods_attr/"+data.goods_attr+"/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 packid = parseInt($('#pack_id'+id).val());
    var query=new Object();
    query.goods_id=id;
    query.pack_id=packid;
    query.number= parseFloat($('#change_num'+id).val());
    $.ajax({
        url:APP + "PurGoods/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');
            }
        }
    })
}
