// JavaScript Document

var childCountWindow = null;
var currentRoom = 1;

showPlanSearchForm();

function showPlanSearchForm() {
	document.write(
		 '			<div class="ReservDataBox2">'
		+'				<div class="w280Box">'
		+'					<div class="ReservMenu2"><img src="/img_e/reserv_ico04.gif" width="79" height="17">'
		+'						<img src="/img_e/reserv_ico08.gif" width="22" height="16">'
		+'						<select id="planSearchFormYear" onChange="onPlanSearchFormMonthChanged();	saveCurrentConditionToCookie();"></select>'
		+'						<img src="/img_e/reserv_ico09.gif" width="32" height="16">'
		+'						<select id="planSearchFormMonth" onChange="onPlanSearchFormMonthChanged(); saveCurrentConditionToCookie();"></select>'
		+'						<img src="/img_e/reserv_ico10.gif" width="22" height="16"></div>'
		+'						<select id="planSearchFormDay" onChange="saveCurrentConditionToCookie();"></select>'
		+'					<div class="DocTypeSwitch"></div>'
		+'					<div class="ReservMenu2"><img src="/img_e/reserv_ico12.gif" width="79" height="16">'
		+'						<select id="planSearchFormNight" onChange="saveCurrentConditionToCookie();"></select>'
		+'					</div>'
		+'					<div class="DocTypeSwitch"></div>'
		+'				</div>'
		+'			</div>'
		+'			<div class="ReservDataBox3">'
		+'				<div class="w280Box2">'

		+'					<div id="planSearchFormRooms"></div>'
		
		+'					<div class="ReservMenu2"><img src="/img_e/reserv_ico06.gif" width="79" height="16">'
		+'						<select id="planSearchFormRoom" onChange="planSearchFormRoomChanged(); saveCurrentConditionToCookie()"></select>'
		+'					</div>'
		+'					<div class="ReservMenu3"><img src="/img_e/reserv_ico07.jpg" width="50" height="20" border="0" onclick="saveCurrentConditionToCookie();location.href=\'/reserve/searchResult_e.html\'" style="cursor:pointer"></div>'

		+'				</div>'
		+'			</div>'
		+'			<div class="ReservDataBox4"><img src="/img/reserv_bg04b.jpg" width="340" height="5"></div>'
	);
	
	var today = new Date();
	for(var i = 0 ; i < 2 ; i++) {
		var year = new String(today.getFullYear() + i);
		document.getElementById("planSearchFormYear").options[i] = new Option(year.substr(year.length - 2), year);
	}
	var year = parseInt(readCookie('year'));
	if(!isNaN(year)) {
		document.getElementById("planSearchFormYear").value = year;
	}
	
	for(i = 0 ; i < 12 ; i++) {
		var month = new String(i + 1);
		document.getElementById("planSearchFormMonth").options[i] = new Option(month, month);
	}
	var month = parseInt(readCookie('month'));
	if(!isNaN(month)) {
		document.getElementById("planSearchFormMonth").value = month;
	} else {
		document.getElementById("planSearchFormMonth").selectedIndex = today.getMonth();
	}
	
	planSearchFormAdjustDay();

	var day = parseInt(readCookie('day'));
	if(isNaN(day)) {
		document.getElementById("planSearchFormDay").selectedIndex = today.getDate() - 1;
	} else {
		document.getElementById("planSearchFormDay").value = day;
	}
	
	for(i = 0 ; i < 14 ; i++) {
		document.getElementById("planSearchFormNight").options[i] = new Option(i + 1, i + 1);
	}
	
	/*for(i = 0 ; i < 5 ; i++) {
		document.getElementById("planSearchFormAdult1").options[i] = new Option(i + 1, i + 1);
	}
	document.getElementById("planSearchFormAdult1").selectedIndex = 1;*/
	
	for(i = 0 ; i < 3 ; i++) {
		document.getElementById("planSearchFormRoom").options[i] = new Option(i + 1, i + 1);
	}
	document.getElementById("planSearchFormRoom").value = getRoomCountFromCookie();
	
	planSearchFormRoomChanged();

	saveCurrentConditionToCookie();
}

function saveCurrentConditionToCookie() {
	writeCookie("year", document.getElementById("planSearchFormYear").value);
	writeCookie("month", document.getElementById("planSearchFormMonth").value);
	writeCookie("day", document.getElementById("planSearchFormDay").value);
	writeCookie("staynights", document.getElementById("planSearchFormNight").value);
	var roomNumber = 1
	for( ; roomNumber <= document.getElementById("planSearchFormRoom").value ; roomNumber++) {
		var adultCount = parseInt(document.getElementById("planSearchFormAdult" + roomNumber).value);
		writeCookie("guests_" + roomNumber + "_1" , adultCount == 0 ? 2 : adultCount);
	}
	for( ; roomNumber <= 3 ; roomNumber++) {
		for(var i = 1 ; i <= 7 ; i++) {
			writeCookie("guests_" + roomNumber + "_" + i, 0);
		}
	}
}

