// Global vars
var current_container = null;

/**
*	Create 2-month calendar
*	- container_id: Object ID of the div that will container the calendars.
*	- date_select_id: Object ID of the date select object
*	- start_year: year for first month
*	- start_month: month for first month (0..11)
**/
function createCalendar(container_id, date_select_id, start_year, start_month) 
{            
	current_container = container_id;

	// Clean first
	$('#' + container_id).empty();

	var cal_container = document.getElementById(container_id);

	// Create for this/next month, or other months
	if (start_year == null && start_month == null) {

		// Get first day of this month
		var someDate1 = new Date();
		someDate1.setDate(1);

	} else {

		// Get date for first day of year/month
		var someDate1 = new Date();
		someDate1.setDate(1);
		someDate1.setMonth(start_month);
		someDate1.setFullYear(start_year);
		
	}

	// Get first day of next month
	var someDate2 = new Date();
	someDate2.setDate(1);
	someDate2.setFullYear(someDate1.getFullYear());
	someDate2.setMonth(someDate1.getMonth() + 1);

	// Create calendar month 1
	var days = getDaysInMonth(someDate1.getMonth(), someDate1.getFullYear());
	drawLayout(someDate1.getFullYear(), someDate1.getMonth(), days, cal_container, "prev", date_select_id);

	// Create calendar month 2
	var days = getDaysInMonth(someDate2.getMonth(), someDate2.getFullYear());
	drawLayout(someDate2.getFullYear(), someDate2.getMonth(), days, cal_container, "next", date_select_id);

	// Show calendar
	$('#' + container_id).fadeIn();
	
	
	// Hide selects in return box in IE6
	if ((container_id == 'pickup_calendar1' || container_id == 'pickup_calendar0') && $.browser.msie && $.browser.version == '6.0')
	{
		$('.box_return select').hide();
	}
}

// -----------------------------------------------------------------------

/**
*	Returns array with all days in given month/year
*	Note: month = 0..11
*	Note: days after the date from the global variable "last_date" will
*	be shown as disabled.
**/
function getDaysInMonth(month, year) {

	var today = new Date();

	// Set starting date
	var firstday = new Date();
	firstday.setDate(1);
	firstday.setMonth(month);
	firstday.setFullYear(year);

	// Set last day of month
	var lastday = new Date();
	lastday.setDate(1);
	lastday.setFullYear(year);
	lastday.setMonth(month + 1);

	var days = Array();
	var index = 0;

	// Add empty days before month start
	var empty_weekdays = (firstday.getDay() == 0 ? 7 : firstday.getDay()) ;
	for (var i = 1; i < empty_weekdays; i++) {
		var someDate = new Object();
		someDate.weekday = i;
		someDate.iso_date = null;
		someDate.day = null;
		someDate.enabled = false;
		days[index++] = someDate;
	}

	// Make array of objects with full date and day
	while (firstday < lastday) {
		var someDate = new Object();
		someDate.weekday = firstday.getDay();
		someDate.iso_date = makeIsoDate(firstday);
		someDate.day = firstday.getDate();

		// Disable days before today and after last date in calendar
//		someDate.enabled = ( (firstday <= today || firstday > last_date) ? false : true);
		someDate.enabled = (firstday < today ? false : true);

		days[index++] = someDate;

		firstday.setDate(firstday.getDate() + 1);
	}

	// Add empty days after month end
	empty_weekdays = 7 - (days[days.length - 1].weekday == 0 ? 7 : days[days.length - 1].weekday - 1) - 1;
	var wkday = days[days.length - 1].weekday + 1;
	for (var i = 0; i < empty_weekdays; i++) {
		var someDate = new Object();
		someDate.weekday = wkday;
		someDate.iso_date = null;
		someDate.day = null;
		someDate.enabled = false;
		days[index++] = someDate;
		wkday++;
	}

	return days;
	
}

// -----------------------------------------------------------------------

/**
*	Draw calendar layout with the supplied days array, using DOM.
*	- This is for 1 month only.
*	- Year / month should be like 2006, 1 (month = 0..11)
*	- Days array should always contain a multiple of 7 elements.
*	- Container is the div in which the two months should be displayed.
*	- Link type should be 'prev' or 'next'
*	- Date select: name of date select in which the date should be set on click
**/
function drawLayout(year, month, days, container, link_type, date_select) {

	// Create div for containing the calendar
	var month_div = document.createElement("div");
	month_div.className = "month";
	month_div.id = "month_" + year + "_" + month;

	// Create table & caption
	var month_table = document.createElement("table");
	var month_tbody = document.createElement("tbody");
	month_table.appendChild(MakeCaption(year, month));

	// Add row with weekday names
	month_tbody.appendChild(MakeWeekdayRow());

	// Iterate over days and create DOM elements
	var cur_row = document.createElement("tr");
	for (var i = 0; i < days.length; i++) {
		
		// Append cells to current row
		cur_row.appendChild(MakeDayCell(days[i], date_select));

		// Append current row to table & create new row
		if ((i + 1) % 7 == 0) {
			month_tbody.appendChild(cur_row);
			cur_row = document.createElement("tr");
		}

	}

	// Add row with prev/next links
	if (link_type == "prev") {
		month--;
		if (month == -1) {
			year--;
			month = 11;
		}
	} else {
		// No change necessary
	}
	month_tbody.appendChild(MakeNavRow(link_type, container.id, year, month, date_select));

	// Add tbody > table > container div
	month_table.appendChild(month_tbody);
	month_div.appendChild(month_table);
	container.appendChild(month_div);

}

