/*
 * Copyright : (c) 2006 Webfish IT Services
 * Website   : http://www.webfish.nl
 * Email     : info@webfish.nl
 *
 * Webfish - Datepicker
 *
 * -------------------------------------------------------------------------
 *   $Id: datepicker.js 212 2008-08-20 07:59:43Z caspar $
 * -------------------------------------------------------------------------
 */
var fixedX         = -1;
var fixedY         = -1;
var strGoto        = "Ga naar huidige maand";
var strToday       = "Vandaag: ";
var strWeek        = "Wk";
var strScrollLeft  = "Klik naar de vorige maand.";
var strScrollRight = "Klik naar de volgende maand.";
var strSelectMonth = "Selecteer een maand.";
var strSelectYear  = "Selecteer een jaar";
var strSelectDate  = "Selecteer [date].";
var strClose       = "Sluit de datepicker.";
if (typeof blnShowToday == "undefined")
	var blnShowToday   = true;
var showNumYears   = 9; // odd number = current year in the middle

var arMonthNames = new Array("Januari",
							"Februari",
							"Maart",
							"April",
							"Mei",
							"Juni",
							"Juli",
							"Augustus",
							"September",
							"Oktober",
							"November",
							"December");
var arDayNames   = new Array("Ma", "Di", "Wo", "Do", "Vr", "Za", "Zo");

var objDatepicker,
	objDatepickerMonth, objDatepickerMonthDrop,
	objDatepickerYear, objDatepickerYearDrop, objDatepickerYearDropList, objDatepickerYearDropDec, objDatepickerYearDropAdd,
	objDatepickerClose,
	objDatepickerToday;
var blnMonthConstructed, blnYearConstructed;
var objDateOutput, objDateButton, strSelDay, strSelMonth, strSelYear, ostrSelDay, ostrSelMonth, ostrSelYear;

var intervalID1, timeoutID1, dateFormat, nStartingYear;

var dateToday = new Date();
var dateDay   = dateToday.getDate();
var dateMonth = dateToday.getMonth();
var dateYear  = dateToday.getYear();

var blnShow = false;
var blnPageLoaded = false;

if (document.write)
{
	document.write("<div id=\"datepicker\" style=\"visibility: hidden; width: 250px;\" onclick=\"blnShow=true;\">");
	document.write(	"<div id=\"datepicker_head\">");
	document.write(		"<div id=\"datepicker_left\"><div style=\"position: relative; top: 4px;\"><img src=\"images/datepicker-left.gif\" alt=\"\"></div></div>");
	document.write(		"<div id=\"datepicker_right\"><div style=\"position: relative; top: 4px;\"><img src=\"images/datepicker-right.gif\" alt=\"\"></div></div>");
	document.write(		"<div id=\"datepicker_month\"><div id=\"datepicker_month_month\"></div><div id=\"datepicker_month_down\"><img src=\"images/datepicker-down.gif\" alt=\"\"></div></div>");
	document.write(		"<div id=\"datepicker_year\"><div id=\"datepicker_year_year\"></div><div id=\"datepicker_year_down\"><img src=\"images/datepicker-down.gif\" alt=\"\"></div></div>");
	document.write(		"<div id=\"datepicker_close\"><div style=\"position: relative; top: 4px; text-align: center;\"><img src=\"images/datepicker-close.gif\" alt=\"\"></div></div>");
	document.write(	"</div>");
	document.write(	"<div id=\"datepicker_content\">");
	document.write(		"<div style=\"width: 100%; height: 2px; line-height: 2px;\"></div>");
	document.write(		"<div id=\"datepicker_content_text\" style=\"position: relative; left: 2px; width: 242px;\"></div>");
	document.write(		"<div style=\"width: 100%; height: 2px; line-height: 2px;\"></div>");
	document.write(	"</div>");
	if (blnShowToday)
	{
		document.write(	"<div id=\"datepicker_foot\"><div style=\"position: relative; top: 2px;\" id=\"datepicker_foot_text\"></div></div>");
	}
	document.write("");
	document.write(	"<div id=\"datepicker_month_drop\" style=\"visibility: hidden; position: absolute; top: 15px; left: 38px; width: 125px; background-color: #ffffff; color: #000000; border: solid black 1px;\">");
	document.write(		"<ul id=\"datepicker_drop_month_list\"></ul>");
	document.write(	"</div>");
	document.write("");
	document.write(	"<div id=\"datepicker_year_drop\" style=\"visibility: hidden; position: absolute; top: 15px; left: 167px; width: 60px; background-color: #ffffff; color: #000000; border: solid black 1px;\">");
	document.write(		"<ul id=\"datepicker_drop_year_list_dec\"><li id=\"datepicker_drop_year_list_dec_dec\" style=\"text-align: center;\">-</li></ul>");
	document.write(		"<ul id=\"datepicker_drop_year_list\"></ul>");
	document.write(		"<ul id=\"datepicker_drop_year_list_add\"><li id=\"datepicker_drop_year_list_add_add\" style=\"text-align: center;\">+</li></ul>");
	document.write(	"</div>");
	document.write("</div>");
}

