//calendar.js by Jun Amanai

var msPerDay = 1000*60*60*24;
var daysInMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
xpos=0; ypos=0;

//*********************************************************
//ネスケで、クリックされた場所を得る
//*********************************************************
function GetPos(theEvent){
	xpos=theEvent.screenX;
	ypos=theEvent.screenY;
}
if(!isExplorer()){
	window.document.captureEvents(Event.MOUSEDOWN);
	window.document.onmousedown=GetPos;
}

//********************************************************
//ＩＥかどうか判別
//********************************************************
function isExplorer() {
	return (navigator.appName == "Microsoft Internet Explorer");
}

//*********************************************************
//なにもしない。イベントをＨＲＥＦで発生させるために使用。
//*********************************************************
function DoNothing() {
	return;
}

//*********************************************************
//初期化
//*********************************************************
function init () {
	cookieString    = "dispMonth="+currMonth+";";
	document.cookie = cookieString;
	startOfString   = document.cookie.indexOf("dispMonth");
	countbegin      = document.cookie.indexOf("=",startOfString) + 1;
	countend        = document.cookie.indexOf(";",countbegin);
	if (countend == -1) countend = document.cookie.length;
	dispMonth     = eval ("document.cookie.substring(countbegin,countend)");
	firstOfMonth  = new Date(currYear,dispMonth,1);
}

//*********************************************************
//カレンダーウインドウを開く（本日が月の末日だとおかしくなるぞ）例：８月３１日に９月２５日を参照しながら帰りを出すと１ヶ月ずれる
//*********************************************************
function Calendar (yearFldName, monthFldName, dateFldname, formNumber, fYName, fMName, fDName, minOff) {
	formNum=formNumber; yearFld=yearFldName; monthFld=monthFldName; dateFld=dateFldname;
	fYear=fYName; fMon=fMName; fDay=fDName; minStay=minOff;
	theDate         = new Date();
	today           = new Date();
	todaysYear      = today.getFullYear();
	todaysMonth     = today.getMonth();
	todaysDate      = today.getDate();
	currMonth       = todaysMonth;
	currYear        = todaysYear;
	flag=0;
	if(fYear!='' && fMon!='' && fDay!=''){			//参照値が入っていたら、それを再優先
		y1=eval("document.forms["+formNum+"]."+fYear+".value");
		m1=eval("document.forms["+formNum+"]."+fMon +".value");
		d1=eval("document.forms["+formNum+"]."+fDay +".value");
		if(validDate(y1,m1,d1)){
			today.setFullYear(y1); today.setMonth(m1-1); today.setDate(d1);
			newTime = today.getTime() + msPerDay*minStay;
			today.setTime(newTime);
			todaysYear      = today.getFullYear();
			todaysMonth     = today.getMonth();		//0..11
			todaysDate      = today.getDate();
			currMonth       = todaysMonth;			//0..11
			currYear        = todaysYear;
			flag=1;
		}
	}
	if((flag==0)&&(yearFld!='' && monthFld!='' && dateFld!='')){	//値が入っていたら、それを現在の年月にする
		y1=eval("document.forms["+formNum+"]."+yearFld +".value");
		m1=eval("document.forms["+formNum+"]."+monthFld+".value");
		d1=eval("document.forms["+formNum+"]."+dateFld +".value");
		if(validDate(y1,m1,d1)){
			currMonth       = m1-1;
			currYear        = y1;
		}
	}
	init();
	currMonth=todaysMonth; currYear=todaysYear;	//ここまでカレンダーを戻せるようにする
	if(isExplorer()){ xpos=event.screenX; ypos=event.screenY; w=220; h=260; }
	else{  w=280; h=240; }
	windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width="+w+",height="+h+",left="+xpos+",top="+ypos+"";
	calendarWindow = this.open("","calendarWindow",windowOptions);
	calendarWindow.callingForm = this;
	redraw();
}

//*********************************************************
//日付が存在するか判定
//*********************************************************
function validDate(yearNum,monthNum,dayNum) {
	if(yearNum=='' || yearNum<todaysYear || yearNum>(todaysYear+2)) return false;
	if(monthNum=='' || monthNum<=0 || monthNum>12) return false;
	if(dayNum=='' || dayNum<=0 || dayNum>daysInMonth[monthNum]) return false;
	if((monthNum == 2) && (dayNum > daysInFeb(yearNum))) return false;
	return true;
}

//*********************************************************
//２月の日数を返す
//*********************************************************
function daysInFeb(year) {
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29:28);
}

