/*
DateChooser 1.9
October 24, 2006
For usage details see http://yellow5.us/projects/datechooser/

Creative Commons Attribution-NoDerivs 2.5 License
http://creativecommons.org/licenses/by-nd/2.5/
*/

var objPrototypes =
{
	aDay: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	aShortDay: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	aMonth: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	aShortMonth: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	aSuffix: ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st'],
	sTimezoneOffset: '',

	GetTimezoneOffset: function()
	{
		var objLocal = new Date();
		objLocal.setHours(0);
		objLocal.setMinutes(0);
		objLocal.setSeconds(0);
		objLocal.setMilliseconds(0);

		var objUTC = new Date();
		objUTC.setFullYear(objLocal.getUTCFullYear());
		objUTC.setMonth(objLocal.getUTCMonth());
		objUTC.setDate(objLocal.getUTCDate());
		objUTC.setHours(objLocal.getUTCHours());
		objUTC.setMinutes(objLocal.getUTCMinutes());
		objUTC.setSeconds(objLocal.getUTCSeconds());
		objUTC.setMilliseconds(objLocal.getUTCMilliseconds());

		this.sTimezoneOffset = ((objLocal.getTime() - objUTC.getTime()) / (1000 * 3600));
		var bNegative = (this.sTimezoneOffset < 0);
		objLocal = null;
		objUTC = null;

		this.sTimezoneOffset  = bNegative ? (this.sTimezoneOffset + '').substring(1) : this.sTimezoneOffset + '';
		this.sTimezoneOffset  = this.sTimezoneOffset.replace(/\.5/, (parseInt('$1', 10) * 60) + '');
		this.sTimezoneOffset += (this.sTimezoneOffset.substring(this.sTimezoneOffset.length - 3) != ':30') ? ':00' : '';
		this.sTimezoneOffset  = (this.sTimezoneOffset.substr(0, this.sTimezoneOffset.indexOf(':')).length == 1) ? '0' + this.sTimezoneOffset : this.sTimezoneOffset;
		this.sTimezoneOffset  = bNegative ? '-' + this.sTimezoneOffset : '+' + this.sTimezoneOffset;

		return true;
	},

	Array_push: function()
	{
		for (var nCount = 0; nCount < arguments.length; nCount++)
		{
			this[this.length] = arguments[nCount];
		}

		return this.length;
	},

	Date_PHPDate: function()
	{
		var sFormat = (arguments.length > 0) ? arguments[0] : '';

		var nYear = this.getFullYear();
		var sYear = nYear + '';

		var nMonth = this.getMonth();
		var sMonth = (nMonth + 1) + '';
		var sPaddedMonth = (sMonth.length == 1) ? '0' + sMonth : sMonth;

		var nDate = this.getDate();
		var sDate = nDate + '';
		var sPaddedDate = (sDate.length == 1) ? '0' + sDate : sDate;

		var nDay = this.getDay();
		var sDay = nDay + '';

		sFormat = sFormat.replace(/([cDdFjlMmNnrSUwYy])/g, 'y5-cal-regexp:$1');
		sFormat = sFormat.replace(/y5-cal-regexp:c/, sYear + '-' + sPaddedMonth + '-' + sPaddedDate + 'T00:00:00' + objPrototypes.sTimezoneOffset);
		sFormat = sFormat.replace(/y5-cal-regexp:D/, objPrototypes.aShortDay[nDay]);
		sFormat = sFormat.replace(/y5-cal-regexp:d/, sPaddedDate);
		sFormat = sFormat.replace(/y5-cal-regexp:F/, objPrototypes.aMonth[nMonth]);
		sFormat = sFormat.replace(/y5-cal-regexp:j/, nDate);
		sFormat = sFormat.replace(/y5-cal-regexp:l/, objPrototypes.aDay[nDay]);
		sFormat = sFormat.replace(/y5-cal-regexp:M/, objPrototypes.aShortMonth[nMonth]);
		sFormat = sFormat.replace(/y5-cal-regexp:m/, sPaddedMonth);
		sFormat = sFormat.replace(/y5-cal-regexp:N/, (nDay == 0) ? 7 : nDay);
		sFormat = sFormat.replace(/y5-cal-regexp:n/, sMonth);
		sFormat = sFormat.replace(/y5-cal-regexp:r/, objPrototypes.aShortDay[nDay] + ', ' + sPaddedDate + ' ' + objPrototypes.aShortMonth[nMonth] + ' ' + sYear + ' 00:00:00 ' + objPrototypes.sTimezoneOffset.replace(/:/, ''));
		sFormat = sFormat.replace(/y5-cal-regexp:S/, objPrototypes.aSuffix[nDate]);
		sFormat = sFormat.replace(/y5-cal-regexp:U/, parseInt((this.getTime() / 1000), 10));
		sFormat = sFormat.replace(/y5-cal-regexp:w/, nDay);
		sFormat = sFormat.replace(/y5-cal-regexp:Y/, sYear);
		sFormat = sFormat.replace(/y5-cal-regexp:y/, sYear.substring(2));

		return sFormat;
	}
};