function getRoomCountFromCookie() {
	var roomCount = 1;
	for(var roomNumber = 2 ; roomNumber <= 3 ; roomNumber++) {
		if(parseInt(readCookie("guests_" + roomNumber + "_1")) > 0) {
			roomCount++;
		}
	}
	
	return roomCount;
}

function planSearchFormRoomChanged() {
	var html = "";
	for(var roomNumber = 1 ; roomNumber <= document.getElementById("planSearchFormRoom").value ; roomNumber++) {
		var adultOptions = "";
		var targetValue = parseInt(readCookie("guests_" + roomNumber + "_1"));
		if(targetValue == 0) {
			targetValue = 2;
			writeCookie("guests_" + roomNumber + "_1", targetValue);
		}
		
		for(var i = 1 ; i <= 5 ; i++) {
			adultOptions += '<option value="' + i + '" ' + (i == targetValue ? 'selected' : '') + '>' + i + '</option>'; 
		}

		html += 
			'		<div class="ReservMenu2"><img src="/img_e/reserv_ico14.gif" width="34" height="16">'
			+'			<select id="planSearchFormAdult' + roomNumber + '" onClick="saveCurrentConditionToCookie();">' + adultOptions + '</select>'
			+'			&nbsp;<img src="/img_e/reserv_ico15.gif" width="43" height="16">'
			+'			<font color="white"><span id="childrenRoom' + roomNumber + '">0</span></font>'
			+'			&nbsp;<a href="javascript:void(0);" onClick="currentRoom = ' + roomNumber + '; planSearchFormOpenKodomoWindow();"><img src="/img_e/reserv_ico17.gif" width="30" height="16" border="0"></a></div>'
			+'		<div class="DocTypeSwitch"></div>';
	}
	document.getElementById("planSearchFormRooms").innerHTML = html;
	
	refreshPersonCount();
}

function refreshPersonCount() {
	var roomNumber = 1;
	for( ; roomNumber <= document.getElementById("planSearchFormRoom").value ; roomNumber++) {
		var count = parseInt(readCookie("guests_" + roomNumber + "_1"));
		document.getElementById("planSearchFormAdult" + roomNumber).value = isNaN(count) || count == 0 ? 2 : parseInt(count);
		
		var totalChildCount = 0;
		for(var personTypeId = 2 ; personTypeId <= 7 ; personTypeId++) {
			count = parseInt(readCookie("guests_" + roomNumber + "_" + personTypeId));
			if(!isNaN(count)) {
				totalChildCount += count;
			}
		}
		document.getElementById("childrenRoom" + roomNumber).innerHTML = totalChildCount;
	}
}

function personCountChange(roomNumber, personTypeId, personCount)
{
	writeCookie("guests_" + roomNumber + "_" + personTypeId, personCount);
	
	if(personTypeId != 1)
	{
		document.getElementById("childrenRoom" + roomNumber).innerHTML = countChildren(currentRoom);
	}
	
	//onDataChanged(null, null);
}

function countChildren(roomNumber)
{
	var count = 0;
	for(var personTypeId = 2 ; personTypeId <= 7 ; personTypeId++)
	{
		var value = parseInt(readCookie("guests_" + roomNumber + "_" + personTypeId));
		if(!isNaN(value))
		{
			count += value;
		}
	}
	return count;
}

function planSearchFormOpenKodomoWindow() {
	childCountWindow = window.open('/reserve/kodomoPlanSearchForm_e.html', 'kodomo', 'width=400, height=500,resizable=yes');
}

function closeChildCountWindow()
{
	childCountWindow.close();
	childCountWindow = null
}

function onPlanSearchFormMonthChanged() {
	planSearchFormAdjustDay();
}

function planSearchFormAdjustDay() {
	var year = document.getElementById("planSearchFormYear").value;
	var month = document.getElementById("planSearchFormMonth").value;
	
	var sectionDay = document.getElementById("planSearchFormDay");
	var selectedDay = 0;
	if(sectionDay.options.length > 0) {
		selectedDay = sectionDay.value;
	}
	
	var targetDayCount = planSearchFormGetDaysInMonth(year, month);
	for(var i = sectionDay.length - 1 ; i >= targetDayCount ; i--) {
		sectionDay.options[i] = null;
	}
	
	for(i = sectionDay.length ; i < targetDayCount ; i++) {
		sectionDay.options[i] = new Option(i + 1, i + 1);
	}
	
	if(selectedDay > targetDayCount) {
		sectionDay.selectedIndex = targetDayCount - 1;
	}
}

function planSearchFormGetDaysInMonth(year, month) {
	// 次の月の1日
	var date = new Date(year, month - 1 + 1, 1);
	
	// 上記より一日引く
	date.setTime(date.getTime() - 24 * 60 * 60 * 1000);
	
	return date.getDate();
}

function planSearchFormNumberFormat(num) {
	var ret = "";
	var strNum = num.toString();
	
	for( ; strNum.length > 3 ; )
	{
		var cutLen = (strNum.length - 1) % 3 + 1;
		ret += strNum.substring(0, cutLen) + ",";
		strNum = strNum.substring(cutLen);
	}
	
	return ret + strNum;
}