//<![CDATA[

function today_disable_btn() {

	var selected_yearNameNode = document.getElementById("disp_year");
	var selected_yearTextNode = selected_yearNameNode.firstChild;
	var selected_year = selected_yearTextNode.nodeValue;

	var selected_monthNameNode = document.getElementById("disp_month");
	var selected_monthTextNode = selected_monthNameNode.firstChild;
	var selected_month = selected_monthTextNode.nodeValue - 0;

	var selected_dayNameNode = document.getElementById("disp_day");
	var selected_dayTextNode = selected_dayNameNode.firstChild;
	var selected_day = selected_dayTextNode.nodeValue - 0;

	var selected_date = selected_year + "/" + selected_month + "/" + selected_day;

	today_date = new Date();
	var y = today_date.getFullYear();
	var m = today_date.getMonth() + 1;
	var d = today_date.getDate();
	today_date_f = y + "/" + m + "/" + d;

	if (today_date_f == selected_date) {
		document.tmp_book.button.disabled = true;
	} else {
		document.tmp_book.button.disabled = false;
	}

}

function getDay3(evt) {

	/*	イベントターゲットとなる要素を特定	*/
	var targetElm = dom.event.target(evt);
	var is_dd = targetElm.id.match(/dd/);
	if (is_dd == "dd") {
		var s_year = document.getElementById("year_list").value;
		var s_month = document.getElementById("month_list").value.replace("\\\"", "").replace("\\\"", "");
		var s_day = targetElm.id.replace("dd", "");
		document.getElementById("disp_day").innerHTML = s_day - 0;

		var url_3 = "admin/php/refresh_3timetbls.php";
		var tt_data_by_post = "year=" + s_year + "&month=" + s_month + "&day=" + s_day + "&sc=" + shisetsu_code;
		var url_by_post = url_3;

		if (document.uniqueID ) {
			xmlhttp_refttbl = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			xmlhttp_refttbl = new XMLHttpRequest();
		}

		xmlhttp_refttbl.onreadystatechange = httpHandler_refttbl;
		xmlhttp_refttbl.open("POST", url_by_post);
		xmlhttp_refttbl.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
		xmlhttp_refttbl.send(tt_data_by_post);
	}
}

function httpHandler_refttbl(){
	if(xmlhttp_refttbl.readyState == 4 && xmlhttp_refttbl.status== 200) {
		document.getElementById("t_tbl").innerHTML = xmlhttp_refttbl.responseText;
	}
	today_disable_btn();
}

function refreshCal() {
	var selected_year = document.getElementById("year_list").value;
	var selected_month = document.getElementById("month_list").value;

	var url_2 = "admin/php/refresh_cal.php";
	var ym_data_by_post = "year=" + selected_year + "&month=" + selected_month;
	var url_by_post = url_2;

	if (document.uniqueID ) {
		xmlhttp_refcal = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlhttp_refcal = new XMLHttpRequest();
	}
	xmlhttp_refcal.onreadystatechange = httpHandler_refcal;
	xmlhttp_refcal.open("POST", url_by_post);
	xmlhttp_refcal.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	xmlhttp_refcal.send(ym_data_by_post);

}

function httpHandler_refcal(){
	if(xmlhttp_refcal.readyState == 4 && xmlhttp_refcal.status== 200) {
		document.getElementById("calendar").innerHTML = xmlhttp_refcal.responseText;
//		document.getElementById("cal_tbl").innerHTML = xmlhttp_refcal.responseText;
	}
}

function old_chgStarthour_old() {
	var startHour_value = document.getElementById("start_hour_list").value;
	var startMinute_value = document.getElementById("start_minute_list").value;

	var endHour_index = startHour_value - 0;		// -0 をすることで数値型へキャストしている
	endHour_index = endHour_index + min_hour;

	var endHour = "";
	endHour += "<select name=\"end_hour\" id=\"end_hour_list\">";
	for (var i = endHour_index; i <= closing_hour; i++) {
		endHour += "<option value=\"" + i + "\">" + i + "</option>\"";
	}
	endHour += "</select> 時";

	document.getElementById("end_hour").innerHTML = endHour;

}

function chgStarthour() {
//	alert("Hello 0");

	var select_room_flag = 0;
	if (document.getElementById("select_room_a").checked == true) {select_room_flag ++;}
	if (document.getElementById("select_room_b").checked == true) {select_room_flag ++;}
	if (document.getElementById("select_room_c").checked == true) {select_room_flag ++;}

	if (select_room_flag == 1) {
		chgEndhour(3);

	} else if (select_room_flag == 2) {
		chgEndhour(1);

	} else if (select_room_flag == 3) {
		chgEndhour(1);

	} else {
		chgEndhour(3);

	}

}

function chgEndhour(diff) {
	var startHour_value = document.getElementById("start_hour_list").value;
	var startMinute_value = document.getElementById("start_minute_list").value;

	var endHour_index = startHour_value - 0;		// -0 をすることで数値型へキャストしている
	endHour_index = endHour_index + diff;

	var endHour = "";
	endHour += "<select name=\"end_hour\" id=\"end_hour_list\">";
	for (var i = endHour_index; i <= closing_hour; i++) {
		endHour += "<option value=\"" + i + "\">" + i + "</option>\"";
	}
	endHour += "</select> 時";

	document.getElementById("end_hour").innerHTML = endHour;

}

function expandStarthour(diff) {
	// 開始時刻リストの選択時刻を変更する
	
	var startHour_index = opening_hour - 0;
	
	var startHour = "";
	startHour += "<select name=\"start_hour\" id=\"start_hour_list\" onchange=\"chgStarthour()\">";
	for (var i = startHour_index; i <= closing_hour - diff; i++) {
		startHour += "<option value=\"" + i + "\">" + i + "</option>\"";
	}
	startHour += "</select> 時";

	document.getElementById("start_hour").innerHTML = startHour;

}

