//===================================================================
//
//	***  All common JavaScript  ***
//
//===================================================================

//-----------------------------------------------------
// -reservation Tab
//-----------------------------------------------------

var tab = {
init: function(){
var tabs = this.setup.tabs;
var pages = this.setup.pages;
 
for(i=0; i<pages.length; i++) {
if(i !== 0) pages[i].style.display = 'none';
tabs[i].onclick = function(){ tab.showpage(this); return false; };
}
},
showpage: function(obj){
var tabs = this.setup.tabs;
var pages = this.setup.pages;
var num;
for(num=0; num<tabs.length; num++) {
if(tabs[num] === obj) break;
}
for(var i=0; i<pages.length; i++) {
if(i == num) {
pages[num].style.display = 'block';
tabs[num].className = 'selected';
}
else{
pages[i].style.display = 'none';
tabs[i].className = null;
}
}
changeBoxSize();
}
}



//-----------------------------------------------------
// -open window
//-----------------------------------------------------

	function open_popup(obj) {
		openWin=window.open(obj.href,"layout_popup","width=667,height=650,scrollbars=yes,toolbar=no");
		openWin.focus();
	}



//-----------------------------------------------------
// -focus parent
//-----------------------------------------------------

	function focus_parent() {
		window.opener.focus();
	}

	function parent_locate(url) {
		if (window.opener!=undefined) {
			window.opener.location.href=url;
		} else {
			window.open(url,"",'resizable=yes,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes');
		}
		focus_parent()
	}



//-----------------------------------------------------
// -easy getelementbyid
//-----------------------------------------------------

	function $$(id) {
		e = document.getElementById(id);
		return e;
	}



//-----------------------------------------------------
// -init history back
//-----------------------------------------------------

	function init_history_back() {
		var e = document.createElement('a'), f = document.createElement('li');
		e.href = "javascript:history.back(-1)";
		e.innerHTML = "前のページへ戻る";
		f.className = "prev";
		f.appendChild(e);
		$$('footerNavigationList').appendChild(f);
	}



//-----------------------------------------------------
// -init history back (en)
//-----------------------------------------------------

	function init_history_back_en() {
		var e = document.createElement('a'), f = document.createElement('li');
		e.href = "javascript:history.back(-1)";
		e.innerHTML = "Previous Page";
		f.className = "prev";
		f.appendChild(e);
		$$('footerNavigationList').appendChild(f);
	}



//-----------------------------------------------------
// -attach actions when open HTML
//-----------------------------------------------------
function set() {
	var img = document.getElementsByTagName('img');
	for ( var i = 0, imgLength = img.length ; i < imgLength ; i ++ ) {
			var cn = img[i].className.toLowerCase().split(' ');
			for ( var j = 0, varCnt = cn.length ; j < varCnt ; j ++ ) {
				if ( cn[j] == 'over' ) {
					img[i].onmouseover = function() {
						rollOverOn(this);
					}
					img[i].onmouseout = function() {
						rollOverOff(this);
					}
				}
			}
	}
	var img = document.getElementsByTagName('input');
	for ( var i = 0, imgLength = img.length ; i < imgLength ; i ++ ) {
		var cn = img[i].className.toLowerCase().split(' ');
		for ( var j = 0, varCnt = cn.length ; j < varCnt ; j ++ ) {
			if ( cn[j] == 'over' ) {
				img[i].onmouseover = function() {
					rollOverOn(this);
				}
				img[i].onmouseout = function() {
					rollOverOff(this);
				}
			}
		}
	}
}

function rollOverOn(obj) {
	if (obj.src.indexOf("_on") == -1 ){
		obj.src = obj.src.replace(/(.*)\.([a-z]{3})$/g,'$1_on.$2');
	}
}
function rollOverOff(obj) {
	if (obj.id != "ignore") {
		obj.src = obj.src.replace(/(.*)_on\.([a-z]{3})$/g,'$1.$2');
	}
}



//-----------------------------------------------------
// -attach events when loaded
//-----------------------------------------------------
if ( window.addEventListener ) {
	window.addEventListener( "load", set, false );
} else if ( window.attachEvent ) {
	window.attachEvent( "onload", set );
} else {
	window.onload = set();
}




//-----------------------------------------------------
// -form js based on tokyu hotels form.js
//-----------------------------------------------------

