﻿$(function() {
    $("form").submit(function() {
        //エラーの初期化
        $("span.error").remove();
        $("dl.mail dd").removeClass("error");
        $(":text,textarea").filter(".validate").each(function() {
            //必須項目のチェック
            $(this).filter(".required").each(function() {
                if ($(this).val() == "") {
                    $(this).parent().append("<span class='error'>必須項目です</span>")
                }
            })
            //数値のチェック
            $(this).filter(".number").each(function() {
                if (isNaN($(this).val())) {
                    $(this).parent().append("<span class='error'>数値のみ入力可能です</span>")
                }
            })
            //メールアドレスのチェック
            $(this).filter(".mail").each(function() {
                if ($(this).val() && !$(this).val().match(/.+@.+\..+/g)) {
                    $(this).parent().append("<span class='error'>アドレス形式が異なります</span>")
                }
            })
            //メールアドレス確認のチェック
            $(this).filter(".mail_check").each(function() {
                if ($(this).val() && $(this).val() != $("input[name=" + $(this).attr("name").replace(/^(.+)_check$/, "$1") + "]").val()) {
                    $(this).parent().append("<span class='error'>アドレスが異なります</span>")
                }
            })
        })
        //ラジオボタンのチェック
        $(":radio").filter(".validate").each(function() {
            $(this).filter(".required").each(function() {
                if ($("input[name=" + $(this).attr("name") + "]:checked").size() == 0) {
                    $(this).parent().append("<span class='error'>選択してください</span>")
                }
            })
        })
        //チェックボックスのチェック
        $(".checkboxRequired").each(function() {
            if ($(":checkbox:checked", this).size() == 0) {
                $(this).append("<span class='error'>選択してください</span>")
            }
        })
        // その他項目のチェック
        $(".validate.add_text").each(function() {
            if ($(this).attr("checked") == true && $("input[name=" + $(this).attr("name").replace(/^(.+)$/, "$1_text") + "]").val() == "") {
                $(this).parent().append("<span class='error'>その他の項目を入力してください。</span>")
            }
        })
        //エラーの際の処理
        if ($("span.error").size() > 0) {
            $('html,body').animate({ scrollTop: $("span.error:first").offset().top - 40 }, 'slow');
            $("span.error").parent().addClass("error");
            return false;
        }
    })
})

