$(function(){

    $("#btn_login").click(function() {
    
        var anv=$("#an").val();  //登录名
        var pwv=$("#pw").val();  //登录密码
        if (anv=='' || /\'/g.test(anv)) { //判断登录名为空  或  用户名包含英文 ' 号
            alert('请填写账号!');
            return false;
        }
        
        if (pwv == '' || /\'/g.test(pwv)) { //判断密码为空  或   密码包含英文 ' 号
            alert('请填写密码!');
            return false;
        }
    
        $.get("loginupload.html?an="+anv+"&pw="+pwv,function(s){
            switch (s) {
              case "SYSTEM_RIGHT":
                    window.location.href='/index.html';
                    break;
              case "SYSTEM_RIGHT_AND_RESETPASSWORD":
                    window.location.href='/ba/pwup.html?flag=2&goindex=true';
                    break;
              case "SYSTEM_USERNAME_EMPTY":
                    alert("用户名不能为空!");
                    $("#an").focus();
                    break;
              case "SYSTEM_PASSWORD_EMPTY":
                    alert("密码不能为空!");
                    $("#pw").focus();
                    break;
              case "SYSTEM_USERNAME_NOT_EXIST":
                    alert("不存在的用户名,请仔细核对!");
                    $("#an").focus();
                    break;
              case "SYSTEM_PASSWORD_ERROR":
                    alert("不正确的密码!");
                    $("#pw").focus();
                    break;
            }
        });
    })
    
    $("btn_reset").click(function(){ //重置按钮事件
        $("#an").val("");
        $("#pw").val("");
    })
    
    $(document).keydown(function (e) { // 获取网页文档对象; 的键盘按下事件
        if (e.keyCode == 13) { // enter 键
            $("#btn_login").click();//键盘登录事件
            return false; //不执行下面语句
        };
    })
    

    var setcookie=function(){
        $.cookie('rememberme',"true",{expires:60}); //创建cookie 记住我; 值为true ;有效期为60天
        $.cookie('usrname',encodeURIComponent($("#an").val()),{expires:60}); //usrname 值为 用户名  有效期60天
        $.cookie('pwd',encodeURIComponent($("#pw").val()),{expires:60}); //pwd 值为密码  有效期60天
    }
                          
    var clrcookie=function(){
        $.cookie('rememberme',"false"); //设置cookie记住我的值为false
        $.cookie('usrname',null); //删除cookie中的值
        $.cookie('pwd',null); //删除cookie中的值
    }
    
    //当更改用户名或密码后更新cookie
    $("#an,#pw").live("blur",function(){ //为用户名框和密码框添加 焦点离开事件
        if($("#rememberme").hasClass("remember")){ //获取a标签判断是否存在类名(remember)
            setcookie();      //存在  设置cookie
        }
    }).live("keyup", function () { //为用户名框和密码框添加 键盘事件
        if($("#an").val()!=""){ //判断用户名不等于空;
            setcookie(); //设置cookie
            console.log(this);
            $(this).addClass("remember"); //添加类
        }else{ //用户名等于空
            clrcookie(); //清除cookie
            $(this).removeClass("remember"); //移除类
        }
    })
    
    //当勾上“记住我”时保存cookie,否则清空
    $("#rememberme").live("click",function(){ //获取a标签对象, 为a标签绑定单击事件;
        if (!$(this).hasClass("remember")) {  //这里的this 指向 触发事件的a标签 . hasClass()检查是否存在类名remember
            setcookie();            //不存在(hasClass()返回flase)   设置cookie
            $(this).addClass("remember"); //为a标签添加一个类
        }else{
            clrcookie(); // 存在 清除cookie,
            $(this).removeClass("remember");// 移除a标签的类(控制图片的类名)
        }
    });
    
    if ($.cookie('rememberme') == "true") {  //窗体加载时候  读取cookie中rememberme的值是否等于true
       // $("#an").val("000"); 
        $("#an").val(decodeURIComponent($.cookie('usrname'))); //获取cookie中usrname的值进行解码;赋值到文本框
        //使用 decodeURIComponent() 对编码后的 URI 进行解码: 
        $("#pw").val(decodeURIComponent($.cookie('pwd'))).focus(); //读取cookie的pwd的值赋值到密码框, 获得焦点
        $("#rememberme").addClass("remember");//添加一个a标签 class名
    } else {
        $("#an").focus(); //读取cookie中rememberme(记住我)的值为false;用户名获取焦点
    }


    // 创建一个cookie并设置有效时间为7天:  
    //$.cookie('cookieName', 'cookieValue', { expires: 7 });
    //读取cookie:  
    //$.cookie('cookieName'); // 若cookie存在则返回'cookieValue';若cookie不存在则返回null 
    //删除cookie:把ncookie的值设为null即可  
    //$.cookie('the_cookie', null);   
    //获得焦点 事件
  // $("input").focus(function(){
  //  $("input").css("background-color","#FFFFCC");
    //});
    //失去焦点
      //$("input").blur(function(){
      //  $("input").css("background-color","#D6D6FF");
      //});
})