function delete_collect(id,type){
     $.get(APP+"Mobile/Collection/delete_collect", { id: id,type:type}, function(data){
         if(data.status==0) {
                $.showErr(data.msg,0,'hc');
            }else if(data.status == 1){
             $.showSuccess(data.msg,0,'hc');
             window.location.reload();
         }
    }); 
}
//修改个人信息
function refer(info){
	var query=new Object();
    if(info=='pwd' || info=='user'){
        if(info=='user'){
            /*var nickname=$("input[name='nickname']").val();
            var real_name=$("input[name='real_name']").val();
            if(real_name == ''){
                $.showErr('真实姓名不能为空',0,'hc');return ;
            }*/

            var re1=/[a-zA-Z]|[0-9]/g;
            var re2=/[\u4e00-\u9fa5]/g;
            var length=0;
            var length2=0;

            var xuanz = $(".xuanz");
            for(var c=0;c<xuanz.length;c++){
                var c_dui = $(xuanz[c]).attr('vaid');
                if(xuanz[c].checked == true){
                    var xuan_name = $("#name_"+c_dui).val();
                    if(xuan_name){
                        $("#name_"+c_dui).val(xuan_name+$(xuanz[c]).val()+",");
                    }else{
                        $("#name_"+c_dui).val(","+$(xuanz[c]).val()+",");
                    }
                }
            }
        }
        query = $("form[name='myform']").serialize();
    }
    if(info=='paypwd'){
        var newpaypassword=$("input[name='newpaypassword']").val();
        if(newpaypassword == ''){
            $.showErr('新密码不能为空',0,'hc');return ;
        }
        var pwd_reg=/^\d{6}$/;
        if(!newpaypassword.match(pwd_reg)){
            $.showErr('支付密码必须为六位数字',0,'hc');return ;
        }
        query = $("form[name='myforms']").serialize();
    }
    if(info=='card_paypwd'){
        query = $("form[name='myforms']").serialize();
    }
    	$.ajax({
        		url:APP + "Mobile/UserInfo/updateinfo",
        		data:query,
        		type:"post",
        		success:function(data){
            			if(data.status==1) {
                			$.showSuccess(data.msg,0,'hc');
                            window.location.href=data.url;
	            		}else{
	                		$.showErr(data.msg,0,'hc');
	            		}
        		}
    	})
}
function jump_orderurl(id,jumpid){
    var order_ids=$("#ids").val();
	if(jumpid == 1){
        var query=new Object();
        query.address_id=id;
        $.ajax({
            url:APP + "Mobile/Address/distance",
            data:query,
            type:"post",
            success:function(data){
                if(data.status==1) {
                    window.location.href=APP+"Mobile/Takeout/order/address_id/"+id;
                }else if(data.status==2){
                    //$.showErr(data.msg,0,'hc');
                    if(confirm("无法配送到此地址，用此下单需回购物车")){
                        window.location.href=APP+"Mobile/Cart/index";
                    }
                }else{
                    $.showErr(data.msg,0,'hc');
                }
            }
        })
	}
    if(jumpid == 2){
	    var query=new Object();
        query.address_id=id;
        $.ajax({
            url:APP + "Mobile/Address/distance",
            data:query,
            type:"post",
            success:function(data){
                if(data.status==1) {
                    window.location.href=APP+"Mobile/Order/index/address_id/"+id;
                }else if(data.status==0){
                    if(confirm("无法配送到此地址，用此下单需回购物车")){
                        window.location.href=APP+"Mobile/Cart/index";
                    }
                }else{
                    $.showErr(data.msg,0,'hc');
                }
            }
        })
	}
}
//添加,修改收货地址 
function update_address(id){
    var query=new Object();
    var order_ids=$("#ids").val();
    //列表页修改默认状态
    if(id){
        query.id = id;query.status = 1;query.changetype = 1;
    }else{
        query = $("form[name='myform']").serialize();
    }
    $.ajax({
        url:APP + "Mobile/Address/update_address",
        data:query,
        type:"post",
        success:function(data){
            if(data.status==1) {
                showtoast(data.msg);
				var body = $('body').css("position");
                if(body=='relative'){
                    $('body').css("position","");
                }
                //$.showSuccess(data.msg,0,'hc');
                if(order_ids && data.order_status==1){
                    window.location.href=APP+"Mobile/Order/index";
                }else if(order_ids && data.order_status==0){
                    if(confirm("无法配送到此地址，用此下单需回购物车")){
                        window.location.href=APP+"Mobile/Cart/index";
                    }
                }else if(order_parm && data.order_status==1){
                    if(order_parm.type==1){
                        window.location.href=APP+"Mobile/Order/index/goods_id/"+order_parm.goods_id+"/number/"+order_parm.number+"/goods_attr/"+order_parm.goods_attr+"/act/"+order_parm.act+"/type/"+order_parm.type+"/addres_id/"+id;
                    }else if(order_parm.type==3){
                        window.location.href=APP+"Mobile/Order/index/goods_id/"+order_parm.goods_id+"/number/"+order_parm.number+"/goods_attr/"+order_parm.goods_attr+"/integra/"+order_parm.integra+"/type/"+order_parm.type+"/addres_id/"+id;
                    }
                } else if(order_parm && data.order_status==0){
                    if(confirm("无法配送到此地址，用此下单需回首页")){
                        window.location.href=APP+"Mobile/Index/index";
                    }
                } else if(data.order_status==0 && data.jump){
                    if(confirm("无法配送到此地址，用此下单需回购物车")){
                        window.location.href=APP+"Mobile/Cart/index";
                    }
                }else{
					// window.location.href=APP+"Mobile/address/address_list.html";
					window.location.href=data.url;
                    // window.history.back();
					// window.location.reload();
                }

            }else{
                //$.showErr(data.msg,0,'hc');
                showtoast(data.msg);
                var body = $('body').css("position");
                if(body=='relative'){
                    $('body').css("position","");
                }
            }
        }
    })
} 
 //删除地址
 function del_address(id){
    $.get(APP+"Mobile/Address/del_address", { id: id}, function(data){
         if(data.status==0) {
                //$.showErr(data.msg,0,'hc');
             showtoast(data.msg);return false;
            }else if(data.status == 1){
             //$.showSuccess(data.msg,0,'hc');
             showtoast(data.msg);
             window.location.reload();
         }
    }); 
 }
 //复制推广链接
 function copy_code(copyText) {
    if (window.clipboardData) {
        window.clipboardData.setData("Text", copyText)
    } 
    else {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
          var divholder = document.createElement('div');
          divholder.id = flashcopier;
          document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="'+ROOT_PATH+'/Public/images/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(copyText)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
    }
    $.showSuccess('代码已复制到剪贴板！',0,'hc');
}
$(document).ready(function(){ 
            $("#copy-dynamic").click( function () { 
                var url=$('input#dynamic').val();
                copy_code(url);
            });
			$("input[name='addr_search']").bind('keydown keyup',function(){
				get_detail_address();
			});
});
function get_detail_address(){
	var ak=$("input[name='ak']").val();
	var city_id=$("#city_id").val();
	var region=$("#city_id").children("option[value='"+city_id+"']").text();
    if(!region)region=$("#city_id_name").val();
	var query=$("input[name='addr_search']").val();
	if(!city_id || !region){$.showErr('请选择城市');return false;}
	if(!query)return false;
	var querydata=new Object();
		querydata.query=query;
		querydata.ak=ak;
		querydata.region=region;
		querydata.scope=1;
		querydata.output='json';
		querydata.page_size=10;
		querydata.region=encodeURIComponent(region);
	var url = '//api.map.baidu.com/place/v2/suggestion?'+ObjectToStr(querydata);
	$.ajax({
		url: url,
		type: "get",
		dataType: "jsonp",
		success: function (ret) {
			//alert(JSON.stringify(ret));
            if(ret.result&&ret.result.length>0){
                var html='';
                for(var i=0;i<ret.result.length;i++){
                    var lat = ret.result[i].location?ret.result[i].location.lat:0;
                    var lng = ret.result[i].location?ret.result[i].location.lng:0;
                    var name=ret.result[i].name;
                    var city=ret.result[i].city;
                    var district=ret.result[i].district;
					if(lat)html+='<li><a onclick="set_addr('+"'"+lng+"'"+','+"'"+lat+"'"+",'"+name+"'"+')"><h3>'+name+'</h3><p class="address">'+city+' '+district+'</p></a></li><div class="clear"></div>';
                }
                if(html){
                    $('#pois').html(html);
                }
            }
		}
	});
}
function set_addr(x,y,name){
	$("input[name='xpoint']").val(x);
	$("input[name='ypoint']").val(y);
	$("input[id='village']").val(name);
	var clos=$("#areaMasks");
	clockAreas(clos);
}
//将对象转化为&链接的字符串
function ObjectToStr(obj){
	if(typeof(obj)!='object'||!obj)return true;
	var newstr = '';
	for(var i in obj){
		newstr += '&'+i+'='+ obj[i];
	}
	return newstr;
}
//删除红包
function del_redPacket(id){
    $.get(APP+"Member/AccountLog/del_redPacket", { id: id}, function(data){
         if(data.status==0) {
                $.showErr(data.msg,0,'hc');
            }else if(data.status == 1){
             $.showSuccess(data.msg,0,'hc');
             window.location.reload();
         }
    }); 
 }
 //提交投诉
