﻿//  登陆脚本 //
//var prefix = "http://v.if.qidian.com";
var prefix = "";

//登陆
function Login(){
  //登录名
  var loginName = encodeURIComponent($("txtLoginName").value.trim());
  //密码
  var loginPass = encodeURIComponent($("txtLoginPass").value.trim());
  if (loginName == ""){
      //AlertDialog("请输入用户登录名！");
      TipsDialog('提示', '请输入用户登录名！', '');
     return ;
  }
  if (loginPass == ""){
      TipsDialog('提示', '请输入用户登录密码！', '');
     return ;
 }
 var imgCode1 = $("txtImgCode").value.trim();
 if (imgCode1.length == 0)
 {
     TipsDialog('提示', '请输入验证码！', '');
     return;
 }
 var imgCode2 = $("hdnImgCode").value.trim();    

 
  var param = "loginName=" + loginName + "&loginPass=" + loginPass + "&txtImgCode=" + encodeURIComponent(imgCode1) + "&hdnImgCode=" + encodeURIComponent(imgCode2)+"&url="+location;

  new Ajax("/Ajax/LoginHandler.ashx",param,Login_CallBack,"post","UserLogin" );    
  return ;
}

//登陆CALL BACK
function Login_CallBack() {

    var result = eval(arguments[0]);

    if (result.ReturnCode == 1) {

        //登陆成功！
        //$("divUnLogin").style.display = "none";
        //$("divLogined").style.display = "inline";

        $("spanUserName").innerHTML = result.NickName;

        if (result.UserType != 4 && result.UserStatus == 3) {
            location.reload();
        }

        var divQuickLogin = $("divLogin");
        if (divQuickLogin != null) {
            divQuickLogin.style.display = "none";
        }
    } else if (result.ReturnCode == -1) {

    if (result.ReturnString.indexOf("/") > -1) {
        //您的账号已绑定安全卡，需要进行安全卡验证！
        location = result.ReturnString + "&ReturnUrl=" + encodeURIComponent(location.href);
    }
    else {
        TipsDialog('提示', "登录失败：" + result.ReturnString, '');
    }

    } else if (result.ReturnCode == -2) {

    if (result.ReturnString.indexOf("/") > -1) {
        //您的账号已绑定密保，需要进行密宝验证！
        location = result.ReturnString + "&ReturnUrl=" + encodeURIComponent(location.href);
    }
    else {
        TipsDialog('提示', "登录失败：" + result.ReturnString, '');
    }

    } else {
        TipsDialog('提示', "登录失败：" + result.ReturnString, '');
    }
}

//退出
function LoginOut(){
  var param = "";
  new Ajax("/Ajax/LoginOutHandler.ashx", param, LoginOut_CallBack, "post", "UserLoginOut");
}

function LoginOut_CallBack() {
    var result = eval(arguments[0]);
    if (result.ReturnCode == 1) {
        //登陆成功！
        location.reload(true);
    } else {
        TipsDialog('提示', "注销失败：" + result.ReturnString, '');
    }
}

function doPrint()
{
    window.print();
}

function refreshValiadCode()
{
    var url = prefix + "/Validator/GetValidateCode.aspx";
    new Ajax(url, "opName=GetValidateCode", refreshValiadCode_callback, "get", "");
}

function refreshValiadCode_callback()
{
    var result = arguments[0];
    if (result != "") {
        var code = eval(result);
        $("hidEditImgCode").value = code;
        $("imgValidCode").src = prefix + "/Validator/validateCode.aspx?" + code;
    }
}

function setImageCode()
{
    if ($("imgValidCode").src.indexOf("/images/blank.gif") != -1)
    {
        refreshValiadCode();
    }
    $("imgValidCode").style.display = "";

    var txtImgCode = $("validatecode").value;
    if (txtImgCode == "点击获取")
        $("validatecode").value = "";
}

function setLoginImageCode()
{
    if ($("imgCodeImg").src.indexOf("/images/blank.gif") != -1)
    {
        refreshLoginImageCode();
    }
    $("imgCodeImg").style.display = "";
    
    var txtImgCode = $("txtImgCode").value;
    if (txtImgCode == "点击获取")
        $("txtImgCode").value = "";
}

function refreshLoginImageCode()
{
    var url = prefix + "/Validator/GetValidateCode.aspx";
    new Ajax(url, "opName=GetValidateCode", refreshImageCode_callback, "get", "");
}

function refreshImageCode_callback()
{
    var result = arguments[0];
    if (result != "") {
        var code = eval(result);
        $("hdnImgCode").value = code;
        $("imgCodeImg").src = prefix + "/Validator/validateCode.aspx?" + code;
    }
}

function checkLoginByCookie()
{
    var cookieId="AUTHTEST";
    if (window.location.href.toLowerCase().indexOf("sd-wx.com") > -1)
    {
        cookieId="cmfuToken";
    }

    if((getCookie(cookieId)!=null && getCookie(cookieId).length > 0 ))
    {
        return true;
    }
    else
    {
        if(getCookie('cmfu_al') != null && getCookie('cmfu_al').length > 0)
        {
            return true;
        }
    }
    return false;
}

function getCookie(cookieName) 
{
    var cookieString = document.cookie;    
    var start = cookieString.indexOf(cookieName + '=');	
    // 加上等号的原因是避免在某些 Cookie 的值里有    // 与 cookieName 一样的字符串。	
    if (start == -1) 
    /* 
    找不到
    */
    return null;

    start += cookieName.length + 1;
    var end = cookieString.indexOf(';', start);
    if (end == -1) return unescape(cookieString.substring(start));
    return unescape(cookieString.substring(start, end));
}

function quickLogin() {
    //登录名
    var loginName = encodeURIComponent($("txtLoginName1").value.trim());
    //密码
    var loginPass = encodeURIComponent($("txtLoginPass1").value.trim());
    if (loginName == "") {
        TipsDialog('提示', '请输入用户登录名！', '');
        return;
    }
    if (loginPass == "") {
        TipsDialog('提示', '请输入用户登录密码！', '');
        return;
    }

    var param = "loginName=" + loginName + "&loginPass=" + loginPass;

    new Ajax("/Ajax/LoginHandler.ashx", param, quickLogin_CallBack, "post", "QuickLogin");
}

function quickLogin_CallBack() {
    var result = eval(arguments[0]);
    
    if (result.ReturnCode == 1) {
        //登陆成功！
        $('divLogin').style.display = "none";
        $('divQuickLogined').style.display = "block";

        //$("lnkQNickName").href = "/UserCenter.aspx?userid=" + result.UserId;
        $("spQNickName").innerHTML = result.NickName;

        $("divUnLogin").style.display = "none";
        $("divLogined").style.display = "inline";

        //$("spanUserName").innerHTML = result.NickName;
    }
    else {
        $('divLogin').style.display = "block";
        $('divQuickLogined').style.display = "none";
        if (result.ReturnString.indexOf("/Validate") >= 0) {
            location = result.ReturnString + "&ReturnUrl=" + encodeURI(location.href);
        }
        else {
            TipsDialog("提示", result.ReturnString, "");
        }
    }
}