function initDatepicker()
{
	dateYear = dateYear % 100;
	dateYear += ((dateYear < 38) ? 2000 : 1900);

	blnMonthConstructed = false;
	blnYearConstructed  = false;
	if (document.getElementById)
	{
		objDatepicker          = document.getElementById('datepicker');
		objDatepickerMonthDrop = document.getElementById('datepicker_month_drop');
		objDatepickerYearDrop  = document.getElementById('datepicker_year_drop');
		objDatepickerYearDropList = document.getElementById('datepicker_drop_year_list');
		objDatepickerYearDropDec  = document.getElementById('datepicker_drop_year_list_dec_dec');
		objDatepickerYearDropAdd  = document.getElementById('datepicker_drop_year_list_add_add');

		objDatepickerLeft  = document.getElementById('datepicker_left');
		objDatepickerRight = document.getElementById('datepicker_right');
		objDatepickerMonth = document.getElementById('datepicker_month');
		objDatepickerYear  = document.getElementById('datepicker_year');
		objDatepickerClose = document.getElementById('datepicker_close');
		if (blnShowToday)
		{
			objDatepickerToday = document.getElementById('datepicker_foot');
		}

		objDatepickerLeft.onmouseover = function ()
		{
			datepicker_onHover(this);
			//popDownYear();
			popDownMonth();
			return statusText(strScrollLeft);
		}
		objDatepickerLeft.onmouseout = function ()
		{
			clearInterval(intervalID1);
			datepicker_offHover(this);
			return statusText('');
		}
		objDatepickerLeft.onclick = function ()
		{
			decMonth();
		}
		objDatepickerLeft.onmousedown = function ()
		{
			clearTimeout(timeoutID1);
			timeoutID1 = setTimeout('StartDecMonth()',500);
		}
		objDatepickerLeft.onmouseup = function ()
		{
			clearTimeout(timeoutID1);
			clearInterval(intervalID1);
		}

		
		objDatepickerRight.onmouseover = function ()
		{
			datepicker_onHover(this);
			popDownYear();
			popDownMonth();
			return statusText(strScrollRight);
		}
		objDatepickerRight.onmouseout = function ()
		{
			clearInterval(intervalID1);
			datepicker_offHover(this);
			return statusText('');
		}
		objDatepickerRight.onclick = function ()
		{
			incMonth();
		}
		objDatepickerRight.onmousedown = function ()
		{
			clearTimeout(timeoutID1);
			timeoutID1 = setTimeout('StartIncMonth()',500);
		}
		objDatepickerRight.onmouseup = function ()
		{
			clearTimeout(timeoutID1);
			clearInterval(intervalID1);
		}

		objDatepickerMonth.onmouseover = function ()
		{
			datepicker_onHover(this);
			return statusText(strSelectMonth);
		}
		objDatepickerMonth.onmouseout = function ()
		{
			datepicker_offHover(this);
			return statusText('');
		}
		objDatepickerMonth.onclick = function ()
		{
			popUpMonth();
		}

		objDatepickerMonthDrop.onmouseover = function ()
		{
			clearTimeout(timeoutID1);
		}
		objDatepickerMonthDrop.onmouseout = function ()
		{
			clearTimeout(timeoutID1);
			timeoutID1 = setTimeout("popDownMonth()",100);
			return false;
		}

		objDatepickerYear.onmouseover = function ()
		{
			datepicker_onHover(this);
			return statusText(strSelectYear);
		}
		objDatepickerYear.onmouseout = function ()
		{
			datepicker_offHover(this);
			return statusText('');
		}
		objDatepickerYear.onclick = function ()
		{
			popUpYear();
		}

		objDatepickerYearDrop.onmouseover = function ()
		{
			clearTimeout(timeoutID1);
		}
		objDatepickerYearDrop.onmouseout = function ()
		{
			clearTimeout(timeoutID1);
			timeoutID1 = setTimeout("popDownYear()",100);
			return false;
		}

		objDatepickerYearDropDec.onmousedown = function ()
		{
			clearInterval(intervalID1);
			intervalID1 = setInterval('decYear()', 30);
		}
		objDatepickerYearDropDec.onmouseup = function ()
		{
			clearInterval(intervalID1);
		}

		objDatepickerYearDropAdd.onmousedown = function ()
		{
			clearInterval(intervalID1);
			intervalID1 = setInterval('incYear()', 30);
		}
		objDatepickerYearDropAdd.onmouseup = function ()
		{
			clearInterval(intervalID1);
		}
				
		objDatepickerClose.onclick = function ()
		{
			hideDatepicker();
		}

		if (blnShowToday)
		{
			document.getElementById('datepicker_foot_text').innerHTML = strToday + 
				arDayNames[(dateToday.getDay() - 1 == -1) ? 6 : (dateToday.getDay() - 1)] +", "+ dateDay +" "+ arMonthNames[dateMonth].substring(0,3) +" "+ dateYear;

			objDatepickerToday.onmouseover = function ()
			{
				return statusText(strGoto);
			}
			objDatepickerToday.onmouseout = function ()
			{
				return statusText('');
			}

			objDatepickerToday.onclick = function ()
			{
				strSelMonth = dateMonth;
				strSelYear = dateYear;
				constructDatepicker();
			}
		}
		blnPageLoaded = true;
	}
}