var tday  = new Date();
var year  = tday.getFullYear();
var month = tday.getMonth();
var day   = tday.getDate();
totalDays = 31;
totalDaysOut = 31;

function keta2(d) { return (d < 10) ? ('0' + d) : d; }

function checkin_y(name) {
	var x = '<select onchange="changedate(\'year\');" name="' + name + '" style="width:54px;">';
	for(k= year; k <=year+3; k++){
		x +="<option value=" + k + ((k==year) ? " selected" : "") + ">" + k + "</option>";
	}
	x += "</select>";
	return x;
}

function checkin_m(name) {
	var y = '<select onchange="changedate(\'month\');" name="'+ name + '" style="width:38px;">';
	for(j= 1; j <=12; j++){
		y +="<option value=" + keta2(j) + ((j==month+1) ? " selected" : "") + ">" + j + "</option>";
	}
	y += "</select>";
	return y;
}

function checkin_d(name) {
	var z = '<select onchange="changedate(\'date\');" name="' + name + '" style="width:38px;">';
	for(i = 1; i <= 31; i++){
		z += "<option value=" + keta2(i) + ((i==(day)) ? " selected" : "") + ">" + i + "</option>";
	}
	z += "</select>";
	return z;
}

var wday  = new Date();
wday.setTime(tday.getTime() + (24 * 3600 * 1000));

var nyear  = wday.getFullYear();
var nmonth = wday.getMonth();
var nday   = wday.getDate();

function checkout_y(name) {
	var x = '<select name="' + name + '" style="width:54px;">';
	for(k= year; k <=year+3; k++){
		x +="<option value=" + k + ((k==nyear) ? " selected" : "") + ">" + k + "</option>";
	}
	x += "</select>";
	return x;
}

function checkout_m(name) {
	var y = '<select name="'+ name + '" style="width:38px;">';
	for(j= 1; j <=12; j++){
		y +="<option value=" + keta2(j) + ((j==nmonth+1) ? " selected" : "") + ">" + j + "</option>";
	}
	y += "</select>";
	return y;
}

function checkout_d(name) {
	var z = '<select name="' + name + '" style="width:38px;">';
	for(i = 1; i <= 31; i++){
		z += "<option value=" + keta2(i) + ((i==(nday)) ? " selected" : "") + ">" + i + "</option>";
	}
	z += "</select>";
	return z;
}

//
//

