﻿var httphrefShort = location.href;
httphrefShort = httphrefShort.replace("http://", "");
var arrhttphrefShort = httphrefShort.split("/");
httphrefShort = "http://" + arrhttphrefShort[0];

var pathwebcheckavailabilityShort = httphrefShort + '/reserve/indexShort.php';
var ckAvailStartTdyShort = new Date();
var ckAvailStartTdyDateShift = 1;
//remove time from Date
ckAvailStartTdyShort = new Date(ckAvailStartTdyShort.getFullYear(),  ckAvailStartTdyShort.getMonth(), ckAvailStartTdyShort.getDate());
var ckAvailStartDateShort = new Date(ckAvailStartTdyShort.getFullYear(),  ckAvailStartTdyShort.getMonth(), ckAvailStartTdyShort.getDate() + ckAvailStartTdyDateShift);

function getElcheckavailabilityShort(tmpname) {
	return document.forms['checkAvailQuick'].elements[tmpname];
}

function shortAddDate(cDate, addDay)
{
	return new Date(cDate.getFullYear(),  cDate.getMonth(), cDate.getDate() + addDay);
}

function showChangeDateShort(values) { 
	if(values=='in') {
		setAllDate(getElcheckavailabilityShort('txtCheck_InShort').value,'in');
	}
	else 
	if (values=='out')
	{
		setAllDate(getElcheckavailabilityShort('txtCheck_OutShort').value,'out');
	}
}

function setAllDate(cDate,InOut) {
	if (InOut=='in')
	{
		var startDate = ChStrtoDate(cDate);
		var endDate = ChStrtoDate(getElcheckavailabilityShort('txtCheck_OutShort').value);
		if(startDate<ckAvailStartTdyShort) startDate = ckAvailStartTdyShort;
		if(startDate>=endDate) endDate = shortAddDate(startDate, ckAvailStartTdyDateShift);
	}
	else 
	if (InOut=='out')
	{
		var startDate = ChStrtoDate(getElcheckavailabilityShort('txtCheck_InShort').value);
		var endDate = ChStrtoDate(cDate);
		if(startDate<ckAvailStartTdyShort) startDate = ckAvailStartTdyShort;
		if(startDate>=endDate) endDate = shortAddDate(startDate, ckAvailStartTdyDateShift);
	}
	else
	{
		var startDate = ckAvailStartTdyShort;
		var endDate = ckAvailStartDateShort;
	}

	var Nights = Math.floor((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24)); 
	getElcheckavailabilityShort('checkHotel[Nights]').value = Nights;
	
	getElcheckavailabilityShort('txtCheck_InShort').value = ChDatetoStr(startDate);
	getElcheckavailabilityShort('txtCheck_OutShort').value = ChDatetoStr(endDate);
	
	var arrtmpDate = ChDatetoStr(startDate).split("/");
	var b = getElcheckavailabilityShort('checkHotel[sMonth2]');
	setElementShort(b, arrtmpDate[1]+'/'+arrtmpDate[2]);
	var b = getElcheckavailabilityShort('checkHotel[sYear]');
	b.value = arrtmpDate[2];
	var b = getElcheckavailabilityShort('checkHotel[sMonth]');
	b.value = arrtmpDate[1];
	var b = getElcheckavailabilityShort('checkHotel[sDay]');
	setElementShort(b, arrtmpDate[0]);

	var arrtmpDate = ChDatetoStr(endDate).split("/");
	var b = getElcheckavailabilityShort('checkHotel[eMonth2]');
	setElementShort(b, arrtmpDate[1]+'/'+arrtmpDate[2]);
	var b = getElcheckavailabilityShort('checkHotel[eYear]');
	b.value = arrtmpDate[2];
	var b = getElcheckavailabilityShort('checkHotel[eMonth]');
	b.value = arrtmpDate[1];
	var b = getElcheckavailabilityShort('checkHotel[eDay]');
	setElementShort(b, arrtmpDate[0]);
}

function createlistYearShort() {
	var a = getElcheckavailabilityShort('checkHotel[sMonth2]');
	a.length = 0;
	createListInOptions(ckAvailStartTdyShort, a);

	var b = getElcheckavailabilityShort('checkHotel[eMonth2]');
	b.length = 0;
	createListInOptions(ckAvailStartDateShort, b);
	
	getElcheckavailabilityShort('checkHotel[Nights]').value = ckAvailStartTdyDateShift;
}

function createListInOptions(tmptoday, obj)
{
	var sMonth = tmptoday.getMonth() + 1;
	var sYear = tmptoday.getFullYear();
	var i = 0;
	var arrmonth;

	var lang = getElcheckavailabilityShort('checkHotel[LanguageCode]').value;
	if(lang=="ja") arrmonth = new Array("", "1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
	else arrmonth = new Array("", "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	for(i=0;i<=17;i++) {
		sYearShow = String(sYear).substring(2,4);
		obj.options[i]=new Option(arrmonth[sMonth] +' ' + sYearShow, padZero(sMonth)+'/'+sYear);
		sMonth = sMonth + 1;
		if(sMonth>=13) {
			sMonth = 1;
			sYear = sYear + 1;
		}
	}
}

var ddd = 0;
function setDefDateShort() {
	ddd = ddd + 1;
	getElcheckavailabilityShort('txtCheck_InShort').value = ChDatetoStr(ckAvailStartTdyShort);
	getElcheckavailabilityShort('txtCheck_OutShort').value = ChDatetoStr(ckAvailStartDateShort);

	setAllDate(ChDatetoStr(ckAvailStartTdyShort), 'in');
	setAllDate(ChDatetoStr(ckAvailStartDateShort), 'out');
}

function ChDatetoStr(cDate) {
	return padZero(cDate.getDate()) + '/' + padZero(cDate.getMonth() + 1) + '/' + cDate.getFullYear();
}

function ChStrtoDate(cDate) {
	var arrcDate = cDate.split("/");
	return new Date(arrcDate[2], Number(arrcDate[1])-1, Number(arrcDate[0]));
}

function setElementShort(cElement,cValue)
{
	for(var i=0;i<cElement.length;i++)
	{
		if(cElement.options[i].value==cValue)
		{
			cElement.selectedIndex = i; 
			break;
		}
	}
	if(i==cElement.length) {
		if(ddd<=2) setDefDateShort();
		return false;
	}
}

function updateCheckAvailAllShort(column, frommonth)
{
	if(column=='sDate')
	{
		var cElement = getElcheckavailabilityShort('checkHotel[sDay]');
		var sDay = cElement[cElement.selectedIndex].value;
		var cElement = getElcheckavailabilityShort('checkHotel[sMonth2]');
		var tmpMonth = cElement[cElement.selectedIndex].value;
		setAllDate(sDay +'/' + tmpMonth, 'in');
	}
	else 
	if(column=='eDate') 
	{
		var cElement = getElcheckavailabilityShort('checkHotel[eDay]');
		var sDay = cElement[cElement.selectedIndex].value;
		var cElement = getElcheckavailabilityShort('checkHotel[eMonth2]');
		var tmpMonth = cElement[cElement.selectedIndex].value;
		setAllDate(sDay +'/' + tmpMonth, 'out');
	}
}

function padZero(num) {
	return (num< 10) ? '0' + num : num ;
}

