﻿var userNameText = ""

$(function() {

    $('.btm_HH_Login').click(function() {

        user = iniUser();

        if (user.isLogin) {
            GetHongHuangAccountInfo();
            userNameText = user.username;
        }

        $('.hh_LoginBg').slideDown(200);
        $('.HH_NewsBox').hide();
        $("input[txtLogin='userName']").focus();
    });

    //关闭按钮

    $('.btm_hh_close').click(function() {
        $('.hh_LoginBg').slideUp(200);
        $('.HH_NewsBox').show();
    });

    $("input[txtLogin='pwd']").keydown(function(event) {
        if (event.keyCode == 13) { $("[submitBtn='login']").click(); }
    });

    $("input[registe='pwd2']").keydown(function(event) {
        if (event.keyCode == 13) { $("[submitbtn='registe']").click(); }
    });

    /**********登陆游戏开始************/

    $("[submitBtn='login']").click(function() {
        var $LoginName = $("input[txtLogin='userName']");
        var $LoginPwd = $("input[txtLogin='pwd']");
        var un = $LoginName.eq($LoginName.length - 2).val();
        var pwd = $LoginPwd.eq($LoginPwd.length - 2).val();

        if (un == "") { $(".HH_Login_sms").html("请输入账号."); return; };
        if (pwd == "") { $(".HH_Login_sms").html("请输入密码."); return; };

        var ajaxUrl = window.location.href.toLowerCase();
        if (ajaxUrl.indexOf("freedom-game.com") > -1) {
            ajaxUrl = "http://www.freedom-game.com/api/Internal/Base/Login.ashx?callback=?";
        }
        else if (ajaxUrl.indexOf("ziyoudalu.com") > -1) {
            ajaxUrl = "http://www.ziyoudalu.com/api/Internal/Base/Login.ashx?callback=?";
        }
        $.ajax({
            type: "get",
            url: ajaxUrl,
            dataType: "json",
            data: "un=" + un + "&pwd=" + pwd + "&j=" + Math.random(),
            success: function(objJson) {
                if (objJson.result != 1) {
                    // alert(objJson.message);
                    $(".HH_Login_sms").html(objJson.message);
                }
                else {
                    loadUserInfo();
                    GetHongHuangAccountInfo();
                    if (objJson.isMobile == 0)
                        updateMobile();
                }
            }
        });
    });

});

/**********注册开始************/
$(function() {
    $("[submitbtn='registe']").click(function() {
        var $RegisteName = $("input[registe='userName']");
        var $RegistePwd = $("input[registe='pwd']");
        var $RegistePwd2 = $("input[registe='pwd2']");

        var un = $RegisteName.eq($RegisteName.length - 2).val();
        var pwd = $RegistePwd.eq($RegistePwd.length - 2).val();
        var pwd2 = $RegistePwd2.eq($RegistePwd2.length - 2).val();

        var patrn = /^[a-zA-Z]{1}([a-zA-Z0-9]){3,19}$/;
        if (!patrn.exec(un)) { $(".HH_Reg_sms").html("请输入通行证账号，通行证账号只能是以英文字母开头长度4-20个的字母数字组合"); return; };
        if (pwd == "") { $(".HH_Reg_sms").html("请输入密码"); return; };
        if (pwd != pwd2) { $(".HH_Reg_sms").html("您两次输入的密码不相同，请重新输入"); return; };

        var ajaxUrl = window.location.href.toLowerCase();
        if (ajaxUrl.indexOf("freedom-game.com") > -1) {
            ajaxUrl = "freedom-game.com";
        }
        else if (ajaxUrl.indexOf("ziyoudalu.com") > -1) {
            ajaxUrl = "ziyoudalu.com";
        }
        $.getJSON("http://www." + ajaxUrl + "/CPSAD/newT6Register.aspx?jsoncallback=?", {
            act: "REGISTER",
            txtUserName: un,
            txtPwd: pwd,
            txtPwd2: pwd2,
            service: 3
        },
        function(data) {
            var TM = data.ak;
            var r = TM.split(":");
            if (r[0] == "1") {
                alert(r[1]);
                return false;
            } else {
                var GAID = r[0];
                if ((GAID != "") && (GAID != "0")) {
                    loadUserInfo();
                    GetHongHuangAccountInfo();
                } else {
                    location.href = "http://www." + ajaxUrl + "/uc/Games_This6_StartGame.aspx?ServerID=1";
                }

            }
        });
    });

});
/**********注册结束************/

