﻿/**
*	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);
			});
/*
			jQuery('.requiredMail').blur(function(){
				jQuery.validator.MailValidation(jQuery(this));
			});
*/
			jQuery('.requiredTest').change(function(){
				jQuery.validator.TestValidation(jQuery(this));
			});
			jQuery('.requiredTest2').change(function(){
				jQuery.validator.Test2Validation(jQuery(this));
			});

			jQuery.validator.Test2Validation(jQuery('.requiredTest2:checked'));
/*
			jQuery('.requiredKana').change(function(){
				jQuery.validator.KanaValidation(jQuery(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, select, textarea').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').length == 0){
					jQuery(tgtElem).parent().append('<p class="warning fl">' + jQuery(tgtElem).attr('title') + '</p>');
				}else
				if((jQuery(tgtElem).siblings('.warning').text() == "")){
					jQuery(tgtElem).siblings('.warning').text(jQuery(tgtElem).attr('title'));
				}
				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;
			}
		},
		/**
		* [MailValidation] Validation - input->type:text, input->type:password, textarea
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		MailValidation: function(tgtElem){
			if((!jQuery(tgtElem).val().match(/[\w.-]+\@[\w.-]+\.[a-zA-Z]{2,3}/)) || jQuery(tgtElem).val() == '' ){
				if(jQuery(tgtElem).siblings('.warning').html() == null){
					jQuery(tgtElem).parent().append('<p class="warning">' + 'メールアドレスが不正です。' + '</p>');
					jQuery(tgtElem).css('background', '#fee');
				}
				return false;
			//}else if( jQuery(tgtElem).val().match(/yahoo.co.jp$/) || jQuery(tgtElem).val().match(/hotmail.co.jp$/) || jQuery(tgtElem).val().match(/hotmail.com$/) || jQuery(tgtElem).val().match(/docomo.ne.jp$/) || jQuery(tgtElem).val().match(/ezweb.ne.jp$/) || jQuery(tgtElem).val().match(/softbank.ne.jp$/)){
			//	if(jQuery(tgtElem).siblings('.warning').html() == null){
			//		jQuery(tgtElem).parent().append('<p class="warning">' + '携帯アドレスおよび一部のフリーアドレスは使用できません。' + '</p>');
			//		jQuery(tgtElem).css('background', '#fee');
			//	}
			//	return false;
			}else{
				jQuery(tgtElem).siblings('.warning').remove();
				jQuery(tgtElem).css('background', '#fff');
				return true;
			}
		},
		TestValidation: function(tgtElem){
			//特別指定校の時は指定課題を必須にする
			if(jQuery(tgtElem).val() == 1){
				$('#RequestSpecifiedProblem').addClass("requiredRadio");
			}else{
				//消す
				$('#RequestSpecifiedProblem').removeClass("requiredRadio");
				jQuery('#RequestSpecifiedProblem').siblings('.warning').remove();
				jQuery('#RequestSpecifiedProblem').css('background', '#fff');
			}
			//alert(jQuery(tgtElem).val());
				return true;
		},
		Test2Validation: function(tgtElem){
			//グレーアウト
			if(jQuery(tgtElem).val() == 5 || jQuery(tgtElem).val() == 6){
				jQuery('#RequestNumberOfTeachers').siblings('.warning').remove();
				jQuery('#RequestNumberOfTeachers').css('background', '#fff');
				jQuery('#RequestNumberOfStudents').siblings('.warning').remove();
				jQuery('#RequestNumberOfStudents').css('background', '#fff');
				jQuery('#RequestLotOfClasses').siblings('.warning').remove();
				jQuery('#RequestLotOfClasses').css('background', '#fff');

				//jQuery(this).unbind('blur', jQuery.validator.textValidation);
/*
				jQuery('#RequestNumberOfStudents').unbind('blur');//, jQuery.validator.textValidation);
				jQuery('#RequestNumberOfTeachers').unbind('blur');//, jQuery.validator.textValidation);
				jQuery('#RequestLotOfClasses').unbind('blur');//, jQuery.validator.textValidation);
*/
				jQuery('#RequestNumberOfStudents').css('background', '#868686');
				jQuery('#RequestNumberOfStudents').attr('readonly', true);
				$('#RequestNumberOfStudents').removeClass("requiredText");
				jQuery('#RequestNumberOfTeachers').css('background', '#868686');
				jQuery('#RequestNumberOfTeachers').attr('readonly', true);
				$('#RequestNumberOfTeachers').removeClass("requiredText");
				//removeListener(".requiredText");
				jQuery('#RequestLotOfClasses').css('background', '#868686');
				jQuery('#RequestLotOfClasses').attr('readonly', true);
				$('#RequestLotOfClasses').removeClass("requiredText");
				//

			}else{
				//リードオンリー
				jQuery('#RequestNumberOfStudents').css('background', '#FFFFFF');
				jQuery('#RequestNumberOfStudents').attr('readonly', false);
				$('#RequestNumberOfStudents').addClass("requiredText");
				jQuery('#RequestNumberOfTeachers').css('background', '#FFFFFF');
				jQuery('#RequestNumberOfTeachers').attr('readonly', false);
				$('#RequestNumberOfTeachers').addClass("requiredText");
				jQuery('#RequestLotOfClasses').css('background', '#FFFFFF');
				jQuery('#RequestLotOfClasses').attr('readonly', false);
				$('#RequestLotOfClasses').addClass("requiredText");
/*
				jQuery('#RequestNumberOfStudents').blur(function(){jQuery.validator.textValidation(jQuery(this));});
				jQuery('#RequestNumberOfTeachers').blur(function(){jQuery.validator.textValidation(jQuery(this));});
				jQuery('#RequestLotOfClasses').blur(function(){jQuery.validator.textValidation(jQuery(this));});
*/
			}
				return true;
		},
		/**
		* [MailValidation] Validation - input->type:text, input->type:password, textarea
		*
		* @param	tgtElem:Object
		* @return	Boolean
		*/
		KanaValidation: function(tgtElem){
			if((!jQuery(tgtElem).val().match(/^[1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜﾝｶﾞｷﾞｸﾞｹﾞｺﾞｻﾞｼﾞｽﾞｾﾞｿﾞﾀﾞﾁﾞﾂﾞﾃﾞﾄﾞﾊﾞﾋﾞﾌﾞﾍﾞﾎﾞﾊﾟﾋﾟﾌﾟﾍﾟﾎﾟｳﾞｯャュｮｦ()-. ,]+$/)) || jQuery(tgtElem).val() == '' ){
				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;
			}
		},
		/**
		* [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; }
			});
			jQuery(validate_form).find('.requiredMail').each(function(){
				if(jQuery.validator.MailValidation(jQuery(this)) != true){ validFlg = false; }
			});
			jQuery(validate_form).find('.requiredKana').each(function(){
				if(jQuery.validator.KanaValidation(jQuery(this)) != true){ validFlg = false; }
			});
			var val = $("input:radio[@id='#ApplicantQuestionary1']:checked").val();
			//alert(val);
			jQuery('#ApplicantAnotherQuestionary').siblings('.warning').remove();
			jQuery('#ApplicantAnotherQuestionary').css('background', '#fff');

			if((val == 1 || val == 7) && jQuery('#ApplicantAnotherQuestionary').val() ==''){
				jQuery('#ApplicantAnotherQuestionary').parent().append('<p class="warning">' + '[新聞・雑誌][その他]選択時は入力してください' + '</p>');
				jQuery('#ApplicantAnotherQuestionary').css('background', '#fee');
				validFlg = false;
			}else{
				jQuery('#ApplicantAnotherQuestionary').siblings('.warning').remove();
				jQuery('#ApplicantAnotherQuestionary').css('background', '#fff');
			}
/*
			var val2 = j$("input:radio[@id='#RequestRequestDivision2']:checked").val();
//alert(val2);
			var val3 = j$("input:radio[@id='#RequestSpecifiedProblem1']:checked").val();
			var val4 = j$("input:radio[@id='#RequestSpecifiedProblem2']:checked").val();
alert(val3+ 'test'+val4);
			if(val2 == 1 && val3 == 0 && val4 == 0){
				alert(val2 + val3 + val4+'ok');
				jQuery('#RequestSpecifiedProblem2').parent().append('<p class="warning">' + '指定課題を選択してください' + '</p>');
				jQuery('#RequestSpecifiedProblem2').css('background', '#fee');
				validFlg = false;
			}else{
				alert(val2 + val3 + val4+'NG');

				jQuery('#RequestSpecifiedProblem2').siblings('.warning').remove();
				jQuery('#RequestSpecifiedProblem2').css('background', '#fff');
			}
*/
			if(validFlg != true){
				// スクロール
				return jQuery.validator.scrollToTop(validate_form);
			}else{
				return true;
			}
		}
	};
})(jQuery);

