// JavaScript Document


window.onload = function() {
	initialize();
	dispReason();
	dispMeetingTime();
	cal();
	calAdult();
	setEvent();
	addTarget();
}



/* ----------------------------------------------------------------------
 2. class共通化イベントリスナユーティリティ
---------------------------------------------------------------------- */

 // getAttribute / setAttribute で使うclassを統一
if(document.documentElement.getAttribute("style") == document.documentElement.style) {
	var unityClass = "className";
}
else {
	var unityClass = "class";
}


/* ----------------------------------------------------------------------
 . 初期設定
---------------------------------------------------------------------- */

// ご利用人数のテキストフィールドのid（合計は除く）
var guestType = ["mail_man","mail_woman","mail_children1","mail_children2","mail_children3","mail_children4","mail_baby"];


/* ----------------------------------------------------------------------
 . ページの初期化
---------------------------------------------------------------------- */

function initialize() {

/*
	dispReason();

	document.getElementById("title_freedrinks").style.display = "none";
	document.getElementById("data_freedrinks").style.display = "none";
*/

}


/* ----------------------------------------------------------------------
 . イベントの設定
---------------------------------------------------------------------- */

function setEvent() {
	
	for(var i=0, len=guestType.length; i<len; i++) {
		document.getElementById(guestType[i]).onkeyup = function() {
			cal();
			calAdult();
		}
	}

	var plans = document.mail_form.mail_plan;
	for(var i=0, len=plans.length; i<len; i++) {
		plans[i].onclick = function() {
			dispReason();
		}
	}

	document.getElementById("mail_meeting1").onclick = function() {
		dispMeetingTime();
	}
	document.getElementById("mail_meeting2").onclick = function() {
		dispMeetingTime();
	}

	document.getElementById("mail_form").onsubmit = function () {
		return Check(this);
	}

	document.getElementById("mail_approval").onclick = function () {
		if(document.getElementById("mail_approval").checked) {
			document.getElementById("mail_preview").disabled = false;
		}
		else{
			document.getElementById("mail_preview").disabled = true;
		}
	}

	document.getElementById("mail_edit").onclick = function () {
		changeViewBlock();
	}

	if(document.getElementById("mail_cancel")) {
		document.getElementById("mail_cancel").onclick = function() {
			var action = confirm("申し込みを中止します！");
			if(action ) {
				window.location.href = "http://www.furutakiya.com/plan/group/";
			}
			else {
				return false;
			}
		}
	}

}


/* ----------------------------------------------------------------------
 . 表示切り替え
---------------------------------------------------------------------- */

function dispReason() {
	if(!document.getElementById("mail_planOther").checked) {
		document.getElementById("title_reason").style.display = "none";
		document.getElementById("data_reason").style.display = "none";
		document.getElementById("mail_reason").value = "";
	}
	else {
		document.getElementById("title_reason").style.display = "block";
		document.getElementById("data_reason").style.display = "block";
	}
}

function dispMeetingTime() {
	if(!document.getElementById("mail_meeting2").checked) {
		document.getElementById("meetingTime").style.display = "none";
		document.getElementById("mail_meeting_time").value = "";
	}
	else {
		document.getElementById("meetingTime").style.display = "block";
	}
}

/* ----------------------------------------------------------------------
 . ご利用人数の合計の計算・表示
---------------------------------------------------------------------- */

function cal() {

	var totalGuest = 0;
	for(var i=0, len=guestType.length; i<len; i++) {
		if(document.getElementById(guestType[i]).value == "") {
			document.getElementById(guestType[i]).value = 0;
		}
		totalGuest += parseFloat(document.getElementById(guestType[i]).value);
	}
	document.getElementById("mail_total").value = totalGuest;

}


function calAdult() {

	var adultGuest = 0;

	for(var i=0; i<2; i++) {
		if(document.getElementById(guestType[i]).value == "") {
			document.getElementById(guestType[i]).value = 0;
		}
		adultGuest += parseFloat(document.getElementById(guestType[i]).value);
	}

	var freeDrinks = document.mail_form.mail_freedrink;

	if(adultGuest < 6) {
		for(var i=1, len=freeDrinks.length; i<len; i++) {
			freeDrinks[i].checked = false;
		}
		document.getElementById("title_freedrinks").style.display = "none";
		document.getElementById("data_freedrinks").style.display = "none";
	}
	else {
		document.getElementById("title_freedrinks").style.display = "block";
		document.getElementById("data_freedrinks").style.display = "block";
		if(adultGuest < 10) {
//			document.getElementById("mail_freedrink1").checked = false;
			document.getElementById("mail_freedrink2").checked = false;
			document.getElementById("mail_freedrink4").checked = false;
			document.getElementById("freedrink2").style.display = "none";
			document.getElementById("freedrink4").style.display = "none";
		}
		else {
			document.getElementById("freedrink2").style.display = "block";
			document.getElementById("freedrink4").style.display = "block";
		}
	}

}


/* ----------------------------------------------------------------------
 . 確認ページの表示の切り替え
---------------------------------------------------------------------- */

