<hi:common_wapheader runat="server" />
<script src="../../../Utility/vshopSelector/vshopSelector.js" type="text/javascript"></script>
<div id="divLogin" class="pbox">
    <p>您好！首次登录需绑定商城账号。</p>
    <input type="text" id="txtUserName" class="form-control" placeholder="请输入您的用户名">
    <div class="input-group">
        <input id="txtPassword" type="password" class="form-control" placeholder="请输入您的密码">
        <span class="input-group-btn">
            <button class="btn btn-default" type="button" ontouchstart="javascript:$('#txtPassword').attr('type','text');" ontouchend="javascript:$('#txtPassword').attr('type','password');"><span class="glyphicon glyphicon-eye-open"></span></button>
        </span>
    </div>
    <button type="button" id="btnBindUser" class="btn btn-success btn-block">确认绑定这个账号</button>
    <p class="text-right"><a id="aRegister" href="javascript:void(0)">我还没有账号，立刻去注册！</a></p>
</div>
<div id="divRegister" class="pbox" style="display:none;">
    <p>您好！首次登录需绑定商城账号。</p>
    <input type="url" id="txtRegisterUserName" class="form-control" placeholder="请输入您的用户名" onkeyup="$('#text_mail').val($(this).val());">
    <div class="input-group mail">
        <input id="text_mail" type="url" class="form-control" placeholder="请输入您的邮箱">
        <div id="emailList" class="input-group-btn"></div>
        < id="text_email_domain" />
    </div>

    <div class="input-group">
        <input id="text_password" type="password" class="form-control" placeholder="请输入您的密码">
        <span class="input-group-btn">
            <button class="btn btn-default" type="button" ontouchstart="javascript:$('#text_password').attr('type','text');" ontouchend="javascript:$('#text_password').attr('type','password');"><span class="glyphicon glyphicon-eye-open"></span></button>
        </span>
    </div>

    <button type="button" id="btnRegisterUser" class="btn btn-danger btn-block">注册并绑定这个账号</button>

    <p class="text-right"><a id="aLogin" href="javascript:void(0)">我已有账号，立刻去登录！</a></p>
</div>
<script>
    //禁用右上角菜单
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        var action = getParam("action");
        if (action != "" && action.toLowerCase() == "register") {
            $("#divRegister").show();
            $("#divLogin").hide();
        }
        else {
            $("#divRegister").hide();
            $("#divLogin").show();
        }
        $("#btnBindUser").bind("click", function () { BindUser(); }); //绑定商城账号
        $("#btnRegisterUser").bind("click", function () { RegisterUser(); }); //绑定商城账号
        $("#aRegister").bind("click", function () { $("#divLogin").hide(); $("#divRegister").show(); });
        $("#aLogin").bind("click", function () { $("#divLogin").show(); $("#divRegister").hide(); });

        var emails = [
          { text: '@qq.com', selected: true },
          { text: '@163.com' },
          { text: '@126.com' },
          { text: '@sina.com.cn' },
          { text: '@gmail.com' },
          { text: '@sohu.com' },
          { text: '@hotmail.com' },
          { type: 'divider' },
          { text: '手动输入', href: "javascript:$('.input-group.mail').after($('#text_mail'));$('.input-group.mail').remove();" }
        ];

        $('#emailList').vshopSelector({
            data: emails, height: '34',
            onchanged: function (item) {
                $('#text_email_domain').val(item.text);
            }
        });
        $('#emailList .btn-group').removeClass('btn-group').addClass('input-group-btn');

    });

    function BindUser() {

        var username = $.trim($("#txtUserName").val()),
           password = $.trim($("#txtPassword").val());

        if (!username || username.length < 2)
            alert_h('用户名不能为空并且至少要2个字符');
        else if (!password || password.length < 6)
            alert_h('密码不能为空并且至少要6个字符');
        else {


            $.ajax({
                url: "/API/VshopProcess.ashx",
                type: 'post', dataType: 'json', timeout: 10000,
                data: { action: "BindUser", openId: getParam("sessionId"), userName: username, password: password },
                success: function (resultData) {
                    if (resultData.Status == "OK") {
                        alert_h("登录成功！", function () {
                            returnUrl = getParam("returnUrl");
                            if (returnUrl != "") {
                                if (returnUrl.toLowerCase().indexOf("login.aspx") == -1 && returnUrl.toLowerCase().indexOf("logout.aspx") == -1)
                                    location.href = "MemberCenter.aspx";
                                else
                                    location.href = returnUrl;
                            }
                            else
                                location.href = "MemberCenter.aspx";
                        });
                    }
                    else if (resultData.Status == "-1") {
                        alert_h("用户名不存在, 请重试");
                    }
                    else if (resultData.Status == "2")
                        alert_h("您要绑定的用户已经被系统禁止登录");
                    else {
                        alert_h("用户名或密码错误, 请重试");
                    }
                }
            });
        }
    }

    function RegisterUser() {
        var username = $.trim($("#txtRegisterUserName").val()),
           email = $.trim($("#text_mail").val() + ($('.input-group.mail').length > 0 ? $('#text_email_domain').val() : '')),
           password = $.trim($("#text_password").val());

        var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if (!username || username.length < 2)
            alert_h('用户名不能为空并且至少要2个字符');
        else if (!email)
            alert_h('邮箱地址不能为空');
        else if (!emailReg.test(email)) {
            alert_h('邮箱地址格式不正确');
        }
        else if (!password || password.length < 6)
            alert_h('密码不能为空并且至少要6个字符');
        else {
            $.ajax({
                url: "/API/VshopProcess.ashx",
                type: 'post', dataType: 'json', timeout: 10000,
                data: { action: "RegisterUser", openId: getParam("sessionId"), userName: username, email: email, password: password },
                success: function (resultData) {
                    if (resultData.Status == "OK") {
                        alert_h("注册成功！", function () {
                            var returnUrl = getParam("returnUrl");
                            if (returnUrl != "" && returnUrl.toLowerCase().indexOf("login.aspx") == -1)
                                location.href = returnUrl;
                            else
                                location.href = "MemberCenter.aspx";
                        });
                    }
                    else if (resultData.Status == "-1") {
                        alert_h("用户名已被注册过, 请重试");
                    }
                    else if (resultData.Status == "-2") {
                        alert_h("邮箱已经被注册过, 请重试");
                    }
                    else {
                        alert_h("注册失败, 请重试");
                    }
                }
            });
        }
    }
</script>
<hi:common_wapfooter runat="server" />