/*
 * Month Pulldown
 */

function StartDecMonth()
{
	intervalID1 = setInterval("decMonth()", 80);
}

function StartIncMonth()
{
	intervalID1 = setInterval("incMonth()",80);
}

function incMonth ()
{
	strSelMonth++;
	if (strSelMonth > 11)
	{
		strSelMonth = 0;
		strSelYear++;
	}
	constructDatepicker();
}

function decMonth ()
{
	strSelMonth--;
	if (strSelMonth < 0)
	{
		strSelMonth = 11;
		strSelYear--;
	}
	constructDatepicker();
}

function constructMonth()
{
	//popDownYear();
	if (!blnMonthConstructed)
	{
		sHTML = "";
		for (i = 0; i < 12; i++)
		{
			sName = arMonthNames[i];
			if (i == strSelMonth)
			{
				sName = sName;
			}
			sHTML += "<li "+
				"onmouseover=\"datepicker_onHover(objDatepickerMonth);datepicker_onHover(this);\" "+
				"onmouseout=\"datepicker_offHover(this);\" "+
				"onclick=\"blnMonthConstructed=false;strSelMonth=" + i + ";constructDatepicker();popDownMonth();\">"+
				sName +"</li>";
		}
		document.getElementById("datepicker_drop_month_list").innerHTML = sHTML;
		blnMonthConstructed = true;
	}
}

function popUpMonth()
{
	constructMonth();
	objDatepickerMonthDrop.style.visibility = "visible";
}

function popDownMonth()
{
	datepicker_offHover(objDatepickerMonth);
	objDatepickerMonthDrop.style.visibility = "hidden";
}

/*
 * Year Pulldown 
 */
function incYear()
{
	for (i = 0; i < showNumYears; i++)
	{
		newYear = (i+nStartingYear)+1;
		if (newYear == strSelYear)
		{
			newYear = "<b>" + newYear + "</"+"b>";
		}
		document.getElementById("y"+i).innerHTML = newYear;
	}
	nStartingYear++;
	blnShow = true;
}

