/**
*	DM_Validator.js (jQuery.js required)
*
*	@version	0.18
*	@author	Yosuke Hiyoshi
*	@require	jquery.js
*
*	[Update history]
*	# May. 25 2010 (Version 0.18)	-	複数フォームのバリデーション対応(バリデーションするフォームには、validatorFormというクラス名をつける)
*	# May. 24 2010 (Version 0.17)	-	バリデーション無効クラス(.escape_validation)追加 && フォームクリアクラス(.clearerTrigger)追加
*	# Mar. 09 2010 (Version 0.16)	-	スクロール改良
*	# Feb. 04 2010 (Version 0.15)	-	バリデーションエラー時のスクロール追加(Method:scrollToTop) && 完全にjQueryプラグイン化
*	# Oct. 19 2009 (Version 0.14)	-	input[type="text"]グループのバリデーション(Method:textGroupHasOneValidation)追加
*	# Oct. 14 2009 (Version 0.13)	-	selectのバリデーション(Method:selectGroupValidation)追加
*	# Sep. 14 2009 (Version 0.12)	-	input[type="checkbox"]グループのバリデーション(Method:chkboxGroupValidation)追加
*	# May. 26 2009 (Version 0.11)	-	input[type="text"]グループのバリデーション(Method:textGroupValidation)追加 && selectのバリデーション(Method:selectValidation)追加
*	# May. 12 2009 (Version 0.10)	-	開発開始
*
*/
(function(jQuery) {
	jQuery(function() {
		jQuery.validator.init();
	});
	jQuery.validator = {
		/**
		 * [初期処理]
		 * 
		 * @param	void
		 * @return	void
		 */
		init: function(){
			jQuery('.requiredText').blur(function(){
				jQuery.validator.textValidation(jQuery(this));
			});
			jQuery('.requiredGroups').find('input').blur(function(){
				jQuery.validator.textGroupValidation(jQuery(this).parents('.requiredGroups'));
			});
			jQuery('.requiredHasOneGroups').find('input').blur(function(){
				jQuery.validator.textGroupHasOneValidation(jQuery(this).parents('.requiredHasOneGroups'));
			});
			jQuery('input[type="submit"],input[type="image"]').click(function(){
				return jQuery.validator.validateAll(this);
			});
			// スクロール用アンカ生成
			var i = 0;
			jQuery('.validatorForm').each(function(){
				jQuery(this).prepend('<a name="validatorForm_head_' + i + '" id="validatorForm_head_' + i + '" class="validatorForm_head"></a>');
				i++;
			});
			// イベントのunbind(バリデーションを無効にしたいボタン)
			jQuery('.escape_validation').unbind('click');
			// イベントのunbind(クリアボタン)
			jQuery('.clearerTrigger').unbind('click');
			// clickイベントbind(クリアボタン)
			jQuery('.clearerTrigger').click(function(){
				jQuery('.validatorForm :text').val('');
				jQuery('.validatorForm :password').val('');
				jQuery('.validatorForm :checkbox').attr('checked', false);
				jQuery('.validatorForm :radio').attr('checked', false);
				jQuery('.validatorForm select').val('');
				jQuery('.validatorForm :file').val('');
				jQuery('.validatorForm textarea').val('');
				return false;
			});
		},
		/**
		* [textValidation] Validation - input->type:text, input->type:password, textarea
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		textValidation: function(tgtElem){
			if(jQuery(tgtElem).val() == '' && jQuery(tgtElem).html() == ''){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
					jQuery(tgtElem).css('background', '#fee');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				jQuery(tgtElem).css('background', '#fff');
				return true;
			}
		},
		/**
		* [textGroupValidation] Validation - ELEMENT->[input->type:text], ELEMENT->[input->type:password], ELEMENT->[textarea]
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		textGroupValidation: function(tgtElem){
			var validFlg = true;
			jQuery(tgtElem).find('input').each(function(){
				if(jQuery(this).val() == ''){ validFlg = false; }
			});
			if(validFlg != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + $(tgtElem).attr('title') + '</p>');
					jQuery(tgtElem).find('input').css('background', '#fee');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				jQuery(tgtElem).find('input').css('background', '#fff');
				return true;
			}
		},
		/**
		* [textGroupValidation] Validation - ELEMENT->[input->type:text], ELEMENT->[input->type:password], ELEMENT->[textarea]
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		textGroupValidation: function(tgtElem){
			var validFlg = true;
			jQuery(tgtElem).find('input').each(function(){
				if(jQuery(this).val() == ''){ validFlg = false; }
			});
			if(validFlg != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
					jQuery(tgtElem).find('input').css('background', '#fee');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				jQuery(tgtElem).find('input').css('background', '#fff');
				return true;
			}
		},
		/**
		* [textGroupHasOneValidation] Validation - ELEMENT->[input->type:text], ELEMENT->[input->type:password], ELEMENT->[textarea]
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		textGroupHasOneValidation: function(tgtElem){
			var validFlg = false;
			jQuery(tgtElem).find('input').each(function(){
				if(jQuery(this).val() != ''){ validFlg = true; }
			});
			if(validFlg != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
					jQuery(tgtElem).find('input').css('background', '#fee');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				jQuery(tgtElem).find('input').css('background', '#fff');
				return true;
			}
		},
		/**
		* [chkboxValidation] Validation - ELEMENT->[input->type:checkbox]
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		chkboxValidation: function(tgtElem){
			if(jQuery(tgtElem).attr('checked') != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				return true;
			}
		},
		/**
		* [chkboxGroupValidation] Validation - input->type:checkbox
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		chkboxGroupValidation: function(tgtElem){
			var validFlg = true;
			if(jQuery(tgtElem).find('input[@type="checkbox"]:checked').attr('checked') != true){
				validFlg = false;
			}
			if(validFlg != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				return true;
			}
		},
		/**
		* [radioValidation] Validation - input->type:radio
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		radioValidation: function(tgtElem){
			if(jQuery(tgtElem).find('input[@type="radio"]:checked').attr('checked') != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				return true;
			}
		},
		/**
		* [selectValidation] Validation - select->option
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		selectValidation: function(tgtElem){
			if(jQuery(tgtElem).val() == ''){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				return true;
			}
		},
		/**
		* [selectGroupValidation] Validation - ELEMENT->select
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		selectGroupValidation: function(tgtElem){
			var validFlg = true;
			jQuery(tgtElem).find('select').each(function(){
				if(jQuery(this).val() == ''){
					validFlg = false;
				}
			});
			if(validFlg != true){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + jQuery(tgtElem).attr('title') + '</p>');
				}
				return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				return true;
			}
		},
		/**
		* [scrollToTop] Scroll to top of Form
		*
		* @param	void
		* @return	void
		*/
		scrollToTop: function(validate_form, options){
			//ドキュメントのスクロールを制御するオブジェクト
			var scroller = (function() {
				var c = $.extend({
					easing:100,
					step:30,
					fps:60,
					fragment:''
				}, options);
				c.ms = Math.floor(1000/c.fps);
				var timerId;
				var param = {
					stepCount:0,
					startY:0,
					endY:0,
					lastY:0
				};
				//スクロール中に実行されるfunction
				function move() {
					if (param.stepCount == c.step) {
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						//通常スクロール時
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms); 
					}
				}
				function setFragment(path){
					location.href = path;
				}
				function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
				}
				function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
				}
				function getDocumentHeight(){
					return document.documentElement.scrollHeight || document.body.scrollHeight;
				}
				function getViewportHeight(){
					return (!$.browser.safari && !$.browser.opera) ? document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight : window.innerHeight;
				}
				function getEasingY() {
					return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
				}
				function getEasing(start, end, stepCount, step, easing) {
					var s = stepCount / step;
					return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
				}
				return {
					set: function(options) {
						this.stop();
						if (options.startY == undefined) options.startY = getCurrentY();
						param = $.extend(param, options);
						param.lastY = param.startY;
						timerId = setTimeout(move, c.ms); 
					},
					stop: function(){
						clearTimeout(timerId);
						param.stepCount = 0;
					}
				};
			})();

			// スクロール
			var target = jQuery(validate_form).children('.validatorForm_head');
			if (target.length == 0) {
				target = jQuery('a[class="validatorForm_head"]');
			}
			if (target.length) {
				scroller.set({
					endY: target.offset().top,
					hrefdata: this.hrefdata
				});
				return false;
			}
		},
		/**
		* [validateAll] Validation - all input elements and textarea elements
		*
		* @param	Void
		* @return	Boolean
		*/
		validateAll: function(caller_elem){
			var validFlg = true;
			var expFlg;
			var validate_form = jQuery(caller_elem).parents('form');

			jQuery(validate_form).find('.requiredText').each(function(){
				if(jQuery.validator.textValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredGroups').each(function(){
				if(jQuery.validator.textGroupValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredHasOneGroups').each(function(){
				if(jQuery.validator.textGroupHasOneValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredChkbox').each(function(){
				if(jQuery.validator.chkboxValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredChkboxGroups').each(function(){
				if(jQuery.validator.chkboxGroupValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredRadio').each(function(){
				if(jQuery.validator.radioValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredSelect').each(function(){
				if(jQuery.validator.selectValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredSelectGroup').each(function(){
				if(jQuery.validator.selectGroupValidation(jQuery(this)) != true){ validFlg = false; }
			});
			// Essence ES 追加要件
			if((jQuery('#class').val() == '英文法クラス') && (jQuery('#week').val() != '土曜日')){
				validFlg = false;
				if(jQuery('#week').siblings('.warning').html() != null){
					jQuery('#week').siblings('.warning').remove();
				}
				jQuery('#week').parent().append('<p class="warning">「英文法クラス」は土曜日のみとなります。</p>');
			}

			/**
			 * 11/05/24 a.adachi supported by s.shirasawa
			 * 無料体験レッスンのみ発動
			 */
			if($('#class.free_lesson').val() == '900点クラブ' && ($('#week').val() == '平日朝')) {
				validFlg = false;
				if(jQuery('#week').siblings('.warning').html() != null){
					jQuery('#week').siblings('.warning').remove();
				}
				jQuery('#week').parent().append('<p class="warning">「900点クラブ」の受付は週末と平日夜のみとなります。</p>');
			}
			
			
			if(validFlg != true){
				// スクロール
				return jQuery.validator.scrollToTop(validate_form);
			}else{
				return true;
			}
		}
	};
})(jQuery);

