﻿

using(["validatebox", "messager"], function () {



    $.extend($.fn.validatebox.defaults.rules, {
        PasswordConfrim: {
            validator: function (value) {
                var inputError = true;
                var pwd1 = $("#Input_Pwd_1").val();
                var pwd2 = $("#Input_Pwd_2").val();

                if (pwd1 != pwd2) {
                    inputError = false;
                }
                return inputError;
            },
            message: 'Please enter at least {0} characters.'
        }, checkboxConfirm: {
            validator: function (value) {
                var inputError = true;
                var pwd1 = $("#chkAgreee").attr("checked");
                if (!pwd1) {
                    inputError = false;
                }
                return inputError;
            },
            message: 'Please enter at least {0} characters.'
        },
        Only: {
            validator: function (value, param) {
                var r = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i.test(value);
                if (r) {
                    this.message = "Email you entered already exists, please re-enter.";
                    return r;
                } else {
                    this.message = "Please enter a valid email address.";
                    return r;
                }
            },
            message: 'Email you entered already exists, please re-enter'
        }
    });

    //  -1
    // 登陆 start
    $("#Input_MemName").validatebox({ required: "true", missingMessage: "Can not be empty!", validType: "email", invalidMessage: "Please enter a valid email address." });
    $("#input_pwd").validatebox({ required: "true", missingMessage: "Can not be empty!", validType: "length[6,20]", invalidMessage: "6-20 characters in length(A-Z, a-z, 0-9) " });
    //$('#checkbox').validatebox({ required: "true", missingMessage: "Please tick here.!" });
    $('#chkAgreee').validatebox({ validType: "checkboxConfirm", invalidMessage: "Please tick here." });
    //end

    $("#Input_MemName_1").validatebox({ required: "true", missingMessage: " Can not be empty ", validType: "Only" });
    $("#Input_Pwd_1").validatebox({ required: "true", missingMessage: " Can not be empty ", validType: "length[6,20]", invalidMessage: "6-20 characters in length(A-Z, a-z, 0-9) " });
    $('#Input_Pwd_2').validatebox({ required: "true", missingMessage: " Can not be empty ", validType: "PasswordConfrim", invalidMessage: "please confrim password." });

    $('#chkAgreee').validatebox({ validType: "checkboxConfirm", invalidMessage: "Please tick here." });



    $("#LoginSubmit").live("click", function () {

        // alert(GetQueryString("url"));
        if ($("#Input_MemName").validatebox("isValid") && $('#input_pwd').validatebox("isValid")) {

            var strN = $("#Input_MemName").val();
            var strP = $("#input_pwd").val();
            $.ajax({
                type: "POST",
                url: "/ASHX/LoginSend.ashx",
                data: "loginName=" + strN + "&loginPwd=" + strP,
                success: function (msg) {
                    if (msg == "ok") {
                        var strUrl = GetQueryString("url");
                        if (strUrl == "" || strUrl == null) {

                            location.href = "/";
                        }
                        else {
                            location.href = strUrl;
                        }

                    }
                    else {
                        var txtHtml = "<font style='color:red;'>Error:<br>Email address or password incorrect, please try again.</font>";
                        $.modal(txtHtml, { opacity: 40, minHeight: 80, minWidth: 300, maxHeight: 430, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });

                    }

                }
            });
        }

    });

    $("#ReginsterSubmit").live("click", function () {
        if ($("#Input_MemName_1").validatebox("isValid") && $('#Input_Pwd_1').validatebox("isValid") && $('#Input_Pwd_2').validatebox("isValid") && $('#chkAgreee').validatebox("isValid")) {

            var strN = $("#Input_MemName_1").val();
            var strP = $("#Input_Pwd_1").val();
            $.ajax({
                type: "POST",
                url: "/ASHX/LoginSend.ashx",
                data: "loginName=" + strN + "&loginPwd=" + strP + "&type=1",
                success: function (msg) {
                    if (msg == "ok") {
                        var strUrl = GetQueryString("url");
                        if (strUrl == "" || strUrl == null) {

                            location.href = "/";
                        }
                        else {
                            location.href = strUrl;
                        }

                    }
                    else {

                        var tHtml = "<font style='color:red;'>Error:<br>Email you entered already exists, please re-enter.</font>";
                        $.modal(tHtml, { opacity: 40, minHeight: 80, minWidth: 300, maxHeight: 430, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });
                        $("#Input_MemName_1").val("");
                        $("#Input_Pwd_1").val("");
                        $('#Input_Pwd_2').val("");
                    }

                }
            });
        }

    });




});
