function ChangeTotal(cartid,gcount,rectype,ob,pid){

    var cartid1 = $(".shop_list :checked");
    var cart = '';
    for(var i=0;i<cartid1.length;i++){
       if(cartid1.eq(i).val() != ''){
           cart += cartid1.eq(i).val()+',';
       }
    }

    if(parseInt(gcount)<parseInt(ob.parent('.shop_list_amount').next().val())){
        gcount = ob.parent('.shop_list_amount').next().val();
        ob.val(gcount);
    }
    var goods_10_goods_l0_number = ob.parent('.shop_list_amount').next().val()
    if(ob.val() >= goods_10_goods_l0_number)
    {
        ob.parent('.shop_list_amount').siblings('.noPay').remove()
    }
    $.ajax({
        url:'/shop/cart/ChangeCart',
        data:{'cartid':cartid,'gcount':gcount,'cart':cart,'rec_type':rectype,'pid':pid},
        type:'post',
        dataType:"json",
        success:function (e){
            var rs =  eval(e);
            if (rs.success == "1") { 
                var total = rs.total;
                $('.shop_bottom_s').html('￥'+total['total1']);
                $('.shop_q').eq(0).html('￥'+total['total']);
                //shop_q
                $('.shop_q').eq(1).html('￥'+total['jian']);
                $('.shop_list_red').html(total['count']);
            } else {
                ob.val(rs.count);
            }
        }
    });
}  
function ReMove(cartid){
    $.ajax({
        url:'/shop/cart/removeCart',
        data:{'cartid':cartid},
        type:'post',
        success:function (e){
            var data = eval('('+e+')');
            if(data['result'] == 1){
                location.reload(true);
            }else{
               // alert('删除失败');
            }
        }
    });
}

function RemoveAll(){
    var cartid = $(".shop_list_input:checked");
    var cart = '';
    for(var i=0;i<cartid.length;i++){
       cart += cartid.eq(i).val()+',';
    }
    $.ajax({
        url:'/shop/cart/removeCarAll',
        data:{'cartid':cart},
        type:'post',
        success:function (e){
            var data = eval('('+e+')');
            if(data['result'] == 1){
                location.reload(true)   
            }else{
            }
        }
    });
}

function address_select(num,ob){
    
    if(num == 3){
        return;
    }
    address = $(ob).val();
  
    $.ajax({
        async:false,
        url:'/shop/api/select_address',
        data:{'address_id':address},
        type:'post',
        success:function(e){
            //alert(e);return;
            var data = eval('('+e+')');
            str = '<option value=\'\'>请选择</option>';
            for(var i in data){
                str += '<option value=\''+data[i].address_id+'\'>'+data[i].address_name+'</option>';
            }
            if(num == 1){
                $('#city').html(str);
                $('#county').html('<option value=\'\'>请选择</option>');
            }
            if(num == 2){
                $('#county').html(str);
            }
        }
    });
}

function addreceuser(){
    var recevingid = $('#recevingid').val();
    //alert(recevingid);
    var isnew = $('#new_address').attr('checked');
    //alert(isnew);
    //return;
    if(isnew == 'checked'){
        isupdate = 0;
    }else{
        isupdate = 1;
    }
    var isnew = $('#newaddress').val();
    
    if(isnew == 1){
        isupdate = 0;
    }
    //recename provice city county addressinfo phone fix_phone
    var recename = $('#recename').val();
    if(!real_name_test(recename)){
        $('#message').html('请输入你的姓名');
        checkingall("#recename","#message");
        return false;
    };
    var provice = $('#provice').val();
    var city = $('#city').val();
    var county = $('#county').val();
    var addressinfo = $('#addressinfo').val();
    var phone = $('#phone').val();

    if(provice == ''){
        $('#message').html('请选择省份');
        checkingall("#county","#message");
        return false;
    }
    if(city == ''){
        $('#message').html('请选择城市');
        checkingall("#county","#message");
        return false;
    }
    if(county == ''){
        $('#message').html('请选择县区');
        checkingall("#county","#message");
        return false;
    }
    if(addressinfo == ''){
        $('#message').html('请填写详细地址');
        checkingall("#addressinfo","#message");
        return false;
    }
    if(!phone_test(phone)){
        //alert('请填写正确的手机号');
        $('#message').html('请填写正确的手机号');
        checkingall("#fix_phone","#message");
        return false;
    }
    var fix_phone = $('#fix_phone').val();
    $.ajax({
        async:false,
        url:'/shop/order/AddReceving',
        data:{'recevingid':recevingid,'recename':recename,'provice':provice,'city':city,'county':county,'addressinfo':addressinfo,'phone':phone,'fix_phone':fix_phone,'isupdate':isupdate},
        type:'post',
        success:function(e){
            //return;
            if(e == 1){
                location.reload();
            }
        }
    });
    
}

