//	written	by Tan Ling	Wee	on 2 Dec 2001
//	last updated 23 June 2002
//	email :	fuushikaden@yahoo.com

var	fixedX = -1 		// x position (-1 if to appear below control)
var	fixedY = -1			// y position (-1 if to appear below control)
var startAt = 1			// 0 - sunday ; 1 - monday
var showWeekNumber = 0	// 0 - don't show; 1 - show
var showToday = 1		// 0 - don't show; 1 - show
var imgDir = VIEW_HTTP + "/img/calendar.js/"			// directory for images ... e.g. var imgDir="/img/"

var gotoString = "Ga naar de huidige maand"
var todayString = "<font id='cal_vandaag'>Vandaag: </font>"
var weekString = "Wk"
var scrollLeftMessage = ""
var scrollRightMessage = ""
var selectMonthMessage = ""
var selectYearMessage = ""
var selectDateMessage = "" // do not replace [date], it will be replaced by date.

var	crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlToPlaceValue, ctlNow, dateFormat, nStartingYear

var	bPageLoaded=false
var	ie=document.all
var	dom=document.getElementById

var	ns4=document.layers
var	today =	new	Date()
var	dateNow	 = today.getDate()
var	monthNow = today.getMonth()
var	yearNow	 = today.getYear()
var	imgsrc = new Array("drop1.gif","drop2.gif","left1.gif","left2.gif","right1.gif","right2.gif")
var	img	= new Array()

var bShow = false;

/* hides <select> and <applet> objects (for IE only) */
function hideElement( elmID, overDiv ){
  if( ie ){
  	var cat = document.all.tags( elmID );
    var catl = cat.length;
    for( var i = 0; i < catl; i++ ){
      obj = cat[i];
      if( !obj || !obj.offsetParent ){
        continue;
      }
  
      // Find the element's offsetTop and offsetLeft relative to the BODY tag.
      objLeft   = obj.offsetLeft;
      objTop    = obj.offsetTop;
      objParent = obj.offsetParent;
      
      while( objParent.tagName.toUpperCase() != "BODY" ) {
      	if(!aTag.offsetParent)
			break;
        objLeft  += objParent.offsetLeft;
        objTop   += objParent.offsetTop;
        objParent = objParent.offsetParent;
      }
  
      objHeight = obj.offsetHeight;
      objWidth = obj.offsetWidth;
  
      if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
      else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
      else if( overDiv.offsetTop >= ( objTop + objHeight ));
      else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
      else{
        obj.style.visibility = "hidden";
      }
    }
  }
}
  
 /*
 * unhides <select> and <applet> objects (for IE only)
 */
 function showElement( elmID ) {
   if( ie ){
   	 var cat = document.all.tags( elmID );
   	 var catl = cat.length;
     for( var i = 0; i < catl; i++ ){
       obj = cat[i];
       
       if( !obj || !obj.offsetParent )
         continue;
       obj.style.visibility = "";
     }
   }
 }

function HolidayRec (d, m, y, desc){
	this.d = d
	this.m = m
	this.y = y
	this.desc = desc
}

var HolidaysCounter = 0
var Holidays = new Array()

function addHoliday (d, m, y, desc){
	Holidays[HolidaysCounter++] = new HolidayRec ( d, m, y, desc )
}

if (dom){
	/*for	(var i=0;i<imgsrc.length;i++){
		img[i] = new Image
		img[i].src = imgDir + imgsrc[i]
	}*/
	document.write ("<div onclick='bShow=true' "+
							"id='calendar' "+
							"onmouseover=\"getEl('close_calendar').src='"+imgDir+"close.gif'\" "+
							"onmouseup=\"getEl('close_calendar').src='"+imgDir+"close.gif'\">"+
						"<table cellpadding='0' "+
								"summary='weken' "+
								"cellspacing='0' "+
								"id='cal_head' "+
								"width="+((showWeekNumber==1)?250:200)+">"+ 
							"<thead>"+
								"<tr>"+
									"<td>"+
										"<span id='caption'></span>"+
										"<img id='close_calendar' " +
												"src='" + imgDir + "close.gif' alt='Close' " + 
												"onclick='javascript:hideCalendar()' " +
												"onmousedown=\"this.src='"+imgDir+"close2.gif'\" "+
												"onmouseover='popDownYear(); popDownMonth();' />"+
									"</td>"+
								"</tr>"+
							"</thead>"+
							"<tbody>"+
								"<tr>"+
									"<td>"+
										"<span id='c_content'></span>"+
									"</td>"+
								"</tr>" +
							"</tbody>")
	if (showToday==1){
		document.write ("<tfoot>"+
							"<tr>"+
								"<td>"+
									"<span id='lblToday'></span>"+
								"</td>"+
							"</tr>" +
						"</tfoot>")
	}	
	document.write ("</table>"+
				"</div>"+
				"<div id='selectMonth'>"+
				"</div>"+
				"<div id='selectYear'>"+
				"</div>");
}

