// クラス定義
var JValidate = function(){};

// 全体バリデートの定義
JValidate.prototype.validate = function(elementsArray){
	// 警告文の定義
	var alertMsgArray = new Array();
	// 装飾用
	var errorList = new Array();
	
    jQuery.each(elementsArray, function(key,val) {
    	var result = null;
    	var filteredValue = null;
    	var validateValue = null;
    	
    	if(val["prefilter"] != undefined){
    	// prefilterが定義されていない場合
    		// prefilter の種類で条件分岐
    		switch (val["prefilter"]) {
    			// ＠で結合
				case 'atmarkCombine':
					filteredValue = JValidate.prototype.prefilterAtmarkCombine( key );
					break;
				default:
					validateValue = $(key).val();
					break;
    		}
    		validateValue = filteredValue;
    	}else{
    	// prefilterが定義されていない場合
    		// keyの頭文字を取得
    		keyInitial = key.charAt(0);
    		if( keyInitial == '.' ){
    		// クラスの場合
    			var array = new Array();
    			$(key).each(function(){
    				if( $(this).val()!="" ){
    					array.push($(this).val());
    				}else{
    					array.push('');
    				}
    				return true;
    			});
    		}else if( keyInitial == '#' ){
    		// IDの場合
    			validateValue = $(key).val();
    		
    		}
    	}
    	
    	// それぞれのエレメントに
    	// 割り当てられているメソッドの数だけ繰り返す
    	jQuery.each(val["method"], function(methodKey,methodVal){
    		switch (methodVal) {
    			case 'require':
   					// IDの場合
    				if( keyInitial == '#' ){
	    				result = JValidate.prototype.validateRequire( validateValue );

	    			// クラスの場合
    				} else if ( keyInitial == '.' ){
    					// 条件が「すべて」の場合
    					if( val["condition"] == 'all' ){
	    					for (var i = 0; i < array.length; i ++) {
	    						result = JValidate.prototype.validateRequire( array[i] );
	    						if(result==false){
	    							break;
	    						}
	    					}
	    					
	    				// 条件が「いずれか」の場合
    					}else if( val["condition"] == 'each' ){
	    					for (var i = 0; i < array.length; i ++) {
	    						result = JValidate.prototype.validateRequire( array[i] );
	    						if(result==true){
	    							break;
	    						}
	    					}
    					}
    				}
    				if(!result){
    					alertMsgArray.push('\'' + val["jp_name"] + '\' ' + 'を入力してください。');
    					errorList.push(key);
    					return false;
    				}
    				break;

    			case 'mail':
    				result = JValidate.prototype.validateMail( validateValue );
    				if(!result){
    					alertMsgArray.push('\'' + val["jp_name"] + '\' ' + 'を正しく入力してください。');
    					errorList.push(key);
    					return false;
    				}
    				break;

    			case 'check':
    				result = JValidate.prototype.validateCheck( key,1 );
    				if(!result){
    					alertMsgArray.push('\'' + val["jp_name"] + '\' ' + 'をチェックしてください。');
    					errorList.push(key);
    					return false;
    				}
    				break;
    				
    			case 'hankana':
    				result = JValidate.prototype.validateHankana( validateValue );
    				if(!result){
    					alertMsgArray.push('\'' + val["jp_name"] + '\' ' + 'は全角で入力してください。');
    					errorList.push(key);
    					return false;
    				}
    				break;

    			default:
    				break;
			}
    	});
    });
    if(alertMsgArray.length==0){
    	$("#shiryo").submit();
    }else{
        var alertMsg = alertMsgArray.join('\n');
        //alertMsg = '下記の内容をご確認ください。\n\n'+alertMsg;
        alert(alertMsg);

        // エラー箇所を装飾
        /*
        var style_value = "";
        jQuery.each(errorList, function(i){
			$("#"+errorList[i]).addClass("error");
			$("#"+errorList[i]).change(function(){
				$("#"+errorList[i]).removeClass("error");
			})
        });
        */
    }
};

// 必須項目のバリデート
JValidate.prototype.validateRequire = function(value){
	if (value=="") {
		return false;
	}else{
		return true;
	}
}

// 半角のバリデート
JValidate.prototype.validateHankana = function(value){
	if ( value != '' ){
		if (value.match(/[ｱ-ﾝｧｨｩｪｫｬｭｮ]/)) {
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}

// メールアドレス妥当性のバリデート
JValidate.prototype.validateMail = function(value){
	if ( value != '' ){
		if (value.match(/^[\w\_\-\.]+\@[\w\-\.]+\.[a-z]+$/)) {
			return true;
		}else{
			return false;
		}
	}else{
		return true;
	}
}

//　チェックボックスのバリデート
JValidate.prototype.validateCheck = function(element,requireCheckNum){
	var n = $(element+":checked").length;
	if( n >= requireCheckNum ){
		return true;
	}else{
		return false;		
	}
}

// 複数の要素を@で連結するプリフィルター
JValidate.prototype.prefilterAtmarkCombine = function(element){
	var array = new Array();
	$(element).each(function(){
		if( $(this).val()!="" ){
			array.push($(this).val());
		}
		return true;
	});
	result = array.join("@");
	return result;
}

// 複合的なルール