function decYear()
{
	for (i = 0; i < showNumYears; i++)
	{
	 newYear = (i+nStartingYear)-1;
		if (newYear == strSelYear)
		{
			newYear = "<b>"	+ newYear +	"</"+"b>"; 
		}
		document.getElementById("y"+i).innerHTML = newYear;
	}
	nStartingYear--;
	blnShow = true;
}

function selectYear(nYear)
{
	strSelYear = parseInt(nYear+nStartingYear);
	blnYearConstructed = false;
	constructDatepicker();
	popDownYear();
}

function constructYear()
{
	popDownMonth();
	sHTML = "";
	if (!blnYearConstructed)
	{
		sHTML = "";
		j = 0;
		showNumYearsHalf = Math.floor((showNumYears - 1) / 2);
		nStartingYear = strSelYear - showNumYearsHalf;
		nMaxYear = nStartingYear + showNumYears;
		for (i = nStartingYear; i < nMaxYear; i++)
		{
			sName = i;
			if (i == strSelYear)
			{
				sName = "<B>" +	sName +	"</"+"B>";
			}
			sHTML += "<li id=\"y"+ j +"\" "+
				"onmouseover=\"datepicker_onHover(this);datepicker_onHover(objDatepickerYear)\" "+
				"onmouseout=\"datepicker_offHover(this);\" "+
				"onclick=\"selectYear("+ j +")\">"+
				sName +"</li>";
			j++;
		}
		objDatepickerYearDropList.innerHTML = sHTML;
		blnYearConstructed = true;
	}
}

function popDownYear()
{
	clearInterval(intervalID1);
	clearTimeout(timeoutID1);
	objDatepickerYearDrop.style.visibility = "hidden";
	datepicker_offHover(objDatepickerYear);
}

function popUpYear()
{
	constructYear();
	objDatepickerYearDrop.style.visibility = "visible";
}
//*/

