/**
 * Created by 刘丹 on 2017/12/26.
 */
(function () {

    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;


})();

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 (!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
                    $('.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;
                }
                $('.error').hide();
                $('.div-ranks a').html("剩余"+time+"S");
                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("" == $("#password").val()) {
            alert("请输入您的密码!");
            return;
        }
        if("" == $("#ranks").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.status == 0) {
                    //成功跳转
                    $("input").val('');
                    $(".conform").html("提交.");
                    window.location.href="/mobile4.0/index.html";


                } else {
                    alert(response.msg);
                }
            },
            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名字