function submit_service(){
  var query=new Object();
            query = $("form[name='myform']").serialize();
      $.ajax({
             url:APP + "Mobile/UserInfo/submit_service",
             data:query,
             type:"post",
             success:function(data){
              if(data.status==1) {
                    $.showSuccess(data.msg,0,'hc');
                    window.location.href=data.mobile_url;
              }else{
                    $.showErr(data.msg,0,'hc');
              }
             }
      })
}
function logout(){
    $.get(APP+"Mobile/UserInfo/logout", '', function(data){
         if(data.status==0) {
                $.showErr(data.info,0,'hc');
                window.location.href=data.url;
            }else if(data.status == 1){
             $.showSuccess(data.info,0,'hc');
             window.location.href=data.url;
         }
    }); 
}
var ob;
function choose_attr(id,obj){
    var choose_id = $("#choose_id").val();
    ob = obj;
    if(choose_id != id){
        $("#choose_id").val(id);
        get_attr_list(id);//显示
        $('.choose_attr').show();
    }else{
        $('.choose_attr').show();
    }
}
function get_attr_list(id){
    $('.choose_goods_main').html('');
    var query = new Object();
    query.id = id;
    var url = APP+"Mobile/UserInfo/search_attr";
    $.ajax({
        url: url,
        data: query,
        type: "post",
        dataType: "json",
        success: function (ret) {
            if(ret.status){
                var html = ht_input = '';
                var xd_ob = $(ob).next(".xuan_show").children();
                if(ret.data){
                    for (var i = 0; i < ret.count; i++){
                        var is_have = 0;
                        for (var n = 0; n < xd_ob.length; n++){
                            if(ret.data[i].name == $(xd_ob[n]).val()){
                                is_have = 1;
                            }
                        }
                        if(ret.data[i].name){
                            if(ret.data[i].is_show || is_have){
                                ht_input = '<input type="checkbox"  checked="true" value="'+ret.data[i].name+'" name="choose_attr"  class="f_l" />';
                            }else{
                                ht_input = '<input type="checkbox" value="'+ret.data[i].name+'" name="choose_attr"  class="f_l" />';
                            }
                            html +='<li class="choose_goods_item clearfix" title="'+ret.data[i].name+'"><label>'+ht_input+ret.data[i].name+'</li>';
                        }
                    }
                }
                $('.choose_goods_main').html(html);
            }
        }
    });
}
function add_span(){
    var html = '';
    var obj_input = $(".choose_goods_main input");
    var choose_id = $("#choose_id").val();
    var choose = $("#name_"+choose_id+"");
    var choose_val = '';
    for (var i = 0; i < obj_input.length; i++){
        if(obj_input[i].checked == true){
            var cont = $(obj_input[i]).val();
            html += "<input type='hidden' value='"+cont+"'/>"+cont+"&nbsp;&nbsp;&nbsp;";
            choose_val += ","+cont;
            choose.val(choose_val);
        }
    }
    $(ob).next(".xuan_show").html(html);
    $('.choose_attr').hide();
}
function update_date(id){
    var currYear = (new Date()).getFullYear();  
    var opt={};
    opt.date = {preset : 'date'};
    opt.datetime = {preset : 'datetime'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式 
        mode: 'scroller', //日期选择模式
        dateFormat: 'yy-mm-dd',
        lang: 'zh',
        startYear: currYear - 100, //开始年份
        endYear: currYear + 100, //结束年份
        onSelect:function(valueText,inst){}
    };
    $("#appDate"+id).mobiscroll($.extend(opt['date'], opt['default']));
}
function close_choose(){
    $('.choose_attr').hide();
}