console.log('root common_js status ok...');

/*
adobe fonts
*/
(function(d) {
var config = {
  kitId: 'qka1qrg',
  scriptTimeout: 3000,
  async: true
},
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
})(document);

/*
	custom location
*/
$('body').on('click','.location',function(e)
{
	$href = $(this).data('href');
	if( $(this).data('target') !== undefined )
	{
		window.open($href);
		return false;
	}
	
	location.href = $href;
	return false;
});

/*
	modal common
*/
$('.js-modal-close').on('click',function(e)
{
	$('html').toggleClass('lock');
	$('.js-modal').fadeOut();
	return false;
});

/*
	パスワードの強さ確認
*/
function checkStrength(password) {

	var strength = 0;
	
	if (password.length < 6) {
		return strength;
	}
	
	if (password.length > 7) strength += 1
	if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1
	if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 1
	if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
	if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
	
	return strength;
}


/*
	cunstom validation
*/
function validation(form_id)
{
	$( form_id ).find('.error_text').remove();
	$( form_id ).find('.error').removeClass('error');
	error = 0;
	

	
	$( form_id ).find('.required').each(function(i,e)
	{
		// $(e).removeClass('error');
		
		if( $(e).attr('type') == 'text' || $(e).attr('type') == 'number' || $(e).attr('type') == 'date' || $(e).attr('type') == 'hidden' || $(e).attr('type') == 'email' || $(e).attr('type') == 'password' || $(e).attr('type') == 'tel')
		{
			//共通必須処理
			if( $(e).val() == '' )
			{
				$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">' + $(e).data('placeholder') + 'が入力されていません</p>');
				error++;
			}
			//文字数処理
			if( $(e).val() != '' && $(e).data('length-min') !== undefined )
			{
				if( $(e).val().length < $(e).data('length-min') )
				{
					$(e).parent().after('<p class="error_text">' + $(e).data('length-min') + '文字以上で入力してください</p>');
					error++;
				}
			}
			if( $(e).val() != '' && $(e).data('length-max') !== undefined )
			{
				if( $(e).val().length > $(e).data('length-max') )
				{
					$(e).parent().after('<p class="error_text">' + $(e).data('length-max') + '文字以下で入力してください</p>');
					error++;
				}
			}
			//数値処理
			if( $(e).val() != '' && $(e).attr('type') == 'number' && $(e).data('number-min') !== undefined )
			{
				if( $(e).val() < $(e).data('number-min') )
				{
					$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">' + $(e).data('number-min') + '以上で入力してください</p>');
					error++;
				}
			}
			if( $(e).val() != '' && $(e).attr('type') == 'number' && $(e).data('number-max') !== undefined )
			{
				if( $(e).val() > $(e).data('number-max') )
				{
					$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">' + $(e).data('number-max') + '以下で入力してください</p>');
					error++;
				}
			}
			//メールアドレス処理
			if( $(e).attr('type') == 'email' && $(e).val() != '' && !$(e).val().match(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/) )
			{
				$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">メールアドレスが正しくありません</p>');
				error++;
			}
			//メールアドレス
			if( $(e).attr('type') == 'email' && $(e).val() != '' && $(e).data('reemail') !== undefined )
			{
				if( $($(e).data('reemail')).val() != $(e).val() )
				{
					$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">メールアドレスが一致しません</p>');
					error++;
				}
			}
			
			//パスワード強度確認
			if( $(e).attr('type') == 'password' && $(e).val() != '' && $(e).data('repassword') === undefined && $(e).data('zxcv') !== undefined )
			{
				if( checkStrength( $(e).val() ) < 2 )
				{
					$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">パスワードが弱すぎます</p>');
					error++;
				}
			}
			
			//パスワード確認
			if( $(e).attr('type') == 'password' && $(e).val() != '' && $(e).data('repassword') !== undefined )
			{
				if( $($(e).data('repassword')).val() != $(e).val() && checkStrength( $('#password').val() ) >= 2 )
				{
					$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">パスワードが一致しません</p>');
					error++;
				}
			}
			
			
			
			
			
			//言語制限
			if( $(e).val() != '' && $(e).data('match') !== undefined )
			{
				
				if( $(e).data('match') == 'ひらがな' && !$(e).val().match( /^[\u3040-\u309f]+$/ ) )
				{
					$(e).parent().after('<p class="error_text">' + $(e).data('match') + 'で入力してください</p>');
					error++;
				}
				if( $(e).data('match') == 'カタカナ' && !$(e).val().match( /^[\u30a0-\u30ff]+$/ ) )
				{
					$(e).parent().after('<p class="error_text">' + $(e).data('match') + 'で入力してください</p>');
					error++;
				}
				if( $(e).data('match') == '数字' && !$(e).val().match( /^([1-9]\d*|0)$/ ) )
				{
					$(e).parent().after('<p class="error_text">' + $(e).data('match') + 'で入力してください</p>');
					error++;
				}
				if( $(e).data('match') == '電話番号' && !$(e).val().match( /^([0-9]\d*|0)$/ ) )
				{
					$(e).parent().after('<p class="error_text">' + $(e).data('match') + 'で入力してください</p>');
					error++;
				}
			}
			
			
		}
		
		//FILE
		if( $(e).val() == '' && $(e).attr('type') == 'file' )
		{
			$(e).parent().addClass('error').after('<p class="error_text mt-3 h13">' + $(e).data('placeholder') + 'が選択されていません</p>');
			error++;
		}
		
		//SELECT
		if( $(e).find('select').val() == '' && $(e).attr('type') == 'select' )
		{
			$(e).addClass('error').after('<p class="error_text mt-3 h13">' + $(e).data('placeholder') + 'が選択されていません</p>');
			error++;
		}
		//textarea
		if( $(e).val() == '' && $(e).attr('type') == 'textarea' )
		{
			$(e).addClass('error').after('<p class="error_text mt-3 h13">' + $(e).data('placeholder') + 'が入力されていません</p>');
			error++;
		}
		
		//CHECKBOX	requirednumberで最低個数を指定可能
		if( $(e).data('type') !== undefined && $(e).data('type') == 'checkbox' )
		{
			$chk = 0;
			$(e).find('input[type="checkbox"]').each( function( index, element ) {
				if( $(element).prop('checked') ){$chk++;}
			});
			if( $(e).data('requirednumber') !== undefined ){$requirednumber = $(e).data('requirednumber');}else{$requirednumber = 1;}
			if( $chk < $requirednumber ){
				$(e).after('<p class="error_text">' + $(e).data('placeholder') + 'が' + $requirednumber + 'つ以上選択されていません</p>');
				error++;
			}
		}
		
		//RADIO
		if( $(e).data('type') !== undefined && $(e).data('type') == 'radio' )
		{
			$chk = 0;
			$(e).find('input[type="radio"]').each( function( index, element ) {
				if( $(element).prop('checked') ){$chk++;}
			});
			if( $chk == 0 ){
				$(e).after('<p class="error_text">' + $(e).data('placeholder') + 'が選択されていません</p>');
				error++;
			}
		}
	});
	return error;
}


/*
	住所取得
*/
$('.zip').on('keyup',function(e){
	setState( $('.csrf_token').val() , $(this).val() , $('#add1') , $('.root').val() );
});
function setState($csrf_token,$zip,$add,$root)
{
	if( $zip.length != 7 )
	{
		return false;
	}
	if( !$zip.match(/^[0-9]+$/) )
	{
		return false;
	}
	
	$.ajax({
		url: $root + "php/api.php",
		type: 'POST',
		data : {
			zip : $zip,
			endpoint : 'state',
			csrf_token : $csrf_token
		}
	})
	.done(function( data, textStatus, jqXHR ) {
		result = JSON.parse(data);
		
		if( result['state'] == 200 )
		{
			$add.val(result['tdfk_name'] + result['address1'] + result['address2']);
		}
	})
	.fail(function( jqXHR, textStatus, errorThrown ) {
	});
	
	return true;
}
