﻿CCJOY.autoChecker = function(ops) {
    var s = this;
    this.form = $(ops.id)[0];
    if (this.form.autoChecker) return;
    this.submitBar = $(ops.submit) || null;
    if (this.submitBar) {
        this.submitBar.bind("click", function() {
            s.submit();
        })
    }
    this.form.autoChecker = true;
    this.init();
}

CCJOY.autoChecker.prototype = {
    init: function() {
        var exes = CCJOY.FORM.checkers;
        $("#username").bind("focus", function() {
            $("#username").removeClass();
            $("#username").addClass("inptext02 font14px");
            $("#username_err").hide();
            $("#username_err").html("");
        })
        $("#username").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#username")) {
                $("#username_right").hide();
                $("#username_err").show();
                $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            } else if (!exes["length"].exe.call(this, "#username,4,16")) {
                $("#username_right").hide();
                $("#username_err").show();
                $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            } else if (!exes["regUserName"].exe.call(this, "#username")) {
                $("#username_right").hide();
                $("#username_err").show();
                $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["regUserName"].error);
            }
            else {
                $("#username_err").hide();
                $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
                $("#username_right").show();
                $("#username").removeClass();
                $("#username").addClass("inptext01 font14px");
            }

        })
        $("#nickname").bind("focus", function() {
            $("#nickname").removeClass();
            $("#nickname").addClass("inptext02 font14px");
            $("#nickname_err").hide();
            $("#nickname_err").html("");
        })
        $("#nickname").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#nickname")) {
                $("#nickname_right").hide();
                $("#nickname_err").show();
                $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            } else if (!exes["length"].exe.call(this, "#nickname,3,20")) {
                $("#nickname_right").hide();
                $("#nickname_err").show();
                $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            } else if (!exes["regNickName"].exe.call(this, "#nickname")) {
                $("#nickname_right").hide();
                $("#nickname_err").show();
                $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["regNickName"].error);
            }
            else {
                $("#nickname_err").hide();
                $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
                $("#nickname_right").show();
                $("#nickname").removeClass();
                $("#nickname").addClass("inptext01 font14px");
            }
        })

        $("#email").bind("focus", function() {
            $("#email").removeClass();
            $("#email").addClass("inptext02 font14px");
            $("#email_err").hide();
            $("#email_err").html("");
        })

        $("#email").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#email")) {
                $("#email_right").hide();
                $("#email_err").show();
                $("#email_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            } else if (!exes["regEmail"].exe.call(this, "#email")) {
                $("#email_right").hide();
                $("#email_err").show();
                $("#email_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["regEmail"].error);
            }
            else {
                $("#email_err").hide();
                $("#email_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
                $("#email_right").show();
                $("#email").removeClass();
                $("#email").addClass("inptext01 font14px");
            }
        })

        $("#password1").bind("focus", function() {
            $("#password1").removeClass();
            $("#password1").addClass("inptext02 font14px");
            $("#password1_err").hide();
            $("#password1_err").html("");
        })

        $("#password1").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#password1")) {
                $("#password1_right").hide();
                $("#password1_err").show();
                $("#password1_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            } else if (!exes["length"].exe.call(this, "#password1,6,20")) {
                $("#password1_right").hide();
                $("#password1_err").show();
                $("#password1_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            }
            else {
                $("#password1_err").hide();
                $("#password1_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
                $("#password1_right").show();
                $("#password1").removeClass();
                $("#password1").addClass("inptext01 font14px");
            }
        })

        $("#password2").bind("focus", function() {
            $("#password2").removeClass();
            $("#password2").addClass("inptext02 font14px");
            $("#password2_err").hide();
            $("#password2_err").html("");
        })

        $("#password2").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#password2")) {
                $("#password2_right").hide();
                $("#password2_err").show();
                $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            } else if (!exes["length"].exe.call(this, "#password2,6,20")) {
                $("#password2_right").hide();
                $("#password2_err").show();
                $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            } else if (!exes["compare"].exe.call(this, "#password1,#password2")) {
                $("#password2_right").hide();
                $("#password2_err").show();
                $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />请检查两次输入的密码是否一致");
            }
            else {
                $("#password2_err").hide();
                $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
                $("#password2_right").show();
                $("#password2").removeClass();
                $("#password2").addClass("inptext01 font14px");
            }

        })

    },
    submit: function() {
        if (this.isAllRight()) {
            this.submitBar.unbind("click");
            this.form.submit();
        }
    },
    isAllRight: function() {
        var rt = true, exes = CCJOY.FORM.checkers;

        if (!exes["blank"].exe.call(this, "#username")) {
            $("#username_right").hide();
            $("#username_err").show();
            $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            return false;
        } else if (!exes["length"].exe.call(this, "#username,4,16")) {
            $("#username_right").hide();
            $("#username_err").show();
            $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            return false;
        } else if (!exes["regUserName"].exe.call(this, "#username")) {
            $("#username_right").hide();
            $("#username_err").show();
            $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["regUserName"].error);
            return false;
        }
        else {
            $("#username_err").hide();
            $("#username_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
            $("#username_right").show();
        }

        if (!exes["blank"].exe.call(this, "#nickname")) {
            $("#nickname_right").hide();
            $("#nickname_err").show();
            $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            return false;
        } else if (!exes["length"].exe.call(this, "#nickname,3,20")) {
            $("#nickname_right").hide();
            $("#nickname_err").show();
            $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            return false;
        } else if (!exes["regNickName"].exe.call(this, "#nickname")) {
            $("#nickname_right").hide();
            $("#nickname_err").show();
            $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["regNickName"].error);
            return false;
        }
        else {
            $("#nickname_err").hide();
            $("#nickname_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
            $("#nickname_right").show();
        }

        if (!exes["blank"].exe.call(this, "#email")) {
            $("#email_right").hide();
            $("#email_err").show();
            $("#email_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            return false;
        } else if (!exes["regEmail"].exe.call(this, "#email")) {
            $("#email_right").hide();
            $("#email_err").show();
            $("#email_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["regEmail"].error);
            return false;
        }
        else {
            $("#email_err").hide();
            $("#email_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
            $("#email_right").show();
        }

        if (!exes["blank"].exe.call(this, "#password1")) {
            $("#password1_right").hide();
            $("#password1_err").show();
            $("#password1_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            return false;
        } else if (!exes["length"].exe.call(this, "#password1,6,20")) {
            $("#password1_right").hide();
            $("#password1_err").show();
            $("#password1_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            return false;
        }
        else {
            $("#password1_err").hide();
            $("#password1_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
            $("#password1_right").show();
        }


        if (!exes["blank"].exe.call(this, "#password2")) {
            $("#password2_right").hide();
            $("#password2_err").show();
            $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["blank"].error);
            return false;
        } else if (!exes["length"].exe.call(this, "#password2,6,20")) {
            $("#password2_right").hide();
            $("#password2_err").show();
            $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />" + exes["length"].error);
            return false;
        } else if (!exes["compare"].exe.call(this, "#password1,#password2")) {
            $("#password2_right").hide();
            $("#password2_err").show();
            $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />请检查两次输入的密码是否一致");
            return false;
        }
        else {
            $("#password2_err").hide();
            $("#password2_err").html("<img src=\"http://statics.ccjoy.com/images/reg/dot/wrong.gif\" />");
            $("#password2_right").show();
        }

        return rt;
    }
};

$(function() {
    new CCJOY.autoChecker({ id: "#regform", submit: "#btn" });
    CCJOY.FORM.checkPasswordStrength("#password1", function(r) {
        $("#tip_pwd").html("密码强度：" + r.strength);
    });

})