function GetHongHuangAccountInfo() {
    var ajaxUrl = window.location.href.toLowerCase();
    if (ajaxUrl.indexOf("freedom-game.com") > -1) {
        ajaxUrl = "freedom-game.com";
    }
    else if (ajaxUrl.indexOf("ziyoudalu.com") > -1) {
        ajaxUrl = "ziyoudalu.com";
    }
    $.ajax({
        type: "get",
        url: "http://www." + ajaxUrl + "/api/Internal/T6/GetLoginInfo.ashx?callback=?",
        dataType: "json",
        data: "j=" + Math.random(),
        success: function(objJson) {
            if (objJson.status == 1) {
                //alert(objJson.gaid1);

                $("a[serverID='1']").attr("href", "http://www." + ajaxUrl + "/uc/Games_This6_StartGame.aspx?GAID=" + objJson.gaid1);
                $("a[serverID='3']").attr("href", "http://www." + ajaxUrl + "/uc/Games_This6_StartGame.aspx?GAID=" + objJson.gaid3);

                $(".HHSelectServer").show();
                $(".HH_loginBoxDiv").hide();
                $(".hh_LoginBg").addClass("hh_LoginBg2");
                $(".hh_Login2_DIV").hide();

                //  alert("o")
                user = iniUser();
                if (user.isLogin) {
                    $(".welName").html("欢迎您:" + user.username);
                }

            }
            else {
                alert(objJson.message);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.statusText);
        }
    });
}
/**********登陆游戏结束************/

/**********登陆后若手机为空则提示用户填写************/

function updateMobile() {
    var ajaxUrl = window.location.href.toLowerCase();
    if (ajaxUrl.indexOf("freedom-game.com") > -1) {
        ajaxUrl = "http://www.freedom-game.com";
    }
    else if (ajaxUrl.indexOf("ziyoudalu.com") > -1) {
        ajaxUrl = "http://www.ziyoudalu.com";
    }
    if (confirm('您还没有进行手机绑定，是否去个人中心绑定手机号码？')) {
        window.location.href = ajaxUrl + "/uc2/UserInfoEdit.html";
    }

    //    var mobile = prompt("您还没有填写过手机号码，请填写手机号码，进一步完善资料。", "");

    //    if (mobile != null) {
    //        if (mobile == "") {
    //            alert("手机号码不能为空。");
    //            updateMobile();
    //            return;
    //        }
    //        var reg = /^((\(\d{3}\))|(\d{3}\-))?(13\d{1}|15[0-9]{1}|18[0-9]{1}|147)\d{8}$/;
    //        if (!reg.test(mobile)) {
    //            alert("手机号码格式不符合要求。");
    //            updateMobile();
    //            return;
    //        }

    //        var ajaxUrl = window.location.href.toLowerCase();
    //        if (ajaxUrl.indexOf("freedom-game.com") > -1) {
    //            ajaxUrl = "freedom-game.com";
    //        }
    //        else if (ajaxUrl.indexOf("ziyoudalu.com") > -1) {
    //            ajaxUrl = "ziyoudalu.com";
    //        }
    //        $.ajax({
    //            type: "get",
    //            url: "http://www." + ajaxUrl + "/api/Internal/Base/UpdateMobile.ashx?callback=?",
    //            dataType: "jsonp",
    //            data: "mobile=" + mobile + "&j=" + Math.random(),
    //            success: function(objJson) {
    //                alert(objJson.message);
    //                if (objJson.status == 2) {
    //                    updateMobile(ajaxUrl);
    //                }

    //            }
    //        });
    //    }
}
/**********修改手机结束************/
