$(function(){
	$("#btnSendCode").attr('style',"cursor:pointer;");
	document.onkeydown = function(e){
    	var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
          var enter_event=$('#enter_event').val();
           is_member(enter_event);return false;
          
        }
	}
});
function sub_f_data(){
    var obj='#open_card';
    if ($(obj).hasClass('submitting')) {
        $.showErr('数据提交中，请勿重复提交',2,'hc');
		return;
    }
    $(obj).addClass('submitting');
    sub_data();
}
function sub_data(){
	var obj='#open_card';
	var card_sn = $("input[name='card_sn']").val();
	var paypassword = $("input[name='paypassword']").val();
	var money = $("input[name='money']").val();
	if(card_sn == '' || card_sn == 0){$.showErr('卡号不能为空',0,'hc');$(obj).removeClass('submitting');return false;}
	if(paypassword == '' || paypassword == 0){$.showErr('支付密码不能为空',0,'hc');$(obj).removeClass('submitting');return false;}
	if(money<0 || isNaN(money)){$.showErr('请填写正确数量',0,'hc');$(obj).removeClass('submitting');return false;}
	var query= new Object();
	query = $("form[name='myform']").serialize();
	$.ajax({
		type: "POST",
		url: APP+"Shop/MemberCard/update_card",
		data: query,
		success: function(data){
			if(data.status == 1){
				$.showSuccess(data.info,0,'hc');
				$("#divId input").val("");
				window.location.reload();
			}else{
				$.showErr(data.info,0,'hc');
				$(obj).removeClass('submitting');
			}
		}
	});
}
function sub_f_recharge(){
    var obj='#sub_recharge';
    if ($(obj).hasClass('submitting')) {
        $.showErr('数据提交中，请勿重复提交',2,'hc');
		return;
    }
    $(obj).addClass('submitting');
    sub_recharge();
}
function sub_recharge(){
	var obj='#sub_recharge';
	var card_sn = $("input[name='keyword']").val();
	var paypassword = $("input[name='paypassword']").val();
	var money = $("input[name='money']").val();
	if(card_sn == '' || card_sn == 0){$.showErr('卡号不能为空',0,'hc');$(obj).removeClass('submitting');return false;}
	if(paypassword == '' || paypassword == 0){$.showErr('支付密码不能为空',0,'hc');$(obj).removeClass('submitting');return false;}
	if(money<=0 || isNaN(money)){$.showErr('请填写正确数量',0,'hc');$(obj).removeClass('submitting');return false;}
	var query= new Object();
	query = $("form[name='myform']").serialize();
	$.ajax({
		type: "POST",
		url: APP+"Shop/MemberCard/sub_recharge",
		data: query,
		success: function(data){
			if(data.status == 1){
				$.showSuccess(data.info,0,'hc');
				$("#divId input").val("");
				window.location.reload();
			}else{
				$.showErr(data.info,0,'hc');
				$(obj).removeClass('submitting');
			}
		}
	});
}
function sub_f_package(){
    var obj='#sub_pack';
    if ($(obj).hasClass('submitting')) {
        $.showErr('数据提交中，请勿重复提交',2,'hc');
		return;
    }
    $(obj).addClass('submitting');
    sub_package();
}
function sub_package(){
    var obj='#sub_pack';
	var phone = $("input[name='phone']").val();
	var paypassword = $("input[name='paypassword']").val();
	var package_id=$("#package_ids option:selected").val();
    var pay_id=$("#pay_id option:selected").val();
	if(phone == '' || phone == 0 || isNaN(phone)){
		$.showErr('卡号或手机号不能为空',2,'hc');
        $(obj).removeClass('submitting');
		return false;
	}
	if(package_id == '' || package_id == 0 || isNaN(package_id)){
		$.showErr('请选择套餐',2,'hc');
        $(obj).removeClass('submitting');
		return false;
	}
    if(pay_id == '' || pay_id == 0 || isNaN(pay_id)){
		$.showErr('请选择支付方式',2,'hc');
        $(obj).removeClass('submitting');
		return false;
	}
	if(paypassword == '' || paypassword == 0){
		$.showErr('请填写支付密码',2,'hc');
        $(obj).removeClass('submitting');
		return false;
	}
	var query= new Object();
	query = $("form[name='myform']").serialize();
	$.ajax({
		type: "POST",
		url: APP+"Shop/Service/sub_packagesend",
		data: query,
		success: function(data){
			if(data.status == 1){
				$.showSuccess(data.info,0,'hc');
				window.location.reload();
			}else{
				$.showErr(data.info,0,'hc');
			}
		}
	});
}
function sub_fill(){
	var paypassword = $("input[name='paypassword']").val();
	var card_sn = $("input[name='card_sn']").val();
	if(card_sn == '' || card_sn == 0){
		$.showErr('卡号不能为空',0,'hc');
		return false;
	}
	if(paypassword == '' || paypassword == 0){$.showErr('支付密码不能为空',0,'hc');return false;}
	var query= new Object();
	query = $("form[name='myform']").serialize();
	$.ajax({
		type: "POST",
		url: APP+"Shop/MemberCard/update_fill",
		data: query,
		success: function(data){
			if(data.status == 1){
				$.showSuccess(data.info,0,'hc');
				window.location.reload();
			}else{
				$.showErr(data.info,0,'hc');
			}
		}
	});
}
//验证会员
function is_member(type){
	var url = APP+'shop/MemberCard/is_member';
    var query = new Object;
	if(type == 3){
		var mobile = $("input[name='keyword']").val();
		if(!mobile){
			$.showErr('手机号或者卡号不能为空',0,'hc');
			return false;
		}
	}else{
		var mobile = $("input[name='phone']").val();
		if(!mobile){
			$.showErr('手机号不能为空',0,'hc');
			return false;
		}
	}
	
	
	query.mobile = mobile;
    query.type = type;
    $.ajax({
        url: url,
        type: "post",
        data: query,
        dataType: "json",
        success: function (ret) {
            if (ret.status) {
                if(type == 5){
                    $(".inforbox").show();
                }
				$("#memberinfo").html(ret.data);
            }else{
				$.showErr(ret.info,0,'hc');
				return false;
			}
        }
    });
}
var InterValObj; //timer变量，控制时间
var curCount;//当前剩余秒数
function sendMessage() {
	var time = $("#true_time").val();
	var count = parseInt(time); //间隔函数，1秒执行
    curCount = count;
	//设置button效果，开始计时
    var url=APP+"Home/Ajax/sendsms";
    var mobile=$('#phone').val();
    var unique=$('#unique').val();
    if(verify_mobile(mobile)==false) return false;
    var param="mobile=" + mobile + "&type=" + 4;
        param += "&unique=" + unique;
    var sms_reg_verify=$('#sms_reg_verify').val();
    if(sms_reg_verify) {
        var regverify = $('#regverify').val();
        param += "&regverify=" + regverify;
    }
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: url,
        data:param,
        success:function (data){
        if(data.status!=0){
            $.showErr(data.msg,0,'hc');
            return false;
        }else{
			$("#btnSendCode").removeAttr('onclick');
			$("#btnSendCode").attr('style',"cursor:not-allowed");
            $("#btnSendCode").attr("disabled", "true");
            $("#btnSendCode").html(curCount + "s后重新获取");
            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        }
    }
});
}
function get_pdata(id){
	if(id == ''){
		return false;
	}
	var url = APP+'shop/MemberCard/get_pdata';
    var query = new Object;
    query.id = id;
    $.ajax({
        url: url,
        type: "post",
        data: query,
        dataType: "json",
        success: function (ret) {
        	console.log(ret);
            if (ret.status) {
				$("#p_price").html(ret.data.price+'元');
				$("#p_time").html(ret.data.continued+'天');
				$("#p_shops").html(ret.data.price);
				$("#p_service").html(ret.data.service);
            }else{
				$("#membername").html('');
				$("#membersex").html('');
				$("#phone").val();
				$.showSuccess(ret.info);
				return false;
			}
        }
    });
	
}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
		$("#btnSendCode").attr('style',"cursor:pointer;");
		$("#btnSendCode").attr('onclick',"return sendMessage()");
        $("#btnSendCode").removeAttr("disabled");//启用按钮
        $("#btnSendCode").html("获取验证码");
    }else {
        curCount--;
        $("#btnSendCode").html(curCount + "s后重新获取");
    }
}
function verify_mobile(phone){
    if(phone == ''){
        $.showErr('手机号不能为空',0,'hc');
        return false;
    }
    if(!$.checkMobile(phone)){
        $.showErr('手机号格式不正确',0,'hc');
        return false;
    }
}
function enter(type){
	if(!type || type==0){
      $.showErr('参数错误');
    }
	$('#enter_event').val(type);
}
