// ■KBS Calendar Script ■See usage in thae teil of this
function MakeArray(n){this.length = n;}
function left(str,len){return str.substring(0,len);}//左からｎ桁
function right(str,len){return str.substring(str.length - len, str.length);}//右からｎ桁

//月末日を返す
function daysOfMonth(year,month){
 var sw = "121010110101".charAt(month);//month番目の文字を抜き出す　1月=1　２月=2
 if(sw=='1')return 31;
 if(sw=='0')return 30;
 if(year % 400 == 0)return 29;
 if(year % 100 ==0) return 28;
 if(year % 4 == 0) return 29;
 return 28;
}

//指定年月までの総日数を返す
function days_total(year, month){
  var total,i;
  //前年までの総日数を求める
  var tyear = year - 1;//Math.floor最も近くて小さい整数を返す
  total = ((tyear*365)+(Math.floor(tyear/4))-(Math.floor(tyear/100))+(Math.floor(tyear/400)));
  //調整分を足しこむ
  for(i=0; i< month; i++){
    total += daysOfMonth(year,i);
  }
  return total;
}

//●祝祭日の登録
function isSpecialday(year,month,date){
  var tmonth = month + 1;
  var str="0101 0211 0320 0429 0503 0504 0505 0923 1103 1123 1223";//Now
  var str2="0115 1010";//befor 2000 year
  var str3="0720 0915";//befor 20030 year
  //文字列.indexOf(語,開始位置) 結果なし-1 見つかった場合は文字の先頭位置
  if(str.indexOf(right("0"+tmonth,2)+right("0"+date,2))>=0){
    return true;
  }//過去祝祭日
  if((year < 2000)&&(str2.indexOf(right("0"+tmonth,2)+right("0"+date,2))>=0)){
    return true;
  }//過去祝祭日
  if((year<2003)&&(str3.indexOf(right("0"+tmonth,2)+right("0"+date,2))>=0)){
    return true;
  }
  return false;
}

function isHoliday(day){return day ==0 || day == 6;}//day ==0 || day == 6;の論理値を返す
function isSunday(day){return day == 0;}
function isSaturday(day){return day== 6;}
function isMonday(day){return day  == 1;}

/* 1danme end */
function getDay2(year,month){//
  var total1 = days_total(1752,10 - 1);
  var total2 = days_total(year,month);
  return((total2 - total1)% 7);
}
//Take a link-tag to each day number on calendar　Didn't use now
function make_anchor(year,month,date,diary_url,target){
  document.write("<A href=\""+diary_url+year+right("0"+(month+1),2));
  if(date<11){
    document.write("_1");}
  else if(date<21){
    document.write("_2");}
  else{
   document.write("_3");}

  document.write(".html#"+year+right("0"+(month+1),2)+right("0"+date,2)+"\">");
}

