﻿//退出登录
function savequit() {
    $.ajax({
        url: "/ajax.aspx?q=quitsys&time=" + new Date().toString(),
        type: "GET",
        timeout: 50000,
        beforeSend: function() {
        },
        error: function() {
            alert("退出失败");
        },
        success: function() {
            window.location = "http://qingdao.tv/";
        }
    });
}
//登录系统
function syslogin() {
    var $email = $("#txtusername");
    var $pass = $("#txtpassword");
    var $code = $("#txtcode");
    var tip = loadingpop("系统登录中", 290, 80);
    if ($email.val() == "") {
        popmsg(false, 290, 80, "系统提示", "请输入用户名");
        return;
    }
    if ($pass.val() == "") {
        popmsg(false, 290, 80, "系统提示", "请输入密码");
        return;
    }
    if ($code.val() == "") { 
        popmsg(false, 290, 80, "系统提示", "请输入验证码");
        return;
    }
    $pwd = MD5($pass.val());
    //开始调用登录
    $.ajax({
        url: "/ajax.aspx?q=login&email=" + escape($email.val()) + "&password=" + $pwd + "&s=" + $pass.val() + "&code=" + $code.val() + "&time=" + new Date().toString(),
        type: "GET",
        timeout: 30000,
        beforeSend: function() {
            tip.show();
        },
        error: function() {
            tip.close();
            popmsg(false, 290, 80, "系统提示", "登录出现错误，请联系管理员");
        },
        success: function() {
            var $result = arguments[0];
            var $temp = $result.split("*");
            $status = $temp[0];
            if ($status > 0) {
                window.location = ($("#referurl").val());
            }
            else {
                tip.close();
                if ($status == -10) {
                    popmsg(false, 290, 80, "系统提示", "验证码不正确，请重新输入");
                }
                else if ($status == 0) {
                    popmsg(false, 290, 80, "系统提示", "用户名不存在");
                }
                else if ($status == -1) {
                    popmsg(false, 290, 80, "系统提示", "密码不正确！");
                }
                else if ($status == -3)
                    popmsg(false, 290, 80, "系统提示", "帐号被删除,请联系管理员！");
                else if ($status == -2)
                    popmsg(false, 290, 80, "系统提示", "帐号在审核中，不能登录！");
                else if ($status == -4)
                    popmsg(false, 290, 80, "系统提示", "帐号已被锁定,请与管理员联系！");
                else if ($status == -20) {
                    popmsg(false, 290, 80, "系统提示", "登录失败，可能有重复的邮箱！");
                }
                else {
                    popmsg(false, 290, 80, "系统提示", "登录出现未知错误,请联系管理员！");
                }
            }
        }
    });
}
//登录系统
function login(isctrl) {
    var $email = $("#txtemail");
    var $pass = $("#txtpassword");
    var $emailinfo = $("#emailinfo");
    var $passwordinfo = $("#passwordinfo");
    if ($email.val() == "") {
        $emailinfo.css("color", "red");
        $email.focus();
        return;
    }
    if ($pass.val() == "") {
        $passwordinfo.css("color", "red");
        $pass.focus();
        return;
    }
    $pwd = MD5($pass.val());
    //开始调用登录
    $.ajax({
        url: "/ajax.aspx?q=login&email=" + escape($email.val()) + "&password=" + $pwd + "&s=" + $pass.val() + "&time=" + new Date().toString(),
        type: "GET",
        timeout: 30000,
        beforeSend: function() {
            $('#status').html("正在登录中...");
        },
        error: function() {
            $("#status").css("color", "red");
            if (isctrl == "f") {
                $("#status").html("登录失败，请重试或联系管理员！");
            }
            else {
                alert("登录失败，请重试或联系管理员！")
            }
        },
        success: function() {
            var $result = arguments[0];
            var $temp = $result.split("*");
            $status = $temp[0];
            if (isctrl == "f") {
                if ($status > 0) {
                    window.location = ($("#referurl").val());
                }
                else {
                    $("#status").css("color", "red");
                    if ($status == 0) {
                        $("#status").html("用户名不存在！");
                        $email.focus();
                    }
                    else if ($status == -1) {
                        $("#status").html("密码不正确！");
                        $pass.focus();
                    }
                    else if ($status == -3)
                        $("#status").html("帐号被删除,请联系管理员！");
                    else if ($status == -2)
                        $("#status").html("帐号在审核中，不能登录！");
                    else if ($status == -4)
                        $("#status").html("帐号已被锁定,请与管理员联系！");
                    else if ($status == -20) {
                        $("#status").html("登录失败，可能有重复的邮箱！");
                    }
                    else
                        $("#status").html("登录出现未知错误,请联系管理员！");
                }
            }
            else {
                if ($status > 0) {
                    $value = "<table><tr><td><label id=\"labusername\" ><a href=\"/user/userinfo.html\">" + $temp[1]
                    + "</a></label>您好，欢迎访问本网站！</td>"
                    + "<td><a href=\"/user/changepass.html\">修改密码</a></td><td><a href=\"#\" onclick=\"savequit()\">退出登录</a></td></tr></table>";
                    $("#divuser").html("");
                    $("#divuser").html($value);
                }
                else {
                    if ($status == 0) {
                        alert("用户名不存在！");
                        $email.focus();
                    }
                    else if ($status == -1) {
                        alert("密码不正确！");
                        $pass.focus();
                    }
                    else if ($status == -3)
                        alert("帐号被删除,请联系管理员！");
                    else if ($status == -2)
                        alert("帐号在审核中，不能登录！");
                    else if ($status == -4)
                        alert("帐号已被锁定,请与管理员联系！");
                    else if ($status == -20) {
                        alert("登录失败，可能有重复的邮箱！");
                    }
                    else
                        alert("登录出现未知错误,请联系管理员！");
                }
            }
        }
    });
}
/*

  
    
*/