function changeViewBlock() {

	document.getElementById("resultDatas").style.display = "none";
	document.getElementById("main-datas").style.display = "block";
	document.getElementById("user-datas").style.display = "block";
//	document.getElementById("user-question").style.display = "block";
	document.getElementById("message-check").style.display = "none";
	document.getElementById("message-edit").style.display = "block";
	document.getElementById("pp_approval").style.display = "block";

	document.getElementById("checkBtn").style.display = "inline";
	document.getElementById("editBtn").style.display = "none";
	document.getElementById("submitBtn").style.display = "none";
	document.getElementById("resetBtn").style.display = "inline";

}


/* ----------------------------------------------------------------------
 . 
---------------------------------------------------------------------- */

function addTarget() {

		var targetNodes = document.getElementById("form-area").getElementsByTagName('a');

		for(var i=0, len=targetNodes.length; i<len; i++) {
			var targetClass = targetNodes[i].getAttribute(unityClass);
			if(targetClass.match("lightwindow")) {
//				targetNodes[i].removeAttribute(unityClass);
//				targetNodes[i].setAttribute("className",other);
//				targetNodes[i].target = "_blank";
				targetNodes[i].setAttribute("target","detail");
			}
		}

}


/* ----------------------------------------------------------------------
 . 「form」の入力内容をチェック
---------------------------------------------------------------------- */

function Check(FmName){

	if ( !checkDate2 ( FmName.mail_date, "「ご利用予定日」が入力されていません！", "「ご利用予定日」の形式が正しくありません！\n「yyyy/mm/dd」の形式で入力してください。", "" ) ) { return false; }
//	if ( !checkNum ( FmName.mail_term, "「ご利用日数」が入力されていません！", "「ご利用日数」の形式が正しくありません！\n「半角の数値」を入力してください。", "" ) ) { return false; }
	if(FmName.mail_term.value < 1) {
		alert("「ご利用日数」には、1以上の数値を指定してください！");
		FmName.mail_term.focus();
		return false;
	}
	if(document.getElementById("mail_total").value == 0) {
		alert("ご利用人数が入力されていません！");
		document.getElementById("mail_man").focus();
		return false;
	}
	if ( !checkEmpNum ( FmName.mail_rooms,  "「お部屋数」の形式が正しくありません！\n「半角の数値」を入力してください。", "" ) ) { return false; }
	if ( !checkRadio ( FmName.mail_plan, "「プランの選択」が選択されていません！", "" ) ) { return false; }
	if(document.getElementById("mail_planOther").checked) {
		if ( !checkNull ( FmName.mail_reason, "「プランの選択」で「その他」を選択された場合、「利用目的」を入力してください！", "" ) ) { return false; }
	}
	if ( !checkNull ( FmName.mail_author, "「お名前」が入力されていません！", "" ) ) { return false; }
	if ( !checkNull ( FmName.mail_authorKana, "「お名前のふりがな」が入力されていません！", "" ) ) { return false; }
	if ( !checkZipCd ( FmName.mail_zipcode, "「郵便番号」が入力されていません！", "「郵便番号」の形式が正しくありません！\n「972-8321」の形式で入力してください。", "" ) ) { return false; }
	if ( !checkSelect ( FmName.mail_ken, "「都道府県」が選択されていません！" ) ) { return false; }
	if ( !checkNull ( FmName.mail_address1, "「市区町村郡/番地 」が入力されていません！", "" ) ) { return false; }
	if ( !checkMail ( FmName.mail_email, "「メールアドレス」が入力されていません！", "「メールアドレス」の形式が正しくありません！\n「example@kondodenki.com」の形式で入力してください。", "" ) ) { return false; }
	if(FmName.mail_telHome.value == "" && FmName.mail_faxHome.value == "" && FmName.mail_telMobile.value == "" && FmName.mail_telOffice.value == "") {
		alert("「TEL（ご自宅）」・「FAX（ご自宅）」・「携帯電話」・「TEL（勤務先）」のうちどれか一つは必ずご入力ください！");
		FmName.mail_telHome.focus();
		return false;
	}
	if ( !checkEmpPhone ( FmName.mail_telHome, "「TEL（ご自宅」の形式が正しくありません！\n「03-1234-5678」の形式で入力してください。", "" ) ) { return false; }
	if ( !checkEmpPhone ( FmName.mail_faxHome, "「FAX（ご自宅）」の形式が正しくありません！\n「03-1234-5678」の形式で入力してください。", "" ) ) { return false; }
	if ( !checkEmpMPhone ( FmName.mail_telMobile, "「携帯」の形式が正しくありません！\n「090-1234-5678」の形式で入力してください。", "" ) ) { return false; }
	if ( !checkEmpPhone ( FmName.mail_telOffice, "「TEL（勤務先）」の形式が正しくありません！\n「03-1234-5678」の形式で入力してください。", "" ) ) { return false; }
//	if ( !checkCheckbox ( FmName.mail_approval, "「 個人情報保護方針」にご同意いただく必要があります！" ) ) { return false; }
	if(!document.getElementById("mail_approval").checked) {
		alert("「 個人情報保護方針」にご同意いただく必要があります！");
		document.getElementById("mail_approval").focus();
		return false;
	}

}