function constructDatepicker()
{
	var aNumDays = new Array(31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	var dateMessage;
	var startDate = new	Date(strSelYear, strSelMonth, 1);
	var endDate;

	if (strSelMonth == 1)
	{ // Februari
		endDate = new Date(strSelYear, strSelMonth + 1, 1);
		endDate = new Date(endDate - (24 * 60 * 60 * 1000));
		numDaysInMonth = endDate.getDate();
	}
	else
	{
		numDaysInMonth = aNumDays[strSelMonth];
	}

	datePointer = 0;
	dayPointer = startDate.getDay() - 1;

	if (dayPointer < 0)
	{
		dayPointer = 6;
	}
	
	sHTML = "";
	sHTML += "<div style=\"width: 100%; height: 18px;\">";
	sHTML += "<div class=\"datepicker_content_head_wk\" style=\"left: 0px;\">" + strWeek + "</div>";
	
	for (i = 0; i < 7; i++)	
	{
		sHTML += "<div class=\"datepicker_content_head\" style=\"left: "+ ((i * 30) + 33) +"px;\">"+ arDayNames[i] +"</div>";
	}
	sHTML += "</div>";

	dayPos = 0;
	sHTML += "<div style=\"width: 100%; height: 18px;\">";
	sHTML += "<div class=\"datepicker_content_day_wk\" style=\"left: 0px;\">" + WeekNbr(startDate) + "&nbsp;</div>";
	dayPos = dayPointer;

	for (datePointer = 1; datePointer <= numDaysInMonth; datePointer++)
	{
		dayPointer++;
		strClass = "datepicker_content_day";
		if ((datePointer == ostrSelDay) && (strSelMonth == ostrSelMonth) && (strSelYear == ostrSelYear))
		{
			strClass += " datepicker_content_day_selected";
		}

		if ((datePointer == dateDay) && (strSelMonth == dateMonth) && (strSelYear == dateYear))
		{
			strClass += " datepicker_content_day_today";
		}
		else if (((dayPointer + 1) % 7) <= 1)
		{
			strClass += " datepicker_content_day_weekend";
		}

		sHTML += "<div class=\""+ strClass +"\" style=\"left: "+ ((dayPos++ * 30) + 33) +"px;\" "+
			"onmouseover=\"return statusText('"+ strSelectDate.replace("[date]", constructDate(datePointer, strSelMonth, strSelYear)) +"')\" "+
			"onmouseout=\"return statusText('')\" "+
			"onclick=\"javascript:strSelDay="+ datePointer +";closeDatepicker();\">&nbsp;";
		sHTML += datePointer;

		sHTML += "</div>";
		
		if ((dayPointer + 1) % 7 == 1)
		{
			dayPos = 0;
			sHTML += "</div>";

			if (datePointer < numDaysInMonth)
			{
				sHTML += "<div style=\"width: 100%; height: 18px;\">";
				sHTML += "<div class=\"datepicker_content_day_wk\" style=\"left: 0px;\">"+
					(WeekNbr(new Date(strSelYear, strSelMonth, datePointer+1))) + "&nbsp;</div>";
			}
		}
	}
	sHTML += "</div>";

	document.getElementById("datepicker_content_text").innerHTML = sHTML;
	document.getElementById("datepicker_month_month").innerHTML = arMonthNames[strSelMonth];
	document.getElementById("datepicker_year_year").innerHTML = strSelYear;
}

function popUpDatepicker(ctl, ctl2, format, frameOffSetLeft, frameOffSetTop)
{
	var leftpos = 0;
	var toppos = 0;

	if (blnPageLoaded)
	{
		if (objDatepicker.style.visibility == "hidden")
		{
			objDateOutput = ctl2;
			dateFormat = format;

			formatChar = " ";
			aFormat = dateFormat.split(formatChar);
			if (aFormat.length < 3)
			{
				formatChar = "/";
				aFormat = dateFormat.split(formatChar);
				if (aFormat.length < 3)
				{
					formatChar = ".";
					aFormat = dateFormat.split(formatChar);
					if (aFormat.length<3)
					{
						formatChar = "-";
						aFormat = dateFormat.split(formatChar);
						if (aFormat.length < 3)
						{
							// invalid date	format
							formatChar = "";
						}
					}
				}
			}

			tokensChanged = 0;
			if (formatChar	!= "")
			{
				// use user's date
				aData = ctl2.value.split(formatChar);

				for (i = 0;i<3;i++)
				{
					if ((aFormat[i] == "d") || (aFormat[i] == "dd"))
					{
						strSelDay = parseInt(aData[i], 10);
						tokensChanged++;
					}
					else if ((aFormat[i] == "m") || (aFormat[i] == "mm"))
					{
						strSelMonth = parseInt(aData[i], 10) - 1;
						tokensChanged++;
					}
					else if (aFormat[i] == "yyyy")
					{
						strSelYear = parseInt(aData[i], 10);
						tokensChanged++;
					}
					else if (aFormat[i] == "mmm")
					{
						for (j = 0; j < 12; j++)
						{
							if (aData[i] == arMonthNames[j])
							{
								strSelMonth = j;
								tokensChanged++;
							}
						}
					}
				}
			}

			if ((tokensChanged != 3) || isNaN(strSelDay) || isNaN(strSelMonth) || isNaN(strSelYear))
			{
				strSelDay   = dateDay;
				strSelMonth = dateMonth;
				strSelYear  = dateYear;
			}

			ostrSelDay   = strSelDay;
			ostrSelMonth = strSelMonth;
			ostrSelYear  = strSelYear;

			leftPos = fixedX;
			topPos = fixedY;
			if (leftPos == -1)
			{
				if (!ctl.offsetWidth)
				{
					ctl.offsetWidth = 0;
				}
				leftPos = findPosX(ctl) + frameOffSetLeft + ctl.offsetWidth;
			}
			if (topPos == -1)
			{
				if (!ctl.offsetHeight)
				{
					ctl.offsetHeight = 0;
				}
				topPos = findPosY(ctl) + frameOffSetTop + ctl.offsetHeight;
			}
			objDatepicker.style.left = leftPos +'px';
			objDatepicker.style.top = topPos +'px';

			constructDatepicker();

			objDatepicker.style.visibility = "visible";
			
			blnShow = true;
		}
		else
		{
			hideDatepicker();
			if (objDateButton != ctl)
			{
				popUpDatepicker(ctl, ctl2, format, frameOffSetLeft, frameOffSetTop);
			}
		}
		objDateButton = ctl;
	}
}

function hideDatepicker()
{
	if (objDatepicker)
	{
		objDatepicker.style.visibility = "hidden";
		if (objDatepickerMonthDrop)
		{
			objDatepickerMonthDrop.style.visibility = "hidden";
		}
		if (objDatepickerYearDrop)
		{
			objDatepickerYearDrop.style.visibility = "hidden";
		}

		blnShow = false;
	}
}

function closeDatepicker()
{
	hideDatepicker();
	objDateOutput.value = constructDate(strSelDay, strSelMonth, strSelYear);
}

/*
 * Utility functions
 */
function datepicker_onHover(objItem)
{
	if (objItem.className.indexOf('hover') == -1)
		objItem.className += ' hover';
}

function datepicker_offHover(objItem)
{
	objItem.className = objItem.className.replace(/ hover/g, '');
	objItem.className = objItem.className.replace(/hover/g, '');
}

function WeekNbr(n) 
{
	// Algorithm used:
	// From Klaus Tondering's Calendar document (The Authority/Guru)
	// http://www.tondering.dk/claus/calendar.html
	// a = (14-month) / 12
	// y = year + 4800 - a
	// m = month + 12a - 3
	// J = day + (153m + 2) / 5 + 365y + y / 4 - y / 100 + y / 400 - 32045
	// d4 = (J + 31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
	// L = d4 / 1460
	// d1 = ((d4 - L) mod 365) + L
	// WeekNumber = d1 / 7 + 1

	year = n.getFullYear();
	month = n.getMonth() + 1;
	day = n.getDate();
	a = Math.floor((14-month) / 12);
	y = year + 4800 - a;
	m = month + 12 * a - 3;
	b = Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400);
	J = day + Math.floor((153 * m + 2) / 5) + 365 * y + b - 32045;
	d4 = (((J + 31741 - (J % 7)) % 146097) % 36524) % 1461;
	L = Math.floor(d4 / 1460);
	d1 = ((d4 - L) % 365) + L;
	week = Math.floor(d1/7) + 1;

	return week;
}
function padZero(num)
{
	return (num	< 10)? '0' + num : num ;
}