var	monthName =	new	Array("Januari","Februari","Maart","April","Mei","Juni","Juli","Augustus","September","Oktober","November","December")
if (startAt==0){
	var dayName = new Array	("Zo","Ma","Di","Wo","Do","Vr","Za")
}else{
	var dayName = new Array	("Ma","Di","Wo","Do","Vr","Za","Zo")
}
var	styleAnchor="text-decoration:none;color:black;"
var	styleLightBorder="border-style:dashed;border-width:1px;border-color:#C44405;"

function swapImage(srcImg, destImg){
	if (ie)	{ document.getElementById(srcImg).setAttribute("src",imgDir + destImg) }
}

function init()	{
	if (!ns4){
		if (!ie){ 
			yearNow += 1900	
		}

		crossobj=(dom)?document.getElementById("calendar").style : ie? document.all.calendar : document.calendar
		hideCalendar()

		crossMonthObj=(dom)?document.getElementById("selectMonth").style : ie? document.all.selectMonth	: document.selectMonth

		crossYearObj=(dom)?document.getElementById("selectYear").style : ie? document.all.selectYear : document.selectYear

		monthConstructed=false;
		yearConstructed=false;

		if (showToday==1){
			document.getElementById("lblToday").innerHTML =	todayString + " <span class='a' onmousemove='window.status=\""+gotoString+"\"' "+
																					"onmouseout='window.status=\"\"' "+
																					"title='"+gotoString+"' "+
																					"onclick='javascript:monthSelected=monthNow;yearSelected=yearNow;constructCalendar();'>"
																				+dayName[(today.getDay()-startAt==-1)?6:(today.getDay()-startAt)]+". " + dateNow + " " + monthName[monthNow] + " " + yearNow + 
																			"</span>";
		}

		var sHTML1="<div id='spafnLeft' "+
						"onmouseover='swapImage(\"changeLeft\",\"left2.gif\");window.status=\""+scrollLeftMessage+"\"; popDownYear(); popDownMonth();' "+
						"onclick='javascript:decMonth()' "+
						"onmouseout='clearInterval(intervalID1);swapImage(\"changeLeft\",\"left1.gif\");window.status=\"\"' "+
						"onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)' "+
						"onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>"+
					"<img id='changeLeft' src='"+imgDir+"left1.gif' alt='Terug'/>"+
				"</div>";
		sHTML1+="<div id='spanRight' "+
						"onmouseover='swapImage(\"changeRight\",\"right2.gif\");window.status=\""+scrollRightMessage+"\"; popDownYear(); popDownMonth();' "+
						"onmouseout='clearInterval(intervalID1);swapImage(\"changeRight\",\"right1.gif\");window.status=\"\"' "+
						"onclick='incMonth()' "+
						"onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)' "+
						"onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>"+
					"<img id='changeRight' src='"+imgDir+"right1.gif'>"+
					"</div>";
		sHTML1+="<div id='spanMonth' "+
						"onmouseover='swapImage(\"changeMonth\",\"drop2.gif\");window.status=\""+selectMonthMessage+"\"; popDownYear();' "+
						"onmouseout='swapImage(\"changeMonth\",\"drop1.gif\");window.status=\"\"' "+
						"onclick='popUpMonth()'></div>";
		sHTML1+="<div id='spanYear' "+
						"style='' "+
						"onmouseover='swapImage(\"changeYear\",\"drop2.gif\");window.status=\""+selectYearMessage+"\"; popDownMonth();' "+
						"onmouseout='swapImage(\"changeYear\",\"drop1.gif\");window.status=\"\"'	"+
						"onclick='popUpYear()'></div>";
		
		document.getElementById("caption").innerHTML  =	sHTML1

		bPageLoaded=true
	}
}

function hideCalendar()	{
if(crossobj)
	crossobj.visibility="hidden"
	if (crossMonthObj != null){crossMonthObj.visibility="hidden"}
	if (crossYearObj !=	null){crossYearObj.visibility="hidden"}

    showElement( 'SELECT' );
	showElement( 'APPLET' );
}

function padZero(num) {
	return (num	< 10)? '0' + num : num ;
}

function constructDate(d,m,y){
	var 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>",monthName[m])
	return sTmp.replace ("yyyy",y)
}

