
var kabe_sun = "#cccccc";
var kabe_mon = "#FFFFB3";
var kabe_tue = "#FFFFB3";
var kabe_wed = "#FFFFB3";
var kabe_thu = "#FFFFB3";
var kabe_fri = "#FFFFB3";
var kabe_sat = "#B0F6B5";
var firstAltMsg = "&nbsp";
var firstAltYMD = "&nbsp";


//2009年行事テーブル(フラグ(1:祝日 2:記念日など),月,日,メッセージ)//2, 1, 10, "誕生日",
var gyouji_tbl = new Array(
1, 1, 1, "元旦",
1, 1, 0, "成人の日",
1, 2, 11, "建国記念の日",
1, 4, 29, "昭和の日",
1, 5, 3, "憲法記念日",
1, 5, 4, "みどりの日",
1, 5, 5, "こどもの日",
1, 7, 0, "海の日",
1, 9, 0, "敬老の日",
1, 10, 0, "体育の日",
1, 11, 3, "文化の日",
1, 11, 23, "勤労感謝の日",
1, 12, 23, "天皇誕生日",
2, 3, 20, "春分の日",
2, 5, 6, "振替休日",
2, 8, 14, "夏休み",
2, 8, 15, "夏休み",
2, 9, 22, "国民の休日",
2, 9, 23, "秋分の日"
);
var kokuminLastCnt = gyouji_tbl.length / 4;

function carenda(num){
var now = new Date();
var year;
var month;
var date;
var dValue = document.getElementById("dValue");

switch(parseInt(num)){
case 0:
year = now.getFullYear();
month = now.getMonth()+1;
date = now.getDate();
break;
case 1:
var backMDate = new Date(parseInt(dValue.innerHTML) - 24*60*60*1000*1);
if(backMDate.getMonth() == now.getMonth() && backMDate.getFullYear() == now.getFullYear()){
year = now.getFullYear();
month = now.getMonth()+1;
date = now.getDate();
}else{
year = backMDate.getFullYear();
month = backMDate.getMonth() + 1;
date = -1;
}
break;
case 2:
var nextMDate = new Date(parseInt(dValue.innerHTML) + 24*60*60*1000*31);
if(nextMDate.getMonth() == now.getMonth() && nextMDate.getFullYear() == now.getFullYear()){
year = now.getFullYear();
month = now.getMonth()+1;
date = now.getDate();
}else{
year = nextMDate.getFullYear();
month = nextMDate.getMonth() + 1;
date = -1;
}
break;
}

dValue.innerHTML = (new Date(year, month-1, 1)).getTime();

var last_date = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var editMsg;
if(month==2){
if(year%4==0){
if((year%100==0) && (year%400!=0)){
}else{
last_date[1] = 29;
}
}
}

editMsg = "";
editMsg += "<TABLE border=0 cellspacing=3><TR><TD colspan='7' align='center'><FONT color=#666666 size='-1'>" + year + "年" + month + "月</FONT></TD></TR>\n";
editMsg += "<TR>" + defTD("日","red",kabe_sun) + defTD("月","#666666",kabe_mon) + defTD("火","#666666",kabe_tue) + defTD("水","#666666",kabe_wed) + defTD("木","#666666",kabe_thu) + defTD("金","#666666",kabe_fri) + defTD("土","green",kabe_sat) + "</TR>\n";
editMsg += "<TR>";

for(dayIndex=0; dayIndex<(new Date(year,month-1,1)).getDay(); dayIndex++){
editMsg += defTD("&nbsp;","white","white");
}


//行事テーブル（祝日）の再設定
//成人の日
gyouji_tbl[1*4+2] = getSyukujituDate(year, 1, 2);
//海の日
gyouji_tbl[7*4+2] = getSyukujituDate(year, 7, 3);
//敬老の日
gyouji_tbl[8*4+2] = getSyukujituDate(year, 9, 3);
//体育の日
gyouji_tbl[9*4+2] = getSyukujituDate(year, 10, 2);
//みどりの日4/29→昭和の日 国民の休日5/4→みどりの日
if(year<=2006){
gyouji_tbl[3*4+3] = "みどりの日";
gyouji_tbl[5*4+3] = "国民の休日";
} else {
gyouji_tbl[3*4+3] = "昭和の日";
gyouji_tbl[5*4+3] = "みどりの日";
}


//当日と行事が重なる場合の初期設定
if(date != -1){
firstAltMsg = "&nbsp";
firstAltYMD = year + "/" + month + "/" + date;
for(var j=0; j<kokuminLastCnt; j++){
if(gyouji_tbl[j*4+1] == month && gyouji_tbl[j*4+2] == date){
firstAltMsg += gyouji_tbl[j*4+3] + "&nbsp";
}
}
}

for(i=1; i<=last_date[month-1]; i++){
if(i!=1 && dayIndex == 0){
editMsg += "<TR>";
}

var kabeColor;
var fontColor;
var altYMD = year + "/" + month + "/" + i;
var altMsg = "&nbsp";
//曜日別基本設定
switch(dayIndex){
case 0: fontColor = "#666666";
kabeColor = kabe_sun; break;
case 1: fontColor = "#666666";
kabeColor = kabe_mon; break;
case 2: fontColor = "#666666";
kabeColor = kabe_tue; break;
case 3: fontColor = "#666666";
kabeColor = kabe_wed; break;
case 4: fontColor = "#666666";
kabeColor = kabe_thu; break;
case 5: fontColor = "#666666";
kabeColor = kabe_fri; break;
case 6: fontColor = "green";
kabeColor = kabe_sat; break;
}


//行事の時
for(var j=0; j<kokuminLastCnt; j++){
if(gyouji_tbl[j*4+1] == month && gyouji_tbl[j*4+2] == i){
//祝日
if(gyouji_tbl[j*4] == 1){
fontColor = "red";
}else{
kabeColor = "pink";
}
altMsg += gyouji_tbl[j*4+3] + "&nbsp";
}
}

//当日
if(i==date){
fontColor = "darkorange";
}

editMsg += defTD2(i,fontColor,kabeColor,altYMD,altMsg);

if(dayIndex == 6){
editMsg += "</TR>\n";
}
dayIndex++; dayIndex%=7;
}
if(dayIndex!=7){
editMsg += "</TR>\n";
}
editMsg += "</TABLE>\n";

document.getElementById("carenda").innerHTML = editMsg;
}