function changedate(m) {
	var yValue = document.r.chkin_yy.value;
	var mValue = document.r.chkin_mm.value;
	var dValue = document.r.chkin_dd.value;
	var dSelect = document.r.chkin_dd.selectedIndex;
	var yValueOut = document.r.chkout_yy.value;

	// leap year settings
	if( mValue=="01" || mValue=="03" || mValue=="05" || mValue=="07" || mValue=="08" || mValue=="10" || mValue=="12" ) {
		if( mValue=="01" && ((yValue % 4 == 0) && (yValue % 100 != 0)) || (yValue % 400 == 0) ){
			if ( ((yValueOut % 4 == 0) && (yValueOut % 100 != 0)) || (yValueOut % 400 == 0) ) {
				totalDays = 31;
				totalDaysOut = 29;
			} else {
				totalDays = 31;
				totalDaysOut = 28;
			}
		} else if(mValue=="01") {
			if ( ((yValueOut % 4 == 0) && (yValueOut % 100 != 0)) || (yValueOut % 400 == 0) ) {
				totalDays = 31;
				totalDaysOut = 29;
			} else {
				totalDays = 31;
				totalDaysOut = 28;
			}
		} else if(mValue=="07" || mValue=="12") {
			totalDays = 31;
			totalDaysOut = 31;
		} else {
			totalDays = 31;
			totalDaysOut = 30;
		}
	} else {
		totalDays = 30;
		totalDaysOut = 31;
		if(mValue=="02") {
			totalDays = 28;
			if ( ((yValue % 4 == 0) && (yValue % 100 != 0)) || (yValue % 400 == 0) ) {
				totalDays = 29;
			}
			totalDaysOut = 31;
		}
	}

	if( m ){
		in_sl_d = document.r.chkin_dd;
		out_sl_d = document.r.chkout_dd;
		while(in_sl_d.lastChild) {
			in_sl_d.removeChild(in_sl_d.lastChild);
		}
		while(out_sl_d.lastChild) {
			out_sl_d.removeChild(out_sl_d.lastChild);
		}
			for ( var i = 0; i <= totalDays-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkin_dd.options[i] = new Option(i+1,ii);
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
		if(dSelect > document.r.chkin_dd.length-1) {
			dSelect = totalDays-1;
		}
		document.r.chkin_dd.options.selectedIndex = dSelect;
	}

	if(document.r.chkin_yy.selectedIndex != 0) {
		if(mValue==12 && dSelect==totalDays-1) {
			if ( document.r.chkin_yy.selectedIndex == 3 ) {
				var element = document.createElement('option');
				element.value = year+4;
				element.innerHTML = year+4;
				document.r.chkout_yy.appendChild(element);
				if ( navigator.userAgent.indexOf('MSIE 6') != -1 ) {
					ie6timer = setInterval(tempIeFunc,1)
				} else {
					document.r.chkout_yy.selectedIndex = 4;
				}
				document.r.chkout_mm.selectedIndex = 0;
				document.r.chkout_dd.selectedIndex = 0;
			} else {
				document.r.chkout_yy.selectedIndex = document.r.chkin_yy.selectedIndex+1;
				document.r.chkout_mm.selectedIndex = 0;
				document.r.chkout_dd.selectedIndex = 0;
			}
		}else if(dSelect==totalDays-1) {
			document.r.chkout_yy.selectedIndex = document.r.chkin_yy.selectedIndex;
			document.r.chkout_mm.selectedIndex = mValue;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDaysOut-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
		} else {
			document.r.chkout_yy.selectedIndex = document.r.chkin_yy.selectedIndex;
			document.r.chkout_mm.selectedIndex = mValue-1;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDays-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
			document.r.chkout_dd.options.selectedIndex = dSelect+1;
		}
		if(dSelect==totalDays-1){
			document.r.chkout_dd.selectedIndex = 0;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDaysOut-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
		} else {
			document.r.chkout_dd.selectedIndex = dValue;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDays-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
			document.r.chkout_dd.options.selectedIndex = dSelect+1;
		}
	} else {
		if(mValue==12 && dSelect==totalDays-1) {
			document.r.chkout_yy.selectedIndex = document.r.chkin_yy.selectedIndex+1;
			document.r.chkout_mm.selectedIndex = 0;
			document.r.chkout_dd.selectedIndex = 0;
		}else if(dSelect==totalDays-1) {
			document.r.chkout_yy.selectedIndex = document.r.chkin_yy.selectedIndex;
			document.r.chkout_mm.selectedIndex = mValue;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDaysOut-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
		} else {
			document.r.chkout_yy.selectedIndex = document.r.chkin_yy.selectedIndex;
			document.r.chkout_mm.selectedIndex = mValue-1;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDays-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
			document.r.chkout_dd.options.selectedIndex = dSelect+1;
		}
		if(dSelect==totalDays-1){
			document.r.chkout_dd.selectedIndex = 0;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDaysOut-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
		} else {
			document.r.chkout_dd.selectedIndex = dValue;
			out_sl_d = document.r.chkout_dd;
			while(out_sl_d.lastChild) {
				out_sl_d.removeChild(out_sl_d.lastChild);
			}
			for ( var i = 0; i <= totalDays-1; i++ ) {
				( i+1 < 10 ) ? ii = "0"+(i+1) : ii = i+1;
				document.r.chkout_dd.options[i] = new Option(i+1,ii);
			}
			document.r.chkout_dd.options.selectedIndex = dSelect+1;
		}
	}

	if ( document.r.chkin_yy.selectedIndex != 3 || document.r.chkin_mm.selectedIndex != 11 || document.r.chkin_dd.selectedIndex != 30 ) {
		if ( document.r.chkout_yy.length > 4 ) {
			document.r.chkout_yy.removeChild(document.r.chkout_yy.childNodes(4));
			document.r.chkout_yy.selectedIndex = 3;
		}
	}
}

function tempIeFunc() {
	document.r.chkout_yy.selectedIndex = 4;
	clearInterval(ie6timer);
}

function resetHidSearch(targetHotel) {
	targetNode = targetHotel.parentNode;
	targetNode.parentNode.parentNode.getElementsByTagName('input')[1].value = targetHotel.value;
}