function constructDate(d, m, y)
{
	sTmp = dateFormat;
	sTmp = sTmp.replace("dd", "<e>");
	sTmp = sTmp.replace("d", "<d>");
	sTmp = sTmp.replace("<e>", padZero(d));
	sTmp = sTmp.replace("<d>", d);
	sTmp = sTmp.replace("mmm", "<o>");
	sTmp = sTmp.replace("mm", "<n>");
	sTmp = sTmp.replace("m", "<m>");
	sTmp = sTmp.replace("<m>", m+1);
	sTmp = sTmp.replace("<n>", padZero(m+1));
	sTmp = sTmp.replace("<o>", arMonthNames[m]);
	return sTmp.replace("yyyy", y);
}

function statusText(sText)
{
	window.status = sText;
	return true;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function hideCallKey (e)
{
	if (!e)
	{
		e = window.event;
	}
	if (!blnShow && (e.keyCode == 27))
	{
		hideDatepicker();
	}
}

function hideCallClick (e)
{
	if (!blnShow)
	{
		hideDatepicker();
	}
	blnShow = false;
}

/*
 * Set global event handlers for keypress, onclick and onload
 */
var prevOnKeypress = (document.onkeypress) ? document.onkeypress : function () { };
document.onkeypress = function (e) { prevOnKeypress(e); hideCallKey(e) }

var prevOnClick = (document.onclick) ? document.onclick : function () { };
document.onclick = function (e) { prevOnClick(e); hideCallClick(e); }

var oldOnload = (window.onload) ? window.onload : function () { };
window.onload = function () { oldOnload(); initDatepicker(); }