$(document).ready(function () {

    $("#top_login,#reg_login,#top_login2,#lxwm_login").click(function () {
        $("#top_registerPanel").hide();
        $("#top_loginPanel").show();
        $("#login_account").focus();
        $("#imgLoginValidateCode").click();
    });

    $("#login_close").click(function () {
        $("#top_loginPanel").hide();
    });

    //验证码图像点击更换
    $("#imgLoginValidateCode").click(function () {
        $(this).attr("src", "/request/userrequest.ashx?action=validatecode&code=" + Math.floor(Math.random() * 100000).toString());
    });

    //回车自动登录
    $("#login_account,#login_password,#login_validateCode").keypress(function (e) {
        if (e.keyCode == 13) {
            $("#login_submit").trigger("click");
        }
    });

    //登录
    $("#login_submit").click(function () {
        if ($(this).text().indexOf("登录中") != -1) { return; }
        if ($("#login_account").val() == "") {
            $("#login_message").show().text("用户名不能为空");
            return;
        } else if ($("#login_password").val() == "") {
            $("#login_message").show().text("密码不能为空");
            return;
        } else if ($("#login_validateCode").val() == "") {
            $("#login_message").show().text("验证码不能为空");
            return;
        }
        $("#login_message").hide();
        $(this).text("登录中");
        var wait = 1;
        var timerid = window.setInterval(function () {
            var max = wait % 4;
            var apostrophe = [];
            for (var i = 0; i < max; i++) {
                apostrophe.push('.');
            }
            $("#login_submit").text("登录中" + apostrophe.join(""));
        }, 1000);
        $.ajax({
            url: "/request/userrequest.ashx",
            type: "post",
            data: { action: "login", username: $("#login_account").val(), password: $("#login_password").val(), code: $("#login_validateCode").val(), auto: $("#login_autoLogin").is(":checked") },
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.Result == 1) {
                    $("#login_submit").text("登录成功");
                    window.location = "/rommaker/earningscenter.aspx";
                } else {
                    switch (data.Result) {
                        case -10:
                            $("#login_validateCode").val("").focus();
                            break;
                        case -11:
                            $("#login_account,#login_password,#login_validateCode").val("");
                            $("#login_account").focus();
                            break;
                        case -12:
                            $("#login_password,#login_validateCode").val("");
                            $("#login_password").focus();
                            break;
                        case -14:
                            window.location.href = "acountdisable.aspx";
                            break;
                    }
                    $("#login_message").show().text(data.Message);
                    $("#imgLoginValidateCode").trigger("click");
                    $("#login_submit").text("登录");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            },
            complete: function (XMLHttpRequest, textStatus) {
                window.clearInterval(timerid);
            }
        });
    });

    //用户注册
    var closeSendSms = function (surplus) {
        if (surplus > 0) {
            var timerArgs = { wait: surplus, timerId: 0 };
            timerArgs.timerId = window.setInterval(function (args) {
                if (args.wait > 0) {
                    $("#reg_sendSms").text("重新发送(" + args.wait + "秒)");
                } else {
                    window.clearInterval(timerArgs.timerId);
                    $("#reg_sendSms").text("点击获取验证码").removeClass("fsz").data("disabled", false);
                }
                args.wait--;
            }, 1000, timerArgs);
        }
    };

    $("#top_register,#login_register").click(function () {
        $("#top_loginPanel").hide();
        $("#top_registerPanel").show();
        $("#reg_account").focus();
    });

    $("#reg_close").click(function () {
        $("#reg_message,#top_registerPanel").hide();
    });

    //$("#reg_sendSms").click(function () {
    //    if (!$(this).data("disabled")) {
    //        var telephone = $("#reg_telephone").val();
    //        if (telephone == "") {
    //            $("#reg_message").text("请输入手机号码").show();
    //            return;
    //        } else if (!/^1[0-9]{10}$/.test(telephone)) {
    //            $("#reg_message").text("手机号码格式不正确").show();
    //            return;
    //        } else {
    //            $("#reg_message").text("").hide();
    //        }
    //        $("#reg_sendSms").data("disabled", true).addClass("fsz");
    //        $.ajax({
    //            url: "/Request/UserRequest.ashx",
    //            type: "POST",
    //            data: { action: "SendSmsValidCode", phone: telephone },
    //            dataType: "json",
    //            success: function (jsonData) {
    //                if (jsonData.Result == 1) {
    //                    closeSendSms(120);
    //                } else if (jsonData.Result == 2) {
    //                    closeSendSms(jsonData.Message);
    //                    $("#reg_message").text("发送频繁,请等待").show();
    //                } else {
    //                    $("#reg_message").text(jsonData.Message).show();
    //                    $("#reg_sendSms").data("disabled", false).removeClass("fsz");
    //                }
    //            }
    //        });
    //    }
    //});

    $("#reg_submit").click(function () {
        if (!$("#reg_agree").is(":checked")) {
            $("#reg_message").text("你没有同意用户协议").show();
        }
        var userType = $(":radio[name=reg_userType]:checked").val();
        var registerData = {};
        var account = $("#reg_account").val();
        if (account == "") {
            $("#reg_message").text("请输入账号").show();
            return;
        }
        var password = $("#reg_password").val();
        if (password == "") {
            $("#reg_message").text("请输入密码").show();
            return;
        } else if (password.length < 8) {
            $("#reg_message").text("密码长度应大于8位").show();
            return;
        }
        var telephone = $("#reg_telephone").val();
        if (telephone == "") {
            $("#reg_message").text("请输入手机号码").show();
            return;
        } else if (!/^1[0-9]{10}$/.test(telephone)) {
            $("#reg_message").text("手机号码格式不正确").show();
            return;
        }

        var validateCode = $("#reg_validateCode").val();
        if (validateCode == "") {
            $("#reg_message").text("请输入验证码").show();
            return;
        }

        $("#reg_message").text("").hide();

        $.ajax({
            url: "/Request/UserRequest.ashx",
            type: "POST",
            data: { action: "Register", account: account, password: password, telephone: telephone, validateCode: validateCode },
            dataType: "json",
            success: function (jsonData) {
                if (jsonData.Result == 1) {
                    if (userType == "1") {
                        location.href = "/rommaker/fillfinanceinfo.aspx";
                    } else {
                        location.href = "/rommaker/applydeveloper.aspx";
                    }
                } else {
                    $("#reg_message").text(jsonData.Message).show();
                }
            }
        });
    });
});