// UTF-8
/**
 * calendar.js
 * Copyright (c) 2011 Satake Studio, LPC.
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 *
*/
	// 月の移動ボタンの有無[1 or 0]
	var changebtn = 0;

	function setcalendar(thisId, strDate) {
		var thisDate = new Array(2);
		thisDate[0] = strDate.substr(0, 4);
		thisDate[1] = strDate.substr(5, 2);
		return calendar(thisId, thisDate);
	}

	function calendar(thisId, thisDate) {
		var toDate		= new Date();
		var toYear		= toDate.getFullYear();
		var toMonth		= toDate.getMonth() + 1;
		var toDays		= toDate.getDate();
		var toWeek		= toDate.getDay();
		var totalDate	= toYear + '' + dateFormat(toMonth) + '' + dateFormat(toDays);

		if (thisId == null && thisId == undefined) {
			thisId = 'calendar';
		}
		if (thisDate != null || thisDate != undefined) {
			toYear	= thisDate[0];
			toMonth	= thisDate[1];
		}

		var prevY = toYear;
		var prevM = toMonth - 1;
		var nextY = toYear;
		var nextM = toMonth + 1;

		if (toMonth == 1) {
			prevY -= 1;
			prevM  = 12;
		} else if (toMonth == 12) {
			nextY += 1;
			nextM  = 1;
		}

		var tmp = '';
		tmp += '<table cellspacing="0" cellpadding="0" border="0" class="cheader">';
		tmp += '<tr>';
		if (changebtn == 1) {
			tmp += '<td><a href="javascript:void(0);" onclick="calendar([' + prevY + ',' + prevM + ']);">&lt;&lt;</a></td>';
		}
		tmp += '<th>' + toYear + '年' + toMonth + '月' + '</th>';
		if (changebtn == 1) {
			tmp += '<td><a href="javascript:void(0);" onclick="calendar([' + nextY + ',' + nextM + ']);">&gt;&gt;</a></td>';
		}
		tmp += '</tr>';
		tmp += '<table cellspacing="0" cellpadding="0" border="0" class="cbody">';
		tmp += '<tr>';
		tmp += '<th>日</th>';
		tmp += '<th>月</th>';
		tmp += '<th>火</th>';
		tmp += '<th>水</th>';
		tmp += '<th>木</th>';
		tmp += '<th>金</th>';
		tmp += '<th>土</th>';
		tmp += '</tr>';

		var count  = 0;
		var monday = 0;

		for (var i = 1;i <= 31;i++) {
			var classAry = [];
			var classStr = '';

			var cDate	= new Date(toYear, toMonth - 1, i);
			var cYear	= cDate.getFullYear();
			var cMonth	= cDate.getMonth() + 1;
			var cDays	= cDate.getDate();

			if (cYear == toYear && cMonth == toMonth && cDays == i) {
				var cTotal	= cYear + '' + dateFormat(cMonth) + '' + dateFormat(cDays);
				var cWeek	= cDate.getDay();

				if (cWeek == 0) {
					tmp += '<tr>';
					count++;
					classAry.push('csun');
				} else if (cWeek == 6) {
					classAry.push('csat');
				} else if (cWeek == 1) {
					monday++;
				}

				if ((cMonth ==  1 && cDays ==  1) || (cMonth ==  2 && cDays == 11) || (cMonth ==  4 && cDays == 29) || (cMonth ==  5 && cDays ==  3) || (cMonth ==  5 && cDays ==  4) || (cMonth ==  5 && cDays ==  5) || (cMonth == 11 && cDays ==  3) || (cMonth == 11 && cDays == 23) || (cMonth == 12 && cDays == 23)) {
					classAry.push('chpy');
				}
				if ((cMonth ==  1 && monday == 2) || (cMonth ==  7 && monday == 3) || (cMonth ==  9 && monday == 3) || (cMonth == 10 && monday == 2)) {
					classAry.push('chpy');
				}
				if (cMonth == 3 && cDays == 20 && (cYear == 2012 || cYear == 2013 || cYear == 2016 || cYear == 2017 || cYear == 2020)) {
					classAry.push('chpy');
				}
				if (cMonth == 3 && cDays == 21 && (cYear == 2011 || cYear == 2014 || cYear == 2015 || cYear == 2018 || cYear == 2019)) {
					classAry.push('chpy');
				}
				if (cMonth == 9 && cDays == 22 && (cYear == 2012 || cYear == 2016 || cYear == 2020)) {
					classAry.push('chpy');
				}
				if (cMonth == 9 && cDays == 23 && (cYear == 2011 || cYear == 2013 || cYear == 2014 || cYear == 2015 || cYear == 2017 || cYear == 2018 || cYear == 2019)) {
					classAry.push('chpy');
				}

				if (i == 1 && cWeek != 0) {
					count++;
					for (var j = 1;j <= cWeek;j++) {
						tmp += '<td> </td>';
					}
				}

				if (totalDate == cTotal) {
					classAry.push('ctodate');
				}

				if (classAry.length >= 1) {
					classStr = ' class="' + classAry.join(' ') + '"';
				}

				tmp += '<td' + classStr + '>' + i + '</td>';

				if (cWeek == 6) {
					tmp += '</tr>';
				}
			}
		}
		if (cWeek != 6) {
			var last = 6 - (cWeek);
			for (var k = 1;k <= last;k++) {
				tmp += '<td> </td>';
			}
			tmp += '</tr>';
		}
		count = 6 - count;
		if (count != 0) {
			for (var l = 1;l <= count;l++) {
				tmp += '<tr>';
				for (var m = 0;m < 7;m++) {
					tmp += '<td> </td>';
				}
				tmp += '</tr>';
			}
		}
		tmp += '</table>';

		document.getElementById(thisId).innerHTML = tmp;

		return false;
	}

	function dateFormat(str) {
		if (str < 10) {
			return '0' + str;
		} else {
			return str;
		}
	}