/* ====calendar ====*/
function calendar(year,month,diary_url,target){
  var i;
  var date = 1;
  var day = getDay2(year,month);
  var days = daysOfMonth(year,month);
  var last_year = year - 1;
  var next_year = year + 1;
  var this_url = location.pathname;//現在ページURLのパス名を取得します。
  var mondays = 0;
  document.writeln("<DIV style='text-align: center'>");
  if(month<1){
    document.write("<A href=\""+this_url+"?"+last_year+"12\">&lt;&lt;&nbsp;</A>");
  }
  else{
    document.write("<A href=\""+this_url+"?"+year+right("0"+month,2)+"\">&lt;&lt;&nbsp;</A>");
  }
  document.write(year+"年"+(month+1)+"月");

  if(month>10){
    document.write("<A href=\""+this_url+"?"+next_year+"01\">&nbsp;&gt;&gt;</A>");
  }
  else{
    document.write("<A href=\""+this_url+"?"+year+right("0"+(month+2),2)+"\">&nbsp;&gt;&gt;</A>");
  }
  document.writeln("</DIV>");
  
  document.writeln("<DIV align='center'><TABLE bgcolor='#ffffff' border='1' cellspacing='0' cellpadding='2'>");//●line control
  document.writeln("<TR><TH class='sun' width='15%'>日</TH>");
  document.writeln("<TH width='14%'>月</TH>");
  document.writeln("<TH width='14%'>火</TH>");
  document.writeln("<TH width='14%'>水</TH>");
  document.writeln("<TH width='14%'>木</TH>");
  document.writeln("<TH width='14%'>金</TH>");
  document.writeln("<TH class='sat' width='15%'>土</TH></TR><TR>");
  // show date number
  for(i = 0 ; i < 7 * 6 ;i++){
    if(date > days){// over on lastday
       if(i == 35 || i == 28){ //28 is top of 5line 35 is 6line
          break;//don't uotput space 
       }
       document.write("<TD align='center' >&nbsp;</TD>");//endt space 
       continue;
    }
      
    if(i % 7 == day){ //day日・表示開始位置
      if(isSunday(day)){
        document.write("<TD align='center' class='sun'>");
        //make_anchor(year,month,date,diary_url,target);
        document.write("<span class='sun'>"+(date++)+"</span>");

      }
      else if(isSaturday(day)){
        document.write("<TD align='center'  class='sat'>");
        if(isSpecialday(year,month,date)){
          document.write("<span class='holiday'>"+(date++)+"</span>");
        }
        else{
          document.write("<span class='sat'>"+(date++)+"</span>");
        }
      }
      else if(isMonday(day)){
        mondays++;
        document.write("<TD align='center' >");
        if(isSpecialday(year,month,date)|| isSpecialday(year,month,date - 1)){
          document.write("<span class='holiday'>"+(date++)+"</span>");
        }
        else if((year >= 2000)&&(mondays == 2)&&((month == 0)||(month == 9))){
          document.write("<span class='holiday'>"+(date++)+"</span>");
        }
        else if((year >= 2003)&& (mondays == 3)&&((month == 6)||(month == 8))){
          document.write("<span class='holiday'>"+(date++)+"</span>");
        }
        else{
          document.write(date++);
        }
      }
      else{
        document.write("<TD align='center' >");
        if(isSpecialday(year,month,date)){
          document.write("<span class='holiday'>"+(date++)+"</span>");
        }
        else {
          document.write(date++);
        }
      }
      document.write("</A></TD>");
      day = (day + 1) % 7;
      if(day == 0)document.writeln("</TR><TR>");
    }
    else document.write("<TD align='center' >&nbsp;</TD>");//front space
  }
  document.write("</TR></TABLE></DIV>");
}

/*===ShowCalendar=========*/
function ShowCalendar(diary_url,target){
  var d = new Date();
  var year_and_manth;
  var year_disp = d.getYear();
  var month_disp = d.getMonth();
  var cal_bg = new MakeArray();
  if(year_disp < 100){year_disp += 1900;}
  if(year_disp < 1900){year_disp += 1900;}
  var query_string = location.search;
  if((query_string)&&(query_string.length == 7)){
    year_and_month = right(query_string,6);
    year_disp = eval(left(year_and_month,4));
    month_disp = eval(right(year_and_month,2))-1;
  }

  if(((year_disp == 1752)&&(month_disp<9))||(year_disp<1752)){
    document.writeln("<P>1752年9月以前は対応していません。</P>");
    document.writeln("<A href='"+location.pathname+"'>[今月に戻る]</A>");
  }
  else{
    calendar(year_disp,month_disp,diary_url,target);
  }
}
//■Use the CSS following mennsioned code, locate between from<HEAD> to </HEAD>
//  .sun{color:#cc0000;background:#ffcccc;}
//  .sat{color:#0000cc;background:#ccccff;}
//  .holiday{color:#cc0000;}
//
//■Call the Calender fubction sequence, locate between from<BODY> to </BODY>
//  ShowCalender("./","target=contents");
//
//■  <!--Show Calender link -->
//  <script language="javascript" type="text/javascript" src="./kbs_Calender.js"></script>