function changefix(id){
    $.ajax({
        async:false,
        url:'/shop/order/ChangeFix',
        data:{'reid':id},
        type:'post',
        success:function(e){
            var data = eval('('+e+')');
            if(e != 0){
                updataShopinformation();
                $('.bd_xianshi p').eq(0).html(data.receving_user+' '+data.phone);
                $('.bd_xianshi p').eq(1).html(data.provice_name+' '+data.city_name+' '+data.county_name+' '+data.address_infomation);
            }else{
                //alert('修改失败');
            }
        }
    });
}

function checktotal(){
    //alert(1111);
    var cartid = $(".shop_list :checked");
    var cart = '';
    for(var i=0;i<cartid.length;i++){
       if(cartid.eq(i).val() != ''){
           cart += cartid.eq(i).val()+',';
       } 
    }
    $.ajax({
        async:false,
        url:'/shop/cart/CheckCart',
        data:{'cart':cart},
        type:'post',
        success:function(e){
            if(e != 0){
                var data = eval('('+e+')');
                $('.shop_bottom_s').html('￥'+data['total1']);
                                //$('.shop_bottom_s').html('￥'+data['total']);
                $('.shop_q').eq(0).html('￥'+data['total']);
                //shop_q
                $('.shop_q').eq(1).html('￥'+data['jian']);
                $('.shop_bottom_btn').attr('href','javascript:AddOrder();');
                $('.shop_list_red').html(data['count']);
            }else{
                $('.shop_bottom_s').html('￥0.00');
                $('.shop_bottom_btn').attr('href','javascript:;');
                                //$('.shop_bottom_s').html('￥'+data['total']);
                $('.shop_q').eq(0).html('￥0.00');
                $('.shop_q').eq(1).html('￥0.00');
                //shop_q
                $('.shop_list_red').html(0);
            }
        }
    });
}
function AddOrder(){
    var kucun = $('#wuhuo').size();
    if(kucun >= 1){
        alert('你添加的商品可能没货，请确认后在下单');
        return false;
    }
    var nopay = 0;
    $('.shop_list_input').each(function(){
        if($(this).is(':checked'))
        {
            if($(this).siblings('.noPay').size()>0){
                nopay++;
            }
        }
    });
    if(nopay>0){
        alert('部分商品未达到起订量！');
        return false;
    }
	/*@author lhf @date 20151217 */
	var cartid = $(".shop_list_input");
	var cart   ='';
	for(var i=0;i<cartid.length;i++){
		if(cartid[i].checked){
			cart += cartid.eq(i).val()+',';
		}
    }
    $.ajax({
        async:false,
        url:'/shop/order/AddOrderAfter',
        data:{'cart':cart},
        type:'post',
        success:function(e){
            if(e == 1){
                location.href='/shop/order/AddReceving';
            }
        }
    });
}

function AddOrders(){
    var receid = $('#receid').val();
    var kucun = $('#wuhuo').size();
    if(receid == undefined || receid == '' ){
        alert('请添加送货地址');
        return false;
    }
    if(kucun >= 1){
        alert('你添加的商品可能没货，请确认后在下单');
        return false;
    }
    document.getElementById("OrderFrm").submit();  
}

function deleteReceving(id,ob){
    alert(id);
    return false;
    $.ajax({
        async:false,
        url:'/shop/order/DeleteReceing',
        data:{'receving':id},
        type:'post',
        success:function(e){
            if(e == 1){
                location.href='/shop/order/AddReceving';
            }
        }
    });
}


