// JavaScript Document

var childCountWindow = null;
var currentRoom = 1;

showPlanSearchFormSmall();

function showPlanSearchFormSmall() {
	document.write(
		 '			<div class="SubReservsearchBox">'
		+'				<div class="SubReservsearchData01c"><img src="img2/check_in.gif" width="80" height="16"></div>'
		+'				<div class="DocTypeSwitch"></div>'
		+'				<div class="SubReservsearchData01"><select id="planSearchFormYear" onChange="onPlanSearchFormMonthChanged();	saveCurrentConditionToCookie();"></select></div>'
		+'				<div class="SubReservsearchData01"><img src="img2/date01.gif" width="17" height="16"></div>'
		+'				<div class="SubReservsearchData01"><select id="planSearchFormMonth" onChange="onPlanSearchFormMonthChanged(); saveCurrentConditionToCookie();"></select></div>'
		+'				<div class="SubReservsearchData01"><img src="img2/date02.gif" width="17" height="16"></div>'
		+'				<div class="SubReservsearchData01"><select id="planSearchFormDay" onChange="saveCurrentConditionToCookie();"></select></div>'
		+'				<div class="SubReservsearchData01"><img src="img2/date03.gif" width="17" height="16"></div>'
		+'				<div class="DocTypeSwitch"></div>'
		+'				<div class="SubReservsearchData01"><img src="img2/nights.gif" width="68" height="16"></div>'
		+'				<div class="SubReservsearchData01"><select id="planSearchFormNight" onChange="saveCurrentConditionToCookie();"></select></div>'
		+'				<div class="SubReservsearchData01"><img src="img2/days.gif" width="17" height="16"></div>'
		+'				<div class="DocTypeSwitch"></div>'

		+'				<span id="planSearchFormRooms"></span>'


		+'			</div>'
		+'			<div class="SubReservsearchBox2">'

		+'				<div class="SubReservsearchData01"><img src="img2/rooms.gif" width="69" height="16"></div>'
		+'				<div class="SubReservsearchData01"><select id="planSearchFormRoom" onChange="planSearchFormRoomChanged(); saveCurrentConditionToCookie()"></select></div>'
		+'				<div class="SubReservsearchData01"><img src="img2/room.gif" width="34" height="16"></div>'
		+'				<div class="DocTypeSwitch"></div>'
		+'				<div class="SubReservsearchData02"><img src="img2/search.gif" width="50" height="20" border="0" onclick="saveCurrentConditionToCookie();location.href=\'/reserve/searchResult.html\'" style="cursor:pointer"></div>'
		+'			</div>'
		+'			<div><img src="img2/sub_reservsearch_under.jpg" width="200" height="5" border="0"></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="SubReservsearchData01b"><img src="img2/adult.gif" width="32" height="16">'
			+'	<select id="planSearchFormAdult' + roomNumber + '" onClick="saveCurrentConditionToCookie();">' + adultOptions + '</select>'
			+'	<img src="img2/people01.gif" width="17" height="16">'
			+'</div>'
			+'<div class="DocTypeSwitch"></div>'
			+'<div class="SubReservsearchData01"><img src="img2/child.gif" width="32" height="16"></div>'
			+'<div class="SubReservsearchData01"><span id="childrenRoom' + roomNumber + '">0</span></div>'
			+'<div class="SubReservsearchData01"><img src="img2/people01.gif" width="17" height="16"></div>'
			+'<div class="SubReservsearchData01"><a href="javascript:void(0);" onClick="currentRoom = ' + roomNumber + '; planSearchFormOpenKodomoWindow();"><img src="img2/selection.gif" width="70" 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.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;
}