/**
 * 引入toaster方法
 */
document.write("<script language=javascript src='static/js/common/toastr.js'></script>");

/**
 * 引入base方法
 */
document.write("<script language=javascript src='static/js/common/base.js'></script>");

// 登录点击
function login() {
    var userName = $('#p_username').val();
    var passWord = $('#p_pwd').val();
    if (null == userName || null == passWord) {
        toastr.error('账号或密码不能为空');
    }
    var user = {userName: userName, passWord: passWord};
    $.ajax('login', {
        data: user,
        method: 'POST',
        async: false,
        success: function(data) {
            if (data.success) {
                window.loginInfo = data;
                $('.x-nologin').hide();
                $('.x-login').show();

                $('.x-login').find('.title').text(data.content.name);
            }
            window.location.href = data.url;
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}

/**
 * 键盘确认按钮
 */
document.onkeydown = function(event) {
    var e = event || window.event;
    if (e && e.keyCode == 13) { // enter 键
        login();
    }
};


/**
 * 注册
 */
function register() {
    checkInfo();
    var user = {
        fullName: $('#u_name').val(),
        password: $('#u_pwd').val(),
        tel: $('#u_tel').val(),
        email: $('#u_email').val()
    };
    $.ajax('register', {
        data: user,
        method: 'POST',
        async: false,
        success: function() {
            window.location.href = "/signIn";
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}

/**
 * 检验信息
 */
function checkInfo() {
    if (isEmpty($('#u_email').val())) {
        alert("邮箱不能为空");
        return;
    }
    if ($('#u_email').val().indexOf('@usoftchina.com') > 0) {
        alert("只填邮箱前缀");
        return;
    }
    if (!isEmpty($('#u_email').val())) {
        $.ajax('valid/email', {
            data: $('#u_email').val(),
            method: 'POST',
            async: false,
            success: function() {
            },
            error: function (error) {
                alert(error);
                return;
            }
        });
    }
    if (!isEmpty($('#u_tel').val())) {
        $.ajax('valid/tel', {
            data: $('#u_tel').val(),
            method: 'POST',
            async: false,
            success: function() {
            },
            error: function (error) {
                alert(error);
                return;
            }
        });
    }
    if (isEmpty($('#u_pwd').val())) {
        alert("密码不能为空");
        return;
    }
    if (isEmpty($('#u_confirmPwd').val())) {
        alert("请确认密码");
        return;
    }
    if ($('#u_pwd').val() != $('#u_confirmPwd').val()) {
        alert("两次密码不一致");
        return;
    }
    validate();
}

/**
 * 验证码
 */
var code;
function createCode() {
    code = "";
    var codeLength = 4;//验证码的长度
    var checkCode = document.getElementById("code");
    var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
        'S','T','U','V','W','X','Y','Z');//随机数
    for (var i = 0; i < codeLength; i++) {//循环操作
        var index = Math.floor(Math.random() * 36);//取得随机数的索引（0~35）
        code += random[index];//根据索引取得随机数加到code上
    }
    checkCode.value = code;//把code值赋给验证码
}

/**
 * 校验验证码
 */
function validate() {
    var inputCode = document.getElementById("captcha").value.toUpperCase(); //取得输入的验证码并转化为大写
    if (inputCode.length <= 0) { //若输入的验证码长度为0
        alert("请输入验证码！"); //则弹出请输入验证码
       return;
    } else if (inputCode != code ) { //若输入的验证码与产生的验证码不一致时
        alert("验证码输入错误"); //则弹出验证码输入错误
        createCode();//刷新验证码
        document.getElementById("captcha").value = "";//清空文本框
        return;
    }
}

/**
 * 重置密码
 */
function resetPwd() {
    var email = $('#p_email').val();
    $.ajax('resetPwd', {
        data: email,
        method: 'POST',
        async: false,
        success: function() {
            var email = $('#p_email').val() + "@usoftchina.com";
            alert('邮件已发送到' + email + ',请注已查收');
            $('#forgetPwd').modal('hide');
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}
$(function() {
    'use strict';

    // 监听页面滚动
    $(window).scroll(function() {
        if($(window).scrollTop() >= 400) {
            $('#nav').addClass('on');
        } else {
            $('#nav').removeClass('on');
        }
    });

    /**
     * 登录
     */
    $('#link-login').click(login);

    /**
     * 注册
     */
    $('#u_register').click(register);

    /**
     * 生成验证码
     */
    createCode();

    $('#p_restPwd').click(resetPwd);
});