function changecount(ob){
    
    var goodid = $('#goods_id').val();
    var goods_l0_number = parseInt($('#goods_l0_number').val());
    var kucun_num=parseInt($(ob).val());
    if(kucun_num <= goods_l0_number || isNaN(kucun_num)){
        $(".sale_text").val(goods_l0_number)
        kucun_num = goods_l0_number;
    }
    //$('.addcar').attr('href','/shop/cart/AddCart/pid/'+goodid+'/ptype/1/pcount/'+kucun_num);
}

function SearchGoods(type,value){
    //alert(type);
    if(type == 'brand'){
        $('#brand').val(value);
    }
    if(type == 'class2'){
        $('#class2').val(value);
    }
    if(type == 'price'){
        $('#price').val(value);
    }
    if(type == 'sort'){
        $('#sort').val(value);
    }
    if(type == 'sort' && value == 'time'){
        $('#sort').val(value);
    }
    
    var start = '';
    var stop = '';
    if(type == 'price' && value == 7){
        var start = $('#price_start').val();
        stop = $('#price_stop').val();
    }
    var class1 = $('#class1').val();
    var sort = $('#sort').val();
    var brand = $('#brand').val();
    var class2 = $('#class2').val();
    var price = $('#price').val();
    start = $('#price_start').val();
    stop = $('#price_stop').val();
    var url = "/shop/index/ShopList";
    var keyword = $('#keyword').val();
    var address = $('#address').val();
    
    if(typeof(keyword) != "undefined" && keyword != ''){
        var url = "/shop/index/SearchList";
    }
    if(typeof(address) != 'undefined' && address !=''){
        url +='/area/'+address;
    }
    if(typeof(class1) != "undefined" && class1 != ''){
        url +='/class1/'+class1;
    }
    if($('.chbyh:checked').size() == 1){
        url +='/stock/1';
    }else{
        url +='/stock/0';
    }

    if(typeof(sort) != "undefined" && sort != ''){
        url +='/sort/'+sort;
    }
    if(typeof(brand) != "undefined" && brand != ''){
        url +='/brand/'+brand;
    }
    if(typeof(class2) != "undefined" && class2!= ''){
        url +='/class2/'+class2;
    }
    if(typeof(price) != "undefined" && price != ''){
        url +='/price/'+price;
    }
    if(typeof(start) != "undefined" && start != ''){
        url +='/start/'+start;
    }
    if(typeof(stop) != "undefined" && stop != ''){
        url +='/stop/'+stop;
    }
    if(typeof(keyword) != "undefined" && keyword != ''){
        url +='/keyword/'+keyword;
    }
    //alert(url);
    location.href=url;
    
}