// -----------------------------------------------------------------------

/**
*	Produce caption for month table, like 'December 2006'
*	Returns DOM caption element.
**/
function MakeCaption(year, month) {

	var month_caption = document.createElement("caption");

	var months = Array();
	months[0] = "Januari";
	months[1] = "Februari";
	months[2] = "Maart";
	months[3] = "April";
	months[4] = "Mei";
	months[5] = "Juni";
	months[6] = "Juli";
	months[7] = "Augustus";
	months[8] = "September";
	months[9] = "Oktober";
	months[10] = "November";
	months[11] = "December";

	var text = document.createTextNode(months[month] + " " + year);
	month_caption.appendChild(text);

	return month_caption;

}

// -----------------------------------------------------------------------

/**
*	Produces a DOM row element with 7 TD elements, each showing
*	an abbreviated weekday in Dutch ('ma', 'di', ...)
**/
function MakeWeekdayRow() {

	var row = document.createElement("tr");

	var days = Array();
	days[0] = "Ma";
	days[1] = "Di";
	days[2] = "Wo";
	days[3] = "Do";
	days[4] = "Vr";
	days[5] = "Za";
	days[6] = "Zo";

	for (var i = 0; i < days.length; i++) {
		var cell = document.createElement("th");
		var text = document.createTextNode(days[i]);
		cell.appendChild(text);
		row.appendChild(cell);
	}

	return row;

}

// EVENT HANDLERS ---------------------------------------------------------

/**
*	Highlights a table cell. Used as event handler for onMouseOver.
**/
function highlightCell() 
{
	this.style.backgroundColor = '#999';
	this.style.color = '#ffffff';
}

/**
*	Un-highlights a table cell. Used as event handler for onMouseOut.
**/
function unhighlightCell() {

	this.style.backgroundColor = ''; 
	this.style.color = '';

}

/**
*	Selects a date in the date-select.
**/
function selectDate() {

	// Get ISO date & date select ID from hidden inputs in cell
	var elems = this.getElementsByTagName("input");
	var iso_date = elems[0].value;
	var date_select = elems[1].value;
	
	// Find ISO date in select and make it selected
	var date_sel = document.getElementById(date_select);
	for (var i = 0; i < date_sel.options.length; i++) {
		if (date_sel.options[i].value == iso_date) {
			date_sel.options[i].selected = true;
			break;
		}
	}

	// Hide container
	$('#' + current_container).fadeOut();

	// Show select boxes in IE6
	if (current_container == 'pickup_calendar' && $.browser.msie && $.browser.version == '6.0')
	{
		var return_type = GetReturnLocType();
		$('select#return_loc_' + return_type).show();
		$('select#return_date').show();
		$('select#return_time').show();
	}
     
	// Trigger onchange event
	if (current_container == 'pickup_calendar0')
	{
		//$('#pickup_date0').change();
        $('#pickup_date0').trigger('change');

	}
    if (current_container == 'pickup_calendar1')
    {
        $('#pickup_date1').trigger('change');

    }
	
}


// -----------------------------------------------------------------------

/**
*	Produces a DOM table cell element, showing a day number.
*	Includes CSS class for disabled days, onClick and 
*	onMouseOver/onMouseOut handlers.
*	- someDay: object with day properties.
*	- date_select: name of date select object
**/
function MakeDayCell(someDay, date_select) {

	var cell = document.createElement("td");

	// Set past days disabled
	if (!someDay.enabled) {
		cell.className = "disabled";
	} 

	// Print day number or space
	if (someDay.day == null) {
		cell.innerHTML = "&nbsp;";
	} else {
		cell.innerHTML = someDay.day;

		// Add hidden input with iso date
		var hidden = document.createElement("input");
		hidden.type = "hidden";
		hidden.name = "iso_date";
		hidden.value = someDay.iso_date;
		cell.appendChild(hidden);

		// Add hidden input with date select
		var hidden = document.createElement("input");
		hidden.type = "hidden";
		hidden.name = "date_select";
		hidden.value = date_select;
		cell.appendChild(hidden);

	}

	// Set mouse over/out & onClick
	if (someDay.enabled) {
		cell.onmouseover = highlightCell;
		cell.onmouseout = unhighlightCell;
		cell.onclick = selectDate;
	}

	return cell;
	
}

// -----------------------------------------------------------------------

/**
*	Produces a DOM row element, with 1 TD spanning 7 columns,
*	showing a 'previous/next month' link, depending on link_type.
*	- Link type: 'prev' or 'next'
*	- Container: Id of object to create calendar in
*	- Year
*	- Month
*	- date_select: object id of date select
**/
function MakeNavRow(link_type, container, year, month, date_select) {

	// Create row & cell
	var row = document.createElement("tr");
	var cell = document.createElement("td");
	cell.colSpan = 7;

	// Add link
	var str = "";
	str = "<a href=\"javascript:createCalendar('" + container + "', '" + date_select + "', " + year + ", " + month + ");\">";
	str += (link_type == "prev" ? "&laquo; Vorige maand" : "Volgende maand &raquo;");
	str += "</a>";
	cell.innerHTML = str;
	row.appendChild(cell);

	return row;
}


