function cal_init(def_date) {
	if (def_date == ""){
		this.today  = new Date();             // 今日の日付取得
	  myDate      = this.today.getDate();   // '日'取得
	  this.today.setDate(myDate+14);         // 2週間後の日付へ 
	}
	else {
		this.today = new Date(def_date);
	}

	var thisMonth = this.today.getMonth();
	var thisDay = this.today.getDate();
	var thisYear = this.today.getFullYear();

	this.link1 = document.getElementById('dateLink1');

	this.selYear1 = document.getElementById('selYear1');
	this.selMonth1 = document.getElementById('selMonth1');
	this.selDay1 = document.getElementById('selDay1');

	for (i=0;i<selYear1.length;i++){
		if (selYear1.options[i].value == thisYear ){
			selYear1.selectedIndex = i;
			break;
		}
	}
	this.selMonth1.selectedIndex = thisMonth;
	this.selDay1.selectedIndex = thisDay-1;

	cal1 = new YAHOO.widget.Calendar2up_JP("cal1","container1",(thisMonth+1)+"/"+thisYear,def_date);
	cal1.title = "チェックイン日をクリックしてください:";
	var renderSunday = function(cal,cell) {
			YAHOO.widget.Calendar_Core.addCssClass(cell, "sunday");
		}
	cal1.addWeekdayRenderer(1, renderSunday);
	cal1.setChildFunction("onSelect",setDate1);
	cal1.render();

}

function showCalendar1() {
	var month = this.selMonth1.selectedIndex;
	var day = this.selDay1.selectedIndex + 1;
	var year = this.selYear1.options[this.selYear1.selectedIndex].value;

	cal1.select((month+1) + "/" + day + "/" + year);
	cal1.setMonth(month);
	cal1.setYear(year);
	cal1.render();

	cal1.outerContainer.style.top = (link1.offsetTop+link1.offsetParent.offsetTop+link1.height-1) + "px";
	cal1.outerContainer.style.left = (link1.offsetLeft+link1.offsetParent.offsetLeft+20) + "px";
	cal1.outerContainer.style.display='block';
}

function setDate1() {
	var date1 = cal1.getSelectedDates()[0];
	//古い日付はエラー
	this.today = new Date();
	selMonth1.selectedIndex=date1.getMonth();
	selDay1.selectedIndex=date1.getDate()-1;
	select_year = date1.getYear();
	if ( date1.getYear() < 1900 ){
		select_year = date1.getYear()+1900;
	}
	else {
		select_year = date1.getYear();
	}
	for (i=0;i<selYear1.length;i++){
		if (selYear1.options[i].value == select_year ){
			selYear1.selectedIndex = i;
			break;
		}
	}
	cal1.hide();
}

function changeDate1() {
	var month = this.selMonth1.selectedIndex;
	var day = this.selDay1.selectedIndex + 1;
	var year = this.selYear1.options[this.selYear1.selectedIndex].value;

	cal1.select((month+1) + "/" + day + "/" + year);
	cal1.setMonth(month);
	cal1.setYear(year);
	cal1.render();
}

function checkDate(){
  var month = this.selMonth1.selectedIndex;
  var day = this.selDay1.selectedIndex + 1;
  var year = this.selYear1.options[this.selYear1.selectedIndex].value;
  var selectDate = new Date((month+1) + "/" + day + "/" + year);
    
  //古い日付はエラー
  var date1 = new Date();
  if ( date1.getYear() < 1900 ){
    date1_year = date1.getYear()+1900;
  }
  else {
    date1_year = date1.getYear();
  }
  var todayDate = new Date(date1.getMonth()+1 + "/" + date1.getDate() + "/" + date1_year);

  if ( todayDate > selectDate ){
  	alert("本日より未来の日付を選択してください");
          
  	return false;
  }

  //2ヶ月以上先の日付はエラー
  var limitDate = new Date();
  limitDate.setMonth(limitDate.getMonth()+2);
  if (selectDate > limitDate){
    alert("本日より2ヶ月以内の日付を選択してください");
    return false;
  }
  
  return true;
}

function checkSearchDate(){
  var limitDate = new Date();
  limitDate.setMonth(limitDate.getMonth()+2);
  var month = this.selMonth1.selectedIndex;
  var day = this.selDay1.selectedIndex + 1;
  var year = this.selYear1.options[this.selYear1.selectedIndex].value;
  var selectDate = new Date((month+1) + "/" + day + "/" + year);
  if (selectDate > limitDate){
    alert("本日より2ヶ月以内の日付を選択してください");
    return false;
  }
  return true;
}

