var today = new Date();

function getMonthLen(theYear, theMonth) {
	var nextMonth = new Date(theYear, theMonth + 1, 1);
	nextMonth.setHours(nextMonth.getHours() - 3);
	return nextMonth.getDate();
}

function getElementPosition(elemID) {
	var offsetTrail = document.getElementById(elemID), offsetLeft = 0, offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft, top:offsetTop};
}

function showCalendar(evt, calendar) {
	evt = (evt) ? evt : event;
	if (evt) {
		if (document.getElementById(calendar + "Calendar").style.visibility != "visible") {
			var elem = (evt.target) ? evt.target : evt.srcElement;
			var position = getElementPosition(elem.id);
			shiftTo(calendar + "Calendar", position.left + elem.offsetWidth, position.top);
			document.getElementById(calendar + "ChooseMonth").selectedIndex = document.dates.elements[calendar + "Month"].selectedIndex;
			document.getElementById(calendar + "ChooseYear").value = document.dates.elements[calendar + "Year"].value;
			populateTable(calendar);
			show(calendar + "Calendar");
		} else {
			hide(calendar + "Calendar");
		}
	}
}

function populateTable(calendar) {
	var chooseMonth = document.getElementById(calendar + "ChooseMonth"), chooseYear = document.getElementById(calendar + "ChooseYear");
	var theMonth = chooseMonth.selectedIndex, theYear = parseInt(chooseYear.options[chooseYear.selectedIndex].text);
	var firstDay = new Date(theYear, theMonth, 1).getDay(), daysInMonth = getMonthLen(theYear, theMonth);

	var j, dayCounter = 1;
	var TBody = document.getElementById(calendar + "TableBody");
	while (TBody.rows.length > 2)
		TBody.deleteRow(2);
	var newR, newC, done = false;
	while (!done) {
		newR = TBody.insertRow(TBody.rows.length);
		if (newR) {
			for (var i = 0; i < 7; i++) {
				newC = newR.insertCell(newR.cells.length);
				if (TBody.rows.length == 3 && i < firstDay) {
					newC.innerHTML = "&nbsp;";
					newC.className = "empty";
					continue;
				}
				if (dayCounter == daysInMonth) {
					done = true;
				}
				if (dayCounter <= daysInMonth) {
					if (new Date(theYear, theMonth, dayCounter) > today) {
						isReserved = false;
						for (j = 0; j < reserved.length; j++) {
							if (reserved[j] - new Date(theYear, theMonth, dayCounter) == 0) {
								isReserved = true;
								break;
							}
						}
						if (isReserved) {
							newC.className = "reserved";
							newC.innerHTML = dayCounter;
						} else {	// if date > today && date is not reserved
							if (calendar == "begin")
								newC.innerHTML = "<a href='#' onclick='chooseDate(" + dayCounter + "," + theMonth + "," + theYear + "," + "\"" + calendar + "\"); " + "setCheckOut(); return false;'>" + dayCounter + "</a>";
							if (calendar == "end")
								newC.innerHTML = "<a href='#' onclick='chooseDate(" + dayCounter + "," + theMonth + "," + theYear + "," + "\"" + calendar + "\"); " + "checkOutChanged = true; return false;'>" + dayCounter + "</a>";
						}
					} else if (new Date(theYear, theMonth, dayCounter) < today) {
						newC.className = "past";
						newC.innerHTML = dayCounter;
					} else {
						newC.className = "today";
						newC.innerHTML = dayCounter;
					}
					dayCounter++;
				} else {
					newC.innerHTML = "&nbsp;";
					newC.className = "empty";
				}
			}
		} else {
			done = true;
		}
	}
}

function fillYears() {
	var thisYear = today.getFullYear();
	var beginYearChooser = document.getElementById("beginChooseYear");
	var endYearChooser = document.getElementById("endChooseYear");
	beginYearChooser.options.length = 0;
	endYearChooser.options.length = 0;
	for (i = thisYear - 1; i <= thisYear + 1; i++) {
		beginYearChooser.options[beginYearChooser.options.length] = new Option(i, i);
		endYearChooser.options[endYearChooser.options.length] = new Option(i, i);
	}
	beginYearChooser.selectedIndex = 1;
	endYearChooser.selectedIndex = 1;
	document.dates.beginChooseMonth.selectedIndex = today.getMonth();
	document.dates.endChooseMonth.selectedIndex = today.getMonth();
}

function chooseDate(date, month, year, calendar) {
	document.dates.elements[calendar + "Date"].selectedIndex = date - 1;
	document.dates.elements[calendar + "Month"].selectedIndex = month;
	document.dates.elements[calendar + "Year"].value = year;
	hide(calendar + "Calendar");
	updateDays();
}

function updateDays() {
	var Wdaynames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	var beginDate = new Date(document.dates.beginYear.value, document.dates.beginMonth.value - 1, document.dates.beginDate.value);
	var endDate = new Date(document.dates.endYear.value, document.dates.endMonth.value - 1, document.dates.endDate.value);

	var beginWdayText = document.createTextNode(Wdaynames[beginDate.getDay()]);
	var beginWdayElem = document.getElementById("beginWday");
	beginWdayElem.replaceChild(beginWdayText, beginWdayElem.firstChild);

	var endWdayText = document.createTextNode(Wdaynames[endDate.getDay()]);
	var endWdayElem = document.getElementById("endWday");
	endWdayElem.replaceChild(endWdayText, endWdayElem.firstChild);
}

var checkOutChanged = false;
function setCheckOut() {
	if (! checkOutChanged) {
		var begin = new Date(document.dates.beginYear.value, document.dates.beginMonth.value - 1, document.dates.beginDate.value);
		var end = new Date(begin);
		end.setDate(begin.getDate() + 7);
		chooseDate(end.getDate(), end.getMonth(), end.getFullYear(), "end");
	}
}

function prevMonth(calendar) {
	var chooseMonth = document.getElementById(calendar + "ChooseMonth");
	var chooseYear = document.getElementById(calendar + "ChooseYear");
	if (chooseMonth.selectedIndex != 0) {
		chooseMonth.selectedIndex--;
	} else {
		chooseMonth.selectedIndex = 11;
		chooseYear.value--;
	}
	populateTable(calendar);
}

function nextMonth(calendar) {
	var chooseMonth = document.getElementById(calendar + "ChooseMonth");
	var chooseYear = document.getElementById(calendar + "ChooseYear");
	if (chooseMonth.selectedIndex != 11) {
		chooseMonth.selectedIndex++;
	} else {
		chooseMonth.selectedIndex = 0;
		chooseYear.value++;
	}
	populateTable(calendar);
}