// selectForm is used throughout as the name of the form collecting the data. IF that is not the name of your form
// you must change it here.
// the SelectDate routine is used to return the selected date to the form. see routine for more info.

var monthArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthNumArray = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
var calDiv, IfrRef;
var Which;

function hideCalendar() {

	calDiv.style.display = "none";
	IfrRef.style.display = "none";
}

function SelectDate(selMo, selDay, selYear)
{
	// used for curent traveldesk with drop downs to be filled in.
	// names of the fields come after the selectForm
	var DaysObject = eval("document.selectForm." + Which + "Day");
	var MonthObject = eval("document.selectForm." + Which + "Month");

	MonthObject.selectedIndex = selMo;
	DaysObject.selectedIndex = selDay - 1;

	hideCalendar();
}

function FormatDate(dateValue)
{
	// At this point all we have is a string like 01/01/2004
	// We must have a date object, so we make one based on the string date
	// and return it to the main function
	var dateValArray = dateValue.split("/");
	var currentDate = new Date(dateValArray[2],dateValArray[0]-1,dateValArray[1]);
	return currentDate;
}

function FormatRawDate(rawDateValue)
{
	// The next/previous buttons have date object references. They need to be strings
	// so we can send them to the function when the next/previous links are clicked.
	// the string date is returned and placed into the link.
	var rawMonth = rawDateValue.getMonth()+1;
	var rawDay = rawDateValue.getDate();
	var rawYear = rawDateValue.getFullYear();
	var stringDate = rawMonth + "/" + rawDay + "/" + rawYear;
	return stringDate;
}

function formatDay(vday) {
	var gNow = new Date();

	if (vday.getDate() == gNow.getDate() && vday.getMonth() == gNow.getMonth() && vday.getFullYear() == gNow.getFullYear()) {
		return ("<FONT COLOR=\"RED\"><B>" + vday.getDate() + "</B></FONT>");
	} else {
		return (vday.getDate());
	}
}
function ShowCalendar(fieldSet, dateValue)
{
	calDiv = document.getElementById("calendar");
	IfrRef = document.getElementById('DivShim');

	Which = fieldSet; // required input for the month drop down

	// Optional input to set the date value to the input value;
	// if there is no date yet, make a new one. Otherwise, use the date in the input box
	// or the date passed by the next/previous buttons.
	var currentDate = (dateValue == null || dateValue == "") ?  new Date() : FormatDate(dateValue);

	var prevMonth = new Date(currentDate);
	prevMonth.setMonth(currentDate.getMonth()-1);

	var nextMonth = new Date(currentDate);
	nextMonth.setMonth(currentDate.getMonth()+1);

	var currentYear = currentDate.getFullYear();

	var currentMonth = currentDate.getMonth();

	// shift the current day to day 1 to make calendar to build it from day 1.
	var firstDay = new Date(currentDate);
	firstDay.setDate(1);

	var currentDay = new Date(firstDay);

	// clear current calendar (if exists)
	var calHTML = "";

	// show calendar
	calDiv.style.display = "block";

	// write out calendar header
	calHTML += '<table cellpadding="4" cellspacing="0" border="0" width="150" class="calendar"><tr><td>';
	calHTML += '<a href="" onclick="ShowCalendar(\'First\',\''+FormatRawDate(prevMonth)+'\'); return false;"><img src="images/arrow_left.jpg" width="15" height="15" border=0 alt="Click for previous month"></a>';
	calHTML += '</td><td colspan="5" class="calhead" align="center">' + monthArray[currentMonth] + ' ' + currentYear + '</td><td>';
        calHTML += '<a href="" onclick="ShowCalendar(\'First\',\''+FormatRawDate(nextMonth)+'\'); return false;"><img src="images/arrow_right.jpg" width="15" height="15" border=0 alt="Click for next month"></a></td></tr>';
	calHTML += '<tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr>';

	var curCell = 1;

	// as long as we are in the current month, write out the calendar days
	while(currentDay.getMonth() == firstDay.getMonth())
	{
		// begin row
		calHTML += '<tr>';
		// iterate through each week
		for (var i=0; i<7; i++)
		{
			calHTML += '<td>';
			// as long as this week is in the same month, write out days
			if(currentDay.getMonth() == firstDay.getMonth())
			{
				// if first day is not reached yet, write a blank
				if(curCell <= firstDay.getDay())
				{
					calHTML += '&nbsp;';
					curCell++
				}
				// otherwise write out the date
				else
				{
					calHTML += '<a href="" title="Click to choose this date" onclick="SelectDate(' + currentDay.getMonth() + ',' + currentDay.getDate() + ',' + currentDay.getFullYear() + '); return false;">' + formatDay(currentDay) + '</a>';
					curCell++
					currentDay.setDate(currentDay.getDate() + 1);
				}
			} else {
				calHTML += '&nbsp;';
				curCell++
			}
			calHTML += '</td>';
		}
	calHTML += '</tr>';
	}
	calHTML += '</table><br><center><a href="" onclick="hideCalendar(); return false;"><img src="images/closebutton.jpg" width="19" height="19" border=0 alt="Click here to close calendar"></a></center>';
	calDiv.innerHTML = calHTML;

	IfrRef.style.width = calDiv.offsetWidth;
	IfrRef.style.height = calDiv.offsetHeight;
	IfrRef.style.top = calDiv.style.top;
	IfrRef.style.left = calDiv.style.left;
	IfrRef.style.zIndex = calDiv.style.zIndex - 1;
	IfrRef.style.display = "block";
}

