﻿$(document).ready(
    function() {
        var isSupport = isSupportCookie();        
        if(isSupport==null){
            $("#noSupportCookie").empty();
            $("#noSupportCookie").append("您好，您的浏览器禁用cookie，您将不能登录和使用购物车，请设置您的浏览器允许使用cookie！");
        }
        var userCookie = $.cookie("jjmUser");        
        if (userCookie!=""&&userCookie!=null){
            $("#userState").empty();           
            var jsonUser = JSON.parse(userCookie);
            $("#userState").append("欢迎<strong>" + jsonUser.user[0].userName + "</strong>,<a id=\"logout\" onclick=\"logout();return false;\" href=\"\" class=\"black\">注销</a> <a href=\"/client/userCenter/userIndex.aspx\" class=\"red\">我的家家买</a>" );
        }       
	    $("#loginBack").click(function(){
            $("#loginDiv").hide();
            $("#userName").val("");
            $("#password").val("");
        });
        $("#loginSubmit").click(function(){
            userLogin();
        });        
    }
);
function keyLogin(){
    if (window.event.keyCode==13){
        userLogin();
        return false;        
    }
}
function userLogin(){
    document.getElementById("loginSubmit").disabled = true;
    document.getElementById("loginSubmit").value = "登录中";    
    $.post('/ajaxService/Login.aspx', {
        userName: $("#userName").val(),
        password: $("#password").val()
        }, function(e) {
            if (e.toString()==""){
                $("#password").blur();
                $("#loginDiv").hide();                        
                alert("对不起，无此用户，请注册或重新检查您输入的用户名和密码！");
                $("#userName").val("");
                $("#password").val("");
            }else{
                createUserCookie(e.toString());
                $("#loginDiv").hide();                        
                $("#userState").empty();                        
                var jsonUser = JSON.parse(e.toString());
                $("#userState").append("欢迎<strong>" + jsonUser.user[0].userName + "</strong>,<a id=\"logout\" onclick=\"logout();return false;\" href=\"\" class=\"black\">注销</a> <a href=\"/client/userCenter/userIndex.aspx\" class=\"red\">我的家家买</a>" );
                $("#userName").val("");
                $("#password").val("");
            }                              
    });
}
//判断是否支持cookie
function isSupportCookie(){
    $.cookie("checkCookie","true",{expires:1,path:'/'}); 
    var checkCookie = $.cookie("checkCookie");    
    return checkCookie;
}
function createUserCookie(userJson){
    $.cookie("jjmUser",userJson,{expires:1,path:'/'});    
}
function logout(){
    $.cookie("jjmUser",null,{expires:1,path:'/'});     
    $("#userState").empty();
    $("#userState").append("<a id=\"login\" href=\"\" onclick=\"loginShow();return false;\" class=\"black\">登录</a> <a href=\"Register.aspx\" target=\"_blank\" class=\"black\">注册</a> <a href=\"/client/userCenter/userIndex.aspx\" class=\"red\">我的家家买</a>" );
    window.location = "/";
};

function loginShow(){
    document.getElementById("loginSubmit").disabled = false;
    document.getElementById("loginSubmit").value = "登录"; 
    $("#loginDiv").show();	
    var loginX = $("#login").offset();
    if (loginX.left + $("#loginDiv").width()<screen.width){
        $("#loginDiv").css("left",loginX.left);
    }else{
        $("#loginDiv").css("left",loginX.left+$("#login").width()-$("#loginDiv").width());
    }		    
    $("#loginDiv").css("top",loginX.top+$("#login").height());    
}

function keySearch(){
    if (window.event.keyCode==13){
        window.location = "/search.aspx?keyword="+ encodeURI($("#searchKeyword").val());
        return false;
    }
}

function mouseSearch(){    
    window.location = "/search.aspx?keyword="+ encodeURI($("#searchKeyword").val());
    return false;    
}