// JavaScript Document

var box;
var div;
var bigDate;
	
function clearCal(value) {
	detachCloseListener();
	
	if (div)
		div.parentNode.removeChild(div);
	
	if (value != '')
		box.value = value;
		
	box = undefined;
	div = undefined;
}

function updateDate(date) {

	var month = bigDate.getMonth() + 1;
	var year = bigDate.getFullYear();

	if (month < 10)
		month = "0" + month;
	if (date < 10)
		date = "0" + date;

	clearCal(year + "-" + month + "-" + date);
}

function initCal(mbox) {	
	if (box)
		clearCal('');

	box = mbox;
	box.setAttribute('autocomplete', 'off');
	if (typeof box.onfocus == 'function')
		box.onclick = box.onfocus;

	div = document.createElement('div');
	document.body.appendChild(div);

	box.onkeyup = function(event) { if (getKey(event) == 27) clearCal(''); };

	var pos = findPos(box);
	div.style.position = "absolute";
	div.style.left = pos.left + "px";
	div.style.top = pos.top + box.offsetHeight + "px";
	div.style.width = box.offsetWidth + "px";
	div.style.zIndex = 10;
	
	if (typeof bigDate == 'undefined' || !bigDate) {
		bigDate = new Date();

		var matches;
		if (box.value && (matches = box.value.match(/(\d{4})-(\d{2})-\d{2}/))) {
			bigDate.setFullYear(matches[1]);
			bigDate.setMonth(matches[2] - 1);
		}
	}

	showCal();
	window.setTimeout('attachCloseListener()', 250);
}

function attachCloseListener()
{
	document.body.onclick = function() { clearCal(""); document.body.onclick = null;  if (div) div.onclick = null; };

	if (div)
		div.onclick =
			function(event) {
				if (!event) event = window.event;
				event.cancelBubble = true;
				if (event.stopPropagation) event.stopPropagation();
			};
}

function detachCloseListener()
{
	document.body.onclick = null;
	div.onclick = null;
}

function changeMonth(change) {
	bigDate.setMonth(bigDate.getMonth() + change);
	showCal();
}

function getMonth(m) {
	var m_array = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	
	return m_array[m];
}

function getMonthLength(m) {
	var d_array = [31,28,31,30,31,30,31,31,30,31,30,31];
	var year = bigDate.getFullYear();
	if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
		d_array[1] = 29;
	
	return d_array[m];
}

function firstOfMonth(d) {
	d.setDate(1);

	return d.getDay();
}

function drawTable(firstOfMonth, numberDays, month) {
	var today = new Date();
	var count = 0;
	var table = '<table border="0" cellspacing="0" cellpadding="0" class="small_calendar" width="100%">';
	
	table += '<tr>';
	table += '<td class="small_calendar_month">';
	table += '<a href="javascript: changeMonth(-1);" class="small_calendar_month_link">&lt;</a>';
	table += '</td><td colspan="5" class="small_calendar_month">';
	table += month + " " + bigDate.getFullYear();
	table += '</td><td class="small_calendar_month">';
	table += '<a href="javascript: changeMonth(1);" class="small_calendar_month_link">&gt;</a>';
	table += '</td></tr>';
	
	table += '<tr align="center">';
	table += '<td width="14%" class="small_calendar_week">S</td>';
	table += '<td width="14%" class="small_calendar_week">M</td>';
	table += '<td width="14%" class="small_calendar_week">T</td>';
	table += '<td width="14%" class="small_calendar_week">W</td>';
	table += '<td width="14%" class="small_calendar_week">T</td>';
	table += '<td width="14%" class="small_calendar_week">F</td>';
	table += '<td width="14%" class="small_calendar_week">S</td>';
	table += '</tr>';
	
	for (var i = 0; i < Math.ceil((numberDays + firstOfMonth) / 7); i++) {
		table += '<tr align="center">';
		for (var j = 0; j < 7; j++) {
			if (firstOfMonth <= 7*i+j)
				count++;

			table += '<td';
			if (count > 0 && count <= numberDays)
			{
				table += ' onclick="updateDate(' + count + ');" style="';
				if ((7*i+j) % 2 == 0)
					table += 'background-color:#DFE9FF;';
							
				table += (today.getFullYear() == bigDate.getFullYear() && today.getDate() == count && today.getMonth() == bigDate.getMonth()) ? 'font-weight: bold;">' : '">';
			
				table += '<a href="javascript: updateDate(' + count + ');" class="small_calendar_date_link">' + count + '</a>';
			}
			else
				table += '>&nbsp;';
			
			table += '</td>';
		}
		table += '</tr>';
	}

	table += '<tr><td colspan="4" class="small_calendar_bottom" onclick="clearCal(\'\');">';
	table += '<a href="javascript: clearCal(\'\');" class="small_calendar_bottom_link">Close</a></td>';
	table += '<td colspan="3" class="small_calendar_bottom">';
	table += '<a href="javascript: now();" class="small_calendar_bottom_link">Today</a></td></tr>';
	table += '</table>';

 	return table;
}

function now()
{
	bigDate = new Date();
	updateDate(bigDate.getDate());
}

function showCal() {
	var month = bigDate.getMonth();

	var monthName = getMonth(month);
	var monthLength = getMonthLength(month);
	var first = firstOfMonth(bigDate);	

	var table = drawTable(first, monthLength, monthName);	
	div.innerHTML = table;	
}