// JavaScript Document
$(function(){
    $("form").submit(function(){
    
        //エラーの初期化
        $("p.error").remove();
        $("tr th").removeClass("error");
        
        $(":text,textarea,:password").filter(".validate").each(function(){
            
            //必須項目のチェック
            $(this).filter(".required").each(function(){
                if($(this).val()==""){
                    $(this).parent().prepend("<p class='error'>必須項目です</p>")
                }
            })
						
						//必須項目のチェック
            $(this).filter(".required_thai").each(function(){
                if($(this).val()==""){
                    $(this).parent().prepend("<p class='error'>จำเป็น</p>")
                }
            })
						
						//ログイン時のメール入力空白チェック
						$(this).filter(".m_required").each(function(){
                if($(this).val()==""){
                    $(this).parent().prepend("<p class='error'>登録アドレスを入力して下さい</p>")
                }
            })
						
						//ログイン時のパスワード入力空白チェック
						$(this).filter(".p_required").each(function(){
                if($(this).val()==""){
                    $(this).parent().prepend("<p class='error'>パスワードを入力して下さい</p>")
                }
            })
						
						//英字のみのチェック
						$(this).filter(".roma").each(function(){
								if($(this).val().match(/[^A-Za-z\s]+/)){
								$(this).parent().prepend("<p class='error'>半角英字のみ入力可能です</p>")
							}
        		})
						
						//カナのみのチェック
						$(this).filter(".kana").each(function(){
								if($(this).val().match(/[^ァ-ヶ\s]+/)){
								$(this).parent().prepend("<p class='error'>カナのみ入力可能です</p>")
							}
        		})
            
            //数値のみのチェック
            $(this).filter(".number").each(function(){
                if(isNaN($(this).val())){
                    $(this).parent().prepend("<p class='error'>数値のみ入力可能です</p>")
                }
            })
						
						//生年月日の生年に4桁必須入力
						$(this).filter(".seinen").each(function(){
								if($(this).val().length < 4 ){
								$(this).parent().prepend("<p class='error'>西暦でご入力下さい</p>")
							}
        		})
            
            //メールアドレスのチェック
            $(this).filter(".mail").each(function(){
                if($(this).val() && !$(this).val().match(/.+@.+\..+/g)){
                    $(this).parent().prepend("<p class='error'>メールアドレスの形式が異なります</p>")
                }
            })
						
						//メールアドレスのチェック
            $(this).filter(".mail_thai").each(function(){
                if($(this).val() && !$(this).val().match(/.+@.+\..+/g)){
                    $(this).parent().prepend("<p class='error'>โปรดเช็คข้อมูลemailใหม่อีกครั้ง</p>")
                }
            })
            
            //メールアドレス確認のチェック
            $(this).filter(".mail_check").each(function(){
                if($(this).val() && $(this).val()!=$(".mail").val()){
                    $(this).parent().prepend("<p class='error'>メールアドレスが一致しません</p>")
                }
            })
						
						//メールアドレス確認のチェック
            $(this).filter(".mail_check_thai").each(function(){
                if($(this).val() && $(this).val()!=$(".mail").val()){
                    $(this).parent().prepend("<p class='error'>emailไม่ถูกต้องตรวจสอบemailดูอีกครั้ง</p>")
                }
            })
		
						//パスワードの入力チェック
            $(this).filter(".password_mojisu").each(function(){
								if($(this).val().length < 6 || $(this).val().length > 10){
								$(this).parent().prepend("<p class='error'>パスワードは6〜10文字で登録して下さい</p>")
							}
        		})
						
						//パスワードの入力チェック
            $(this).filter(".password_mojisu_thai").each(function(){
								if($(this).val().length < 6 || $(this).val().length > 10){
								$(this).parent().prepend("<p class='error'>passwordต้องเป็น6-10ตัวแค่นั้น</p>")
							}
        		})
						
						$(this).filter(".password").each(function(){
								if($(this).val().match(/[^A-Za-z0-9\s.-]+/)){
								$(this).parent().prepend("<p class='error'>パスワードは半角英数字のみ使用して下さい</p>")
							}
        		})
						
						
						$(this).filter(".password_konzai").each(function(){
								if(!$(this).val().match(/[A-Za-z]+/) || !$(this).val().match(/[0-9]+/)){
								$(this).parent().prepend("<p class='error'>パスワードは英字と数字を必ず使用して下さい</p>")
							}
        		})
						
						$(this).filter(".password_konzai_thai").each(function(){
								if(!$(this).val().match(/[A-Za-z]+/) || !$(this).val().match(/[0-9]+/)){
								$(this).parent().prepend("<p class='error'>passwordต้องมีทั้งตัวอักษรและหมายเลขรวมกัน</p>")
							}
        		})
						
						$(this).filter(".password_mojisu_s").each(function(){
							if($(this).val() !=""){
								if($(this).val().length < 6 || $(this).val().length > 10){
								$(this).parent().prepend("<p class='error'>パスワードは6〜10文字で登録して下さい</p>")
								}
							}
        		})
						
						//パスワード確認のチェック
            $(this).filter(".password_check").each(function(){
                if($(this).val() && $(this).val()!=$(".password").val()){
                    $(this).parent().prepend("<p class='error'>パスワードが一致しません</p>")
                }
            })
						
						//パスワード確認のチェック
            $(this).filter(".password_check_thai").each(function(){
                if($(this).val() && $(this).val()!=$(".password").val()){
                    $(this).parent().prepend("<p class='error'>passwordไม่ถูกต้องตรวจสอบpasswordดูอีกครั้ง</p>")
                }
            })
						
						//nullでもOKの場合
						
						$(this).filter(".password_s").each(function(){
							if($(this).val() !=""){
								if($(this).val().match(/[^A-Za-z0-9\s.-]+/)){
								$(this).parent().prepend("<p class='error'>パスワードは半角英数字のみ使用して下さい</p>")
								}
							}
        		})
						
						$(this).filter(".password_konzai_s").each(function(){
							if($(this).val() !=""){
								if(!$(this).val().match(/[A-Za-z]+/) || !$(this).val().match(/[0-9]+/)){
								$(this).parent().prepend("<p class='error'>パスワードは英字と数字を必ず使用して下さい</p>")
								}
							}
        		})
					
						
        });
							
        //ラジオボタンのチェック
        $(":radio").filter(".validate").each(function(){
            $(this).filter(".required").each(function(){
                if($("input[name="+$(this).attr("name")+"]:checked").size() == 0){
                    $(this).parent().prepend("<p class='error'>選択してください</p>")
                }
            })
        });
        
        //チェックボックスのチェック
				 $(":checkbox").filter(".validate").each(function(){
            $(this).filter(".required").each(function(){
                if($("input[name="+$(this).attr("name")+"]:checked").size() == 0){
                    $(this).parent().prepend("<p class='error'>規約にご同意の上、必ずチェックを入れて下さい</p>")
                }
            })
        });
        
        // その他項目のチェック
        $(".validate.add_text").each(function(){
            if($(this).attr("checked")==true && $("input[name="+$(this).attr("name").replace(/^(.+)$/, "$1_text")+"]").val()==""){
                $(this).parent().prepend("<p class='error'>その他の項目を入力してください。</p>")
            }
        })
        
        //エラーの際の処理
        if($("p.error").size() > 0){
                $('html,body').animate({ scrollTop: $("p.error:first").offset().top-40 }, 'slow');
                $("p.error").parent().addClass("error");
                return false;
        }
    })
})

//input要素の記述
$(function(){
	$(":input").focus(function(){
		$(this).css("background","#DFEEFF");
	}).blur(function(){
		$(this).css("background","");
	});
});
