﻿//万年暦  by「菊池さん」 <http://village.infoweb.ne.jp/~tkiku/>
   nDate = new Date();
   thisDay = nDate.getDate();
   thisMonth = nDate.getMonth();
function koyomi(mymonth) {
   var str = new Array();
   str = mymonth.split("/");
   while( str[1] <  1 ) { str[1] = str[1]*1 + 12; str[0] = str[0]*1 - 1; }
   while( str[1] > 12 ) { str[1] = str[1]*1 - 12; str[0] = str[0]*1 + 1; }
   mymonth = str[0] +"/"+ str[1];
   myday = new Date(mymonth+"/01");
   monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   kyuDays = new Array("0101","0111","0211","0321","0429","0503","0504","0505"
      ,"0719","0920","0923","1011","1103","1123","1223"); //休日設定(変更出来ません)
   tokuDays = new Array("0102","0104","0130","0206","0306","0403","0410","0529","0605","0612","0703","0807","0814","0816","0904","0921","1002","1106","1205","1231");	//特別日設定
   year = myday.getYear(); if (year < 2000) year = year + 1900;
   month = myday.getMonth()+1;

   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
      { monthDays[1] = 29; }  // うるう年計算
   kyuDays[3] = "03" + Math.floor(20.8431 + 0.242194 * (year - 1980)
      - Math.floor((year - 1980)/4));  // 春分の日を求める（1980-2099まで）
   kyuDays[10] = "09" + Math.floor(23.2488 + 0.242194 * (year - 1980)
      - Math.floor((year - 1980)/4));  // 秋分の日を求める（1980-2099まで）

   nDays = monthDays[myday.getMonth()];

   firstDay = myday;
   firstDay.setDate(1);
   startDay = firstDay.getDay();

// 2000年以降の成人の日/体育の日(第２月曜日)の計算
if (year >= 2000 &&(month == 1 || month == 10)) {
   if (startDay <= 1) happyMon = 7 + (2 - startDay);
   else               happyMon = 14 - (startDay - 2);
   if (happyMon < 10) happyMon = "0" + happyMon;
   if (month == 1) kyuDays[1] = "01" + happyMon;
   else            kyuDays[11] = "10" + happyMon;
}

for (i = 0; i < kyuDays.length; i++) { // 振替休日の自動計算
   hmon = kyuDays[i].substring(0,2);
   if (hmon == month) {
      hday = kyuDays[i].substring(2,4)*1;
      if (hmon <= 2) { zyear = year - 1;	zmon = parseInt(hmon) + 12; }
      else { zyear = year;	zmon = hmon; }
      zday = (zyear + parseInt(zyear/4) - parseInt(zyear/100)
             + parseInt(zyear/400) + parseInt((13*zmon + 8)/5) + hday ) % 7;
      if (zday == 0) {
         hday = hday + 1;
         if (hday < 10) hday = "0"+ hday;
         kyuDays[kyuDays.length] = hmon + hday;
      }
   }
}
   doc ="<h3>" + month + "月　営業日カレンダー</h3>";
   doc+="<table cellspacing=1>";
   //doc+="<TR><TD COLSPAN=7 class='cal-header'><center>";
   //doc+=year + "年 ";
   //doc+=month + "月　営業日カレンダー</center></td></tr>";
   doc+="<tr><th><font color=red>日</font></th>";
   doc+="<th>月</th><th>火</th>";
   doc+="<th>水</th><th>木</th>";
   doc+="<th>金</th><th><font color=blue>土</font></th></tr>";

   doc+="<tr bgcolor=#ffffff>";
   column = 0;
   for (i=0; i<startDay; i++)
   {
      doc+="<td>　</td>";
      column++;
   }
   for (i=1; i<=nDays; i++)
   {
      if (i==thisDay && myday.getMonth()==thisMonth) {  //本日の背景色変更
        doc+="<td bgcolor='#92EF72'>";
      } else { doc+="<td>"; }
      weekDay = myday;
      weekDay.setDate(i);
      if (weekDay.getDay() == 0)      doc+="<div class='red'>";
      else if (weekDay.getDay() == 6) doc+="<div class='white'>";
	  else if (i==thisDay && myday.getMonth()==thisMonth) doc+="<div class='green'>";
      else                            doc+="<div class='white'>";
      for (j=0; j<kyuDays.length; j++)		//休日判定
      {
        if (myday.getMonth()+1 == kyuDays[j].substring(0,2) &&
            i == kyuDays[j].substring(2,4)*1) doc+="<div class='red'>";
      }
      for (j=0; j<tokuDays.length; j++)		//特別日判定
      {
        if (myday.getMonth()+1 == tokuDays[j].substring(0,2) &&
            i == tokuDays[j].substring(2,4)*1)
            doc+="<div class='red'>";
      }
      doc+=i;
      doc+="</div></td>"
      column++;
      if (column == 7 && i != nDays)
      {
         doc+="</tr><tr bgcolor=#ffffff>";
         column = 0;
      }
   }
   for (i=0; column<7; i++){
      doc+="<td>　</td>";
      column++;
   }
   doc+="</tr></table>";
   document.write(doc);
}