function Goodspage(p){
    var start = '';
    var stop = '';
    var price = $('#price').val();
    if(price == 7){
        start = $('#price_start').val();
        stop = $('#price_stop').val();
    }
    var class1 = $('#class1').val();
    var sort = $('#sort').val();
    var brand = $('#brand').val();
    var class2 = $('#class2').val();
    $.ajax({
        url:'/shop/index/ShopListSearch',
        data:{'brand':brand,'class2':class2,'price':price,'class1':class1,'start':start,'stop':stop,'sort':sort,'p':p},
        type:'post',
        success:function(e){
            //alert(e);return;
            if(e!=1){
                var data = eval('('+e+')');
                var str = "";
                for(var i in data['list']){
                    //alert(data['list'][i]._id);
                    //alert(data['list'][i]._id);
                    if((parseInt(i)+1)%4 == 0){
                        str+="<div class=\"list_cot_one mgrn\">";
                    }else{
                        str+="<div class=\"list_cot_one\">";
                    }
                    str+="<p><a href=\"/shop/index/Goodsinfo/goods/"+data['list'][i]._id+"\">";
                    if(data['list'][i].image == '' || data['list'][i] == 'undefined'){
                         str+="<img src=\"http://resource.nsyh001.com/Photo/pic/img06.jpg\"/>";
                    }else{
                        str+="<img src=\"http://static.nsyh001.com/upload/"+data['list'][i].image+"\"/>";
                    }
                    str+="</a></p><p class=\"mgb8\"><a href=\"/shop/index/Goodsinfo/goods/"+data['list'][i]._id+"\">"+data['list'][i].name+"</a></p><p class=\"mgb8\"><a href=\"javascript:void(0)\" class=\"font_16 clc1 mgr10\">￥"+data['list'][i].price_now+"</a><span class=\"tdlt clh2\">门店价:￥"+data['list'][i].price+"</span></p><p><a href=\"/shop/cart/AddCart/pid/"+data['list'][i]._id+"/ptype/1/pcount/1\" class=\"addcar2\">加入购物车</a>";
                    if(data['list'][i]['iscollection'] == 0){
                        str+="<a href=\"#\" class=\"shoucang\">收藏</a>";
                    }else{
                        str+="<a href=\"#\" class=\"shoucang\">已收藏</a>";
                    }
                    
                    
                    
                    str+="</p></div>";
               
                }
                str+= "<div class=\"p_pages pdtbn\"> "+data['page']+"</div><div class=\"clear\"></div>";
                
                $('.list_cot').html(str);
                pagecss('Goodspage');
		$('.clred').eq(1).html(p);		

               // $(window).load("http://resource.nsyh001.com/Js/jquery.scrollLoading.min.js");
                   // </a></p><p class=\"mgb8\"><a href=\"/shop/index/Goodsinfo/goods/{$vo._id}\">{$vo.name|cut=###,60}</a></p><p class=\"mgb8\"><a href=\"javascript:void(0)\" class=\"font_16 clc1 mgr10\">￥{$vo.price_now}</a><span class=\"tdlt clh2\">门店价:￥{$vo.price}</span></p><p><a href=\"/shop/index/AddCart/pid/{$vo._id}/ptype/1/pcount/1\" class=\"addcar2\">加入购物车</a><a href=\"#\" class=\"shoucang\">收藏</a></p></div>";
               }else{
                   $('.list_cot').html('无数据');
               }
        }
    });
}


function SeachComment(star){
    var goods = $('#goods').val();
    $.ajax({
        url:'/shop/index/SeachComment',
        data:{'star':star,'goods':goods},
        type:'post',
        success:function(e){
            //alert(e);return;
            if(e != 1){
                var data = eval('('+e+')');
                var str = '';
                for(var i in data){
                    str +="<li><div class=\"left\"><p><a href=\"#\"><img src=\""+data[i]['user'].user_face+"\" width=\"40\" height=\"40\" /></a></p><p>"+data[i]['user'].show_name+"</p></div><div class=\"right\"><p>"+data[i].content_r+"</p><p>评分：<img src=\"http://resource.nsyh001.com/Photo/star"+data[i].comment_star+".gif\" width=\"93\" height=\"14\" /></p>";
                    if(data[i].comment_time == undefined){
                         str +="<p></p></div><div class=\"clear\"></div></li>";
                    }else{
                        str +="<p>"+data[i].comment_time+"</p></div><div class=\"clear\"></div></li>";
                    }       
                }
                $('#goods_comment').html(str);
            }else{
                $('#goods_comment').html('');
            }
        }
    });
}