function chgTimelist() {
//	alert ("hello. chk_room was changed.");

	var select_room_flag = 0;
	if (document.getElementById("select_room_a").checked == true) { select_room_flag ++; }
	if (document.getElementById("select_room_b").checked == true) { select_room_flag ++; }
	if (document.getElementById("select_room_c").checked == true) { select_room_flag ++; }

	var start_hour = document.getElementById("start_hour_list").value;
	var start_minute = document.getElementById("start_minute_list").value;
	var end_hour = document.getElementById("end_hour_list").value;
	var end_minute = document.getElementById("end_minute_list").value;
//	alert ("from " + start_hour + ":" + start_minute + " to " + end_hour + ":" + end_minute);

	if (select_room_flag == 1) {
		expandStarthour(3);
//		alert ("expandStarthour_3");
		chgEndhour(3);

	} else if (select_room_flag == 2) {
		expandStarthour(2);
//		alert ("expandStarthour_2");
		chgEndhour(1);

	} else if (select_room_flag == 3) {
		expandStarthour(1);
//		alert ("expandStarthour_1");
		chgEndhour(1);

	}
}

function setListeners(e) {
	var eSource2 = document.getElementById("calendar");
	addListener(eSource2, "click", getDay3, false);
}

function send_data() {
	var s_year = document.getElementById("year_list").value;
	var s_month = document.getElementById("month_list").value.replace("\\\"", "").replace("\\\"", "");
	var dayNode = document.getElementById("disp_day");
	var dayTextNode = dayNode.firstChild;
	var s_day = dayTextNode.nodeValue;

	var select_room = "";
	var select_room_flag = 0;

	if (document.getElementById("select_room_a").checked == true) {
	//	alert ("room a is checked");
		select_room += "a";
		select_room_flag ++;
	}

	if (document.getElementById("select_room_b").checked == true) {
	//	alert ("room b is checked");
		select_room += "b";
		select_room_flag ++;
	}

	if (document.getElementById("select_room_c").checked == true) {
	//	alert ("room c is checked");
		select_room += "c";
		select_room_flag ++;
	}
	
	var start_hour = document.getElementById("start_hour_list").value;
	var start_minute = document.getElementById("start_minute_list").value;
	var end_hour = document.getElementById("end_hour_list").value;
	var end_minute = document.getElementById("end_minute_list").value;
	var total_time = (end_hour - start_hour) + (end_minute - start_minute) / 60;

	if (select_room_flag > 0) {
		if (select_room_flag == 1) {
			// ３時間以上の利用が必要
			if (total_time >= 3) {
				// 先へ進める
				document.tmp_book.booking_y.value = document.getElementById("year_list").value;
				document.tmp_book.booking_m.value = document.getElementById("month_list").value.replace("\\\"", "").replace("\\\"", "");
				document.tmp_book.booking_d.value = s_day;
				document.tmp_book.shisetsu_c.value = shisetsu_code;
				document.tmp_book.room_list = select_room;
				document.tmp_book.submit();
				return true;

			} else {
				alert ("１室でのご利用は、３時間以上となります。");
			}

		} else if (select_room_flag == 2) {
			// 1.5時間以上の利用が必要
			if (total_time >= 1.5) {
				// 先へ進める
				document.tmp_book.booking_y.value = document.getElementById("year_list").value;
				document.tmp_book.booking_m.value = document.getElementById("month_list").value.replace("\\\"", "").replace("\\\"", "");
				document.tmp_book.booking_d.value = s_day;
				document.tmp_book.shisetsu_c.value = shisetsu_code;
				document.tmp_book.room_list = select_room;
				document.tmp_book.submit();
				return true;

			} else {
				alert ("２室でのご利用は、１時間30分以上となります。");
			}

		} else if (select_room_flag == 3) {
			// １時間以上の利用が必要
			if (total_time >= 1) {
				// 先へ進める
				document.tmp_book.booking_y.value = document.getElementById("year_list").value;
				document.tmp_book.booking_m.value = document.getElementById("month_list").value.replace("\\\"", "").replace("\\\"", "");
				document.tmp_book.booking_d.value = s_day;
				document.tmp_book.shisetsu_c.value = shisetsu_code;
				document.tmp_book.room_list = select_room;
				document.tmp_book.submit();
				return true;

			} else {
				alert ("３室でのご利用は、１時間以上となります。");
			}

		}

	} else if (select_room_flag == 0) {
		alert ("ご希望のお部屋を選択してください。");
	}

}

function check_form4() {
	var s_year = document.getElementById("year_list").value;
	var s_month = document.getElementById("month_list").value.replace("\\\"", "").replace("\\\"", "");
	var dayNode = document.getElementById("disp_day");
	var dayTextNode = dayNode.firstChild;
	var s_day = dayTextNode.nodeValue;

	alert ("selected year is " + s_year);
	alert ("selected month is " + s_month);
	alert ("selected day is " + s_day);

	var url_4 = "tmp_book.htm";
	var submit_data_by_post = "year=" + s_year + "&month=" + s_month + "&day=" + s_day + "&sc=" + shisetsu_code + "&rc=" + room_code;
	var url_by_post = url_4;

	if (document.uniqueID ) {
		xmlhttp_submit = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlhttp_submit = new XMLHttpRequest();
	}

	xmlhttp_submit.onreadystatechange = httpHandler_submit;
	xmlhttp_submit.open("POST", url_by_post);
	xmlhttp_submit.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	xmlhttp_submit.send(submit_data_by_post);

}

function initialize() {
	setListeners();
//	checkRoomcode();
}

addListener(window, "load", setListeners, false);
//]]>