//*********************************************************
//カレンダーを書き直す
//*********************************************************
function redraw() {
	calendarWindow.callingForm = this;
	firstOfMonth = new Date(currYear,dispMonth,1);
	calendarWindow.document.open();
	calTitle = "<TITLE>[JTRIPS.COM]カレンダー</TITLE>";
	calendarWindow.document.write(calTitle);
	drawCalendar(firstOfMonth);
	calendarWindow.document.write(htmlBuffer);
	calendarWindow.document.close();
	calendarWindow.callingForm = this;
	calendarWindow.focus();
}

//*********************************************************
//フォームに年月日を書き込む
//*********************************************************
function fillDate(filler) {
	eval("document.forms[" + formNum + "]." + yearFld  + ".value=" + yearNum);
	eval("document.forms[" + formNum + "]." + monthFld + ".value=" + monthNum);
	eval("document.forms[" + formNum + "]." + dateFld  + ".value=" + filler);
	calendarWindow.close();
}

//*********************************************************
//月をセットする
//*********************************************************
function changeMonth (increment) {
	nextMonth = dispMonth;
	if (increment == 1)	nextMonth++;
	else			nextMonth--;
	if ((nextMonth-currMonth >= 13) || (nextMonth < currMonth))  nextMonth = currMonth;
	dispMonth = nextMonth;
	document.cookie="dispMonth="+nextMonth;
	redraw();
}

//*********************************************************
//カレンダーを生成する
//*********************************************************
function drawCalendar (theDate) {
	yearNum  = theDate.getFullYear();
	monthNum = theDate.getMonth() + 1;
	htmlBuffer  = "<HTML><BODY BGCOLOR=#FFFFFF><FORM><CENTER>";
	htmlBuffer += "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0><TR><TD WIDTH=15%>";
	if(yearNum==todaysYear && monthNum==(todaysMonth+1)){
		htmlBuffer += "　";
	}else{
		htmlBuffer += "<INPUT TYPE=BUTTON NAME=monthDn VALUE=\"←\" onClick=callingForm.changeMonth(-1)>";
	}
	htmlBuffer += "</TD><TH WIDTH=70% NOWRAP>" + yearNum + "年"+ monthNum + "月";
	if(yearNum==(todaysYear+1) && monthNum==(todaysMonth+1)){
		htmlBuffer += "</TH><TD WIDTH=15%>　";
	}else{
		htmlBuffer += "</TH><TD WIDTH=15%><INPUT TYPE=BUTTON NAME=monthUp VALUE=\"→\" onClick=callingForm.changeMonth(1)>";
	}
	htmlBuffer += "</TD></TR></TABLE><BR>";
	htmlBuffer += "<TABLE CELLPADDING=1 CELLSPACING=0 BORDER=1>";
	htmlBuffer += "<TR><TH><font color=red>日</font><TH>月<TH>火<TH>水<TH>木<TH>金<TH><font color=blue>土</font>";
	drawBody(theDate);
	htmlBuffer += "</TABLE></FORM></BODY></HTML>";
}

//*********************************************************
//カレンダーのBODYを生成する
//*********************************************************
function drawBody (theDate) {
	thisYear  = theDate.getFullYear();
	thisMonth = theDate.getMonth();
	thisDate  = theDate.getDate();
	firstSunday(theDate);
	for (w=0,f=0; w<6; w++) {
		htmlBuffer += "<TR>";
		for (d=0; d<7; d++) {
			date = theDate.getDate();
			htmlBuffer   += "<TD ALIGN=CENTER>";
			// skip previous month
			if (theDate.getMonth() != thisMonth) {
				htmlBuffer += "<BR>";
			} else {
				if( thisYear==todaysYear && thisMonth==todaysMonth && date<todaysDate){
					if (date<10) htmlBuffer+="0";
					htmlBuffer+=date;
				}else{
					htmlBuffer += "<INPUT TYPE=BUTTON NAME=" + date;
					htmlBuffer += " VALUE=";
					if (date<10) htmlBuffer += "0";
					htmlBuffer += date;
					htmlBuffer += " onClick=callingForm.fillDate(" + date + ")>";
				}
				f=1;
			}
			//日付の更新
			timezoneoffset = theDate.getTimezoneOffset();

			if ( timezoneoffset == 0) theDate.setTime(newTime);
			theDate.setTime(newTime);

			newTime = theDate.getTime() + msPerDay;
			theDate.setTime(newTime);
			if (theDate.getMonth() != thisMonth && f==1) w=6;	//抜ける
			if (theDate.getHours() != 23) theDate.setTime(newTime + 3600000);
		}
	}
}

//*********************************************************
//日曜日から書けるように現在の日にちのひとつ前の日曜を探す
//*********************************************************
function firstSunday (fromDate) {
	while (fromDate.getDay() != 0) {
		newTime = fromDate.getTime() - msPerDay;
		fromDate.setTime(newTime);
		if ( fromDate.getDay() == 6 ) return;
	}
}
