// グローバル変数
var aDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // 月毎の日数
var aWeek = new Array("日", "月", "火", "水", "木", "金", "土");  // 曜日表示文字列
var objParent;        // 親オブジェクト
var tCalendar;        // カレンダーオブジェクト（テーブル）
var nYear = 0;        // 現在表示中の年
var nMonth = 0;      // 現在表示中の月

// 初期化（最初に呼び出す）
function InitCalendar(parent_id) {
    if (document.getElementById) {
        objParent = document.getElementById(parent_id);
        SetThisMonth();
    }
}

// カレンダーの表示
// 引数 : year（表示年）, month（表示月）
function SetCalendar(year, month) {
    // 変数
    var nDate = new Date();            // 現在のDateオブジェクト
    var nToday = nDate.getDate();   // 今日の日にち 
    var nDays;            // 表示月の日数
    var nFirstDay;       // 表示月初日の曜日インデックス
    var bToday;          // 表示月に今日が含まれるかどうか
    var i, j;

    nYear = year;
    nMonth = month
    bToday = (nYear == nDate.getFullYear() && nMonth == nDate.getMonth() + 1)? true: false;

    // ひと月の日数
    nDays = aDays[nMonth-1];
    if (nMonth == 2) {    /* 閏月のチェック */
        if (nYear % 400 == 0) nDays =29;
        else if (nYear % 100 == 0) nDays = 28;
        else if (nYear % 4 == 0) nDays= 29;
    }
	
    // 年月の描画
    tCalendar = document.createElement("table");
    with (tCalendar) {
        style.width = "150px";
        style.margin = "5px auto 10px";
        style.tableLayout = "fixed";
        style.border = "1px solid #333333";
        style.backgroundColor = "#ffffff";
        style.fontSize = "12px";
        style.lineHeight = "18px";
	style.borderCollapse = "separate";
	style.borderSpacing = "1px"
    }
    var tbCalendar = document.createElement("tbody");

    // 見出し行（年月）
    var trCaption = document.createElement("tr");
    var tdCaption = document.createElement("td");
    var strCaption = document.createTextNode(nYear + "年 " + nMonth + "月")
    with (tdCaption) {
        colSpan = "7";
	style.height = "24px"
        style.padding = "2px auto 2px";
        style.textAlign = "center";
	style.backgroundColor = "#8f6552";
	style.fontSize = "14px";
	style.fontWeight = "bold";
	style.color = "white";
    }
    trCaption.appendChild(tdCaption);    // 見出しセル
    tdCaption.appendChild(strCaption);

    tbCalendar.appendChild(trCaption);  // 見出し行

    // 月移動ボタン（リンク）
    var trMove = document.createElement("tr");
    var tdMove = document.createElement("td");
    with (tdMove) {
        colSpan = "7";
        style.padding = "3px";
	style.margin = "2px auto 2px"
        style.textAlign = "right";
	style.backgroundColor = "#ffffff";
	style.fontSize = "12px";
	style.color = "white";
    }
    var aHref = new Array("SetPrevMonth()", "SetThisMonth()", "SetNextMonth()");
    var aTarget = new Array("<< ", "今月", " >>");
    for (i = 0; i < 3; i++) {
        var lnk = document.createElement("a");
        lnk.style.marginLeft = "5px";
        lnk.style.fontSize = "12px";
        lnk.style.color = "#660000";
        lnk.href = "javascript:" + aHref[i];
        lnk.appendChild(document.createTextNode(aTarget[i]));
        tdMove.appendChild(lnk);
    }
    trMove.appendChild(tdMove);    // 見出しセル

    tbCalendar.appendChild(trMove);  // 移動行


    // 曜日行
    var trWeek = document.createElement("tr");
    for (i = 0; i < 7; i++) {
        var tdWeek = document.createElement("td");
        with (tdWeek) {
            style.padding = "0";
            style.textAlign = "center";
	    style.color = "#ffffff"
	    style.backgroundColor = "#8f6552";
            appendChild(document.createTextNode(aWeek[i]));
        }
        trWeek.appendChild(tdWeek);
    }
    tbCalendar.appendChild(trWeek);  // 曜日行

    // 日にち行
    nDate.setFullYear(nYear);
    nDate.setMonth(nMonth-1);
    nDate.setDate(1);                // Dateオブジェクトに表示月の1日をセット
    nFirstDay = nDate.getDay();  // 月初の曜日インデックス

    // 月末まで表示
    var row = document.createElement("tr");
    var cell = new Array(nDays);
    for (i = -nFirstDay, j = 0; i < nDays; i++) {
        cell[i] = document.createElement("td");
        with (cell[i]) {
            style.padding = "3px";
            style.textAlign = "center";
            if (i < 0) {    /* 一日まで空白で埋める */
                appendChild(document.createTextNode(" "));
            } else {
                appendChild(document.createTextNode(i+1));    // 日付
                // 背景色
                if (j == 0) style.backgroundColor = "#e3acae"; //日曜日の背景色
                if (j == 0) style.color = "#FFFFFF";  //日曜日
                else if (j == 6) style.backgroundColor = "#FFFFFF"; // 土曜日の背景色
                else style.backgroundColor = "#FFFFFF";     // 平日の背景色
            }
            row.appendChild(cell[i]);
        }
        if (++j == 7 && i != nDays-1) {    /* 土曜でかつ月末以外の時 */
            tbCalendar.appendChild(row);
            row = document.createElement("tr");
            j = 0;
        } 
	
    }

    // 祭日の背景色を変更 
    var aRefHoliday = aYear["A.D."+nYear];       // 祭日配列の参照
    if (aRefHoliday) {
        for (i=0, j=1; j+1<=nMonth; i++) {    /* 該当月までスキップ */
            if (aRefHoliday[i] == -1) break;
            if (aRefHoliday[i] == 0) j++;
        }
        while (aRefHoliday[i] > 0) {    /* 該当月の終り(0か-1)までループ */
            cell[aRefHoliday[i++]-1].style.backgroundColor = "#eee7e0"; //祭日の背景色

　      }
    }

    // 今日が含まれる時は該当部分の背景色を変更
	if (bToday) cell[nToday-1].style.color = "#660000";
    if (bToday) cell[nToday-1].style.textDecoration = "underline";
    tbCalendar.appendChild(row);

    //テーブルの表示
    tCalendar.appendChild(tbCalendar);
    objParent.appendChild(tCalendar);
}    

// 前月の表示
function SetPrevMonth() {
    if (nMonth == 1) {    /* 表示が1月の時は年を調整 */
        nMonth = 12;
        nYear--;
    } else {
        nMonth--;
    }
    if (tCalendar) objParent.removeChild(tCalendar);
    SetCalendar(nYear, nMonth);
}

// 翌月の表示
function SetNextMonth() {
    if (nMonth == 12) {    /* 表示が12月の時は年を調整 */
        nMonth = 1;
        nYear++;
    } else {
        nMonth++;
    }
    if (tCalendar) objParent.removeChild(tCalendar);
    SetCalendar(nYear, nMonth);
}

// 今月の表示
function SetThisMonth() {
    if (tCalendar) objParent.removeChild(tCalendar);
    var nDate = new Date();        // 現在の年、月の取得
    SetCalendar(nDate.getFullYear(), nDate.getMonth() + 1);
	}
	

