﻿// ==UserScript==
// @name                邻所登录
// @description         
// @created date        2009/11/12   14:36
// @created author      Martin
// @version             
// @version owner       Copyright ? 2009-2010 keerr.com All rights reserved.
// ==/UserScript==

/// <summary>
/// 登录主方法
/// </summary>
function login() {
    $("#btlogin").attr("disabled", "disabled");
    $("body").unbind("keydown");
    if (_LoginErrNumber > 9 && ($("#txtC").val() == "" || $("#txtC").val().toLowerCase() != _reg_checkCode)) {
        ShowlogErrMsg('验证码错误。', 5000);
        if ($("#txtC").val() != "") {
            changeCode_reg($("#codeimg"));
        }
        $("#txtC").select();
        $("#licode").css("display", "");
        return;
    }
    if ($("#txtU").val() === "") {
        //ShowlogErrMsg('请输入账号。', 1000);
        //$('#errmsg').css({top: "130px", left: "280px"}).jGrowl('请输入账号');
        ShowMsgbox({ errmsg: "请输入账号", cssOptions: { top: "130px", left: "280px", opacity: "0.8" }, top: 30, left: 50, delay: 5000, isID: true }, $("#txtU"));
        $("#txtU").select();
        resetBt();
        return;
    }
    if ($("#txtP").val() === "") {
        //ShowlogErrMsg('请输入密码。', 1000);
        ShowMsgbox({ errmsg: "请输入密码。", cssOptions: { top: "190px", left: "280px", opacity: "0.8" }, top: 30, left: 50, delay: 5000, isID: true }, $("#txtP"));
        $("#txtP").select();
        resetBt();
        return;
    }


    //$("#byloujs").attr('src', 'http://www.bylou.com/members/login.aspx?txtU=' + $("#txtU").val() + '&txtP=' + $("#txtP").val());
    $.ajax({
        url: '/ajax/GetJson.aspx',
        type: 'POST',
        dataType: 'text',
        timeout: 5000,
        data: 'type=Login&txtU=' + $("#txtU").val() + '&txtP=' + $("#txtP").val() + '&checkCode=' + _reg_checkCode,
        cache: false,
        error: function () {
        },
        success: function (str) {
            if (str === "1") {
                var _s = "个人管理首页";
                var furl = "/admin/index.aspx";
                if (_f_url !== "http://linsuo.bylou.com/admin/index.aspx" && _f_url.indexOf("/admin/") > -1) {
                    _s = "最后访问的页面";
                    furl = _f_url;
                }
                redirectIndex(_s);  // 程序在floatbox.js
                var CookieOptions = {// cookie的统一参数集
                    path: '/',
                    domain: '',
                    expires: 365,
                    secure: false
                };
                $.cookie("userEmail", $("#txtU").val(), CookieOptions);
                setTimeout("window.location.href =  '" + furl + "';", 3000);
                //'/admin/index.aspx';//
            }
            else if (str === "2") {
                var _s2 = "企业管理首页";
                var furl2 = "/customer/index.aspx";
                if (_f_url !== "http://linsuo.bylou.com/customer/index.aspx" && _f_url.indexOf("/customer/") > -1) {
                    _s2 = "最后访问的页面";
                    furl2 = _f_url;
                }
                redirectIndex(_s2);  // 程序在floatbox.js
                var CookieOptions2 = {// cookie的统一参数集
                    path: '/',
                    domain: '',
                    expires: 365,
                    secure: false
                };
                $.cookie("userEmail", $("#txtU").val(), CookieOptions2);
                setTimeout("window.location.href =  '" + furl2 + "';", 3000);
            }
            else if (str === "-2") {
                _LoginErrNumber = parseInt(str);
                $("#txtP").val("");
                ShowlogErrMsg('你今天登录失败次数太多了。', 5000);
            }
            else {
                _LoginErrNumber = parseInt(str);
                $("#txtP").val("");
                ShowlogErrMsg('账号或密码不正确。', 5000);
                getLoginErrNumber();
                //                if (_LoginErrNumber == 8) {
                //                    $("#licode").fadeIn("slow");
                //                }
            }
        }
    });
    return false;
}
/// <summary>
/// 错误提示
/// </summary>
/// <param name="msg">提示内容</param>
/// <param name="delay">显示时间</param>
function ShowlogErrMsg(msg, delay) {
    $(".font12").text(msg);
    $("#lg_Msg").fadeIn("slow");
    setTimeout('$("#lg_Msg").fadeOut("slow")', delay);
    setTimeout('resetBt();', delay);
    if ($("#licode:visible").length === 1) {
        $("#txtC").val("").focus();
        $("#codeimg").click();
    }
    $("#txtP").focus();
}
/// <summary>
/// 回车登录
/// </summary>
function enterSubmit(e) {
    if ($("#btlogin").attr("disabled") == "disabled") {
        return false;
    }
    if (window.event)
        keyPressed = window.event.keyCode; // IE
    else
        keyPressed = e.which; // Firefox
    if (keyPressed === 13) {
        //login();
        $("#btlogin").click();
        return false;
    }

}
/// <summary>
/// 激活回车及按钮事件
/// </summary>
function resetBt() {
    $("#btlogin").attr("disabled", "");
    $("body").keydown(function(event) {
        return enterSubmit(event);
    });
}

// 文档加载完后执行入口程序
$(function() {
    // 点击登录后续操作
    $("#btlogin").isReady({
        readyFunction: function() {
            login();
        }
    });
    // enter事件
    $("body").keydown(function(event) {
        return enterSubmit(event);
    });

    $("input").keyup(function() {
        $("#btlogin").removeAttr("disabled");
        $("#lg_Msg").hide();
    });

    if ($.cookie("userEmail") != null) {
        $("#txtU").val($.cookie("userEmail"));
        $("#txtP").focus();
    }

    getLoginErrNumber();    // 在judge.js中
    //    $("#txtU").focus(function() {
    //        if (_LoginErrNumber > 1) {
    //            $(".lg_hlp li:gt(2)").hide();
    //            $("#licode").css("display", "");
    //        }
    //    });
});

//$.bgFun = {
//    entrance: function() {
//    },
//    showInformation: function() {
//        var $_infor = $("#sh_cp span");
//        $("#sh_cp").hover(
//            function() {
//                $_infor.show();
//            },
//            function() {
//                $_infor.hide();
//            }
//        );
//    }
//    
//};
