$(function(){
	$('.text')
		// フォーカスしたときに枠線を表示
		.focus(function() {
			$(this).addClass('focus');
			var x = $(this).offset().top-120;
			var t = $(this).attr('title');
			$("#hint")
				.css('top',x)
				.find('p').text(t);
			$('#hint').fadeIn();
		})
		// フォーカスが外れたとき枠線を非表示・エラーがあれば表示
		.blur(function() {
			$(this).removeClass('focus');
			$('#hint').hide();
			if($(this).val()=='') {
				$(this).parent().find('.error').slideDown(); 
			} else {
				$(this).parent().find('.error').slideUp(); 
			}
		});
	// 同意するチェック
	$('#cb_privacy').change(function() {
		if($(this).attr('checked')==false) {
			$('#err_privacy').slideDown();
		} else {
			$('#err_privacy').slideUp();
		}
	});
	// 送信時のチェック
	$('#btn_submit').click(function() {
		// 未入力カウント用変数
		var count = 0;
		$('.error',$('#form_contact')).each( function() {
			if($(this).parent().find('input').val()==''||$(this).parent().find('textarea').val()=='') {
				// 必須で未入力があればカウントをインクリメントしてエラーを表示
				count++;
				$(this).parent().find('.error').slideDown();
			}
		});
		if(count>0) {
			alert('未入力項目があります');
			var target = $('#form_contact');
			var sclpos = 30;
			var scldurat = 1200;
			var targetOffset = target.offset().top - sclpos;
			$('html,body')
			.animate({scrollTop: targetOffset}, {duration: scldurat, easing: "backout"});
			return false;
		// 同意するチェック
		} else if($('#cb_privacy').attr('checked')==false) {
			$('#err_privacy').slideDown();
			return false;
		} else {
		// 項目がすべて埋まっていればエラーを非表示にして送信処理
			$('.error').slideUp();
			$('#form_contact').submit();
		}
	});
	$('#btn_send').click(function(){
		$(this).parent().parent('form').submit();
	});
	$('#btn_back').click(function(){
		history.go(-1);
		return false;
	});
	$('#text_company').focus();
});
