﻿// JavaScript Document

YahhoCal.loadYUI();

function computeDate(year, month, day, addDays) {
    var dt = new Date(year, month - 1, day);
    var baseSec = dt.getTime();
    var addSec = addDays * 86400000;//日数 * 1日のミリ秒数
    var targetSec = baseSec + addSec;
    dt.setTime(targetSec);
    return dt;
}

function checkDate(year, month, day) {
    var dt = new Date(year, month - 1, day);
    if(dt == null || dt.getFullYear() != year || dt.getMonth() + 1 != month || dt.getDate() != day) {
        return false;
    }
    return true;
}

function initDateSelects() {
	var d = document;
	var now = new Date();
	var nYear = now.getFullYear();
	var nMonth = now.getMonth() + 1;
	var nDate = now.getDate();

	d.getElementById("checkin_year").options[0].text = nYear;
	d.getElementById("checkin_year").options[0].value = nYear;
	d.getElementById("checkin_year").options[1].text = nYear + 1;
	d.getElementById("checkin_year").options[1].value = nYear + 1;
	d.getElementById("checkin_month").selectedIndex = nMonth -1;
	d.getElementById("checkin_day").selectedIndex = nDate - 1;
	
	var tmr = computeDate(nYear, nMonth, nDate, 1);

	//d.getElementById("checkout_year").selectedIndex = tmr.getFullYear() - 2010;
	d.getElementById("checkout_year").options[0].text = nYear;
	d.getElementById("checkout_year").options[0].value = nYear;
	d.getElementById("checkout_year").options[1].text = nYear + 1;
	d.getElementById("checkout_year").options[1].value = nYear + 1;
	d.getElementById("checkout_year").options[2].text = nYear + 2;
	d.getElementById("checkout_year").options[2].value = nYear + 2;
	d.getElementById("checkout_month").selectedIndex = tmr.getMonth();
	d.getElementById("checkout_day").selectedIndex = tmr.getDate() - 1;
}

if(window.addEventListener) {
window.addEventListener("load", initDateSelects, false);
}
else if(window.attachEvent) {
window.attachEvent("onload", initDateSelects);
}

function changeCheckout(){
	// 現在の選択されている日にちを取得
	var inYear = document.getElementById("checkin_year").value;
	var inMonth = document.getElementById("checkin_month").value;
	var inDay = document.getElementById("checkin_day").value;
	
	var tmr2 = computeDate(inYear, inMonth, inDay, 1);
	
	// チェックアウトを1日後にする
	document.getElementById("checkout_year").value = tmr2.getFullYear();
	document.getElementById("checkout_month").selectedIndex = tmr2.getMonth();
	document.getElementById("checkout_day").selectedIndex = tmr2.getDate() - 1;
}

function dateCheck(){
	var checkinYear = document.getElementById("checkin_year").value;
	var checkinMonth = document.getElementById("checkin_month").value;
	var CheckinDay = document.getElementById("checkin_day").value;
	
	var result1 = checkDate(checkinYear, checkinMonth, CheckinDay);
	
	if(result1 == false){
		window.alert("日付が正しくありません");
		return false;
	}
	
	var checkoutYear = document.getElementById("checkout_year").value;
	var checkoutMonth = document.getElementById("checkout_month").value;
	var CheckoutDay = document.getElementById("checkout_day").value;
	
	var result2 = checkDate(checkoutYear, checkoutMonth, CheckoutDay);
	
	if(result2 == false){
		window.alert("日付が正しくありません");
		return false;
	}
}
