//************** calendar utilities **********************
var defaultAge = 16;
function showCalendar(id)
{
	var calendarArea = document.getElementById(id);
	if (document.getElementById(calendarDivID) != null && id != calendarDivID)
	{ document.getElementById(calendarDivID).style.display = 'none'; }
	
	if (calendarArea.style.display == 'none')
	{
		calendarArea.style.display = '';
		calendarDivID = id;
		
		var box = document.getElementById(id.substring(0, id.length - 5));
		//removeYellow();
		//editing(rowID, box);
		
		if (box.value == '')
		{
			var tempDate = new Date();
			calendarArea.innerHTML = drawCalendar(tempDate.getFullYear() - defaultAge, tempDate.getMonth() + 1, tempDate.getDay());
		}
		else
		{
			var dateArr;
			dateArr = box.value.split('/');
			calendarArea.innerHTML = drawCalendar(parseInt(dateArr[2]), parseInt(dateArr[0]), parseInt(dateArr[1]));
		}
	}
	else
	{ hideCalendar(); }
}

function browseCalendar(year, month, increment)
{
	month += increment;
	if (month < 1)
	{
		year--;
		month = 12;
	}
	else if (month > 12)
	{
		year++;
		month = 1;
	}
	
	document.getElementById(calendarDivID).innerHTML = drawCalendar(year, month);
	
	return false;
}

function drawCalendar(year, month, date)
{
	//alert(month + '/' + date + '/' + year);
	var returnStr, i, lastDay, weekDay, tempDate = new Date();
	returnStr = '<center><table style=""color: #000000;"">';
	//returnStr += '<tr><td colspan="13" align="center"><a href="#" onclick="return setCalendarValue(' + tempDate.getFullYear() + ',' + (tempDate.getMonth() + 1) + ',' + tempDate.getDate() + ');">Today</a><br><br></td></tr>';
	returnStr += '<tr><td colspan="13" align="center"><img src="' + html_dirPrefix + 'images/x.gif" alt="spacer image" height="10" width="1"></td></tr>';
	
	if (year < firstCalendarYear || (year == firstCalendarYear && month == 1))
	{ returnStr += '<td></td>'; }
	else
	{ returnStr += '<td><img src="' + html_dirPrefix + 'images/prev.gif" onclick="browseCalendar(' + year + ',' + month + ',-1);"></td>'; }
	
	returnStr += '<td colspan="11" align="center">' + monthArr[month] + ', <select onfocus="calendarOver(calendarDivID);" onchange="document.getElementById(calendarDivID).innerHTML = drawCalendar(this.options[this.selectedIndex].value,' + month + ',' + date + ');">';
	for (i = firstCalendarYear; i <= lastCalendarYear; i++)
	{
		returnStr += '<option value="' + i + '"';
		if(i == year)
		{ returnStr += ' selected'; }
		returnStr += '>' + i + '</option>';
	}
	returnStr += '</select></td>';
	
	if (year > lastCalendarYear || (year == lastCalendarYear && month == 12))
	{ returnStr += '<td></td>'; }
	else
	{ returnStr += '<td align="right"><img src="' + html_dirPrefix + 'images/next.gif" onclick="browseCalendar(' + year + ',' + month + ',1);"></td>'; }
	returnStr += '<tr><td colspan="13">&nbsp;</td></tr>';
	
	returnStr += '<tr><td align="center">' + weekArr[0] + '</td>';
	for (i = 1; i < 7; i++)
	{ returnStr += '<td>&nbsp;&nbsp;</td><td align="center">' + weekArr[i] + '</td>'; }
	returnStr += '</tr><tr><td colspan="13"><img src="' + html_dirPrefix + 'images/x.gif" alt="spacer image" height="2" width="1"></td></tr>';
	
	month--;
	for (i = 31; i > 27; i--)
	{
		tempDate.setFullYear(year, month, i);
		if (tempDate.getDate() == i)
		{
			lastDay = i;
			break;
		}
	}
	tempDate.setFullYear(year, month, 1);
	weekDay = tempDate.getDay();
	
	if (weekDay > 0)
	{
		returnStr += '<tr>';
		for (i = 0; i < weekDay; i++)
		{ returnStr += '<td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td>'; }
	}
	
	for (i = 1; i <= lastDay; i++)
	{
		if (weekDay == 0)
		{ returnStr += '<tr>'; }
		
		returnStr += '<td align="center"'
		tempDate.setDate(i);
		if (compareDate(tempDate))
		{ returnStr += ' class="grayInput"'; }
		returnStr += '><a href="#" onclick="return setCalendarValue(' + year + ',' + (month + 1) + ',' + i + ');">' + i + '</a></td>';
		
		weekDay++;
		
		if (weekDay < 7)
		{ returnStr += '<td>&nbsp;&nbsp;</td>'; }
		else
		{
			returnStr += '</tr><tr><td colspan="13"><img src="' + html_dirPrefix + 'images/x.gif" alt="spacer image" height="5" width="1"></td></tr>';
			weekDay = 0;
		}
	}
	
	if (weekDay > 0)
	{
		for (i = 0; i < ((2 * (7 - weekDay)) - 1); i++)
		{ returnStr += '<td>&nbsp;&nbsp;</td>'; }
		returnStr += '</tr><tr><td colspan="13"><img src="' + html_dirPrefix + 'images/x.gif" alt="spacer image" height="5" width="1"></td></tr></table></center><img src="' + html_dirPrefix + 'images/x.gif" alt="spacer image" height="5" width="1">';
	}
	else
	{ returnStr += '</table></center><img src="' + html_dirPrefix + 'images/x.gif" alt="spacer image" height="5" width="1">'; }
	
	//alert(returnStr);
	return returnStr;
}

function compareDate(date)
{
	var bDateArr = document.getElementById(calendarDivID.substring(0, calendarDivID.length - 5)).value.split('/');
	var bDate = new Date();
	bDate.setFullYear(parseInt(bDateArr[2]), parseInt(bDateArr[0]) - 1, parseInt(bDateArr[1]));
	return (date.toDateString() == bDate.toDateString());
}

function setCalendarValue(year, month, date)
{
	var box = document.getElementById(calendarDivID.substring(0, calendarDivID.length - 5));
	box.value = month + '/' + date + '/' + year;
	hideCalendar();
	
	return false;
}

function calendarOver(id)
{ eval('over' + id + ' = true;'); }

function calendarOut(id)
{
	eval('over' + id + ' = false;');
	clearInterval(myInterval);
	//myInterval = setInterval(removeCalendar, calendarDelay * 1000);
}

function removeCalendar()
{
	if (calendarDivID != '' && !eval('over' + calendarDivID))
	{
		hideCalendar();
		clearInterval(myInterval);
	}
}

function hideCalendar()
{
	document.getElementById(calendarDivID).style.display = 'none';
	//calendarDivID = '';
	//removeYellow();
}
//********************************************************