﻿
///////////////////////////////登录部分-Start//////////////////////////////////////////////

///更换图片
function Refresh() {
    var r = parseInt(Math.random() * 100);
    $("#mgcode").attr("src", "../../AjaxValid/DrawingCode/" + r);
}

function toUrl() {
    window.location.href = "../../Home/Index/";
}

function SubmitLogin(url) {
    var account = $("#account").val();
    if (trim(account) == "") {
        alert("用户名不可以为空！");
        $("#account").val("");
        $("#account").focus();
        return;
    }
    var password = $("#pwd").val();
    if (trim(password) == "") {
        alert("密码不可以为空！");
        $("#pwd").val("");
        $("#pwd").focus();
        return;
    }

    var imgcode = $("#imgcode").val();
    if (trim(imgcode) == "") {
        alert("验证码不可以为空！");
        $("#imgcode").val("");
        $("#imgcode").focus();
        return;
    }

    var rem = false;
    if ($("#checkbox").attr("checked")) {
        rem = true;
    }

    $.ajax({
        type: "POST",
        url: url,
        data: { account: account,
            password: password,
            imgcode: imgcode,
            rem: rem.toString()
        },
        success: function(msg) {
            AjaxLoginEnd(msg);
        }
    });
}

function AjaxLoginEnd(msg) {
    if (msg.toString().toLowerCase() == "ok") {
        window.location.href = "passport/usercenter";
    }
    else {
        switch (msg.toString().toLowerCase()) {
            case "imgcode": alert("验证码输入错误！"); $("#imgcode").val(""); $("#imgcode").focus(); break;
            case "account": alert("用户名或密码错误！"); $("#account").val(""); $("#pwd").val(""); $("#account").focus(); break;
            case "用户名不存在": alert("用户名或密码错误！"); $("#account").val(""); $("#pwd").val(""); $("#account").focus(); break;
            case "密码不正确": alert("用户名或密码错误！"); $("#account").val(""); $("#pwd").val(""); $("#account").focus(); break;
            case "pwd": alert("用户名或密码错误！"); $("#account").val(""); $("#pwd").val(""); $("#account").focus(); break;
            default: alert(msg); break;
        }
    }
}

///////////////////////////////登录部分-End//////////////////////////////////////////////


///////////////////////////////修改密码-Start//////////////////////////////////////////////

function SubmitPwdChange(url) {
    var account = $("#account").val();
    var opwd = $("#oldpwd").val();
    var npwd = $("#newpwd").val();
    var rnpwd = $("#renewpwd").val();
    if (opwd == "") {
        alert("旧密码不可为空");
        $("#oldpwd").focus();
        return;
    }
    if (HasSpaceInString(opwd)) {
        alert("旧密码不可包含空格或%");
        $("#oldpwd").focus();
        return;
    }
    if (npwd == "") {
        alert("新密码不可为空");
        $("#newpwd").focus();
        return;
    }
    if (HasSpaceInString(npwd)) {
        alert("新密码不可包含空格或%");
        $("#newpwd").focus();
        return;
    }
    if (npwd.toString().length < 6 || npwd.toString().length > 16) {
        alert("新密码长度应为6-16位");
        $("#newpwd").focus();
        return;
    }
    if (rnpwd == npwd) {
    }
    else {
        alert("两次输入密码不一致");
        $("#renewpwd").focus();
        return;
    }
    $.ajax({
        type: "POST",
        url: url,
        data: { oldpwd: opwd, newpwd: npwd, renewpwd: rnpwd, account: account },
        success: function(msg) {
            SubmitPwdChangeAjaxSubEnd(msg);
        }
    });
}

function SubmitPwdChangeAjaxSubEnd(msg) {
    if (msg.toString().toLowerCase() == "ok") {
        alert("修改密码成功! 请重新登陆!");
        window.location.href = "/";
    }
    else {
        var mgs = msg.toString().split('\r\n');
        switch (mgs[0]) {
            case "oldpassword":
                alert(mgs[1]);
                $("#oldpwd").focus();
                break;
            case "newpassword":
                alert(mgs[1]);
                $("#newpwd").focus();
                break;
            case "renewpassword":
                alert(mgs[1]);
                $("#renewpwd").focus();
                break;
            case "fail":
                alert(mgs[1]);
                break;
        }
    }
}

function ResetPasswordinputs() {
    $("#oldpwd").val("");
    $("#newpwd").val("");
    $("#renewpwd").val("");
}

///////////////////////////////修改密码-End//////////////////////////////////////////////

///////////////////////////////找回密码-Start//////////////////////////////////////////////

