﻿//写cookies函数
//两个参数，一个是cookie的名称，一个是值
function getCookie(name) {
    var cookieValue = "";
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue.replace('+',' ');
}

function setCookie(cookieName, cookieValue, DayValue) {
    var expire = "";
    var day_value = 1;
    if (DayValue != null) {
        day_value = DayValue;
    }
    expire = new Date((new Date()).getTime() + day_value * 86400000);
    expire = "; expires=" + expire.toGMTString();
    document.cookie = cookieName + "=" + escape(cookieValue) + ";path=/" + expire;
}

function delCookie(cookieName) {
    var expire = "";
    expire = new Date((new Date()).getTime() - 1);
    expire = "; expires=" + expire.toGMTString();
    document.cookie = cookieName + "=" + escape("") + ";path=/" + expire;
    //path=/
}
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
$(document).ready(function () {
    // 判断是否已经登录
    // username是随便定义的，可以是其他名称
    if (getCookie("wxuser") != null && getCookie("wxuser") != "") {
        $("#LoginName").html(getCookie("wxuser"));
        $("#left_logintime").html(getCookie("lastlogin"));
        $("#left_username").html(getCookie("wxuser"));
        $("#left_point").html(getCookie("point"));
        $("#Top_Logined").show();
        $("#Top_Login").hide();
        $("#Left_Logined").show();
        $("#Left_Login").hide();
        //location.href = "/index.aspx";
    }
    else {
        $("#LoginName").html("没有登录");
        $("#Top_Logined").hide();
        $("#Top_Login").show();
    }
});
function CheckLogin() {
    var txtName = $("#txtUserName");
    var txtPwd = $("#txtPassword");
    var txtCode = $("#txtCode");
    if (txtName.val() == "" || txtName.val() == null) {
        alert("请输入用户名！");
        return;
    }
    if (txtPwd.val() == "" || txtPwd.val() == null)
    {
      alert("请输入密码！");
      return;
  }
  if (txtCode.val() == "" || txtCode.val() == null) {
      alert("请输入验证码");
      return;
    }
    $.getJSON("/Ajax/CheckLogin.aspx?Name=" + txtName.val() + "&Pwd=" + txtPwd.val() + "&Code=" + txtCode.val() + "&jsoncallback=?",
    function (json) {
        if (json.msg == '1' || json.msg == 1) {
            $("#LoginName").html($.cookie('wxuser'));
            $("#left_logintime").html(getCookie("lastlogin"));
            $("#left_username").html($.cookie("wxuser"));
            $("#left_point").html($.cookie("point"));
            $("#Left_Logined").show();
            $("#Left_Login").hide();
            $("#Top_Logined").show();
            $("#Top_Login").hide();
            //window.location.href = "/Index.aspx"; 
        }
        else if (json.msg == '2' || json.msg == 2) {
            alert("验证码错误");
        }
        else {
            alert("用户名或密码错误");
        }

    });
}
function boxLogin() {//session为1记录登陆状态
    var username = $('#loginuser').val();
    var userpwd = $('#loginpwd').val();
    var jump_href = $('#jump_href').attr('href');
    if (username == '' || userpwd == '') {
        alert('用户名或密码不能为空');
        return false;
    }
    else {
        $.getJSON("/Ajax/Login.aspx?Name=" +username+ "&Pwd=" + userpwd + "&jsoncallback=?",
    function (json) {
        if (json.msg == '1' || json.msg == 1) {
                window.location.href = jump_href;
            }
            
            else {
                alert('账号密码错误');
            }
        });
    }
}
