var cals = new Array();
var cal_month = new Array();
var cal_year = new Array();
var now_month;
var now_year;

function init(prefix)
{
	cals[prefix] = document.getElementById('cal_' + prefix);
	
	if(cals[prefix].offsetLeft != null && cals[prefix].style.left == '')
	{
		cals[prefix].style.left = (cals[prefix].offsetLeft + 10) + "px";
		cals[prefix].style.top = (cals[prefix].offsetTop - 152) + "px";
	}
	
	var date = new Date();
	now_year = date.getFullYear();
	now_month = new Date(now_year, date.getMonth(), date.getDate()+1).getMonth(); 
	// On the last day of the month, should display next month, not this month, otherwise this month.

	// If a date is saved, load calendar according to that, otherwise current date
	if(document.getElementById(prefix + "_mm").value != "")
		cal_month[prefix] = document.getElementById(prefix + "_mm").value - 1;
	else
		cal_month[prefix] = now_month;
	
	
	if(document.getElementById(prefix + "_yyyy").value != "")
		cal_year[prefix] = document.getElementById(prefix + "_yyyy").value;
	else
		cal_year[prefix] = now_year;
		
	// Make sure saved date is not out of valid range
	if(prefix != 'departure') // For arrivals, cannot be more than 4 months ahead
	{
		if((now_month + 4) % 12 < cal_month[prefix])
		{
			cal_month[prefix] = now_month;
			cal_year[prefix] = now_year;
		}
	}
	
	// Can never be in the past
	if(cal_month[prefix] < now_month && cal_year[prefix] == now_year)
	{
		cal_month[prefix] = now_month;
		cal_year[prefix] = now_year;
	}
	
	create(prefix);
}

function show(prefix)
{
	var cal = cals[prefix];
	
	// Close all other calendars
	for(key in cals)
	{
		if(key != prefix)
			cals[key].style.visibility = "hidden";
	}
	
	// Create a new calendar object
	if(!cal)
	{
		init(prefix);
		cal = cals[prefix];	
	}
	if(cal.style.visibility == "visible")
		cal.style.visibility = "hidden";
	else
		cal.style.visibility = "visible";
}

function create(prefix)
{	
	var month = cal_month[prefix];
	var year = cal_year[prefix]
	weekday = new Date(month, year, 1).getDay();
	var html = "<table class='calendar' cellspacing='0'><tr><td align='left'>";
	if(month != now_month || year != now_year) 
		html += "<input type='button' value='&lt;' onclick='previous_month(\"" + prefix + "\")' />";
	html += "</td><td colspan='5' style='text-align:center'><b>" + months[month] + " " + year + "</b></td>";
	html += "<td align='right'>";
	if(month != 11 || year != now_year + 1)
		html += "<input type='button' value='&gt;' onclick='next_month(\"" + prefix + "\")' />";
	html += "</td></tr>";
	
	html += "<tr>";
	for(i = 0; i < 7; i++)
		html += "<th>" + days[i] + "</th>";
	
	html += "</tr>";
	
	var day = 0 - weekday; // So we don't see the first few non-days in the week
	var date = new Date();
	var today = date.getDate();
	var now_month_actual = date.getMonth(); // This is normally the same value as now_month, but if it's the last day of the month will be different
											// Without this, on the last day of the month, the next month's entries are not clickable (see code below)
	
	days_in_month = 32 - new Date(year, month, 32).getDate();
	while(day <= days_in_month)
	{
		html += "<tr>";
		for(i = 0; i < 7; i++)
		{
			html += "<td>";
			if(day > 0 && day <= days_in_month)
			{
				// Don't allow users to select days that are either before tomorrow OR
				// more than 4 months in advance (arrival only)
				if((month == now_month_actual && year == now_year && day < today + 1) || (prefix != 'departure' && month == (now_month + 4) % 12 && day >= today))
					html += day;
				else
					html += "<a href='javascript:select_day(" + day + ",\"" + prefix + "\")' class='day_link'>" + day + "</a>";
			}
			html += "</td>";

			day++;
		}
		html += "</tr>";
	}
	
	html += "</table>";
	cals[prefix].innerHTML = html;
}

function previous_month(prefix)
{
	cal_month[prefix]--;
	if(cal_month[prefix] < 0)
	{
		cal_month[prefix] += 12;
		cal_year[prefix]--;
	}
	create(prefix);
}

function next_month(prefix)
{	
	if(prefix != 'departure')	// For arrival dates
	{
		if((now_month + 4) % 12 == cal_month[prefix])
		{
			alert(four_month_error);
			return false;
		}
	}
	
	cal_month[prefix]++;
	
	if(cal_month[prefix] > 11)
	{
		cal_month[prefix] -= 12;
		cal_year[prefix]++;
	}
	create(prefix);
}

function select_day(day, prefix)
{	
	document.getElementById(prefix + "_dd").selectedIndex = day;
	document.getElementById(prefix + "_mm").selectedIndex = cal_month[prefix] + 1;
	document.getElementById(prefix + "_yyyy").selectedIndex = cal_year[prefix] - now_year + 1;
	cals[prefix].style.visibility = "hidden";
}