function FindBackPwd(url) {
    var account = $("#account").val();
    if (account == "") {
        alert("帐号不可为空");
        $("#account").focus();
        return;
    }
    if (confirm("您的帐号是：" + account + "，您是否确认找回密码？")) {
        $.ajax({
            type: "POST",
            url: url,
            data: { account: account },
            success: function(msg) {
                AjaxFindBackPwdEnd(msg);
            }
        });
    }
}

function AjaxFindBackPwdEnd(msg) {
    if (msg.toString().indexOf("系统已发送邮件至", 0) == -1) {
        alert(msg);
    }
    else {
        alert(msg);
        window.location.href = "/";
    }
}


var pwdC = false;
var repwdC = false;
var imgIconRight = "../../Images/icon_right.jpg";
var imgIconWrong = "../../Images/icon_wrong.jpg";
var HiddenString = "visibility: hidden;";
var ViewString = "visibility: visible;";
var ColorRight = "color: Green;";
var ColorWrong = "color: Red;";

///密码验证
function pwdCheck() {
    var pwd = $("#Pwd").val();
    var bkpwd = $("#Pwdhid").val();
    if (pwd == "") {
        pwdC = false;
        $("#Pwdvalid").text("密码不可为空");
        $("#Pwdvalid").attr("style", ColorWrong);
        $("#Pwdhid").val(pwd);
        $("#pwd_valid_div").attr("style", ViewString);
        $("#imgpwd").attr("src", imgIconWrong);
        return;
    }
    if (pwd == bkpwd) {
        return;
    }
    if (HasSpaceInString(pwd)) {
        pwdC = false;
        $("#Pwdvalid").text("密码不可包含空格或%");
        $("#Pwdvalid").attr("style", ColorWrong);
        $("#Pwdhid").val(pwd);
        $("#pwd_valid_div").attr("style", ViewString);
        $("#imgpwd").attr("src", imgIconWrong);
        return;
    }
    else {
        if (5 < pwd.length && pwd.length < 17) {
            pwdC = true;
            $("#Pwdvalid").text("OK！");
            $("#Pwdvalid").attr("style", ColorRight);
            $("#Pwdhid").val(pwd);
            $("#pwd_valid_div").attr("style", ViewString);
            $("#imgpwd").attr("src", imgIconRight);
            return;
        }
        else {
            pwdC = false;
            $("#Pwdvalid").text("密码长度必须在6-16位之间");
            $("#Pwdvalid").attr("style", ColorWrong);
            $("#Pwdhid").val(pwd);
            $("#pwd_valid_div").attr("style", ViewString);
            $("#imgpwd").attr("src", imgIconWrong);
            return;
        }
    }
}

///重复密码验证
function repwdCheck() {
    var pwd = $("#Pwd").val();
    var RePwd = $("#RePwd").val();
    if (RePwd == "") {
        $("#RePwd_valid_div").attr("style", ViewString);
        $("#imgRePwd").attr("src", imgIconWrong);
        $("#RePwdvalid").text("重复输入密码不可为空！");
        $("#RePwdvalid").attr("style", ColorWrong)
        repwdC = false;
        return;
    }
    if (pwd != RePwd) {
        $("#RePwd_valid_div").attr("style", ViewString);
        $("#imgRePwd").attr("src", imgIconWrong);
        $("#RePwdvalid").text("2次输入密码不同！");
        $("#RePwdvalid").attr("style", ColorWrong)
        repwdC = false;
    }
    else {
        $("#RePwd_valid_div").attr("style", ViewString);
        $("#imgRePwd").attr("src", imgIconRight);
        $("#RePwdvalid").attr("style", ColorRight)
        $("#RePwdvalid").text("OK！");
        repwdC = true;
    }
}

function SubmitResetPassword(url) {
    pwdCheck();
    repwdCheck();
    if (!pwdC) {
        $("#Pwd").focus();
        return false;
    }
    if (!repwdC) {
        $("#RePwd").focus();
        return false;
    }
    var account = $("#account").val();
    var code = $("#code").val();
    var pwd = $("#Pwd").val();
    $.ajax({
        type: "POST",
        url: url,
        data: { pwd: pwd,
            code: code,
            account: account
        },
        success: function(msg) {
            AjaxResetPasswordEnd(msg);
        }
    });
}

function AjaxResetPasswordEnd(msg) {
    if (msg.toString() == "密码已成功重设！") {
        alert("密码已成功重设！请登录！");
        window.location.href = "/";
    }
    else {
        alert(msg);
    }
}
///////////////////////////////找回密码-End//////////////////////////////////////////////