(function($){
	//////////////////////////////////////////////////////////////////////////////////////
	// 入力内容チェック処理
	//////////////////////////////////////////////////////////////////////////////////////
	
	//処理実行
	$.inputCheckProcess = function(t,p) {
		
		//設定----------------------------------------------------------
		p = $.extend({
			//エラー文を表示するか
			error_txt	: false
			//エラー
			,error		: false
			//必須項目
			,need		: "入力必須"
			//半角のみ
			,half		: "半角入力"
			//半角英数のみ
			,fig_spel	: "半角英数"
			//半角英字のみ
			,spel	 	: "半角英字"
			//半角数字のみ
			,fig		: "半角数字"
			//メールアドレスのみ
			,mail	 	: "正しいメールアドレスを入力してください"
		}, p);
		//設定ここまで----------------------------------------------------
		
		
		if (t.incheck) return false;
		
		var f = {
			format: function () {
				$(":input[check]",t).css('ime-mode','disabled');
				$('input[name="reset"]').click(function(){
					f.resetForm();
					return false;
				});
			}
			
			,submit: function () {
				p.error = false;
				//必須項目のチェック
				$(":input[need]",t).each(function(){
					if($(this).attr('type') == 'checkbox'){
						if(!$(this).attr("checked")){
							if($(this).attr('name').match(/\[.*?\]$/)){
								var name = $(this).attr('name').replace(/\[.*?\]$/,'');
							}else{
								var name = $(this).attr('name');
							}
							if($(this).attr('type') == 'checkbox'){
								var checkbox = $('input[type="checkbox"][checked][name^="'+name+'["]',t);
							}else{
								var checkbox = $('input[type="radio"][checked][name^="'+name+'["]',t);
							}
							if(!checkbox.length){
								f.errorProcess(this,p.need);
							}else{
								f.errorRelease(this);
							}
						}
					}else if($(this).attr('type') == 'radio'){
						var name = $(this).attr('name');
						if($('input[type="checkbox"][checked][name="'+name+'"]',t).length){
							f.errorProcess(this,p.need);
						}else{
							f.errorRelease(this);
						}
					}else{
						if(!$(this).val()){
							f.errorProcess(this,p.need);
						}else{
							f.errorRelease(this);
						}
					}
				});
				//半角のみ
				$(":input[check='half'][value]",t).each(function(){
					if($(this).val().match(/[^\x20-\x7E]/)){
						f.errorProcess(this,p.half);
					}else{
						f.errorRelease(this);
					}
				});
				//半角英数のみ
				$(":input[check='fig_spel'][value]",t).each(function(){
					if(!$(this).val().match(/^[0-9a-zA-Z\_\-]+$/)){
						f.errorProcess(this,p.fig);
					}else{
						f.errorRelease(this);
					}
				});
				//半角英字のみ
				$(":input[check='spel'][value]",t).each(function(){
					if(!$(this).val().match(/^[0-9a-zA-Z\_\-]+$/)){
						f.errorProcess(this,p.fig);
					}else{
						f.errorRelease(this);
					}
				});
				//半角数字のみ
				$(":input[check='fig'][value]",t).each(function(){
					if(!$(this).val().match(/^[0-9\_\-]+$/)){
						f.errorProcess(this,p.fig);
					}else{
						f.errorRelease(this);
					}
				});
				//メールアドレスのみ
				$(":input[check='mail'][value]",t).each(function(){
					if(!$(this).val().match(/^[0-9a-z!#\$%\&'\*\+\/\=\?\^\|\-\_\{\}\.]+@[0-9a-z!#\$%\&'\*\+\/\=\?\^\|\-\_\{\}\.]+$/)){
						f.errorProcess(this,p.mail);
					}else{
						f.errorRelease(this);
					}
				});
				if(p.error){
					alert("入力に不備があります。");
					return false;
				}else{
					return true;
				}
			}
			,errorProcess: function (target,error_txt) {
				if(!$(target).attr("error")){
					$(target).addClass("input_error").attr("error","error");
					if($(target).attr('type') != "checkbox"){
						if(p.error_txt)	$(target).after('<span class="input_error_text">'+error_txt+'</span>');
					}
				}
				p.error = true;
			}
			,errorRelease: function (target) {
				$(target).removeClass("input_error").removeAttr("error");
				if(p.error_txt)	$(target).next(".input_error_text").remove();
			}
			,resetForm: function () {
				$('input,select,textarea',t).each(function(){
					var type = $(this).attr('type');
					if(type == "checkbox" || type == "radio"){
						$(this).removeAttr('checked');
					}else{
						$(this).val("");
					}
				});
			}
		}
		f.format();
		t.incheck = f;
		return t;
	}
	
	//処理実行
	$.fn.inputCheck = function(p) {
		return this.each( function() {
			$.inputCheckProcess(this,p);
		});
	}
	
	
	
	//////////////////////////////////////////////////////////////////////////////////////
	//処理分岐
	//////////////////////////////////////////////////////////////////////////////////////
	$(function() {
		$("form").each(function () {
			$("input[type='submit']:not([name='return'])",this).each(function(){
				var target = $(this).parents("form:first");
				if($(":input[check]",target).length || $(":input[need]",target).length){
					$(target).inputCheck();
				}
			}).click(function(){
				var target = $(this).parents("form:first");
				var check = true;
				$(target).each(function(){
					if(this.incheck){
						check = this.incheck.submit();
					}
				});
				if(!check)	return false;
			});
		});
	});
	
})(jQuery);