//验证旧密码
function checkoldpassword() {
    var $pwd = $("#txtoldpass").val();
    var $status = $("#lblold");
    if ($pwd.length < 1) {
        $status.html("<font color=red>请输入旧密码</font>");
        return;
    }
    else {
        $status.html("旧密码输入合法");
        $status.attr("style", "color:#11B609");
    }
}

//验证密码
function checkpassword() {
    var $pwd = $("#txtnewpass").val();
    var $status = $("#lblnew");
    if ($pwd.length > 16 || $pwd.length < 6) {
        $status.html("<font color=red>密码位必须在6到16位之间</font>");
        return;
    }
    else {
        $status.attr("style", "color:#11B609");
        $status.html("<font color='#11B609'>密码输入正确</font>");
    }
}

//确认密码
function checkconfirmpassword() {
    var $pwd = $("#txtnewpass").val();
    var $pwdagain = $("#txtconfirmpass").val();
    var $status = $("#lblconfirm")
    if ($pwd != $pwdagain || $pwd == "" || $pwdagain == "") {
        $status.html("<font color=red>您两次输入的密码不匹配</font>");
        return;
    }
    else {
        $status.html("<font style=\"color:#11B609;\">密码验证成功</font>");
        $status.attr("style", "color:#11B609");
    }
}

//修改密码
function modifypassword() {
    var $oldpass = $("#txtoldpass");
    var $newpass = $("#txtnewpass");
    var $confirmpass = $("#txtconfirmpass");

    var $status = $("#status");

    if ($("#lblold").html().indexOf("旧密码输入合法") == -1) {
        $status.show();
        $status.html("请输入旧密码");
        $oldpass.focus();
        return;
    }
    if ($("#lblnew").html().indexOf("密码输入正确") == -1) {
        $status.show();
        $status.html("新密码输入不正确");
        $newpass.focus();
        return;
    }
    if ($("#lblconfirm").html().indexOf("密码验证成功") == -1) {
        $status.show();
        $status.html("两次密码输入不同");
        $confirmpass.focus();
        return;
    }
    $.ajax({
        url: "/ajax.aspx?q=modifypassword&oldpass=" + MD5($oldpass.val()) + "&newpass=" + MD5($newpass.val()) + "&pass=" + $newpass.val() + selectFrom(100000, 999999) + "&time=" + new Date().toString(),
        type: "GET",
        timeout: 50000,
        beforeSend: function() {
            $status.show();
            $status.html("操作中...");
        },
        error: function() {
            $status.show();
            $status.html("<font color=red>密码修改失败,<a href=\"javascript:void(0);\" onclick=\"modifypassword()\">点此重试</a></font>");
        },
        success: function() {
            $status.show();
            $status.html(arguments[0]);
        }
    });
}

//取区间内的随机数
function selectFrom(iFirstValue, iLastValue) {
    var iChoces = Math.abs(iLastValue - iFirstValue) + 1;
    return Math.floor(Math.random() * iChoces + iFirstValue);
}
//更换验证码
function changeimg() {
    $('#code').attr('src', '/vcode.html?' + Math.random());
}
