var cals = new Array();

function writeCalendar(num){
	if(obj = document.getElementById("calObj")){
		obj.innerHTML = cals[num];
	}
}

function changeCalender(sel){
	var index = sel.selectedIndex
	if(index >= 0){
		writeCalendar(index);
	}
}

function calendar(){
	var now = new Date();
	var year = 2012;
	var month = 02;
	var date = now.getDate();
	var day = now.getDay();
	var last_date = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(month==2){
		if(year%4==0){
			if((year%100==0) && (year%400!=0)){
			}else{
				last_date[1] = 29;
			}
		}
	}

	var calhead001 =
		'<table class="cal" id="cal0" cellpadding="0" cellspacing="0" border="0" summary="図書館カレンダー">' +
		'<caption>本館：' + year + '年' + month + '月</caption>' +
		'<colgroup>' +
		'<col width="14%" span="7">' +
		'</colgroup>' +
		'<thead>'
	;
	var calstr001 =
		'<tr><th scope="col" abbr="日">日</th><th scope="col" abbr="月">月</th><th scope="col" abbr="火">火</th><th scope="col" abbr="水">水</th><th scope="col" abbr="木">木</th><th scope="col" abbr="金">金</th><th scope="col" abbr="土">土</th></tr>' +
		'</thead>' +
		'<tbody><tr>'
	;
	for(dayIndex=0; dayIndex<(new Date(year,month-1,1)).getDay(); dayIndex++){
		calstr001 += defTD("","blank");
	}
	for(i=1; i<=last_date[month-1]; i++){
		if(i!=1 && dayIndex == 0){
			calstr001 += "<tr>";
		}
		if(i==""){
			
		} else if ((i==06)||(i==13)||(i==20)||(i==24)||(i==27)) {	// CLOSED
			if(i==date){
				calstr001 += defBTD(i,"close");
			}else{
				calstr001 += defTD(i,"close");
			}
		} else if(i==date){					// TODAY
			calstr001 += defBTD(i,"today");
		} else {
			switch(dayIndex){
			case 0:						// SUNDAY
				calstr001 += defTD(i,"sunday");
				break;
			case 6:						// SATURDAY
				calstr001 += defTD(i,"saturday");
				calstr001 += "</tr>";
				break;
			default:					// Normal
				calstr001 += defTD(i,"white");
				break;
			}
		}
		dayIndex++;
		dayIndex%=7;
	}

	if(dayIndex!=7){
		calstr001 += "</TR>";
	}
	calstr001 += "</TABLE>";

	cals.push(calhead001 + calstr001);

	var calhead002 =
		'<table class="cal" id="cal0" cellpadding="0" cellspacing="0" border="0" summary="図書館カレンダー">' +
		'<caption>江別分館：' + year + '年' + month + '月</caption>' +
		'<colgroup>' +
		'<col width="14%" span="7">' +
		'</colgroup>' +
		'<thead>'
	;
	var calstr002 =
		'<tr><th scope="col" abbr="日">日</th><th scope="col" abbr="月">月</th><th scope="col" abbr="火">火</th><th scope="col" abbr="水">水</th><th scope="col" abbr="木">木</th><th scope="col" abbr="金">金</th><th scope="col" abbr="土">土</th></tr>' +
		'</thead>' +
		'<tbody><tr>'
	;
	for(dayIndex=0; dayIndex<(new Date(year,month-1,1)).getDay(); dayIndex++){
		calstr002 += defTD("","blank");
	}
	for(i=1; i<=last_date[month-1]; i++){
		if(i!=1 && dayIndex == 0){
			calstr002 += "<tr>";
		}
		if(i==""){
			
		} else if ((i==06)||(i==13)||(i==20)||(i==24)||(i==27)) {	// CLOSED
			if(i==date){
				calstr002 += defBTD(i,"close");
			}else{
				calstr002 += defTD(i,"close");
			}
		} else if(i==date){					// TODAY
			calstr002 += defBTD(i,"today");
		} else {
			switch(dayIndex){
			case 0:						// SUNDAY
				calstr002 += defTD(i,"sunday");
				break;
			case 6:						// SATURDAY
				calstr002 += defTD(i,"saturday");
				calstr002 += "</tr>";
				break;
			default:					// Normal
				calstr002 += defTD(i,"white");
				break;
			}
		}
		dayIndex++;
		dayIndex%=7;
	}

	if(dayIndex!=7){
		calstr002 += "</TR>";
	}
	calstr002 += "</TABLE>";

	cals.push(calhead002 + calstr002);

	var calhead003 =
		'<table class="cal" id="cal0" cellpadding="0" cellspacing="0" border="0" summary="図書館カレンダー">' +
		'<caption>大麻分館：' + year + '年' + month + '月</caption>' +
		'<colgroup>' +
		'<col width="14%" span="7">' +
		'</colgroup>' +
		'<thead>'
	;
	var calstr003 =
		'<tr><th scope="col" abbr="日">日</th><th scope="col" abbr="月">月</th><th scope="col" abbr="火">火</th><th scope="col" abbr="水">水</th><th scope="col" abbr="木">木</th><th scope="col" abbr="金">金</th><th scope="col" abbr="土">土</th></tr>' +
		'</thead>' +
		'<tbody><tr>'
	;
	for(dayIndex=0; dayIndex<(new Date(year,month-1,1)).getDay(); dayIndex++){
		calstr003 += defTD("","blank");
	}
	for(i=1; i<=last_date[month-1]; i++){
		if(i!=1 && dayIndex == 0){
			calstr003 += "<tr>";
		}
		if(i==""){
			
		} else if ((i==06)||(i==13)||(i==20)||(i==24)||(i==27)) {	// CLOSED
			if(i==date){
				calstr003 += defBTD(i,"close");
			}else{
				calstr003 += defTD(i,"close");
			}
		} else if(i==date){					// TODAY
			calstr003 += defBTD(i,"today");
		} else {
			switch(dayIndex){
			case 0:						// SUNDAY
				calstr003 += defTD(i,"sunday");
				break;
			case 6:						// SATURDAY
				calstr003 += defTD(i,"saturday");
				calstr003 += "</tr>";
				break;
			default:					// Normal
				calstr003 += defTD(i,"white");
				break;
			}
		}
		dayIndex++;
		dayIndex%=7;
	}

	if(dayIndex!=7){
		calstr003 += "</TR>";
	}
	calstr003 += "</TABLE>";

	cals.push(calhead003 + calstr003);

	var calhead004 =
		'<table class="cal" id="cal0" cellpadding="0" cellspacing="0" border="0" summary="図書館カレンダー">' +
		'<caption>豊幌小学校：' + year + '年' + month + '月</caption>' +
		'<colgroup>' +
		'<col width="14%" span="7">' +
		'</colgroup>' +
		'<thead>'
	;
	var calstr004 =
		'<tr><th scope="col" abbr="日">日</th><th scope="col" abbr="月">月</th><th scope="col" abbr="火">火</th><th scope="col" abbr="水">水</th><th scope="col" abbr="木">木</th><th scope="col" abbr="金">金</th><th scope="col" abbr="土">土</th></tr>' +
		'</thead>' +
		'<tbody><tr>'
	;
	for(dayIndex=0; dayIndex<(new Date(year,month-1,1)).getDay(); dayIndex++){
		calstr004 += defTD("","blank");
	}
	for(i=1; i<=last_date[month-1]; i++){
		if(i!=1 && dayIndex == 0){
			calstr004 += "<tr>";
		}
		if(i==""){
			
		} else if ((i==02)||(i==03)||(i==06)||(i==07)||(i==09)||(i==10)||(i==11)||(i==13)||(i==14)||(i==16)||(i==17)||(i==20)||(i==21)||(i==23)||(i==24)||(i==27)||(i==28)) {	// CLOSED
			if(i==date){
				calstr004 += defBTD(i,"close");
			}else{
				calstr004 += defTD(i,"close");
			}
		} else if(i==date){					// TODAY
			calstr004 += defBTD(i,"today");
		} else {
			switch(dayIndex){
			case 0:						// SUNDAY
				calstr004 += defTD(i,"sunday");
				break;
			case 6:						// SATURDAY
				calstr004 += defTD(i,"saturday");
				calstr004 += "</tr>";
				break;
			default:					// Normal
				calstr004 += defTD(i,"white");
				break;
			}
		}
		dayIndex++;
		dayIndex%=7;
	}

	if(dayIndex!=7){
		calstr004 += "</TR>";
	}
	calstr004 += "</TABLE>";

	cals.push(calhead004 + calstr004);

}

function defTD(str, iro){
	return "<td class='" + iro + "'>" + str + "</td>";
}

function defBTD(str, iro){
	return "<td class='" + iro + "'><B>" + str + "</B></td>";
}

function calendar_select(){
	document.write(
		'<div id="calObj"></div>' +
		'切替<select tabindex="30" name="selectLib" id="selectLib" onchange="changeCalender(this);" style="width:120px;">' +
		'<option value="001">本館</option>' +
		'<option value="002">江別分館</option>' +
		'<option value="003">大麻分館</option>' +
		'<option value="004">豊幌小学校</option>' +
		'</select>'
	);
}

calendar();
calendar_select();
writeCalendar(0);