function closeCalendar() {
	var	sTmp

	ctlToPlaceValue.focus();
	ctlToPlaceValue.value =	constructDate(dateSelected,monthSelected,yearSelected)
	ctlToPlaceValue.blur();
	
}

/*** Month Pulldown	***/

function StartDecMonth(){
	intervalID1=setInterval("decMonth()",80)
}

function StartIncMonth(){
	intervalID1=setInterval("incMonth()",80)
}

function incMonth () {
	monthSelected++
	if (monthSelected>11) {
		monthSelected=0
		yearSelected++
	}
	constructCalendar()
}

function decMonth () {
	monthSelected--
	if (monthSelected<0) {
		monthSelected=11
		yearSelected--
	}
	constructCalendar()
}

function constructMonth() {
	popDownYear()
	if (!monthConstructed) {
		var sHTML =	""
		for	(var i = 0; i<12;	i++) {
			var sName =	monthName[i];
			if (i==monthSelected)
				sName =	sName

			sHTML += "<tr>"+
						"<td id='m" + i + "' "+
								"onmouseover='this.style.backgroundColor=\"#CAD2E4\"; this.style.borderColor=\"#666666\"' "+
								"onmouseout='this.style.backgroundColor=\"\"; this.style.borderColor=\"#FFFFFF\"' "+
								"style='cursor:pointer; border: 1px solid #ffffff; background-color:#ffffff;' "+
								"onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>"+
							"&nbsp;" + sName + "&nbsp;"+
						"</td>"+
					"</tr>";
		}

		document.getElementById("selectMonth").innerHTML = "<table style='width:70px;z-index:+4000;position:absolute;font-family:arial; font-size:12px; border:1px solid #666666; border-top: 0px; background-color:#FFFFFF;' "+
																	"cellspacing='1' "+
																	"cellpadding='0' "+
																	"summary='maanden' "+
																	"onmouseover='clearTimeout(timeoutID1)'	"+
																	"onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" 
																+ sHTML + 
															"</table>"
		monthConstructed=true
	}
}

function popUpMonth() {
	constructMonth()
	crossMonthObj.visibility = (dom||ie)? "visible"	: "show"
	var leftOffset = parseInt(crossobj.left) + document.getElementById("spanMonth").offsetLeft

	crossMonthObj.left = leftOffset + 1 + 'px';
	crossMonthObj.top =	parseInt(crossobj.top) + 19 +'px';
	crossMonthObj.zIndex = '+1';
	crossobj.zIndex = '1'
	hideElement( 'SELECT', document.getElementById("selectMonth") );
	hideElement( 'APPLET', document.getElementById("selectMonth") );
}

function popDownMonth()	{
	crossMonthObj.visibility= "hidden";
}

/*** Year Pulldown ***/

