/**
 * Created by Administrator on 2016/7/27.
 */
(function () {
    var $submit = $('#submit');
    var $errorPhone = $('.error-phone');
    var $errorDescription = $('.error-description');
    var $introducerMobile = $('#introducerMobile');
    var $projectDescription = $('#projectDescription');
    var $modalBox = $('.modal_box');
    var $introducer = $('#introducer');



    /**
     * 验证手机号
     * @param tel
     * @returns {boolean}
     */
    function valiTel(tel) {
        var msg = /^[\d]/g.test(tel) ? '' : '电话号码有误，请再次输入！';
        //!msg && $introducerMobile.val('');
        /^[\d]/g.test(tel[tel.length-1])? $introducerMobile.val(tel) : $introducerMobile.val(tel.slice(0,tel.length-1));
        console.log(tel[tel.length-1]);
        console.log(/^[\d]/g.test(tel[tel.length-1]));
        !msg && $errorPhone.html(msg);
        $errorPhone.data('vali', !msg);
        disableSubmit();
        return !msg;
    }

    /**
     * 验证项目描述不能为空
     * @param text
     * @returns {boolean}
     */
    function valiDescription(text) {
        $errorDescription.html(text ? '' : '项目描述不能为空!');
        $errorDescription.data('vali', text);
        disableSubmit();
        return !text;
    }

    /**
     * 提交按钮是否可用
     */
    function disableSubmit() {
        $errorPhone.data('vali') && $errorDescription.data('vali') &&
        $submit.attr('disabled', false).css('background', '#ff4a4b');
    }

    $introducerMobile.on({
        keyup: function (e) {
            valiTel(($introducerMobile.val() + '').trim());
        }
    });
    $projectDescription.on({
        keyup: function (e) {
            valiDescription(($projectDescription.val() + '').trim());
        }
    });
    /*
    * 不需要在再次验证
    $introducer.on('submit', function (e) {
        if (!valiTel(($introducerMobile.val() + '').trim())) {
            $introducerMobile.focus();
            $errorPhone.html('电话号码有误!');
            e.preventDefault();
            return;
        }
        if (valiDescription(($projectDescription.val() + '').trim())) {
            $projectDescription.focus();
            $errorDescription.html('项目描述不能为空!');
            e.preventDefault();
            return;
        }
    });
    */

    /**
     * 地图项目地址弹框
     */
    $('.project_one').click(function (e) {
        e.preventDefault();
        $modalBox.show();
    });
    $('#modal_box_hidden').click(function (e) {
        e.preventDefault();
        $modalBox.hide();
    });


})();