$(function () {
    function ValidTemp() {
        var ModelName = $("#ModelName"),
                AppPoolQueueLength = $("#AppPoolQueueLength"),
                CPULimit = $("#CPULimit"),
                CPUResetInterval = $("#CPUResetInterval"),
                CPUAction = $("#CPUAction"),
                PeriodicRestartPrivateMemory = $("#PeriodicRestartPrivateMemory"),
                PeriodicRestartMemory = $("#PeriodicRestartMemory"),
                PeriodicRestartRequests = $("#PeriodicRestartRequests"),
                PingingEnabled = $("#PingingEnabled"),
                PingInterval = $("#PingInterval"),
                RapidFailProtection = $("#RapidFailProtection"),
                StartupTimeLimit = $("#StartupTimeLimit"),
                ShutdownTimeLimit = $("#ShutdownTimeLimit"),
                PeriodicRestartSchedule = $("#PeriodicRestartSchedule"),
                RapidFailProtectionInterval = $("#RapidFailProtectionInterval"),
                RapidFailProtectionMaxCrashes = $("#RapidFailProtectionMaxCrashes"),
                allFields = $([])
                .add(ModelName)
                .add(AppPoolQueueLength)
                .add(CPULimit)
                .add(CPUResetInterval)
                .add(CPUAction)
                .add(PeriodicRestartPrivateMemory)
                .add(PeriodicRestartMemory)
                .add(PeriodicRestartRequests)
                .add(PingingEnabled)
                .add(PingInterval)
                .add(RapidFailProtection)
                .add(StartupTimeLimit)
                .add(ShutdownTimeLimit)
                .add(PeriodicRestartSchedule)
                .add(RapidFailProtectionInterval)
                .add(RapidFailProtectionMaxCrashes)
        ;
        var bValid = true;
        allFields.removeClass("ui-state-error");
        bValid = bValid && checkLength(ModelName, "模版名称", 3, 16);
        bValid = bValid && checkNumlength(AppPoolQueueLength, "核心队列", 10, 65535);
        bValid = bValid && checkNumlength(CPULimit, "CPU限制必须为1到3位的数字", 0, 100);
        bValid = bValid && checkNumlength(CPUResetInterval, "CPU刷新时间", 0, 1440);
        bValid = bValid && checkNumlength(PeriodicRestartPrivateMemory, "最大物理内存", 0, 2000000);
        bValid = bValid && checkNumlength(PeriodicRestartMemory, "最大虚拟内存", 0, 2000000);
        bValid = bValid && checkNumlength(PeriodicRestartRequests, "回收请求数目", 0, 4000000);
        bValid = bValid && checkNumlength(PingInterval, "PING时间间隔", 0, 4000000);
        bValid = bValid && checkNumlength(StartupTimeLimit, "启动时间限制", 0, 4000000);
        bValid = bValid && checkNumlength(ShutdownTimeLimit, "关闭时间限制", 0, 4000000);
        bValid = bValid && checkRegexp(PeriodicRestartSchedule, Regex_AppTime, "程序池回收时间格式不正确");
        bValid = bValid && checkNumlength(RapidFailProtectionInterval, "失败时间间隔", 0, 4000000);
        bValid = bValid && checkNumlength(RapidFailProtectionMaxCrashes, "失败次数限制", 0, 4000000);
        return bValid;
    }
    $("form").submit(function () {
        if (!ValidTemp()) {
            $(this).find("button[type=submit]").removeClass("loading").removeAttr("disabled");
            return false;
        }
    });
});