﻿function FormCheck(id) {
    this.form = $("form#" + id);
    this.attribute = {};
    var root = this;
    if (this.form.length == 0) {
        alert("初始化失败,缺少form id"); return false;
    }
    this.form.find("select").change(function () {
        Item($("#" + $(this).attr("id") + "Info"));
    })
    this.form.find("input[type=radio]").click(function () {
        Item($("#" + $(this).attr("id") + "Info"));
    })
    this.form.find("input[type=checkbox]").click(function () {
        Item($("#" + $(this).attr("id") + "Info"));
    })
    this.form.find("input").blur(function () {
        Item($("#" + $(this).attr("id") + "Info"));
    })
    this.form.find("input").keyup(function () {
        Item($("#" + $(this).attr("id") + "Info"));
    })
    function Item(span) {
        var min = span.attr("min");
        var max = span.attr("max");
        var check = span.attr("check");
        var compare = span.attr("compare");
        var checkedcount = span.attr("checkedcount");
        var id = span.attr("id");
        var errMessage = span.attr("message")
        var okMessage = span.attr("okmessage");
        if (okMessage == undefined) {
            okMessage = span.text();
            span.attr("okmessage", okMessage);
        }
        var input = root.form.find("#" + (id.length > 4 ? id.substring(0, id.length - 4) : id));

        var State = {
            ok: function () {
                if (root.attribute.itemcomplete != undefined) {
                    root.attribute.itemcomplete(control);
                }
                if (span.attr("class") != "spanerror") return;
                input.removeClass("error");
                span.removeClass("spanerror");
                span.text(okMessage);
            },
            error: function () {
                input.addClass("error");
                span.addClass("spanerror");
                span.text(errMessage);
            }
        }
        if (input.length == 0) return true;
        var control = { span: span, input: input }
        if (compare != undefined && input.get(0).tagName == "INPUT") {
            if (root.form.find("#" + compare).length == 1 && root.form.find("#" + compare).val() != input.val()) {
                State.error();
                return;
            }
        }
        if (min != undefined) {
            if (input.val().length < min) {
                State.error();
                return false;
            }
        }
        if (max != undefined) {
            if (input.val().length > max) {
                State.error();
                return false;
            }
        }
        if (check != undefined) {
            var reg = new RegExp(check, "i");
            if (!reg.test(input.val())) {
                State.error();
                return false;
            }
        }
        if (checkedcount != undefined && input.attr("type") == "checkbox" || input.attr("type") == "radio") {
            var count = 0;
            input.each(function () {
                if ($(this).attr("checked") == "checked") {
                    count++;
                }
            })
            if (count < parseInt(checkedcount)) {
                State.error();
                return false;
            }
        }
        State.ok();
        return true;
    }
    function Items() {
        var isok = true;
        root.form.find("span").each(function () {
            if (isok == true) {
                isok = Item($(this));
            }
            else {
                Item($(this))
            }
        })
        return isok;
    };
    this.form.submit(function () {
        if (!Items()) return false;
        if (root.attribute.complete == undefined) return true;
        return root.attribute.complete(this);
    })
}
