﻿
///*************************************
///  注册
///*************************************
function reg()
{
    var regDiv = $("#reg_main");
    
    if(!!!regDiv.html())
    {
        window.location.href = "index.aspx?back-reg";
        return; 
    }
    
    regDiv.show();
    $("#floatDiv").hide(); 
    $(".hiddenElement").show();
    $("#lm_MainLayer").show();
    clearregpage();
    clearreginput();
    document.getElementById("txtUserID").focus();
}

function clearregpage()
{
    $("#msg_userid").html("4-20个字符(字母、数字、下划线)");
    $("#msg_userid").attr("className","spanNm");
    $("#msg_pass").html("登陆游戏的重要密码,6-20字符");
    $("#msg_pass").attr("className","spanNm");
    $("#msg_pass2").html("再次确认您的游戏密码    ");
    $("#msg_pass2").attr("className","spanNm");
    $("#msg_email").html("填写您的常用邮箱，方便找回密码时使用");
    $("#msg_email").attr("className","spanNm");
}

function clearreginput()
{
    $(".txtUserID").val("");
    $(".txtPassWord").val("");
    $(".txtPassWord2").val("");
    $(".txtMail").val("");
}

var isCheckName = false;
function checkName()
{
    var userid = $(".txtUserID").val().replace(/^\s*|\s*$/g,"");
        
    if( !/^[a-zA-Z0-9_]+$/g.test(userid) || userid.length< 4 || userid.length>20)
    {
        $("#msg_userid").html("4-20个字符(字母、数字、下划线)");
        $("#msg_userid").attr("className","red");
        return;
    }
    
    if(userid.split('').sort().join('').replace(/(.)\1+/g, '$1').length <=2)
    {
        $("#msg_userid").html("用户名已被使用");
        $("#msg_userid").attr("className","red");
        return;
    }
        
    if(!!userid)
    {
        $.ajax({
            type:"Post",
            dataType:"html",
            url:"Request.ashx",
            data:"action=checkUserName&name=" + escape(userid),
            cache:false,
            error:function(a,b,c){
                alert(a + "\n" + b + "\n" + c);
            },
            success:function(data){
                    $("#msg_userid").html(data);
                    $("#msg_userid").attr("className","red");
                    
                    if(data.indexOf("可以使用") != -1)
                    {
                       $("#msg_userid").attr("className","green");
                       isCheckName = true; 
                    }
            }            
        });
    }
}

function checkregpage()
{
   var isReg = true;
   var userid = $(".txtUserID").val().replace(/^\s*|\s*$/g,""); 
   var password = $(".txtPassWord").val().replace(/^\s*|\s*$/g,"");
   var password2 = $(".txtPassWord2").val().replace(/^\s*|\s*$/g,"");
   var email = $(".txtMail").val().replace(/^\s*|\s*$/g,"");
   var service = document.getElementById("cbService").checked;

   if(!!!isCheckName)
   {
        $("#msg_userid").html("用户名已被使用");
        $("#msg_userid").attr("className","red");   
        $("#msg_userid").select();
        isReg = false;
   }

   if(!!!userid || !/^[a-zA-Z0-9_]+$/g.test(userid) || userid.length< 4 || userid.length>20)
   {
    $("#msg_userid").html("4-20个字符(字母、数字、下划线)");
    $("#msg_userid").attr("className","red");   
    $("#msg_userid").select();
    isReg = false;
   }
   
   if($("#msg_userid").html() == "用户名可以使用"){
        $("#msg_userid").attr("className","green");
   }else{
        $("#msg_userid").attr("className","red");
   }
   
   if(!!!password || password.length < 6 || password.length > 20)
   {
    $("#msg_pass").html("登陆游戏的重要密码,6-20字符");
    $("#msg_pass").attr("className","red");
    $("#msg_pass").select();
    isReg = false;
   }
   else
   {
    $("#msg_pass").html("填写正确");
    $("#msg_pass").attr("className","green");
   }
   
   if(!!!password2 || password != password2)
   {
    $("#msg_pass2").html("再次确认您的游戏密码    ");
    $("#msg_pass2").attr("className","red");
    $("#msg_pass2").select();
    isReg = false;
   }
   else
   {
    $("#msg_pass2").html("填写正确");
    $("#msg_pass2").attr("className","green");
   }

   if( !!!email || !!!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(email) )
   {
    $("#msg_email").html("填写您的常用邮箱，方便找回密码时使用");
    $("#msg_email").attr("className","red");
    $("#msg_email").select();
    isReg = false;
   }
   else
   {
    $("#msg_email").html("填写正确");
    $("#msg_email").attr("className","green");
   }
   
   if(!!!service)
   {
    $("#msg_service").html( "请同意服务条款！");
    $("#msg_service").attr("className" , "red");
    isReg = false;
   }
   else
   {
    $("#msg_service").html( "");
   }
   return isReg;
}

