/**
 * Created by 刘丹 on 2017/12/26.
 */
(function () {
    $(document.body).show();
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
        }
    }
    _referrer_id=theRequest.id;
    _referrer_source=theRequest.referrer_source;


})();
//$("#phone").change(function(){
//    send();
//});
var sends = {
    checked:1,
    send:function(){
        var numbers = /^1\d{10}$/;
        var val = $('#phone').val().replace(/\s+/g,""); //获取输入手机号码
        if($('.div-phone').find('span').length == 0 && $('.div-ranks a').attr('class') == 'send1'){
            if(!numbers.test(val) || val.length ==0){
                $('.div-phone').append('<span class="error">手机格式错误!</span>');
                return false;
            }
        }
        if("" == $("#phone").val()) {
            alert("请输入您的手机号!");
            return;
        }

       if (!is_mobile(val)) {
           alert("手机格式错误!")
           return false;
        }
        if(!sends.checked)return;
        if(numbers.test(val)){
            $.ajax({
                url: ServerHost+"/api/sendSms/"+val,
                data: {
                    "type":'add'
                },
                type: "GET",
                dataType: "json",
                beforeSend: function () {},
                success: function (response) {
                    _token=response.data.token;

                    if(response.msg=="您已注册，请登录"){
                        alert("您已注册，请登录!");
                        clearInterval(timer);
                        $('.div-ranks a').addClass('send1').removeClass('send0').html("获取验证码");
                        location.reload();
                    }else if(response.msg=="1分钟内不能再次获取验证码"){
                        alert("1分钟内不能再次获取验证码");
                        $(".send1").attr("disabled","");
                        clearInterval(timer);
                        $('.div-ranks a').addClass('send1').removeClass('send0').html("获取验证码");
                        sends.checked = 1;
                        return true;
                    }else{
                        $('.div-phone').append('<span class="error">'+response.msg+'</span>');
                    }


                },
                error: function () {
                    $('.div-phone').append('<span class="error">验证码发送失败!</span>');
                }
            });
            var time = 60;

            function timeCountDown(){
                if(time==0){
                    $(".send1").attr("disabled","");
                    clearInterval(timer);
                    $('.div-ranks a').addClass('send1').removeClass('send0').html("获取验证码");
                    sends.checked = 1;
                    return true;
                }

                $('.div-ranks a').html("剩余"+time+"S");
                $(".error").hide();
                time--;
                sends.checked = 0;
            }
            $('.div-ranks a').addClass('send0').removeClass('send1');
            timeCountDown();
            var timer = setInterval(timeCountDown,1000);
        }
    },

    conform:function(){
         var zc=$(".zc_nor_button").attr("src");
        if("" == $("#phone").val()) {
            alert("请输入您的手机号!");
            return;
        }
        if("" == $("#ranks").val()) {
            alert("请输入您的验证码!");
            return;
        }
        if("" == $("#password").val()) {
            alert("请输入您的密码!");
            return;
        }

        if(zc=="/app/images/but_yuedu_nor.png")
        {
            alert("请勾选协议!");
            return;
        }
        $(".conform").html("提交中...");
        $.ajax({
            url: ServerHost+"/api/register",
            data: {
                "phone":$("#phone").val(),
                "pwd":$("#password").val(),
                "code":$("#ranks").val(),
                "token":_token,
                "referrer_id":_referrer_id,
                "referrer_source":_referrer_source,
                "type":"add"
            },
            type: "POST",
            dataType: "json",
            beforeSend: function () {},
            success: function (response) {
                if (response.code == 200) {
                    //成功跳转
                    $(".conform").html("提交.");
                    window.location.href="/app/download";


                } else {
                    alert(response.msg);
                    $(".conform").html("提交");
                }
            },
            error: function () {
                $(".conform").html("提交");
                $('.div-').append('<span class="error">提交失败!</span>');
            }
        });
    }
}
//  小图片切换
var a = 1;
$('.zc_nor_button').click(function(){
    if(a == 1) {
        $(this).attr("src","/app/images/but_yuedu_nor.png")
        a = 2;
    } else {
        a = 1;
        $(this).attr("src","/app/images/but_yuedu-.png")
    }

})

//获取id名字

