﻿// JScript 文件
(function(){
if (typeof window.$5 == 'undefined') window.$5 = {};
var user = window.$5.user = {};
var showInfo=function(info){
    $('#loginInfo').html(info || '');
};
user.redirectAfterLogout = false;
user.redirect2 = "/index.aspx";
var fpLogin = floatPanel({
    target:$('#loginDiv'),
    modal:true,
    enableEsc:true,
    onHide:function(){
        $('#txtUsername').blur();
        showInfo('');
    },
    width:260
});
user.init=function(){
    $('#loginLink').click(function(){
        fpLogin.show();
        $('#txtUsername').focus();
    });
    $('#logoutLink').click(function(){
        user.logout();
    });
    $5.smartInput($('#txtUsername'));
    $5.smartInput($('#txtPassword'));
};
user.login=function(form){
    var username,password;
    if (!(username = $.trim(form.username.value))) {
        showInfo('帐号不能为空');
        form.username.focus();
        return false;
    }
    if (!(password = $.trim(form.password.value))) {
        showInfo('密码不能为空');
        form.password.focus();
        return false;
    }
    showInfo('登陆中...');
    $5.ajax('User.Login', {username:username,password:password,isPersist:form.isPersist.checked}, function(ret){
        switch(ret.Status){
            case -1:
                showInfo('验证码错误');
                break;
            case 0:
                showInfo('登陆成功');
                $('#loginStatusDiv').html(ret.Data);
                user.init();
                setTimeout(function(){
                    $('#txtUsername').val('');
                    $('#txtPassword').val('');
                    showInfo();
                    fpLogin.hide();
                }, 800);
                break;
            case 1:
                showInfo('用户名或密码错误');
                break;
        }
    });
    return false;
};
user.logout=function(){
    $5.ajax('User.Logout', {}, function(ret){
        if(ret.Status == 0) {
            $('#loginStatusDiv').html(ret.Data);
            user.init();
            alert("退出成功");
            if (user.redirectAfterLogout)window.location = user.redirect2;
        } else alert(ret.Error);
    });
};

})()
