﻿$J(document).ready(function(){
//try
//    {
//    AppPath =  $J.ajax({url:path+"bi/loginservice.aspx?method=AppPath",async:false,cache:false}).responseText;
//	if(AppPath.length>1)
//	AppPath = AppPath+"/";
//    }
//    catch(error)
//    {
//       
//    }
    
    //以下两句仅测试时用,正式发布时应注释掉   
    //    $J("#UserName").val("test@163.com");
    //    $J("#Password").val("test");
    setTimeout(function(){ChangeVerifyCode();},10);
    
    
    //CheckUserState();
    $J(".ToConsole").click(function(){
        window.top.location = "ui/console/console.aspx";
        return false;
    });
    $J("#Console").click(function(){
        window.top.location = "ui/console/console.aspx";
        return false;
    });
   
    $J("#Regist").click(function(){
        window.top.location = "ui/register/register.aspx";
        return false;
    });
    
    $J("#UserName").keypress(function(e){
        if (e.which == 13 || e.which == 10) {
            Login_Click();
            return false;
        }
    }).keypress(function(e){
        if (e.which == 32) 
            return false;
        
    }).click(function(){
        $J("#Error").text("");
    });
    $J("#Password").keypress(function(e){
        if (e.which == 13 || e.which == 10) {
            Login_Click();
            return false;
        }
        
    }).keypress(function(e){
        if (e.which == 32) 
            return false;
        
    }).click(function(){
        $J("#Error").text("");
    });
    $J("#VerifyCode").keypress(function(e){
        if (e.which == 13 || e.which == 10) {
            Login_Click();
            return false;
        }
        
    }).click(function(){
        $J("#Error").text("");
    });
    $J("#Login").click(function(){
        Login_Click();
        return false;
    });
    $J("#CancelLogin").click(function(){
        CancelLogin_Click();
        return false;
    });
    
    $J("#ForgetPWD").click(function(){
        window.parent.location = "ui/register/forgetpwd.aspx";
    });
    
    $J("#UserName").watermark("请输入您的注册邮箱|null");
    
});
function CheckUserState(data){
    var isLoginResult = data;
//    var isLoginResult = $J.ajax({
//        url: "bi/loginservice.aspx?method=IsLogin",
//        async: false,
//        cache: false
//    }).responseText;
    
    if (isLoginResult == "NotLogin" || isLoginResult == "" || isLoginResult == null || isLoginResult == undefined) {
    
        $J("#divLogin").show();
        $J("#divLoginSuccessed").hide();
    }
    else {
        var registerInfo = eval( '(' + isLoginResult + ')' );
        $J("#divLogin").hide();
        
        ShowRegister(registerInfo)
        
    }
}

function Login_Click(){
    var userNameValue = $J("#UserName").val();
    var passwordValue = $J("#Password").val();
    var verifyCodeValue = $J("#VerifyCode").val();
    if (userNameValue == "") {
        $J("#Error").text("请输入用户名");
        
        $J("#UserName").focus();
        return;
    }
    
    if (!userNameValue.match(EMAIL_REG)) {
        $J("#Error").text("请输入有效的Email地址");
        
        $J("#UserName").focus();
        return;
    }
    if (passwordValue == "") {
        $J("#Error").text("请输入密码");
        $J("#Password").focus();
        return;
    }
    if (verifyCodeValue == "") {
        $J("#Error").text("请输入校验码");
        $J("#VerifyCode").focus();
        return;
    }

    
      $J("#loading").show();
  
    $J.ajax({
        type: "POST",
        url: "bi/loginservice.aspx",
        data: {
            method: "Login",
            username: userNameValue,
            passowrd: passwordValue,
            verifyCode: verifyCodeValue
        },
        cache: false,
        
        success: function(data){
            LoginCompleted(data);
            $J("#loading").hide();
            return false;
            
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $J("#loading").hide();
            alert('login error:' + errorThrown);
        }
    });
}

function LoginCompleted(data){
    if (data == "VerifyCodeError") {
        $J("#Error").text("校验码错误");
        ChangeVerifyCode();
        $J("#VerifyCode").val("").focus();
    }
    else 
        if (data == "UserNameOrPasswordError") {
            $J("#Error").text("Email地址或密码错误");
        }
        else 
            if (data.substr(0, 6) == "Error:") {
                alert(data);
            }
            else {
                Loginclear();
                //var registerInfo = JSON.parseJSON(data);
                var registerInfo =eval( '(' + data + ')' );
                $J("#divLogin").hide();
                
                
                
                //window.top.location = "UI/Console/Console.aspx";
                if ($J("#LoginSelf").html() == "true") {
                	LoginSuccessDo(registerInfo);
                }
                else {
                    if ($J.browser.msie) {
                        var frame = window.parent;
                        frame.LoginSuccessDo(registerInfo);
                    }
                    else {
                    
                    
                        parent.LoginSuccessDo(registerInfo);
                        
                    }
                    
                }
                
                //ShowRegister(registerInfo);
                
            }
}

function CancelLogin_Click(){
    var cancellogin = $J.ajax({
        url: "bi/loginservice.aspx?method=CancelLogin",
        async: false,
        cache: false
    }).responseText;
    
    if (cancellogin == "OK") {
        setTimeout(function(){ChangeVerifyCode();},10);
        $J("#divLoginSuccessed").hide();
        ClearRegisterInfo();
        $J("#divLogin").show();
    }
    return false;
}

function Loginclear(){
    $J("#UserName").val("");
    $J("#Password").val("");
    $J("#VerifyCode").val("");
    $J("#Error").text("");
}
function ShowRegister(registerInfo)
{
    $J("#divLogedUserName").text(registerInfo.Name);
    $J("#CareerState").text(registerInfo.State);
    $J("#CareerUpdateDate").html(CheckCareerUpdateDate(registerInfo.JobSeekerUpdateTime.toJSON().substring(0,10),registerInfo));
    $J("#LastLoginDate").html(registerInfo.LastLogTime.toJSON().substring(0,10));
    $J("#divLoginSuccessed").show();
}
function CheckCareerUpdateDate(date,registerInfo)
{
    if(date=="1900-01-01")
    {
        
        return  registerInfo.RegisterTime.toJSON().substring(0,10);
        
    }
    return date;
}

function ClearRegisterInfo()
{
 $J("#divLogedUserName").text("");  
  $J("#CareerState").text(""); 
   $J("#CareerUpdateDateSpan").show();   
   $J("#CareerUpdateDate").text("");
   $J("#LastLoginDate").text("");
}