/**
 * Created by Administrator on 2016/7/26.
 */
(function () {
    var $errorName = $('#errorName');
    var $errorTel = $('#errorTel');
    var $userName = $('#userName');
    var $userTel = $('#userTel');
    var $submit = $('#submit');

    var myBalance = $('#myBalance').html();
    var total = $('#total').html();
    var progressBar = parseFloat($('#progressBar').css('width'));

    $('#progressBar i').animate({
        width:myBalance/total*progressBar+'px',
        opacity:1
    },1000);
    /**
     * 验证用户姓名
     * @param obj
     * @returns {boolean}
     */
    function valiUserName(userName) {
        $errorName.html(userName ? '' : '用户名不能为空，请重新输入！');
        return !userName;
    }

    /**
     * 验证电话号码
     * @param obj
     * @returns {boolean}
     */
    function valiMobile(mobile) {
        var msg = /^[\d]{11}$/.test(mobile) ? '' : '手机号有误，请重新输入！';
        $errorTel.html(msg);
        return !mobile;
    }

    $userName.on('keyup', function () {
        valiUserName($.trim($userName.val()+''));
    });
    $userTel.on('keyup', function () {
        valiMobile($userTel.val());
    });

    /**
     * 提交验证
     */
    $submit.on('click',function (e) {
        if( valiUserName($.trim($userName.val()+'')) ){
            $userName.focus();
            e.preventDefault();
            $errorName.html('用户名输入有误,请再次输入!');
            return;
        }else{
            if( valiMobile( $.trim( $userTel.val()+'' ) ) ){
                $userTel.focus();
                e.preventDefault();
                $errorTel.html('密码有误,请再次输入!');
                return;
            }else{
                $("#form").submit;
                console.log('ok');
            }
        }
    });


})();