function UpdateReceving(receving,ob){
    //alert(receving);
    
    $.ajax({
        url:'/shop/order/UpdateReceving',
        data:{'receving':receving},
        type:'post',
        success:function(e){
            //alert(e);return;
            var data = eval('('+e+')');
            //return;
            $('#recename').val(data['info'].receving_user);
            $('#addressinfo').val(data['info'].address_infomation);
            $('#phone').val(data['info'].phone);
            $('#fix_phone').val(data['info'].Fixed);
            var str = '';
            for(var p in data['provice'] ){
                if(data['info'].provice == data['provice'][p].address_id){
                    //alert(1);
                    str += '<option selected value=\''+data['provice'][p].address_id+'\'>'+data['provice'][p].address_name+'</option>';
                }else{
                    str += '<option value=\''+data['provice'][p].address_id+'\'>'+data['provice'][p].address_name+'</option>';
                }
            }
            var str1 = '';
            //alert(data['info'].city);
            for(var ci in data['city'] ){
                if(data['info'].city == data['city'][ci].address_id){
                  
                    str1 += '<option selected value=\''+data['city'][ci].address_id+'\'>'+data['city'][ci].address_name+'</option>';
                }else{
                    str1 += '<option value=\''+data['city'][ci].address_id+'\'>'+data['city'][ci].address_name+'</option>';
                }
//              str1 += '<option value=\''+data['city'][ci].address_id+'\'>'+data['city'][ci].address_name+'</option>';
            }
            var str2 = '';
            for(var co in data['county'] ){
                if(data['info'].county == data['county'][co].address_id){
                
                    str2 += '<option selected value=\''+data['county'][co].address_id+'\'>'+data['county'][co].address_name+'</option>';
                }else{
                    str2 += '<option value=\''+data['county'][co].address_id+'\'>'+data['county'][co].address_name+'</option>';
                }
                //str2 += '<option value=\''+data['county'][co].address_id+'\'>'+data['county'][co].address_name+'</option>';
            }
            //alert(str2);
            //recevingid
       
            $('#recevingid').val(receving);
            $('#provice').html(str);
            $('#city').html(str1);
            $('#county').html(str2);
            //alert(e);
        }
    });
}
function updataShopinformation(){
    var rec_type = $('#rec_type').val();
    var goods = $('#goods').val();
    var goods_count = $('#goods_count').val();
    
    if(typeof(rec_type) != 'undefinded' && rec_type!=''){
        rec_type = rec_type;
    }else{
        rec_type= '';
    }
    if(typeof(goods) != 'undefinded' && goods!=''){
        goods = goods;
    }else{
        goods= '';
    }
    if(typeof(goods_count) != 'undefinded' && goods_count!=''){
        goods_count = goods_count;
    }else{
        goods_count= '';
    }
    var photo = $('#image').val();
    $.ajax({
        url:'/shop/order/getShopInformation',
        data:{'rec_type':rec_type,'goods':goods,'goods_count':goods_count},
        type:'post',
        dataType:"json",
        success:function (e){
            var rs =  eval(e);
            if (rs.success == "1") { 
                var good = rs.good;
                var str = '<ul>';
                var flag = 0;
                for(var i in good){
                    str +="<li class=\"first\"><img src=\""+photo+good[i].goods.image+"\" width=\"53\" height=\"51\" />";
                    str+="<a target=\"_blank\" href=\"/shop/index/Goodsinfo/goods/"+good[i].goods._id+"\" class=\"cllan2\">"+good[i].goods.name+"</a>";
                    str+="</li><li>"+good[i].goods.number+"</li><li>￥"+good[i].goods.price_now+"/袋</li>";

                    str+="<li>";
                    if(good[i]['kucun'] == 0){
                         str+="<input type=\"hidden\" id=\"wuhuo\">无货";
                         flag = 1;
                    }else{
                        str+="有货";
                    }
                    str+="</li>";

                    str+=" <li>"+good[i].goods_count+"</li><li class=\"clred1\">￥"+good[i].goods_count*good[i].goods.price_now+"</li>";
                }
                str+='</ul>';
                
                if(flag == 1){
                    $('.bd_xianshi p').eq(2).show();
                }else{
                    $('.bd_xianshi p').eq(2).hide();
                }
                $('#shop_show').html(str);

            } else {
               
            }
        }
    });
}
function deleteReceving(receving,ob){
    //alert(receving);
    $.ajax({
        url:'/shop/order/deleteReceving',
        data:{'receving':receving},
        type:'post',
        success:function(e){
            if(e == 1){
                location.reload();
            }else{
                alert('删除失败');
            }
        }
    });
}