function incYear() {
	var txtYear;
	for	(var i = 0; i<7; i++){
		var newYear	= (i+nStartingYear)+1
		if (newYear==yearSelected){ 
			txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;"
		}else{ 
			txtYear =	"&nbsp;" + newYear + "&nbsp;" 
		}
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear ++;
	bShow=true
}

function decYear() {
	var txtYear;
	for	(var i=0; i<7; i++){
		var newYear	= (i+nStartingYear)-1
		if (newYear==yearSelected){ 
			txtYear =	"&nbsp;<b>"	+ newYear +	"</b>&nbsp;" 
		}else{ 
			txtYear =	"&nbsp;" + newYear + "&nbsp;" 
		}
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear --;
	bShow=true
}

function selectYear(nYear) {
	yearSelected=parseInt(nYear+nStartingYear);
	yearConstructed=false;
	constructCalendar();
	popDownYear();
}

function constructYear() {
	popDownMonth()
	var sHTML =	""
	if (!yearConstructed) {
		sHTML =	"<tr>"+
					"<td align='center'	onmouseover='this.style.backgroundColor=\"#FFFFFF\"; this.style.borderColor=\"#666666\"' "+ 
							"onmouseout='this.style.backgroundColor=\"\"; this.style.borderColor=\"#FFFFFF\"' " +
							"style='cursor:pointer; border: 1px solid #FFFFFF;' " + 
							"onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' " + 
							"onmouseup='clearInterval(intervalID1)'>"+
						"-"+
					"</td>"+
				"</tr>"

		var j =	0
		nStartingYear =	yearSelected-3
		for	(var i=(yearSelected-3); i<=(yearSelected+3); i++) {
			var sName =	i;
			if (i==yearSelected){
				sName =	"<b>" +	sName +	"</b>"
			}

			sHTML += "<tr>"+ 
						"<td id='y" + j + "' "+
								"onmouseover='this.style.backgroundColor=\"#CAD2E4\"; this.style.borderColor=\"#666666\"' "+
								"onmouseout='this.style.backgroundColor=\"\"; this.style.borderColor=\"#FFFFFF\"' "+
								"style='cursor:pointer; border: 1px solid #ffffff;background-color:#ffffff;' "+
								"onclick='selectYear("+j+");event.cancelBubble=true'>"+
							"&nbsp;" + sName + "&nbsp;"+ 
						"</td>"+
					"</tr>"
			j++;
		}

		sHTML += "<tr>"+
					"<td align='center' "+
							"onmouseover='this.style.backgroundColor=\"#FFFFFF\"; this.style.borderColor=\"#666666\"' "+
							"onmouseout='this.style.backgroundColor=\"\"; this.style.borderColor=\"#FFFFFF\"' "+
							"style='cursor:pointer; border: 1px solid #ffffff;background-color:#ffffff;' "+
							"onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	"+
							"onmouseup='clearInterval(intervalID2)'>"+
						"+"+
					"</td>"+
				"</tr>"

		document.getElementById("selectYear").innerHTML	= "<table style='width:44px;font-family:arial; font-size:12px; border:1px solid #666666; border-top: 0px; background-color:#ffffff' "+
																	"onmouseover='clearTimeout(timeoutID2)' "+
																	"onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear();\",100)' "+
																	"summary='jaren' "+
																	"cellspacing='1' "+
																	"cellpadding='0'>"
																+ sHTML	+ 
															"</table>"

		yearConstructed	= true
	}
}

function popDownYear() {
	clearInterval(intervalID1)
	clearTimeout(timeoutID1)
	clearInterval(intervalID2)
	clearTimeout(timeoutID2)
	crossYearObj.visibility= "hidden"
}

function resetColor(){
	getEl('spanMonth').style.borderColor='#D4D0C8';
}

function popUpYear() {
	var	leftOffset

	constructYear()
	crossYearObj.visibility	= (dom||ie)? "visible" : "show"
	leftOffset = parseInt(crossobj.left) + document.getElementById("spanYear").offsetLeft

	crossYearObj.left = leftOffset + 1 + 'px'
	crossYearObj.top = parseInt(crossobj.top) +	19 + 'px'
	crossYearObj.zIndex = '+1';
	crossobj.zIndex = '1'
	
	getEl('spanYear').style.borderColor='#666666';
}

/*** calendar ***/
function WeekNbr(n) {
	// Algorithm used:
	// From Klaus Tondering's Calendar document (The Authority/Guru)
	// hhtp://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;
	if (startAt == 0) {
	   day = n.getDate() + 1;
	}else
	   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 constructCalendar () {
	var aNumDays = Array (31,0,31,30,31,30,31,31,30,31,30,31)

	var dateMessage
	var	startDate =	new	Date (yearSelected,monthSelected,1)
	var endDate
	var numDaysInMonth;
	
	if (monthSelected==1){
		endDate	= new Date (yearSelected,monthSelected+1,1);
		endDate	= new Date (endDate	- (24*60*60*1000));
		numDaysInMonth = endDate.getDate()
	}else{
		numDaysInMonth = aNumDays[monthSelected];
	}

	var datePointer	= 0
	var dayPointer = startDate.getDay() - startAt
	
	if (dayPointer < 0)
		dayPointer = 6

	var sHTML =	"<table summary='kalender'><tr>"

	if (showWeekNumber==1){
		sHTML += "<td width='27'>"+
					"<b>" + weekString + "</b>"+
				"</td>"+
				"<td width='1' rowspan='7' style='background-color:#d0d0d0;padding:0px'>"+
					"<img src='"+imgDir+"divider.gif' width='1'>"+
				"</td>"
	}

	for	(var i=0; i<7; i++)	{
		sHTML += "<td width='27' align='right'><B>"+ dayName[i]+"</B></td>"
	}
	sHTML +="</tr><tr>"
	
	if (showWeekNumber==1){
		sHTML += "<td align='right'>" + WeekNbr(startDate) + "&nbsp;</td>"
	}

	for	( var i=1; i<=dayPointer;i++ ){
		sHTML += "<td>&nbsp;</td>"
	}

	for	( var datePointer=1; datePointer<=numDaysInMonth; datePointer++ ){
		dayPointer++;
		sHTML += "<td align='right'>"
		var sStyle=styleAnchor
		if ((datePointer==odateSelected) &&	(monthSelected==omonthSelected)	&& (yearSelected==oyearSelected))
		{ sStyle+=styleLightBorder }

		var sHint = "";
		for (var k=0;k<HolidaysCounter;k++){
			if ((parseInt(Holidays[k].d)==datePointer)&&(parseInt(Holidays[k].m)==(monthSelected+1))){
				if ((parseInt(Holidays[k].y)==0)||((parseInt(Holidays[k].y)==yearSelected)&&(parseInt(Holidays[k].y)!=0))){
					sStyle+="background-color:#FFDDDD;"
					sHint+=sHint==""?Holidays[k].desc:"\n"+Holidays[k].desc
				}
			}
		}

		var regexp= /\"/g
		sHint=sHint.replace(regexp,"&quot;")

		dateMessage = "onmousemove='window.status=\""+selectDateMessage.replace("[date]",constructDate(datePointer,monthSelected,yearSelected))+"\"' onmouseout='window.status=\"\"' "

		if ((datePointer==dateNow)&&(monthSelected==monthNow)&&(yearSelected==yearNow)){ 
			sHTML += "<b><span class='a' "+dateMessage+" title=\"" + sHint + "\" style='"+sStyle+"' onclick='javascript:dateSelected="+datePointer+";closeCalendar();'><font color=#ff0000>&nbsp;" + datePointer + "</font>&nbsp;</span></b>"
		}else if	(dayPointer % 7 == (startAt * -1)+1){
			sHTML += "<span class='a' "+dateMessage+" title=\"" + sHint + "\" style='"+sStyle+"' onclick='javascript:dateSelected="+datePointer + ";closeCalendar();'>&nbsp;<font color=#909090>" + datePointer + "</font>&nbsp;</span>"
		}else{
			sHTML += "<span class='a' "+dateMessage+" title=\"" + sHint + "\" style='"+sStyle+"' onclick='javascript:dateSelected="+datePointer + ";closeCalendar();'>&nbsp;" + datePointer + "&nbsp;</span>"
		}
		sHTML += ""
		if ((dayPointer+startAt) % 7 == startAt) { 
			sHTML += "</tr><tr>" 
			if ((showWeekNumber==1)&&(datePointer<numDaysInMonth)){
				sHTML += "<td align=right>" + (WeekNbr(new Date(yearSelected,monthSelected,datePointer+1))) + "&nbsp;</td>"
			}
		}
	}

	document.getElementById("c_content").innerHTML   = sHTML
	document.getElementById("spanMonth").innerHTML = "<img id='changeMonth' src='"+imgDir+"drop1.gif' alt ='' />&nbsp;" + monthName[monthSelected] + "&nbsp;";
	document.getElementById("spanYear").innerHTML =	"<img id='changeYear' src='"+imgDir+"drop1.gif' alt='' />&nbsp;" + yearSelected + "&nbsp;";
}

function popUpCalendar(ctl, ctl2, format, frameOffSetLeft, frameOffSetTop) {
	var	leftpos=0
	var	toppos=0

	if (bPageLoaded){
		getEl('calendar').style.zIndex = 90000;
		if ( crossobj.visibility ==	"hidden" ) {
			ctlToPlaceValue	= ctl2
			dateFormat = format;

			var formatChar = " ";
			var 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=""
						}
					}
				}
			}

			var tokensChanged =	0
			if ( formatChar	!= "" ){
				// use user's date
				var aData =	ctl2.value.split(formatChar)

				for	(i=0;i<3;i++){
					if ((aFormat[i]=="d") || (aFormat[i]=="dd")){
						dateSelected = parseInt(aData[i], 10)
						tokensChanged ++
					}else if	((aFormat[i]=="m") || (aFormat[i]=="mm")){
						monthSelected =	parseInt(aData[i], 10) - 1
						tokensChanged ++
					}else if	(aFormat[i]=="yyyy"){
						yearSelected = parseInt(aData[i], 10)
						tokensChanged ++
					}else if	(aFormat[i]=="mmm"){
						for	(var j=0; j<12;	j++){
							if (aData[i]==monthName[j]){
								monthSelected=j
								tokensChanged ++
							}
						}
					}
				}
			}

			if ((tokensChanged!=3)||isNaN(dateSelected)||isNaN(monthSelected)||isNaN(yearSelected)|| yearSelected < 100){
				dateSelected = dateNow
				monthSelected =	monthNow
				yearSelected = yearNow
			}

			odateSelected=dateSelected
			omonthSelected=monthSelected
			oyearSelected=yearSelected

			aTag = ctl
			
			do{
				if(!aTag.offsetParent)
					break;
				aTag = aTag.offsetParent;
				leftpos	+= aTag.offsetLeft;
				toppos += aTag.offsetTop;
			} while(aTag.tagName!="BODY");

			crossobj.left =	fixedX==-1 ? ctl.offsetLeft + leftpos + frameOffSetLeft + "px" : fixedX
			crossobj.top = fixedY==-1 ? ctl.offsetTop + toppos + frameOffSetTop + ctl.offsetHeight + "px" : fixedY
			constructCalendar (1, monthSelected, yearSelected);
			crossobj.visibility=(dom||ie)? "visible" : "show"

			hideElement( 'SELECT', document.getElementById("calendar") );
			hideElement( 'APPLET', document.getElementById("calendar") );			

			bShow = true;
		}else{
			hideCalendar()
			if (ctlNow!=ctl) {popUpCalendar(ctl, ctl2, format, leftpos, toppos)}
		}
		ctlNow = ctl
	}
}

