var calName; // the name of last calendar called
var calDate;  // the last selected date in the calendar
var pickedCell;  // last cell picked, so we know to remove the gold background colour
var pickedDates = new Array(); // associative array of selected times (uses calName as key)

function toggleSelects(state)
{
	visib = (state) ? 'visible' : 'hidden';
	for (var j = 0; j < document.forms.length; j++)
	{
		for (var i = 0; i < document.forms[j].elements.length; i++)
		{
			if (document.forms[j].elements[i].selectedIndex >= 0 && document.forms[j].elements[i].id != 'calHour' && document.forms[j].elements[i].id != 'calMinute')
				document.forms[j].elements[i].style.visibility = visib;
		}
	}

}

function isLeapYear(yr) {
	return new Date(yr,2-1,29).getDate()==29;
}

function monthName(someDate)
{
	var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	return months[someDate.getMonth()];
}

function daysInMonth(someDate)
{
	var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	monthDays[1] = (isLeapYear(someDate.getYear())) ? 29 : 28;
	return monthDays[someDate.getMonth()];	
}

function showCal(calName, someDate)
{
	// loop through the form and hide all select's because of the IE bug
	toggleSelects(false);

	// make the calendar visible and position it where the mouse is
	document.getElementById('calendar').style.left = event.clientX+5;
	document.getElementById('calendar').style.top = event.clientY-210 + document.body.scrollTop;
	document.getElementById('calendar').style.visibility = 'visible';
	this.calName = calName;
	
	// check if there is a unix time in the calName already, if so, use it as default time
	if (someDate != null)
		makeCal(someDate);
	if (this.pickedDates[calName])
		makeCal(this.pickedDates[calName]);
	else
		makeCal();
}

function hideCal()
{
	// loop through the form and show all select's because of the IE bug
	toggleSelects(true);

	document.getElementById('calendar').style.visibility = 'hidden';
	// the end coder defines callDone()
	this.pickedDates[this.calName] = new Date(this.calDate.getTime());
	calDone(this.calName, this.calDate);
}

function cancelCal()
{
	// loop through the form and show all select's because of the IE bug
	toggleSelects(true);

	document.getElementById('calendar').style.visibility = 'hidden';
	// the end coder defines callDone()
	this.pickedDates[this.calName] = null;
	calDone(this.calName, null);
}

function makeCal(someDate)
{
	if (someDate)
		this.calDate = someDate;
	else if (!this.calDate)
		this.calDate = new Date();

	// do some rounding and cleaning up
	this.calDate.setMinutes(Math.round(this.calDate.getMinutes() / 10) * 10);
	this.calDate.setSeconds(0);

	// update the header to show the month, year
	document.getElementById('cal0').innerHTML = monthName(this.calDate) + ', ' + this.calDate.getYear();

	// how many cells to skip until we get the 1st day of the month
	var firstDay = new Date(this.calDate.getYear(), this.calDate.getMonth(), 1);
	//firstDay.setDate(1);
	var offset = firstDay.getDay();
	
	// iterate every cell to populate with dates
	var currDay = 1;
	for (var i = 1; i <= 42; i++)
	{
		if (i < offset + 1 || currDay > daysInMonth(this.calDate))
		{
			document.getElementById('cal'+i).innerHTML = '&nbsp;';
		}
		else
		{
			document.getElementById('cal'+i).innerHTML = currDay;
			if (currDay == this.calDate.getDate())
				pickDay(i);
			currDay++;
		}
	}
	
	// set the time on the drop down lists
	for (var i = 0; i < document.getElementById('calHour').length; i++)
	{
		if (document.getElementById('calHour').options[i].value == this.calDate.getHours())
			document.getElementById('calHour').options[i].selected = true;
	}

	for (var i = 0; i < document.getElementById('calMinute').length; i++)
	{
		if (document.getElementById('calMinute').options[i].value == this.calDate.getMinutes())
			document.getElementById('calMinute').options[i].selected = true;
	}
}

function pickDay(i)
{
	pickedDay = parseInt(document.getElementById('cal'+i).innerHTML);
	if (isNaN(pickedDay))
		return false;

	this.calDate.setDate(pickedDay);
	if (pickedCell)
		document.getElementById('cell'+pickedCell).style.backgroundColor = 'white';
	document.getElementById('cell'+i).style.backgroundColor = 'gold';

	pickedCell = i;
	return true;
}

function pickHour()
{
	// this code is done in-line, see cal.php
}

function pickMinute()
{
	// this code is done in-line, see cal.php
}

function prevMonth()
{
	this.calDate.setMonth(calDate.getMonth()-1);
	makeCal();
}

function nextMonth()
{
	this.calDate.setMonth(calDate.getMonth()+1);
	makeCal();
}