function defTD(str, iro, kabe){
return "<TD style='cursor:default' align='center' bgcolor='" + kabe + "'><FONT size='-1' color='" + iro + "'>" + str + "</FONT></TD>";
}
function defTD2(str, iro, kabe, altYMD,altMsg){
return "<TD style='cursor:default' align='center' bgcolor='" + kabe + "' onMouseOver=setAltMsg('" + altYMD + "','" + altMsg + "') onMouseOut=setAltMsg('" + firstAltYMD + "','" + firstAltMsg + "')><FONT size='-1' color='" + iro + "'>" + str + "</FONT></TD>";
}
function setAltMsg(altYMD, altMsg){
var editAltMsg = "";
editAltMsg += "<FONT color=#666666 size = '-1' face='MS Mincho,MS PMincho,Hiragino Mincho Pro W3'>本日は </FONT>";
editAltMsg += "<FONT color=#666666 size = '-1' face='MS Mincho,MS PMincho,Hiragino Mincho Pro W3'>" + altYMD + "</FONT>";
editAltMsg += "<FONT color=#666666 size = '-1'>" + altMsg + "</FONT>";
editAltMsg += "<FONT color=#666666 size = '-1' face='MS Mincho,MS PMincho,Hiragino Mincho Pro W3'></FONT>";
document.getElementById("altMsg").innerHTML = editAltMsg;
}

//祝日の日にち取得(年、月、第？週の月曜日)
function getSyukujituDate(year, month, syuu){
var syuuCnt = 0;
for(var i=1; i<=31; i++){
var date = (new Date(year, month-1, i)).getDay();
if((new Date(year, month-1, i)).getDay() == 1){
syuuCnt++;
}
if(syuuCnt == syuu){
return i;
}
}
}

