/*
DateChooser 1.5
http://beggarchooser.com/datechooser/
05.11.2006

Copyright (c) 2006 John Hansen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

var objPrototypes =
{
	aDay: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
	aShortDay: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
	aMonth: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
	aShortMonth: ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', '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 ctr = 0; ctr < arguments.length; ctr++)
		{
			this[this.length] = arguments[ctr];
		}

		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/, this.getTime() / 1000);
		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 (!document.getElementById
	|| !document.getElementsByTagName
	|| !arguments)
	{
		return false;
	}

	var aUpdateField = [];
	var aPHPFormat = [];
	var nXOffset = 0;
	var nYOffset = 0;
	var nTimeout = 0;
	var objTimeout = null;
	var fnUpdate = null;

	var nDateChooserID;
	for (nDateChooserID = 0; document.getElementById('calendar' + nDateChooserID); ++nDateChooserID){}
	var sDateChooserID = 'calendar' + nDateChooserID;

	var objSelectedDate = new Date();
	objSelectedDate.setHours(0);
	objSelectedDate.setMinutes(0);
	objSelectedDate.setSeconds(0);
	objSelectedDate.setMilliseconds(0);

	var objMonthYear = new Date();
	objMonthYear.setTime(objSelectedDate.getTime());
	objMonthYear.setDate(1);

	var ndDateChooser = document.createElement('div');
	ndDateChooser.id = sDateChooserID;
	ndDateChooser.className = 'calendar';
	ndDateChooser.style.display = 'none';
	ndDateChooser.style.position = 'absolute';
	ndDateChooser.style.top = '0px';
	ndDateChooser.style.left = '0px';
	document.body.appendChild(ndDateChooser);

	var AddClickEvents = function()
	{
		var aNavLinks = 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.className == 'previousyear')
				{
					objMonthYear.setFullYear(objMonthYear.getFullYear() - 1);
				}
				else if (ndClicked.className == 'previousmonth')
				{
					objMonthYear.setMonth(objMonthYear.getMonth() - 1);
				}
				else if (ndClicked.className == 'currentdate')
				{
					var objCurrentDate = new Date();

					objMonthYear.setFullYear(objCurrentDate.getFullYear());
					objMonthYear.setMonth(objCurrentDate.getMonth());

					objCurrentDate = null;
				}
				else if (ndClicked.className == 'nextmonth')
				{
					objMonthYear.setMonth(objMonthYear.getMonth() + 1);
				}
				else if (ndClicked.className == 'nextyear')
				{
					objMonthYear.setFullYear(objMonthYear.getFullYear() + 1);
				}


				RefreshDisplay();
				return false;
			}
		}

		var aDateLinks = 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;

				for (var nLink = 0; nLink < aDateLinks.length; ++nLink)
				{
					if (aDateLinks[nLink].className == 'selecteddate')
					{
						aDateLinks[nLink].removeAttribute('class');
					}
				}

				ndClicked.className = 'selecteddate';
				objSelectedDate.setFullYear(objMonthYear.getFullYear());
				objSelectedDate.setMonth(objMonthYear.getMonth());
				objSelectedDate.setDate(parseInt(ndClicked.innerHTML, 10));

				ndDateChooser.style.display = 'none';

				if (objTimeout)
				{
					clearTimeout(objTimeout);
				}

				if (fnUpdate)
				{
					fnUpdate(objSelectedDate);
				}

				for (var nField = 0; nField < aUpdateField.length; ++nField)
				{
					aUpdateField[nField].value = objSelectedDate.getPHPDate(aPHPFormat[nField]);
				}
				return false;
			};
		}

		return true;
	};

	var RefreshDisplay = function()
	{
		var sClass = "";
		var objTempDate = new Date();
		objTempDate.setTime(objMonthYear.getTime());
		var objToday = new Date();
		objToday.setHours(0);
		objToday.setMinutes(0);
		objToday.setSeconds(0);
		objToday.setMilliseconds(0);

		var aTable = [];
		aTable.push('<table summary="DateChooser">');
		aTable.push('<thead>');
		aTable.push('<tr>');
		aTable.push('<th><a class="previousyear" href="#" title="Previous Year">&laquo;</a></th>');
		aTable.push('<th><a class="previousmonth" href="#" title="Previous Month">&lt;</a></th>');
		aTable.push('<th colspan="3"><a class="currentdate" href="#" title="Current Date">' + objMonthYear.getPHPDate("M Y") + '</a></th>');
		aTable.push('<th><a class="nextmonth" href="#" title="Next Month">&gt;</a></th>');
		aTable.push('<th><a class="nextyear" href="#" title="Next Year">&raquo;</a></th>');
		aTable.push('</tr>');
		aTable.push('<tr>');
		aTable.push('<td>Z</td>');
		aTable.push('<td>M</td>');
		aTable.push('<td>D</td>');
		aTable.push('<td>W</td>');
		aTable.push('<td>D</td>');
		aTable.push('<td>V</td>');
		aTable.push('<td>Z</td>');
		aTable.push('</tr>');
		aTable.push('</thead>');
		aTable.push('<tbody>');

		while (objTempDate.getMonth() == objMonthYear.getMonth())
		{
			aTable.push('<tr>');
			for (var nWeek = 0; nWeek < 7; ++nWeek)
			{
				if ((objTempDate.getDay() == nWeek) && (objTempDate.getMonth() == objMonthYear.getMonth()))
				{
					sClass  = (objTempDate.getTime() == objSelectedDate.getTime()) ? 'selectedday' : '';
					sClass += (objTempDate.getTime() == objToday.getTime()) ? ' today' : '';
					sClass  = ((sClass.length > 0) && (sClass[1] == ' ')) ? sClass.substr(1, sClass.length - 1) : sClass;
					sClass  = (sClass.length > 0) ? ' class="' + sClass + '"' : '';
					aTable.push('<td><a href="#"' + sClass + '>' + objTempDate.getDate() + '</a></td>');
					objTempDate.setDate(objTempDate.getDate() + 1);
				}
				else
				{
					aTable.push('<td></td>');
				}
			}
			aTable.push('</tr>');
		}
		aTable.push('</tbody>');
		aTable.push('</table>');
		ndDateChooser.innerHTML = aTable.join('');

		objTempDate = null;
		objToday = null;

		AddClickEvents();
		return true;
	};

	var DisplayDateChooser = function()
	{
		var nPositionX = (arguments.length > 0) ? arguments[0] : 'auto';
		var nPositionY = (arguments.length > 1) ? arguments[1] : 'auto';

		var ndStyle = ndDateChooser.style;
		ndStyle.top = (nPositionY == 'auto') ? nPositionY : nPositionY + 'px';
		ndStyle.left = (nPositionX == 'auto') ? nPositionX : nPositionX + 'px';

		objMonthYear.setFullYear(objSelectedDate.getFullYear());
		objMonthYear.setMonth(objSelectedDate.getMonth());

		ndDateChooser.style.display = 'block';

		if (objTimeout)
		{
			clearTimeout(objTimeout);
		}

		RefreshDisplay();
		return true;
	};

	this.displayPosition = function()
	{
		var nPositionX = (arguments.length > 0) ? arguments[0] : 'auto';
		var nPositionY = (arguments.length > 1) ? arguments[1] : 'auto';

		return DisplayDateChooser(nPositionX, nPositionY);
	};

	this.display = function(e)
	{
		e = e || window.event;

		var nPositionX = 'auto';
		var nPositionY = 'auto';
		if (e)
		{
			if (e.pageX || e.pageY)
			{
				nPositionX = e.pageX + nXOffset;
				nPositionY = e.pageY + nYOffset;
			}
			else if (e.clientX || e.clientY)
			{
				nPositionX = e.clientX + document.body.scrollLeft + nXOffset;
				nPositionY = e.clientY + document.body.scrollTop + nYOffset;
			}
		}

		DisplayDateChooser(nPositionX, nPositionY);

		if (e.preventDefault)
		{
			e.preventDefault();
		}

		if (e.stopPropagation)
		{
			e.stopPropagation();
		}

		e.returnValue = false;
		e.cancelBubble = true;

		return false;
	};

	this.setXOffset = function()
	{
		nXOffset = ((arguments.length > 0) && (typeof(arguments[0]) == 'number')) ? parseInt(arguments[0], 10) : nXOffset;

		return true;
	};

	this.setYOffset = function()
	{
		nYOffset = ((arguments.length > 0) && (typeof(arguments[0]) == 'number')) ? parseInt(arguments[0], 10) : nYOffset;

		return true;
	};

	this.setCloseTime = function()
	{
		nTimeout = ((arguments.length > 0) && (typeof(arguments[0]) == 'number') && (arguments[0] >= 0)) ? arguments[0] : nTimeout;

		return true;
	};

	this.setUpdateFunction = function()
	{
		if ((arguments.length > 0) && (typeof(arguments[0]) == 'function'))
		{
			fnUpdate = arguments[0];
		}

		return true;
	};

	this.setUpdateField = function()
	{
		if (arguments.length != 2)
		{
			return false;
		}

		aUpdateField = [];
		aPHPFormat = [];
		if ((typeof(arguments[0]) == 'string') && (typeof(arguments[1]) == 'string') && document.getElementById(arguments[0]))
		{
			aUpdateField.push(document.getElementById(arguments[0]));
			aPHPFormat.push(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;
				}

				if (document.getElementById(arguments[0][nField]))
				{
					aUpdateField.push(document.getElementById(arguments[0][nField]));
					aPHPFormat.push(arguments[1][nField]);
				}
			}

		}

		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;
		if (sTitleText.length > 0)
		{
			ndIcon.setAttribute('title', sTitleText);
		}
		ndIcon.setAttribute('alt', 'DateChooser Icon ' + (nDateChooserID + 1));

		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()
	{
		var objStartDate = ((arguments.length > 0) && (typeof(arguments[0]) == 'object') && (arguments[0].getFullYear)) ? arguments[0] : null;
		if (objStartDate !== null)
		{
			objSelectedDate.setFullYear(objStartDate.getFullYear());
			objSelectedDate.setMonth(objStartDate.getMonth());
			objSelectedDate.setDate(objStartDate.getDate());
		}

		return true;
	};

	this.getSelectedDate = function()
	{
		return objSelectedDate;
	};

	ndDateChooser.onmouseover = function()
	{
		if (objTimeout)
		{
			clearTimeout(objTimeout);
		}

		return true;
	};

	ndDateChooser.onmouseout = function()
	{
		if (nTimeout > 0)
		{
			objTimeout = setTimeout('document.getElementById("' + sDateChooserID + '").style.display = "none";', nTimeout);
		}

		return true;
	};

	return true;
}
