$(function(){
    tel={
        //总入口
        init:function(){
            var that=this;
            that.validate('#hack',that.msg.no_tel,that.btnname.just_white);
            //that.validate('#bind',that.msg.no_tel,that.btnname.just_white);
            that.time('#hack');
            that.close('#close');
            that.close('#go');
            that.active();
            that.blurValidatetel('keyup');
            that.blurValidatetel('blur');
        },
        //弹出信息
        msg:{
            no_tel:'请认真填写你的手机号',
            no_yanz:'请填写验证码',
            yanz_err:'验证码错误',
            lg:'该手机已经领取过红包',
            no_lq:'该手机不能领取红包',
            remove:'获取验证码哦',
            over:'该手机已经领取过红包',
            no_pass:'该手机号不能领取红包'
        },
        //按钮信息
        btnname:{
            just_white:'这就写',
            rewrite:'重新填写',
            check:'知道了'
        },
        //判断验证顺序
        b:false,
        //插入验证结构
        validate:function(id,msg,btnmsg,sj){
            var that=this;
            $(id).on('touchstart',function(e){
                e.preventDefault();//阻止浏览器默认事件
                var telVal=$('#tel_number').val(),
                    tel=new RegExp("^[1][3-8]+\\d{9}");
                //console.log(tel.test(telVal));
                //验证输入手机规则
                if(telVal==''||(!tel.test(telVal))){
                    that.appendDom(msg,btnmsg);//插入HTML弹窗结构
                    that.b=false;
                }
                else{
                    that.b=true;
                    return;
                }
            })
        },
        blurValidatetel:function(eve){
            var that=this;
            $("#tel_number").on(eve,function(){
                var thisVal=$(this).val(),
                    tel=new RegExp("^[1][3-8]+\\d{9}");
                if(thisVal==''||(!tel.test(thisVal))){
                    that.b=false;
                    return;
                }
                else {
                    that.b=true;
                }
            })
        },
        //dom结构
        appendDom:function(msg,btnmsg){
            var dom='<div class="bomb" id="bomb">'+
                '<div class="bomb_box">'+
                '<p class="center font14">'+msg+'</p>'+
                '<div class="btn center">'+
                '<div>'+
                '<a href="javascript:void(0)" class="remove inline font14" id="go">'+btnmsg+'</a>'+
                '</div>'+
                '</div>'+
                '<div class="close" id="close"></div>'+
                '</div>'+
                '</div>';
            $('#fullPage').append(dom);
        },
        //关闭弹出
        close:function(id){
            $(id).live('touchstart',function(e){
                e.preventDefault();
                $('#bomb').remove();
            });
        },
        //倒计时
        time:function(id,msg,btnmsg){
            var that=this,
                times=function(){
                    if(that.b==false){
                        return;
                    }
                    tel = $("#tel_number").val();
                    $.post($("#get_code_link").val(), {
                        phone_num: tel
                    }), function (data, status) {
                        return;
                    };

                    var yzVal = $('#yanz').val(),
                        time = 60,
                        a = setInterval(function () {
                            time = time - 1;
                            $(id).text(time);
                            if (time == 0) {
                                clearInterval(a);
                                $(id).text('重新发送');
                            }
                        }, 1000);
                };
            $(id).on('touchstart', function () {
                //判断倒计时是否开始了
                var text = $(this).text();
                if (text == '重新发送' || text == '去获取') {
                    times();
                }
                else {
                    return;
                }
            })
        },
        //验证码验证
        active: function () {
            var that = this;
            $('#bind').on('touchstart', function () {
                var b = that.b,
                    yanzVal = $('#yanz').val();
                if (b == false && yanzVal == '') {
                    that.appendDom(that.msg.no_tel, that.btnname.just_white);
                }
                else {
                    if (yanzVal == '') {
                        that.appendDom(that.msg.no_yanz, that.btnname.just_white);
                    }
                    else {
                        var yanzVal = $("#yanz").val();
                        var tel = $("#tel_number").val();
                        var alink = $("#url").val();

                        $.post($("#verify_code_link").val(), {
                            phone_num: tel,
                            verifyCode: yanzVal
                        }, function (data, status) {
                            //alert(data+status);

                            if (data) {
                                location.href = alink + "&phone_num=" + tel + "&verifyCode=" + yanzVal;
                            } else {
                                that.appendDom(that.msg.yanz_err, that.btnname.rewrite);
                            }
                        });

                        return;
                    }
                }


           })
        }
    };
    tel.init();
});