var SelectItem;
var SetDateItem;
var now = new Date();

function OpenCalendar(item,item2) {
	var win = window.open("J/html/calendar.html",'','width=220,height=200,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no');
	SelectItem = item;
	SetDateItem = item2;
}
function DateSelect(seldate) {
	window.opener.SelectItem.value=seldate;
 	window.close();
}
function _year(_dt) {
	var dt_year = _dt.getYear() ;
	if( dt_year < 2000){
		dt_year += 1900 ;
	}
	return dt_year ;
}

function ShowCalender(nYear,nMonth,ctlName,nMode)
{
	var svnYear = nYear;

	if (nMode == 1) {
		if ( (! nYear) && window.opener.SetDateItem) {
			var aryDate = window.opener.SetDateItem.value.split("/");
			nYear = aryDate[0];
			nMonth = aryDate[1];
		}
	}
	if( isNaN(nYear) || isNaN(nMonth) ) {
		nYear = 0 ;
		nMonth = 0 ;
	}
	else{
		nYear = nYear * 1;
		nMonth = nMonth * 1;
	}

	var _off_M = nMonth - (now.getMonth() + 1) ;
	_off_M += (nYear - _year(now)) * 12 ;
	if ( _off_M < 0 || _off_M > 24 || nMonth < 1 || nMonth > 12) {
		var dDate = now ;
		dDate.setHours( dDate.getHours( ) + 24 ) ;
		nYear = _year( dDate ) ;
		nMonth = dDate.getMonth()+1;
	}

	var ArrivalCalenderHTML ;
	if (ctlName == "DepartureDate") {
		DepartureCalender.innerHTML = ShowCalenderExec("now", nYear, nMonth, ctlName, nMode);
		if (svnYear != "" && nMode != 1) {
			ArrivalCalenderHTML = ShowCalenderExec("input", nYear, nMonth, "ArrivalDate", nMode);
		}
	} else {
		ArrivalCalenderHTML = ShowCalenderExec("input", nYear, nMonth, ctlName, nMode);
	}
	if (nMode != 1 && document.getElementById("ArrivalCalender") ) {
		ArrivalCalender.innerHTML = ArrivalCalenderHTML ;
	}

}
function SelCalender(SelYM,CtlName,nMode)
{
	eval( "ShowCalender( "+ SelYM + ",\""+ CtlName + "\", nMode);" ) ;
}
function ShowCalenderExec(Orign, nYear, nMonth, ctlName, nMode)
{
	var strCalString;
	var dtCal = new Date;
	var strYear;
	var strMonth;
	var strDate;
	var nRemain;
	var nCurDay;
	var strDay = new Array("日","月","火","水","木","金","土");
	var nMonthCount = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	//ここから表示
	var strCalString = "<table cellpadding=\"2\" cellspacing=\"2\" class=\"sz11_e\">" +
		"<TR align=\"center\" valign=\"top\">";

	//年月日を取得
	strYear = nYear;

	//表示のための準備
	dtCal.setYear(strYear);
	dtCal.setDate(1);
	dtCal.setMonth(nMonth - 1);
	strYear = _year(dtCal);
	strMonth = dtCal.getMonth()+1;

	strTodaysMonth = dtCal.getMonth()+1;
	var nDayCount = nMonthCount[strMonth -1];
	if ((strMonth == 1)&&(((strYear%4 == 0)&&(strYear%100 != 0))||(strYear%400 == 0)))
	{
		nDayCount = 29;
	}
	strTodaysMonthSave = strTodaysMonth;
	strYearSave = strYear;

	strCalString+= "<TD align=\"center\">";

	strCalString+= "<table cellpadding=\"1\" cellspacing=\"0\" class=\"sz11_e\" background=\"../img/calbk" + strTodaysMonth + ".gif\" style=\"background-repeat:no-repeat;\">";
	strCalString+= "<TR align=\"center\" valign=\"center\" height=\"18\" bgcolor=\"#6699cc\"><TD>";
	strCalString+= "</TD>";
	strCalString+= "<TD colspan=\"5\" class=\"sz12_e\"><select onChange=\"SelCalender( this.value, '"+ ctlName +"',"+nMode+");\">" ;

	// 年月選択PULLDOWN
	var SelYear ;
	var SelMon ;
	if( Orign == "now" )
	{
		SelYear = _year(now);
		SelMon = now.getMonth()+1;
	}
	else
	{
		SelYear = nYear ;
		SelMon = nMonth ;
	}

	for( var Seloop=0; Seloop < 12; Seloop ++ ) {

		var SelSelected = "" ;
		if( strYear == SelYear && strMonth == SelMon )
		{
			SelSelected = " selected " ;
		}
		strCalString+=	"<option value=\"" + SelYear + "," + SelMon + "\"" + SelSelected + ">"+ SelYear+"年 "+ SelMon + "月</option>" ;
		if( (++ SelMon) > 12 ) {
			SelMon = 1 ;
			SelYear ++ ;
		}
	}
	strCalString+= "</select></TD><TD>";
	strCalString+= "</TD></TR>";

	//一行目は曜日
	strCalString+= "<TR align=\"center\" valign=\"center\" bgcolor=\"#eeeeee\" height=\"18\">";
	for(nLoop=0; nLoop < 7; nLoop++)
	{
		if (nLoop == 6) {
			tdClass = "class=\"l_blr_69c\"";
		} else {
			tdClass = "class=\"l_bl_69c\"";
		}
		strCalString+= "<TD " + tdClass + " width=\"24\"><B>";
		if (nLoop == 0) {
			strCalString+= "<FONT color=\"#cc0033\">";
		} else if (nLoop == 6) {
			strCalString+= "<FONT color=\"#336699\">";
		}
		strCalString+= strDay[nLoop];
		if (nLoop == 0 || nLoop == 6) {
			strCalString+= "</FONT>";
		}
		strCalString+= "</B></TD>";
	}
	strCalString+= "</TR>";

	//ブランクを挿入
	if (dtCal.getDay() > 0) {
		strCalString+= "<TR align=\"center\" valign=\"center\" height=\"24\">";
		for(nLoop = 0; nLoop < dtCal.getDay(); nLoop++)
		{
			strCalString = strCalString + "<TD class=\"l_bl_69c\">&nbsp;</TD>";
		}
	}
	//日付を挿入
	for(nLoop = 0; nLoop < nDayCount; nLoop++)
	{
		if(dtCal.getDate() > nLoop)
		{
			nCurDay = dtCal.getDay();
			if (nCurDay == 0)
			{
				strCalString+= "<TR align=\"center\" valign=\"center\" height=\"24\">";
			}
			if (nCurDay == 6) {
				tdClass = "class=\"l_blr_69c\"";
			} else {
				tdClass = "class=\"l_bl_69c\"";
			}
			if (nCurDay != 7)
			{
				strCalString+= "<TD " + tdClass + " onMouseover=\"this.style.backgroundColor='#FFcc00'\" onClick=\"";
				if (nMode == 1) {
					strCalString+= "DateSelect('" + strYear + "/" + strTodaysMonth + "/" + dtCal.getDate() + "');";
				} else {
					strCalString+= "this.style.backgroundColor='#ff0000'; document.search_b.Set" + ctlName + ".value='" + strYear + "/" + strTodaysMonth + "/" + dtCal.getDate() + "'; document.search_b." + ctlName + ".value = document.search_b.Set" + ctlName + ".value; rightCalender(358)";
				}
				strCalString+= "\" onMouseout=\"this.style.backgroundColor=''\" style=\"cursor:pointer\">";
				if (nCurDay == 0) {
					strCalString+= "<FONT color=\"#cc0033\">";
				} else if (nCurDay == 6) {
					strCalString+= "<FONT color=\"#336699\">";
				}
				strCalString+= dtCal.getDate();
				if (nCurDay == 0 || nCurDay == 6) {
					strCalString+= "</FONT>";
				}
				strCalString+= "</B></TD>";
			}
			if (nCurDay == 7)
			{
				strCalString = strCalString + "</TR>";
			}
		}
		dtCal.setDate(dtCal.getDate()+1);
		nRemain = 7-dtCal.getDay();
	}
	//ブランクを挿入
	if (nRemain < 7) {
		for(nLoop = 0; nLoop <nRemain; nLoop++)
		{
			if (nLoop == (nRemain-1)) {
				tdClass = "class=\"l_blr_69c\"";
			} else {
				tdClass = "class=\"l_bl_69c\"";
			}
			strCalString = strCalString + "<TD " + tdClass+ " >&nbsp;</TD>";
		}
	}
	strCalString = strCalString + "</TR></TABLE>";
	strCalString = strCalString + "</TD></TR></TABLE>";
	return strCalString;
}