function jindou1(jin){
    var jindou = $('#tjindou').val();
    var balance = $('#tbalance').val();
    var dikou = $('#dikou').html();
    var number = /^[1-9][0-9]*$/;
    var rmb = 0;
    if(jin-jindou < 0){
        return false;
    }
    if (number.test(jindou)) {  
        $('#jindou').val(jindou);
        rmb = jindou/100;
        dikou = parseFloat(rmb)+parseFloat(dikou);
        $('#dikou').html(dikou);
        $('#jindou1').html(jindou);
        $('#needrmb').html(rmb);
        var total = $('#total1').val();
        $('#stotal').html((total-dikou).toFixed(2));
    } else{
        return false;
    }
}
function balance(ob,balan){
    var balance = $('#tbalance').val();
    var jindou = $('#tjindou').val();
    var dikou = $('#dikou').html();
    var total = $('#total1').val();
    var number = /^[1-9][0-9]*$/;  
    if(number.test(balance)) { 
        if(balan-balance < 0){
            return false;
        }
        if(total-balance < 0){
            return false;
        }
        rmb = jindou/100;
        $('#balance').val(balance);
        
        dikou = parseFloat(balance)+parseFloat(dikou);
        $('#dikou').html(dikou);
        $('#stotal').html((total-dikou).toFixed(2));
        $('#syye').html(balance);
        //alert(balance);
    } else{
        return false;
    }
}

function checkBanlance(ob,balan){

    var balance = $(ob).val();
    var number = /^[1-9][0-9]*$/;  
    if(number.test(balance)) { 
        var total = $('#stotal').html();
        if(balance-balan > 0){
            $(ob).parent().next().hide();
            return false;
        }else{
           $(ob).parent().next().show(); 
        }
        if(total-balance < 0){
            $(ob).parent().next().hide();
        }else{
            $(ob).parent().next().show();
        }
    }else{
        $(ob).parent().next().hide();
    }
}
function checkJindou(ob,jindou){
   
    var jin = $(ob).val();
    //$(ob).parent().parent().next().hide();
    var number = /^[1-9][0-9]*$/;  
    if(number.test(jin)) { 
        var total = $('#ytotal1').html();
        rmb = jin/100;
        //alert(rmb-total);
        
        if(rmb-total > 0){
            $(ob).parent().parent().next().hide();
            return;
        }
        
        if(jindou-jin < 0){
            $(ob).parent().parent().next().hide();
            return false;
        }else{
            //alert(jindou);
            $(ob).parent().parent().next().show();
        }
    }else{
        $(ob).parent().parent().next().hide();
    }
}
function Uncheckbalance(){
    var balance = $('#tbalance').val();
    $('#tbalance').val(''); //余额
    $('#balance').val(''); //余额
    var dikou = $('#dikou').html();
    dikou = (dikou-balance).toFixed(2);
    $('#dikou').html(dikou);
    var total = $('#total1').val();
    $('#stotal').html((total-dikou).toFixed(2));


}

function Uncheckjindou(){
    //var balance = $('#tbalance').val(); //余额
    var dikou = $('#dikou').html();
    var jindou = $('#tjindou').val();   //金豆
    rmb = jindou/100;
    $('#tjindou').val('');
    $('#jindou').val('');
    dikou = (dikou-rmb).toFixed(2);
    $('#dikou').html(dikou);
    var total = $('#total1').val();
    $('#stotal').html((total-dikou).toFixed(2));
}
function Collection(ob,id){
    //alert(id);
    $.ajax({
        url:'/shop/index/Collection',
        data:{'id':id},
        type:'post',
        success:function(e){
            //alert(e);
            var rs =  eval(e);
            if(rs.success == "1"){
                //$(ob).html('收藏('+rs.count+')');
                $(ob).html('已收藏');
            }else if(rs.success == "2"){
                location.href=rs.return_url;
   
            }else{

            }
        }
    });
}
//collectioncount
function Collections(ob,id){
    //alert(id);
    $.ajax({
        url:'/shop/index/Collection',
        data:{'id':id},
        type:'post',
        dataType:'json',
        success:function(e){
            //alert(e);
            var rs =  eval(e);
            if(rs.success == "1"){
                $(ob).parent().html("<a  href=\"javascript:;\" onclick='unCollections(this,\""+id+"\")'>取消收藏</a>");
            }else if(rs.success == "2"){
                location.href=rs.return_url;
            }else{
                
            }
           
            
        }
    });
}