document.onkeydown = function hidecal1 (event) { 
	if(window.event != undefined)
		if (window.event.keyCode == 27 || window.event.keyCode == 9){
			hideCalendar()
		}
}
document.onclick = function hidecal2 () { 		
	if (!bShow){
		hideCalendar()
	}
	bShow = false
}

if(ie){
	init()
}else{
	window.onload=init
}
	
// Check browser version
var strSeperator = "-"; 
// If you are using any Java validation on the back side you will want to use the / because 
// Java date validations do not recognize the dash as a valid date separator.
var sMessage = "U heeft geen juiste datum ingevoerd.";
var vYearType = 4; //Set to 2 or 4 for number of digits in the year for Netscape
var vYearLength = 2; // Set to 4 if you want to force the user to enter 4 digits for the year before validating.
var err = 0; // Set the error code to a default of zero

function DateFormat(vDateName, vDateValue, e, dateCheck, defaultDate) {
	if(getSelectionStart(vDateName) < vDateName.value.length)
		return false; 
	// vDateName = object name
	// vDateValue = value in the field being checked
	// e = event
	// dateCheck 
	// True  = Verify that the vDateValue is a valid date
	// False = Format values being entered into vDateValue only
	
	var whichCode = (window.Event) ? e.keyCode : e.which;
	//alert(whichCode)
	//Eliminate all the ASCII codes that are not valid
	var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/";
	if (alphaCheck.indexOf(vDateValue) >= 1) {
		vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
		return false;
	}
	
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else {
		//Create numeric string values for 0123456789/
		//The codes provided include both keyboard and keypad values
		var strCheck = '0,16,27,37,38,39,40,47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105,109';
		
		if (strCheck.indexOf(whichCode) != -1 || whichCode == undefined) {
			if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
				var mToday = new Date();
				var mMonth = mToday.getMonth() + 1;
				
				if( mMonth <= 9)
					mMonth = "0" + mMonth;
					
				if (vDateValue.length == 1){
					vDateName.value = "0" + vDateValue + "-" + mMonth + "-" + mToday.getFullYear();
				}else if (vDateValue.length == 2){
					vDateName.value = vDateValue + "-" + mMonth + "-" + mToday.getFullYear();
				}else if (vDateValue.length == 3){
					vDateName.value = vDateValue + mMonth + "-" + mToday.getFullYear();
				}else if (vDateValue.length == 4){
					vDateName.value = vDateName.value.substr(0,2) + "-0" + vDateName.value.substr(3,1)+ "-" + mToday.getFullYear();
				}else if (vDateValue.length == 5){
					vDateName.value = vDateValue + "-" + mToday.getFullYear();
				}else if (vDateValue.length == 6){
					vDateName.value = vDateValue + mToday.getFullYear();
				}else if (vDateValue.length == 7)
					vDateName.value = vDateName.value.substr(0,2) + "-" + vDateName.value.substr(3,2)+ "-0" + vDateName.value.substr(6,1);
				vDateValue = vDateName.value
				
				//alert(sMessage);
				//vDateName.value = defaultDate;
				//vDateName.focus();
				//return true;
			}
			// Reformat date to format that can be validated. mm/dd/yyyy
			if (vDateValue.length >= 8 && dateCheck) {
				// Additional date formats can be entered here and parsed out to
				// a valid date format that the validation routine will recognize.
				var mDay = vDateName.value.substr(0,2);
				var mMonth = vDateName.value.substr(3,2);
				var mYear = vDateName.value.substr(6,4)
				if (vYearLength == 4) {
					if (mYear.length < 4) {
						alert(sMessage);
						vDateName.value = defaultDate;
						//vDateName.focus();
						return true;
					}
				}

				var vDateValueCheck = mMonth + "/" + mDay + "/" + mYear;
				if (mYear.length == 2 && vYearType == 4 && dateCheck) {
					//Turn a two digit year into a 4 digit year
					var mToday = new Date();
					//If the year is greater than 30 years from now use 19, otherwise use 20
					var checkYear = mToday.getFullYear() + 30; 
					var mCheckYear = '20' + mYear;
	
					if (mCheckYear >= checkYear)
						mYear = '19' + mYear;
					else
						mYear = '20' + mYear;
					vDateValueCheck = mMonth + "/" + mDay + "/" + mYear;
					vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
				} 
	
				if (!dateValid(vDateValueCheck)) {
					alert(sMessage);
					vDateName.value = defaultDate;
					//vDateName.focus();
					return true;
				}
				return true;
			}else {
				/*if (vDateValue.length == 1 && vDateValue > 1){
					if(whichCode == 109)
						vDateName.value = "0" + vDateValue;
					else
						vDateName.value = "0" + vDateValue + strSeperator;
				}else*/
				if (vDateValue.length == 2 && whichCode == 109){
					if(vDateValue == 0)
						vDateName.value = "01";
					else	
						vDateName.value = "0" + vDateValue;
				}else if (vDateValue.length == 2){
					if(vDateValue == "00")
						vDateName.value = "01" + strSeperator;
					else if(vDateValue < 31)
						vDateName.value = vDateValue + strSeperator;
					else
						vDateName.value = "31" + strSeperator;
				}else if (vDateValue.length == 3){
					if(vDateName.value.substr(vDateValue.length-1, 1) != strSeperator)
						vDateName.value = vDateName.value.substr(0, (vDateValue.length-1)) + strSeperator + vDateName.value.substr(vDateValue.length-1, 1);
				}else if (vDateValue.length == 4){
					if(vDateName.value.substr(vDateValue.length-1, 1) > 1) 
						vDateName.value = vDateName.value.substr(0, (vDateValue.length-1)) + "0" + vDateName.value.substr(vDateValue.length-1, 1);
				}else if (vDateValue.length == 5 && whichCode == 109){
					vDateName.value = vDateName.value.substr(0, (vDateValue.length-3)) + strSeperator + "0" + vDateName.value.substr(vDateValue.length-2, 1) + strSeperator;
				}else if(vDateValue.length == 5){
					if(vDateName.value.substr(vDateValue.length-2, 2) == "00") 
						vDateName.value = vDateName.value.substr(0, (vDateValue.length-2)) + "01" +strSeperator;
					else if(vDateName.value.substr(vDateValue.length-2, 2) > 12) 
						vDateName.value = vDateName.value.substr(0, (vDateValue.length-2)) + "12" +strSeperator;
					else
						vDateName.value = vDateValue+strSeperator;
				}else if (vDateValue.length == 6){
					if(vDateName.value.substr(vDateValue.length-1, 1) != strSeperator)
						vDateName.value = vDateName.value.substr(0, (vDateValue.length-1)) + strSeperator + vDateName.value.substr(vDateValue.length-1, 1);
				}
				if(whichCode == 109){
					if(vDateValue.length == 4 ||
							vDateValue.length == 1 ||
							(vDateValue.length >= 7 &&
							vDateValue.length <= 10 )){
						vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
						return false;
					}
				}
				return true;
			}
			if (vDateValue.length == 10 && dateCheck) {
				if (!dateValid(vDateName)) {
					// Un-comment the next line of code for debugging the dateValid() function error messages
					//alert(err);  
					alert(sMessage);
					//vDateName.focus();
					vDateName.select();
					//vDateName.value = defaultDate;
				}
			}
			return false;
		}else {
			// If the value is not in the string return the string minus the last
			// key entered.
			vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
			return false;
		}
	}
}

