﻿function login() {
    var username = $.trim($("#username").val());
    var password = $.trim($("#password").val());
    if (!checkEmail(username)) {
        alert("电子邮箱格式不对！");
    }
    else if(password=="")
    {
        alert("请输入密码！");
    }
    else {
        $("#loginText").html("<img src='/images/loading1.gif' /> 正在验证用户名密码...")
        $.ajax({
            url: "/ajax/login.ashx?username=" + username + "&password=" + password,
            type: "GET",
            cache: false,
            success: function(val) {
                if (val == "0") {
                   
                    $("#loginText").html("用户名：<input class='input1' id='username' type='text' name='username' />&nbsp;密码：<input class='input2' id='password' type='password' name='passowrd' />&nbsp;<input onclick='login()' type='button' value='登录' class='btn1' />&nbsp;<input type='button' onclick='document.location='/register.aspx'' class='btn1' value='注册' />");
                     alert("用户名或密码不对！");
                }
                else {
                    $("#loginText").html(val);
                }
               
            }

        })
    }
}

function login1() {
    var username = $.trim($("#username1").val());
    var password = $.trim($("#password1").val());
    if (!checkEmail(username)) {
        alert("电子邮箱格式不对！");
    }
    else if (password == "") {
        alert("请输入密码！");
    }
    else {
        $("#loginText1").html("<img src='/images/loading1.gif' /> 正在验证用户名密码...")
        $.ajax({
            url: "/ajax/login.ashx?type=1&username=" + username + "&password=" + password,
            type: "GET",
            cache: false,
            success: function(val) {
                if (val == "0") {

                    $("#loginText1").html("用户名：<input type='text' name='username1' id='username1' class='input1' /> 密码：<input class='input1' type='password' id='password1' name='password1' /> <input type='button' onclick='login1()' value='登录' class='btn4' /> <a href='/register.aspx'>新会员注册</a><input type='hidden'  id='islogin' value='0' />");
                    alert("用户名或密码不对！");
                }
                else {
                    $("#loginText1").html(val);
                }

            }

        })
    }
}

function logout() {
    $.ajax({
     url: "/ajax/logout.ashx?n=" + Math.random(),
     type:"GET",
     cache:false,
     success:function(val)
     {
         if (val == "1") {
             $("#loginText").html("用户名：<input class='input1' id='username' type='text' name='username' />&nbsp;密码：<input class='input2' id='password' type='password' name='passowrd' />&nbsp;<input onclick='login()' type='button' value='登录' class='btn1' />&nbsp;<input type='button' onclick=document.location='register.aspx' class='btn1' value='注册' />");
             alert("退出登录成功！");
               
            }
     }
     })
}
function getloginstate() {
    $.ajax({
        url: "/ajax/checkLogin.ashx?n=" + Math.random(),
        type: "GET",
        cache: false,
        success: function(val) {
            if (val != "-1") {
                $("#loginText").html(val);
            }
        }
    });
}

function getloginstate1() {
    $.ajax({
        url: "/ajax/checkLogin.ashx?type=1&n=" + Math.random(),
        type: "GET",
        cache: false,
        success: function(val) {
            if (val != "-1") {
                $("#loginText1").html(val);
            }
        }
    });
}

function checkEmail(email) {
    if ($.trim(email) == "") {
        return false;
    }
    else {
        var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (reg.test(email))
            return true;
        else
            return false;
    }
}
