//Set constants
//TODO:特別日程の終了日を算出


//合宿開始曜日
var crtDay = 5;
//合宿期間
var lastdays = 5;
//行数
var lines = 18;//14,18
//TOEIC試験日程
var toeicDates = new Array("20111030","20111127","20120129","20120318");
//削除日程
var deletingDates = new Array("20120203","20120224","20120406","20120427","20120504");
var deletingKouzaDates = new Array("20110630");
//特別日程 ※サンプルの通り合宿・週末を表示
//var specialDates = new Array("20070209");
var tokubetsu = new Object();

/*サンプル
tokubetsu["YYYYMMDD"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gasshuku.gif\" alt=\"\" width=\"25\" height=\"12\"></td><td>12/25<small>（土）</small>-12/30<small>（木）</small></strong></td></tr>";
tokubetsu["YYYYMMDD"] = "<tr><td width=\"10\"><img src=\"images/top/icn_kouza.gif\" alt=\"\" width=\"25\" height=\"12\"></td><td class=\"testdate2\">1/8,9,15,16,22,23</small></td></tr>";
*/

/*以下に追加*/

//土日日程は例えば10月11日が第一土曜日の場合["20111010"]のように一日前の日を書くことに注意。


tokubetsu["20120203"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gasshuku.gif\" alt=\"合宿\" width=\"25\" height=\"12\"></td><td>2/4<small>（土）</small>-2/9<small>（木）</small></strong></td></tr>";

tokubetsu["20120204"] = "<tr><td width=\"10\"><img src=\"images/top/icn_kouza.gif\" alt=\"講座\" width=\"25\" height=\"12\"></td><td class=\"blue\">2/4,5,11,12,18,19</small></td></tr>";

tokubetsu["20120224"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gasshuku.gif\" alt=\"合宿\" width=\"25\" height=\"12\"></td><td>2/25<small>（土）</small>-3/1<small>（木）</small></strong></td></tr>";
tokubetsu["20120302"] = "<tr><td width=\"10\"><img src=\"images/top/icn_kouza.gif\" alt=\"講座\" width=\"25\" height=\"12\"></td><td class=\"blue\">2/25,26,3/3,4,10,11</small></td></tr>";

tokubetsu["20120407"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gasshuku.gif\" alt=\"合宿\" width=\"25\" height=\"12\"></td><td>4/7<small>（土）</small>-4/12<small>（木）</small></strong></td></tr>";
tokubetsu["20120408"] = "<tr><td width=\"10\"><img src=\"images/top/icn_kouza.gif\" alt=\"講座\" width=\"25\" height=\"12\"></td><td class=\"blue\">4/7,8,14,15,21,22</small></td></tr>";

tokubetsu["20120428"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gw.png\" alt=\"GW\" width=\"25\" height=\"12\"></td><td class=\"olive\">4/28<small>（土）</small>-5/3<small>（木）</small></strong></td></tr>";
tokubetsu["20120429"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gw.png\" alt=\"GW\" width=\"25\" height=\"12\"></td><td class=\"olive\">5/1<small>（火）</small>-5/6<small>（日）</small></strong></td></tr>";

tokubetsu["20120505"] = "<tr><td width=\"10\"><img src=\"images/top/icn_gasshuku.gif\" alt=\"合宿\" width=\"25\" height=\"12\"></td><td>5/5<small>（土）</small>-5/10<small>（木）</small></strong></td></tr>";
tokubetsu["20120510"] = "<tr><td width=\"10\"><img src=\"images/top/icn_kouza.gif\" alt=\"講座\" width=\"25\" height=\"12\"></td><td class=\"blue\">5/5,6,12,13,19,20</small></td></tr>";


//講座日程
var kouzaDates = new Array("20080118");

//Set variables
var strTrStart = "<tr><td width=\"30\"><img src=\"images/top/icn_gasshuku.gif\" alt=\"\" width=\"25\" height=\"12\"></td><td>";
var strTrKouzaStart = "<tr><td width=\"27\"><img src=\"images/top/icn_kouza.gif\" alt=\"\" width=\"25\" height=\"12\"></td><td>";
var strTrArwStart = "<tr><td width=\"27\" align=\"right\"><img src=\"images/top/arw_red.gif\" alt=\"\" width=\"8\" height=\"12\">&nbsp;</td><td>";
var strTrEnd = "</td></tr>";

var startTime = new Date();
var startMonth;
var startDate;
var startDay;
var endTime = new Date();
var endMonth;
var endDate;
var endDay;
var kouzaEndTime = new Date();
var kouzaEndMonth;
var kouzaEndDate;
var kouzaEndDay;
var weekdays = new Array("日", "月", "火", "水", "木", "金", "土");
var daygap;
var comparingDate;
var lastGasshukuStart;
var lastKouzaStart;

//Get localtime
var now = new Date();
var nowDay = now.getDay();

//Output
document.write("<table class=\"nittei\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">");

//現在曜日から出力開始日程を算出
if( nowDay < crtDay ) {
	daygap = crtDay - nowDay ;
	startTime.setTime( now.getTime() + (daygap * 24 * 3600 * 1000) );
}
else if( nowDay > crtDay ) {
	daygap = crtDay - nowDay + 7;
	startTime.setTime( now.getTime() + (daygap * 24 * 3600 * 1000) );
}

//行数分ループし、日程出力
var i = 0;
while ( i < lines ) {

	//初回終了日Time設定
	endTime.setTime( startTime.getTime() + (lastdays * 24 * 3600 * 1000) );

	//日付関連変数設定
	startMonth = startTime.getMonth() + 1;
	startDate = startTime.getDate();
	startDay = startTime.getDay();
	endMonth = endTime.getMonth() + 1;
	endDate = endTime.getDate();
	endDay = endTime.getDay();

	//講座終了日Time設定
//2008/08/24	kouzaEndTime.setTime( startTime.getTime() + (14 * 24 * 3600 * 1000) );
	kouzaEndTime.setTime( startTime.getTime() + (13 * 24 * 3600 * 1000) );

	//講座日付関連変数設定
	kouzaEndMonth = kouzaEndTime.getMonth() + 1;
	kouzaEndDate = kouzaEndTime.getDate();
	kouzaEndDay = kouzaEndTime.getDay();
	
//TOEIC試験日程と比較し、1週間前以内の開催なら出力
	//比較用日程文字列設定
	comYear = startTime.getYear();
	//document.write(comYear);
	if (comYear < 2000) { comYear += 1900; }
	if ( startMonth < 10 ) { comMonth = "0" + startMonth; } else { comMonth = startMonth + ""; }
	if ( startDate < 10 ) { comDate = "0" + startDate; } else { comDate = startDate + ""; }
	comparingDate = comYear + comMonth + comDate;
	comparingDate = parseInt( comparingDate );
	//TOEIC日時Arrayループ
	for ( n in toeicDates ) {
		//TOEIC日程文字列設定
		var intToeicDate = parseInt(toeicDates[n]);
		//document.write(intToeicDate+"<br>");
		//TOEIC日程が1週間以内ならTOEIC日程出力
		if( ((intToeicDate - comparingDate < 7) && (intToeicDate - comparingDate >= 0) && (i < lines))
			||//以下追記(2010/10)　年をまたぐ日程への対応のため 6007,6000などの数値は１年先の日付を拾わないため
			((intToeicDate - comparingDate < 6007) && (intToeicDate - comparingDate >= 6000) && (i < lines)) ){
			toeicDates[n].match(/\d\d\d\d(\d\d)(\d\d)/);
			var toeicMonth = RegExp.$1;
			var toeicDate = RegExp.$2;
			if( toeicMonth.match(/0(\d)/) ){ toeicMonth = RegExp.$1; }
			if( toeicDate.match(/0(\d)/) ){ toeicDate = RegExp.$1; }
			document.write(strTrArwStart);
			document.write( "<span class=\"red\"><small>TOEIC公開試験</small> " + toeicMonth + "/" + toeicDate + "</span>");
			document.write(strTrEnd);
			i++;
		}
	}
/*
	//特別日時Arrayループ
	if( i < lines ){
		for ( s in specialDates ) {
			//特別日程文字列設定
			var intSpecialDate = parseInt(specialDates[s]);
			//特別日程が1週間以内なら特別日程出力
			if( ( intSpecialDate - comparingDate < 7 ) && ( intSpecialDate - comparingDate >= 0 ) && ( i < lines ) ){
				specialDates[s].match(/(\d\d\d\d)(\d\d)(\d\d)/);
				var specialYear = RegExp.$1;
				var specialMonth = RegExp.$2;
				var specialDate = RegExp.$3;
				//開始曜日算出
				var startSpecialTime = new Date( specialYear, specialMonth-1, specialDate );
				var startSpecialDay = startSpecialTime.getDay();
				//終了日を算出
				var endSpecialTime = new Date();
				endSpecialTime.setTime( startSpecialTime.getTime() + (lastdays * 24 * 3600 * 1000) );
				var endSpecialMonth =  endSpecialTime.getMonth() + 1;
				var endSpecialDate = endSpecialTime.getDate();
				var endSpecialDay = endSpecialTime.getDay();
	
				//出力
				if( specialMonth.match(/0(\d)/) ){ specialMonth = RegExp.$1; }
				if( specialDate.match(/0(\d)/) ){ specialDate = RegExp.$1; }
				document.write("<tr><td width=\"10\"><span class=\'red\'><small>★</small></span></td><td>");
				document.write( specialMonth + "/" + specialDate + "<small>（" + weekdays[startSpecialDay] + "）</small>-");
				document.write( endSpecialMonth + "/" + endSpecialDate +"<small>（" + weekdays[endSpecialDay] + "）</small>");
				document.write(strTrEnd);
				i++;
			}
		}
	}
*/
	//特別日時Arrayループ
	if( i < lines ){
		for ( t in tokubetsu ) {
			//特別日程文字列設定
			var intTokubetsu = parseInt(t);
			
			//特別日程が1週間以内なら特別日程出力
			if( ( intTokubetsu - comparingDate < 7 ) && ( intTokubetsu - comparingDate >= 0 )  ){

				t.match(/(\d\d\d\d)(\d\d)(\d\d)/);
				var tokubetsuYear = RegExp.$1;
				var tokubetsuMonth = RegExp.$2;
				var tokubetsuDate = RegExp.$3;
				
				//出力
				document.write(tokubetsu[t]);
				i++;
			}
		}
	}

	//講座日程Arrayループ
	/*
	if( i < lines ){
		for ( s in kouzaDates ) {
			//特別日程文字列設定
			var intKouzaDate = parseInt(kouzaDates[s]);
			//特別日程が1週間以内なら特別日程出力
			if( ( intKouzaDate - comparingDate < 7 ) && ( intKouzaDate - comparingDate >= 0 ) && ( i < lines ) ){
				kouzaDates[s].match(/(\d\d\d\d)(\d\d)(\d\d)/);
				var kouzaYear = RegExp.$1;
				var kouzaMonth = RegExp.$2;
				var kouzaDate = RegExp.$3;
				//開始曜日算出
				var startKouzaTime = new Date( kouzaYear, kouzaMonth-1, kouzaDate );
				var startKouzaDay = startKouzaTime.getDay();
				//終了日を算出
				var endKouzaTime = new Date();
				endKouzaTime.setTime( startKouzaTime.getTime() + (14 * 24 * 3600 * 1000) );
				var endKouzaMonth =  endKouzaTime.getMonth() + 1;
				var endKouzaDate = endKouzaTime.getDate();
				var endKouzaDay = endKouzaTime.getDay();
	
				//出力
				if( kouzaMonth.match(/0(\d)/) ){ kouzaMonth = RegExp.$1; }
				if( kouzaDate.match(/0(\d)/) ){ kouzaDate = RegExp.$1; }
				document.write(strTrKouzaStart);
				document.write( kouzaMonth + "/" + kouzaDate + "<small>（" + weekdays[startKouzaDay] + "）</small>-");
				document.write( endKouzaMonth + "/" + endKouzaDate + "<small>（" + weekdays[endKouzaDay] + "）</small>");
				document.write(strTrEnd);
				i++;
			}
		}
	}
	*/
	
	//合宿日程出力
	nowrite = 0;
	if( i < lines ){
		for ( c in deletingDates ) {
			var intDeletingDate = parseInt(deletingDates[c]);
			if( ( intDeletingDate == comparingDate ) || ( i > lines ) ) {
				nowrite++;
			}
		}
	}
        if( comparingDate == 20081024) nowrite++;
	if( nowrite == 0 ){
			document.write(strTrStart);
			document.write(startMonth + "/" + startDate  + "<small>（" + weekdays[startDay] + "）</small>-");
			document.write(endMonth + "/" + endDate + "<small>（" + weekdays[endDay] + "）</small>");
			document.write(strTrEnd);
			i++;
			lastGasshukuStart = comparingDate;
	}
	
	//講座日程出力
/*	nowrite = 0;
	for ( d in deletingKouzaDates ) {
		var intDeletingKouzaDate = parseInt(deletingKouzaDates[d]);
		if( ( intDeletingKouzaDate == comparingDate ) || ( i > lines ) ) {
			nowrite++;
		}
	}
	if( comparingDate - lastKouzaStart < 14 ){
		nowrite++;
	}
        if( comparingDate == 20081017) nowrite++;
//        if( comparingDate == 20081024) nowrite++;
        if( comparingDate == 20081114) nowrite=0;
	if( nowrite == 0 ){
			document.write(strTrKouzaStart);
			document.write(startMonth + "/" + startDate  + "<small>（" + weekdays[startDay] + "）</small>-");
			document.write(kouzaEndMonth + "/" + kouzaEndDate + "<small>（" + weekdays[kouzaEndDay] + "）</small>");
			document.write(strTrEnd);
			i++;
			lastKouzaStart = comparingDate;
	}
*/	
	startTime.setTime( startTime.getTime() + (7 * 24 * 3600 * 1000) );

}

document.write("<tr><td colspan=\"2\">※毎週金〜水曜日<span class=\"text-s\">（通年開催）</span></td></tr></table>");


