﻿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("blur", function() {
            if (!exes["blank"].exe.call(this, "#username")) {
                $("#msg").html("用户名不能为空");
                return false;
            } else if (!exes["length"].exe.call(this, "#username,4,16")) {
                $("#msg").html("用户名长度在4-16位字符之间");
                return false;
            } else if (!exes["regUserName"].exe.call(this, "#username")) {
                $("#msg").html("用户名已经存在");
                return false;
            }
            else {
                $("#msg").html("");
            }
        })

        $("#email").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#email")) {
                $("#msg").html("邮箱不能为空");
                return false;
            } else if (!exes["regEmail"].exe.call(this, "#email")) {
                $("#msg").html("邮箱不正确或者已经存在");
                return false;
            }
            else {
                $("#msg").html("");
            }
        })

        $("#password1").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#password1")) {
                $("#msg").html("密码不能为空");
                return false;
            } else if (!exes["length"].exe.call(this, "#password1,6,20")) {
                $("#msg").html("密码长度在6-20位字符之间");
                return false;
            }
            else {
                $("#msg").html("");
            }
        })

        $("#password2").bind("blur", function() {
            if (!exes["blank"].exe.call(this, "#password2")) {
                $("#msg").html("确认密码不能为空");
                return false;
            } else if (!exes["length"].exe.call(this, "#password2,6,20")) {
                $("#msg").html("确认密码长度在6-20位字符之间");
                return false;
            } else if (!exes["compare"].exe.call(this, "#password1,#password2")) {
                $("#msg").html("请检查两次输入的密码是否一致");
                return false;
            }
            else {
                $("#msg").html("");
            }

        })

    },
    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")) {
            $("#msg").html("用户名不能为空");
            return false;
        } else if (!exes["length"].exe.call(this, "#username,4,16")) {
            $("#msg").html("用户名长度在4-16位字符之间");
            return false;
        } else if (!exes["regUserName"].exe.call(this, "#username")) {
            $("#msg").html("用户名已经存在");
            return false;
        }

        if (!exes["blank"].exe.call(this, "#password1")) {
            $("#msg").html("密码不能为空");
            return false;
        } else if (!exes["length"].exe.call(this, "#password1,6,20")) {
            $("#msg").html("密码长度在6-20位字符之间");
            return false;
        }

        if (!exes["blank"].exe.call(this, "#password2")) {
            $("#msg").html("确认密码不能为空");
            return false;
        } else if (!exes["length"].exe.call(this, "#password2,6,20")) {
            $("#msg").html("确认密码长度在6-20位字符之间");
            return false;
        } else if (!exes["compare"].exe.call(this, "#password1,#password2")) {
            $("#msg").html("请检查两次输入的密码是否一致");
            return false;
        }

        if (!exes["blank"].exe.call(this, "#email")) {
            $("#msg").html("邮箱不能为空");
            return false;
        } else if (!exes["regEmail"].exe.call(this, "#email")) {
            $("#msg").html("邮箱不正确或者已经存在");
            return false;
        }

        if ($("input:checked").length < 1) {
            $("#msg").html("请同意我们的协议");
            return false;
        }

        return rt;
    }
};

$(function() {
    new CCJOY.autoChecker({ id: "#qcform", submit: "#btn" });
})