objPrototypes.GetTimezoneOffset();
Date.prototype.getPHPDate = objPrototypes.Date_PHPDate;
if (typeof(Array.prototype.push) == 'undefined')
{
	Array.prototype.push = objPrototypes.Array_push;
}

function DateChooser()
{
	if (!arguments
	|| !document.getElementById
	|| !document.getElementsByTagName
	|| (!document.createElement && !document.createElementNS))
	{
		return null;
	}

	var createElement = function(sElement)
	{
		if (typeof(document.createElement) != 'undefined')
		{
			return document.createElement(sElement);
		}

		return (typeof(document.createElementNS) != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', sElement) : false;
	};

	var objDateChooser = this;

	this.objUpdateFields = {};
	this.objAllowedDays = {'0':true, '1':true, '2':true, '3':true, '4':true, '5':true, '6':true};
	this.nXOffset = 0;
	this.nYOffset = 0;
	this.nTimeout = 0;
	this.objTimeout = null;
	this.fnUpdate = null;
	this.objEarliestDate = null;
	this.objLatestDate = null;
	this.ndBodyElement = (document.getElementsByTagName('body').length > 0) ? document.getElementsByTagName('body')[0] : document;

	this.ndFrame = null;
	/*@cc_on@*/
	/*@if(@_jscript_version < 6)
		if (document.getElementById('iframehack'))
		{
			this.ndFrame = document.getElementById('iframehack');
		}
		else
		{
			this.ndFrame = createElement('iframe');
			this.ndFrame.id = 'iframehack';
			this.ndFrame.setAttribute('src', 'about:blank');
			this.ndFrame.setAttribute('scrolling', 'no');
			this.ndFrame.style.display = 'none';
			this.ndFrame.style.position = 'absolute';
			this.ndFrame.style.zIndex = '5000';
			this.ndFrame.style.padding = '0';
			this.ndFrame.style.border = '0';
			this.ndBodyElement.appendChild(this.ndFrame);
		}
	/*@end@*/

	this.nDateChooserID = 0;
	while (document.getElementById('calendar' + this.nDateChooserID)) ++this.nDateChooserID;
	this.sDateChooserID = 'calendar' + this.nDateChooserID;

	this.objSelectedDate = null;

	this.objStartDate = new Date();
	this.objStartDate.setHours(0);
	this.objStartDate.setMinutes(0);
	this.objStartDate.setSeconds(0);
	this.objStartDate.setMilliseconds(0);

	this.objMonthYear = new Date(this.objStartDate);
	this.objMonthYear.setDate(1);

	this.ndDateChooser = document.createElement('div');
	this.ndDateChooser.id = this.sDateChooserID;
	this.ndDateChooser.className = 'calendar';
	this.ndDateChooser.style.visibility = 'hidden';
	this.ndDateChooser.style.position = 'absolute';
	this.ndDateChooser.style.zIndex = '5001';
	this.ndDateChooser.style.top = '0';
	this.ndDateChooser.style.left = '0';
	this.ndBodyElement.appendChild(this.ndDateChooser);

	var AddClickEvents = function()
	{
		var aNavLinks = objDateChooser.ndDateChooser.getElementsByTagName('thead')[0].getElementsByTagName('a');
		for (var nNavLink = 0; nNavLink < aNavLinks.length; ++nNavLink)
		{
			aNavLinks[nNavLink].onclick = function(e)
			{
				e = e || window.event;
				var ndClicked = e.target || e.srcElement;
				if (ndClicked.nodeName == '#text') ndClicked = ndClicked.parentNode;

				var sClass = ndClicked.className;

				if (sClass == 'previousyear')
				{
					objDateChooser.objMonthYear.setFullYear(objDateChooser.objMonthYear.getFullYear() - 1);
					if (objDateChooser.objEarliestDate && objDateChooser.objEarliestDate.getTime() > objDateChooser.objMonthYear.getTime())
					{
						objDateChooser.objMonthYear.setFullYear(objDateChooser.objEarliestDate.getFullYear());
						objDateChooser.objMonthYear.setMonth(objDateChooser.objEarliestDate.getMonth());
					}
				}
				else if (sClass == 'previousmonth')
				{
					objDateChooser.objMonthYear.setMonth(objDateChooser.objMonthYear.getMonth() - 1);
					if (objDateChooser.objEarliestDate && objDateChooser.objEarliestDate.getTime() > objDateChooser.objMonthYear.getTime())
					{
						objDateChooser.objMonthYear.setFullYear(objDateChooser.objEarliestDate.getFullYear());
						objDateChooser.objMonthYear.setMonth(objDateChooser.objEarliestDate.getMonth());
					}
				}
				else if (sClass == 'currentdate')
				{
					objDateChooser.objMonthYear.setFullYear(objDateChooser.objStartDate.getFullYear());
					objDateChooser.objMonthYear.setMonth(objDateChooser.objStartDate.getMonth());
				}
				else if (sClass == 'nextmonth')
				{
					objDateChooser.objMonthYear.setMonth(objDateChooser.objMonthYear.getMonth() + 1);
					if (objDateChooser.objLatestDate && objDateChooser.objLatestDate.getTime() < objDateChooser.objMonthYear.getTime())
					{
						objDateChooser.objMonthYear.setFullYear(objDateChooser.objLatestDate.getFullYear());
						objDateChooser.objMonthYear.setMonth(objDateChooser.objLatestDate.getMonth());
					}
				}
				else if (sClass == 'nextyear')
				{
					objDateChooser.objMonthYear.setFullYear(objDateChooser.objMonthYear.getFullYear() + 1);
					if (objDateChooser.objLatestDate && objDateChooser.objLatestDate.getTime() < objDateChooser.objMonthYear.getTime())
					{
						objDateChooser.objMonthYear.setFullYear(objDateChooser.objLatestDate.getFullYear());
						objDateChooser.objMonthYear.setMonth(objDateChooser.objLatestDate.getMonth());
					}
				}

				RefreshDisplay();
				return false;
			}
		}

		var aDateLinks = objDateChooser.ndDateChooser.getElementsByTagName('tbody')[0].getElementsByTagName('a');
		for (var nDateLink = 0; nDateLink < aDateLinks.length; ++nDateLink)
		{
			aDateLinks[nDateLink].onclick = function(e)
			{
				e = e || window.event;
				var ndClicked = e.target || e.srcElement;
				if (ndClicked.nodeName == '#text') ndClicked = ndClicked.parentNode;

				for (var nLink = 0; nLink < aDateLinks.length; ++nLink)
				{
					if (aDateLinks[nLink].className == 'selecteddate') aDateLinks[nLink].removeAttribute('class');
				}

				var objTempDate = new Date(objDateChooser.objMonthYear);
				objTempDate.setDate(parseInt(ndClicked.childNodes[0].nodeValue, 10));

				var nTime = objTempDate.getTime();
				var sWeekday = objTempDate.getPHPDate('w');
				objTempDate = null;

				if (objDateChooser.objEarliestDate && objDateChooser.objEarliestDate.getTime() > nTime) return false;
				if (objDateChooser.objLatestDate && objDateChooser.objLatestDate.getTime() < nTime) return false;
				if (!objDateChooser.objAllowedDays[sWeekday]) return false;

				objDateChooser.objMonthYear.setTime(nTime);
				objDateChooser.objMonthYear.setDate(1);
				if (!objDateChooser.objSelectedDate) objDateChooser.objSelectedDate = new Date();
				objDateChooser.objSelectedDate.setTime(nTime);
				ndClicked.className = 'selecteddate';

				if (objDateChooser.ndFrame) objDateChooser.ndFrame.style.display = 'none';
				objDateChooser.ndDateChooser.style.visibility = 'hidden';

				if (objDateChooser.objTimeout) clearTimeout(objDateChooser.objTimeout);
				if (objDateChooser.fnUpdate) objDateChooser.fnUpdate(objDateChooser.objSelectedDate);

				for (var sFieldName in objDateChooser.objUpdateFields)
				{
					var ndField = document.getElementById(sFieldName);
					if (ndField) ndField.value = objDateChooser.objSelectedDate.getPHPDate(objDateChooser.objUpdateFields[sFieldName]);
				}
				return false;
			};
		}

		return true;
	};

	var RefreshDisplay = function()
	{
		var ndTable, ndTHead, ndTR, ndTH, ndA, ndTBody, ndTD;
		var sClass = '';
		var objTempDate = new Date(objDateChooser.objMonthYear);

		var objToday = new Date();
		objToday.setHours(0);
		objToday.setMinutes(0);
		objToday.setSeconds(0);
		objToday.setMilliseconds(0);

		ndTable = createElement('table');
		ndTable.setAttribute('summary', 'DateChooser');

		ndTHead = createElement('thead');
		ndTable.appendChild(ndTHead);

		ndTR = createElement('tr');
		ndTHead.appendChild(ndTR);

		ndTH = createElement('th');
		ndTR.appendChild(ndTH);
		ndA = createElement('a');
		ndA.className = 'previousyear';
		ndA.setAttribute('href', '#');
		ndA.setAttribute('title', 'Previous Year');
		ndTH.appendChild(ndA);
		ndA.appendChild(document.createTextNode(String.fromCharCode(171)));

		ndTH = createElement('th');
		ndTR.appendChild(ndTH);
		ndA = createElement('a');
		ndA.className = 'previousmonth';
		ndA.setAttribute('href', '#');
		ndA.setAttribute('title', 'Previous Month');
		ndTH.appendChild(ndA);
		ndA.appendChild(document.createTextNode(String.fromCharCode(60)));

		ndTH = createElement('th');
		ndTH.setAttribute('colspan', '3');
		/*@cc_on@*/
		/*@if (@_jscript_version < 6) ndTH.colSpan = '3';
		/*@end@*/
		ndTR.appendChild(ndTH);
		ndA = createElement('a');
		ndA.className = 'currentdate';
		ndA.setAttribute('href', '#');
		ndA.setAttribute('title', 'Current Date');
		ndTH.appendChild(ndA);
		ndA.appendChild(document.createTextNode(objDateChooser.objMonthYear.getPHPDate("M Y")));

		ndTH = createElement('th');
		ndTR.appendChild(ndTH);
		ndA = createElement('a');
		ndA.className = 'nextmonth';
		ndA.setAttribute('href', '#');
		ndA.setAttribute('title', 'Next Month');
		ndTH.appendChild(ndA);
		ndA.appendChild(document.createTextNode(String.fromCharCode(62)));

		ndTH = createElement('th');
		ndTR.appendChild(ndTH);
		ndA = createElement('a');
		ndA.className = 'nextyear';
		ndA.setAttribute('href', '#');
		ndA.setAttribute('title', 'Next Year');
		ndTH.appendChild(ndA);
		ndA.appendChild(document.createTextNode(String.fromCharCode(187)));

		ndTR = createElement('tr');
		ndTHead.appendChild(ndTR);

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('S'));

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('M'));

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('T'));

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('W'));

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('T'));

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('F'));

		ndTD = createElement('td');
		ndTR.appendChild(ndTD);
		ndTD.appendChild(document.createTextNode('S'));

		ndTBody = createElement('tbody');
		ndTable.appendChild(ndTBody);

		while (objTempDate.getMonth() == objDateChooser.objMonthYear.getMonth())
		{
			ndTR = createElement('tr');
			ndTBody.appendChild(ndTR);

			for (var nWeek = 0; nWeek < 7; ++nWeek)
			{
				if ((objTempDate.getDay() == nWeek) && (objTempDate.getMonth() == objDateChooser.objMonthYear.getMonth()))
				{
					sClass  = (objDateChooser.objSelectedDate && (objTempDate.getTime() == objDateChooser.objSelectedDate.getTime())) ? 'selectedday' : '';
					sClass += (objTempDate.getTime() == objToday.getTime()) ? ' today' : '';
					sClass  = ((sClass.length > 0) && (sClass[1] == ' ')) ? sClass.substr(1, sClass.length - 1) : sClass;

					ndTD = createElement('td');
					ndTR.appendChild(ndTD);

					ndA = createElement('a');
					if (sClass.length > 0) ndA.className = sClass;
					ndA.setAttribute('href', '#');
					ndTD.appendChild(ndA);
					ndA.appendChild(document.createTextNode(objTempDate.getDate()));

					objTempDate.setDate(objTempDate.getDate() + 1);
				}
				else
				{
					ndTD = createElement('td');
					ndTR.appendChild(ndTD);
				}
			}
		}

		while (objDateChooser.ndDateChooser.hasChildNodes()) objDateChooser.ndDateChooser.removeChild(objDateChooser.ndDateChooser.firstChild);
		objDateChooser.ndDateChooser.appendChild(ndTable);

		if (objDateChooser.ndFrame)
		{
			objDateChooser.ndFrame.style.display = 'block';
			objDateChooser.ndFrame.style.top = objDateChooser.ndDateChooser.style.top;
			objDateChooser.ndFrame.style.left = objDateChooser.ndDateChooser.style.left;
			objDateChooser.ndFrame.style.width = (ndTable.clientWidth + 2) + 'px';
			objDateChooser.ndFrame.style.height = (ndTable.clientHeight + 4) + 'px';
		}

		objTempDate = null;
		objToday = null;

		AddClickEvents();
		return true;
	};

	var DisplayDateChooser = function()
	{
		var sPositionX = (arguments.length > 0) ? arguments[0] : 'auto';
		var sPositionY = (arguments.length > 1) ? arguments[1] : 'auto';

		var ndStyle = objDateChooser.ndDateChooser.style;
		ndStyle.top = sPositionY + '';
		ndStyle.left = sPositionX + '';

		objDateChooser.ndDateChooser.style.visibility = 'visible';
		if (objDateChooser.objTimeout) clearTimeout(objDateChooser.objTimeout);

		RefreshDisplay();
		return true;
	};

	this.displayPosition = function()
	{
		var sPositionX = (arguments.length > 0) ? arguments[0] : 'auto';
		var sPositionY = (arguments.length > 1) ? arguments[1] : 'auto';

		return DisplayDateChooser(sPositionX, sPositionY);
	};

	this.display = function(e)
	{
		e = e || window.event;

		var sPositionX = 'auto';
		var sPositionY = 'auto';
		if (e)
		{
			if (e.pageX || e.pageY)
			{
				sPositionX = e.pageX + objDateChooser.nXOffset + 'px';
				sPositionY = e.pageY + objDateChooser.nYOffset + 'px';
			}
			else if (e.clientX || e.clientY)
			{
				sPositionX = e.clientX + objDateChooser.ndBodyElement.scrollLeft + objDateChooser.nXOffset + 'px';
				sPositionY = e.clientY + objDateChooser.ndBodyElement.scrollTop + objDateChooser.nYOffset + 'px';
			}

			if (e.preventDefault) e.preventDefault();
			if (e.stopPropagation) e.stopPropagation();
			e.returnValue = false;
			e.cancelBubble = true;
		}

		DisplayDateChooser(sPositionX, sPositionY);
		return false;
	};

	this.setXOffset = function()
	{
		this.nXOffset = ((arguments.length > 0) && (typeof(arguments[0]) == 'number')) ? parseInt(arguments[0], 10) : this.nXOffset;
		return true;
	};

	this.setYOffset = function()
	{
		this.nYOffset = ((arguments.length > 0) && (typeof(arguments[0]) == 'number')) ? parseInt(arguments[0], 10) : this.nYOffset;
		return true;
	};

	this.setCloseTime = function()
	{
		this.nTimeout = ((arguments.length > 0) && (typeof(arguments[0]) == 'number') && (arguments[0] >= 0)) ? arguments[0] : this.nTimeout;
		return true;
	};

	this.setUpdateFunction = function()
	{
		if ((arguments.length > 0) && (typeof(arguments[0]) == 'function')) this.fnUpdate = arguments[0];
		return true;
	};

	this.setUpdateField = function()
	{
		this.objUpdateFields = {};
		if ((typeof(arguments[0]) == 'string') && (typeof(arguments[1]) == 'string') && document.getElementById(arguments[0]))
		{
			this.objUpdateFields[arguments[0]] = arguments[1];
		}
		else if ((typeof(arguments[0]) == 'object') && (typeof(arguments[1]) == 'object'))
		{
			for (var nField = 0; nField < arguments[0].length; ++nField)
			{
				if (nField >= arguments[1].length) break;
				this.objUpdateFields[arguments[0][nField]] = arguments[1][nField];
			}
		}
		else if (typeof(arguments[0]) == 'object')
		{
			this.objUpdateFields = arguments[0];
		}

		return true;
	};

	this.setLink = function()
	{
		var sLinkText = ((arguments.length > 0) && (typeof(arguments[0]) == 'string')) ? arguments[0] : 'Choose a date';
		var ndNode = ((arguments.length > 1) && (typeof(arguments[1]) == 'string')) ? document.getElementById(arguments[1]) : null;
		var bPlaceRight = ((arguments.length > 2) && !arguments[2]) ? false : true;
		var sTitleText = ((arguments.length > 3) && (typeof(arguments[3]) == 'string')) ? arguments[3] : 'Click to choose a date';

		if (!ndNode) return false;

		var ndAnchor = document.createElement('a');
		ndAnchor.className = 'calendarlink';
		ndAnchor.href = '#';

		if (sTitleText.length > 0) ndAnchor.setAttribute('title', sTitleText);
		ndAnchor.appendChild(document.createTextNode(sLinkText));

		if (bPlaceRight)
		{
			if (ndNode.nextSibling)
			{
				ndNode.parentNode.insertBefore(ndAnchor, ndNode.nextSibling);
			}
			else
			{
				ndNode.parentNode.appendChild(ndAnchor);
			}
		}
		else
		{
			ndNode.parentNode.insertBefore(ndAnchor, ndNode);
		}

		ndAnchor.onclick = this.display;
		return true;
	};

	this.setIcon = function()
	{
		var sIconFile = ((arguments.length > 0) && (typeof(arguments[0]) == 'string')) ? arguments[0] : false;
		var ndNode = ((arguments.length > 1) && (typeof(arguments[1]) == 'string')) ? document.getElementById(arguments[1]) : null;
		var bPlaceRight = ((arguments.length > 2) && !arguments[2]) ? false : true;
		var sTitleText = ((arguments.length > 3) && (typeof(arguments[3]) == 'string')) ? arguments[3] : 'Click to choose a date';

		if (!ndNode || !sIconFile) return false;

		var ndIcon = document.createElement('img');
		ndIcon.className = 'calendaricon';
		ndIcon.src = sIconFile;
		ndIcon.setAttribute('alt', 'DateChooser Icon ' + (this.nDateChooserID + 1));
		if (sTitleText.length > 0) ndIcon.setAttribute('title', sTitleText);

		if (bPlaceRight)
		{
			if (ndNode.nextSibling)
			{
				ndNode.parentNode.insertBefore(ndIcon, ndNode.nextSibling);
			}
			else
			{
				ndNode.parentNode.appendChild(ndIcon);
			}
		}
		else
		{
			ndNode.parentNode.insertBefore(ndIcon, ndNode);
		}

		ndIcon.onclick = this.display;
		return true;
	};

	this.setStartDate = function()
	{
		if (!arguments.length || !(typeof(arguments[0]) == 'object') || !arguments[0].getTime) return false;

		this.objStartDate.setTime(arguments[0].getTime());
		this.objStartDate.setHours(0);
		this.objStartDate.setMinutes(0);
		this.objStartDate.setSeconds(0);
		this.objStartDate.setMilliseconds(0);

		if (this.objEarliestDate && this.objEarliestDate.getTime() > this.objStartDate.getTime())
		{
			this.objStartDate.setTime(this.objEarliestDate.getTime());
		}
		else if (this.objLatestDate && this.objLatestDate.getTime() < this.objStartDate.getTime())
		{
			this.objStartDate.setTime(this.objLatestDate.getTime());
		}

		this.objMonthYear.setMonth(this.objStartDate.getMonth());
		this.objMonthYear.setFullYear(this.objStartDate.getFullYear());

		if (!this.objSelectedDate) this.objSelectedDate = new Date();
		this.objSelectedDate.setTime(this.objStartDate);

		return true;
	};

	this.setEarliestDate = function()
	{
		if (!arguments.length || !(typeof(arguments[0]) == 'object') || !arguments[0].getTime) return false;

		this.objEarliestDate = arguments[0];
		this.objEarliestDate.setHours(0);
		this.objEarliestDate.setMinutes(0);
		this.objEarliestDate.setSeconds(0);
		this.objEarliestDate.setMilliseconds(0);

		if (this.objEarliestDate.getTime() > this.objStartDate.getTime())
		{
			this.objStartDate.setTime(this.objEarliestDate.getTime());
			this.objMonthYear.setMonth(this.objEarliestDate.getMonth());
			this.objMonthYear.setFullYear(this.objEarliestDate.getFullYear());
		}

		return true;
	};

	this.setLatestDate = function()
	{
		if (!arguments.length || !(typeof(arguments[0]) == 'object') || !arguments[0].getTime) return false;

		this.objLatestDate = arguments[0];
		this.objLatestDate.setHours(0);
		this.objLatestDate.setMinutes(0);
		this.objLatestDate.setSeconds(0);
		this.objLatestDate.setMilliseconds(0);

		if (this.objLatestDate.getTime() < this.objStartDate.getTime())
		{
			this.objStartDate.setTime(this.objLatestDate.getTime());
			this.objMonthYear.setMonth(this.objLatestDate.getMonth());
			this.objMonthYear.setFullYear(this.objLatestDate.getFullYear());
		}

		return true;
	};

	this.setAllowedDays = function()
	{
		if (!arguments.length || !(typeof(arguments[0]) == 'object')) return false;

		var nCount;
		for (nCount = 0; nCount < 7; ++nCount)
		{
			this.objAllowedDays[nCount] = false;
		}

		for (nCount = 0; nCount < arguments[0].length; ++nCount)
		{
			this.objAllowedDays[arguments[0][nCount] + ''] = true;
		}

		return true;
	};

	this.getSelectedDate = function()
	{
		return this.objSelectedDate;
	};

	var clickWindow = function(e)
	{
		e = e || window.event;
		var ndTarget = e.target || e.srcElement;
		if (ndTarget.nodeName == '#text') ndTarget = ndTarget.parentNode;

		while (ndTarget && (ndTarget != document))
		{
			if (ndTarget.className == 'calendar')
			{
				return true;
			}
			ndTarget = ndTarget.parentNode;
		}

		for (var nCount = 0; nCount <= objDateChooser.nDateChooserID; ++nCount)
		{
			if (objDateChooser.ndFrame) objDateChooser.ndFrame.style.display = 'none';
			document.getElementById('calendar' + nCount).style.visibility = 'hidden';
		}

		return true;
	};

	var mouseoverDateChooser = function()
	{
		if (objDateChooser.objTimeout) clearTimeout(objDateChooser.objTimeout);
		return true;
	};

	var mouseoutDateChooser = function()
	{
		if (objDateChooser.nTimeout > 0) objDateChooser.objTimeout = setTimeout('document.getElementById("' + objDateChooser.sDateChooserID + '").style.visibility = "hidden"; if (document.getElementById("iframehack")) document.getElementById("iframehack").style.display = "none";', objDateChooser.nTimeout);
		return true;
	};

	// This is the addEvent script written by Dean Edwards (dean.edwards.name)
	// It has been edited for better readability.

	var addEvent = function(ndElement, sType, fnHandler)
	{
		if (!fnHandler.$$nEventID) fnHandler.$$nEventID = addEvent.nEventID++;
		if (typeof(ndElement.aEvents) == 'undefined') ndElement.aEvents = {};

		var aHandlers = ndElement.aEvents[sType];
		if (!aHandlers)
		{
			aHandlers = ndElement.aEvents[sType] = {};
			if (ndElement['on' + sType]) aHandlers[0] = ndElement['on' + sType];
		}

		aHandlers[fnHandler.$$nEventID] = fnHandler;
		ndElement['on' + sType] = handleEvent;
	};

	var handleEvent = function(e)
	{
		var bReturn = true;
		e = e || window.event;
		var aHandlers = this.aEvents[e.type];
		for (var nIndex in aHandlers)
		{
			this.$$handleEvent = aHandlers[nIndex];
			if (this.$$handleEvent(e) === false) bReturn = false;
		}
		return bReturn;
	}

	addEvent.nEventID = 1;

	addEvent(objDateChooser.ndDateChooser, 'mouseover', mouseoverDateChooser);
	addEvent(objDateChooser.ndDateChooser, 'mouseout', mouseoutDateChooser);
	addEvent(document, 'mousedown', clickWindow);

	return true;
}
function initDateChooser() { var objEarlyDate = new Date(); var objDateChooser = new DateChooser(); objDateChooser.setEarliestDate(objEarlyDate); objDateChooser.setUpdateField({'reservation_day':'d', 'reservation_month':'m', 'reservation_year':'Y'}); objDateChooser.setIcon('/images/datechooser.png', 'reservation_day', false); objDateChooser.setXOffset(-170); objDateChooser.setYOffset(-50);}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
if (document.getElementById) { addLoadEvent(initDateChooser);}