function closereg()
{
    $("#reg_main").hide();
    $(".hiddenElement").hide();
    $("#lm_MainLayer").hide();
}



///*************************************
///  登陆
///*************************************
function login(serverName)
{
    $("#ddlServer option").eq(parseInt(serverName) - 1).attr("selected" , "true");
    var loginDiv = $("#floatDiv");
    
    if(!!!loginDiv.html())
    {
        window.location.href = "index.aspx?back-login";
        return;
    }
    $("#reg_main").hide();
    $("#lm_MainLayer").show();
    $("#floatDiv").show(); 
    $(".hiddenElement").show();
    clearLoginPage();
    document.getElementById("UserName").focus();
}
function closeLogin()
{
    $("#lm_MainLayer").hide();
    $("#floatDiv").hide();
    $(".hiddenElement").hide();
    
    clearLoginPage();
}

function clearLoginPage()
{
    $("#UserName").val("");
    $("#PassWord").val("");
}

function checkloginpage()
{
    var username =$("#UserName").val().replace(/^\s*|\s*$/g,""); 
    var password = $("#PassWord").val().replace(/^\s*|\s*$/g,""); 
    
    var isLogin = true;
    
    if(!!!username || !/^[a-zA-Z0-9_]+$/g.test(username) || username.length< 4 || username.length>20)
   { 
        $("#UserName").val("");
        $("#PassWord").val("");
        isLogin = false;
    }
    if(!!!password || password.length > 20 || password.length < 6)
    {
        $("#PassWord").val("");
        isLogin = false;
    }   
    return isLogin;
}


///*************************************
///  回车登陆
///*************************************
function checklogin()
{
    if(window.event.keyCode == 13)
    {
        document.getElementById("btnEnterGame").click();
    }
}
///*************************************
///  回车注册
///*************************************
function checkreg()
{
    if(window.event.keyCode == 13)
    {
        document.getElementById("btnStartGame").click();
    }
}


///*************************************
///  每一步都更改提示 改善用户注册体验
///*************************************
function cc_ckPwd()
{
    var password = $(".txtPassWord").val().replace(/^\s*|\s*$/g,"");
    if(password.length > 0){
       if(password.length < 6 || password.length > 20)
       {
        $("#msg_pass").html("登陆游戏的重要密码,6-20字符");
        $("#msg_pass").attr("className","red");
        $("#msg_pass").select();
       }
       else
       {
        $("#msg_pass").html("填写正确");
        $("#msg_pass").attr("className","green");
       }
    }
}

function cc_ckPwd2()
{
   var password2 = $(".txtPassWord2").val().replace(/^\s*|\s*$/g,"");
   if(password2.length < 6 || password2.length > 20)
   {
    $("#msg_pass2").html("再次确认您的游戏密码    ");
    $("#msg_pass2").attr("className","red");
    $("#msg_pass2").select();
   }
}

function cc_ckPwd_Auto()
{
    var password = $(".txtPassWord").val().replace(/^\s*|\s*$/g,"");
    var password2 = $(".txtPassWord2").val().replace(/^\s*|\s*$/g,"");
    if(!!!password || password.length < 6 || password.length > 20){
    }
    else{
        if(password == password2){
            $("#msg_pass2").html("填写正确");
            $("#msg_pass2").attr("className","green");
        }else{
            if(password2.length > 0){
                $("#msg_pass2").html("再次确认您的游戏密码    ");
                $("#msg_pass2").attr("className","red");
            }
        }
    }
}