function dateValid(objName) {
	if(objName == '00/00/0000')
		return true;
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	// var err = 0;
	var strMonthArray = new Array(12);
		strMonthArray[0] = "Jan";
		strMonthArray[1] = "Feb";
		strMonthArray[2] = "Mar";
		strMonthArray[3] = "Apr";
		strMonthArray[4] = "May";
		strMonthArray[5] = "Jun";
		strMonthArray[6] = "Jul";
		strMonthArray[7] = "Aug";
		strMonthArray[8] = "Sep";
		strMonthArray[9] = "Oct";
		strMonthArray[10] = "Nov";
		strMonthArray[11] = "Dec";
	//strDate = datefield.value;
	strDate = objName;
	
	if (strDate.length < 1)
		return true;

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length > 5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   	}
	}
	//Adjustment for short years entered
	if (strYear.length == 2)
		strYear = '20' + strYear;

	var strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
	   		}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
	   	}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
		   	}
		}else
			if (intday > 28) {
				err = 10;
				return false;
	     	}
	}
	return true;
}
	
function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}else 
		if ((intYear % 4) == 0) { return true; }
	return false;
}

var sTimeMessage = "U heeft geen juiste tijd ingevoerd.";

function TimeFormat(vTimeName, vTimeValue, e, timeCheck, defaultTime) {
	if(getSelectionStart(vTimeName) < vTimeName.value.length)
		return false; 
	// vTimeName = object name
	// vTimeValue = value in the field being checked
	// e = event
	// timeCheck 
	// True  = Verify that the vTimeValue is a valid time
	// False = Format values being entered into vTimeValue only
	
	var whichCode = (window.Event) ? e.keyCode : e.which;
	//alert(whichCode)
	//Eliminate all the ASCII codes that are not valid
	var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
	if (alphaCheck.indexOf(vTimeValue) >= 1) {
		vTimeName.value = vTimeName.value.substr(0, (vTimeValue.length-1));
		return false;
	}

	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else {
		//Create numeric string values for 0123456789/
		//The codes provided include both keyboard and keypad values
		var strCheck = '0,16,27,37,38,39,40,47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105,186';
	
		if (strCheck.indexOf(whichCode) != -1 || whichCode == undefined) {
			if ((vTimeValue.length < 5 && timeCheck) && (vTimeValue.length >=1)) {
				if (vTimeValue.length == 1){
					vTimeName.value = "0" + vTimeValue + ":00";
				}else if (vTimeValue.length == 2){
					vTimeName.value = vTimeValue + ":00";
				}else if (vTimeValue.length == 3){
					vTimeName.value = vTimeValue + "00";
				}else if (vTimeValue.length == 4)
					vTimeName.value = vTimeName.value.substr(0,2) + ":0" + vTimeName.value.substr(3,1);
				vTimeValue = vTimeName.value
			}
			
			if (vTimeValue.length == 5 && timeCheck) {
				// Additional time formats can be entered here and parsed out to
				// a valid time format that the validation routine will recognize.
				var mHour = vTimeName.value.substr(0,2);
				var mMinute = vTimeName.value.substr(3,2);
				var vTimeValueCheck = mHour + ":" + mMinute;

				if (!timeValid(vTimeValueCheck)) {
					alert(sTimeMessage);
					vTimeName.value = defaultTime;
					//vTimeName.focus();
					return false;
				}
				return true;			
			}else{
				if (vTimeValue.length == 1 && vTimeValue > 2){
					if(whichCode == 186)
						vTimeName.value = "0" + vTimeValue;
					else
						vTimeName.value = "0" + vTimeValue + ":";
				}else if (vTimeValue.length == 2 && whichCode == 186){
					vTimeName.value = "0" + vTimeValue;
				}else if (vTimeValue.length == 2){
					//if(vTimeValue == "00")
					//	vTimeName.value = "01:";
					//else 
					if(vTimeValue < 24)
						vTimeName.value = vTimeValue + ":";
					else
						vTimeName.value = "00:";
				}else if (vTimeValue.length == 3){
					if(whichCode != 16 && 
							vTimeName.value.substr(vTimeValue.length-1, 1) != ":")
						vTimeName.value = vTimeName.value.substr(0, (vTimeValue.length-1)) + ":" + vTimeName.value.substr(vTimeValue.length-1, 1);
				}else if(vTimeValue.length == 4){
					if(vTimeName.value.substr(vTimeValue.length-1, 1) > 5) 
						vTimeName.value = vTimeName.value.substr(0, (vTimeValue.length-1)) + "0" + vTimeName.value.substr(vTimeValue.length-1, 1)
				}else if (vTimeValue.length == 5){
					if(vTimeName.value.substr(vTimeValue.length-2, 2) > 59) 
						vTimeName.value = vTimeName.value.substr(0, (vTimeValue.length-1)) + "0" + vTimeName.value.substr(vTimeValue.length-1, 1)
				}
				if(whichCode == 186){
					if(vTimeValue.length == 1 ||
							(vTimeValue.length >= 4 &&
							vTimeValue.length <= 5 )){
						vTimeName.value = vTimeName.value.substr(0, (vTimeValue.length-1));
						return false;
					}
				}
				return true;
			}
			return false;
		}else{
			// If the value is not in the string return the string minus the last
			// key entered.
			vTimeName.value = vTimeName.value.substr(0, (vTimeValue.length-1));
			return false;
		}
	}
}

function timeValid(objName) {
	var mHour = objName.substr(0,2);
	var mMinute = objName.substr(3,2);
	if(mHour < 0 || mHour > 23)
		return false;
	if(mMinute < 0 || mMinute > 59)
		return false;
	return true;
}
