/**
 * 初始化界面行为
 * 
 */
function loginInit() {
    $('.log_form input').keypress(function (e) {
        if (e.which == 13) {
            loginAction();
        }
    });

    //登录事件与重置事件
    $('.login_reset').click(function () {
        loginReset();
    });

    $('.login_btn').click(function () {
        loginAction();
    });

    $('.sys_init').click(function () {
        initSystem();
    });

}

/**
 * 重置登录信息
 * 
 */
function loginReset() {
    $('#userName').val('');
    $('#pwd').val('');
    $('#request-answer').html('');
    $('#request-answer').removeClass('has-errorinfo');
}

/**
 * 处理操作
 * 
 * @returns 
 */
function loginAction() {
    //alert('enter');
    //获取登录参数
    var loginname = $('#userName').val();
    if (!loginname || loginname == '') {
        $('#request-answer').addClass('has-errorinfo');
        $('#request-answer').html('登录名为空');
        return;
    }
    var pwd = $('#pwd').val();
    if (!pwd || pwd == '') {
        $('#request-answer').addClass('has-errorinfo');
        $('#request-answer').html('密码为空');
        return;
    }
    var params = {};
    params.loginname = loginname;
    params.pwd = pwd;
    //清空以前提示信息
    $('#request-answer').removeClass('has-errorinfo');
    $('#request-answer').html('');
    $.ajax({
        url: loginUrl,
        data: params,
        type: 'POST',
        dataType: 'JSON',
        success: function (result) {
            if (result.success) {
                var url = indexUrl;
                var winUrl = window.location.href;
                var pos = winUrl.indexOf('RU=');
                if (pos && pos > 0) {
                    url = decodeURIComponent(winUrl.substring(pos + 3));
                }

                window.location = url;
            } else {
                $('#request-answer').addClass('has-errorinfo');
                $('#request-answer').html(result.errorMessage);
            }
        },
        error: function () {
            $('#request-answer').addClass('has-errorinfo');
            $('#request-answer').html('登录异常');
        }
    });

}


function initSystem() {
    $.ajax({
        url: "../ibizutil/initsystemenv.do",
        type: "POST",
        dataType: "JSON",
        success: function (result) {
            if (result.success) {
                if (result.info == '') {
                    result.info = '系统初始化成功！'
                }
                $('.sysinit_successinfo').html(result.info);
                $('.popo').modal({
                    backdrop: false,
                    keyboard: false,
                    show: true
                });
            } else {
                if (result.info == '') {
                    result.info = '初始化系统失败！'
                }
                $('.sysinit_errorinfo').html(result.info);
                $('.popo1').modal({
                    backdrop: false,
                    keyboard: false,
                    show: true
                });
            }
        },
        error: function (result) {
            $('.sysinit_errorinfo').html('初始化系统失败！');
            $('.popo1').modal({
                backdrop: false,
                keyboard: false,
                show: true
            });
        }
    });
}