function calInit ( dateToday, dateFrom, dateTo, iOffset ) {
	var sHtml = new String();
	dateOffset = iOffset;
	sDateFrom = dateFrom;
	sDateTo = dateTo;
	oNow = new Date();
	sNow = String(oNow.getFullYear()) + (oNow.getMonth()+1<10?'0':'') + String(oNow.getMonth()+1) + (oNow.getDate()<10?'0':'') + String(oNow.getDate());

	calForbiddenDays = new Array();
	j = 0;
	args = calInit.arguments;
	for( var i=3; i<args.length; i++ ) { calForbiddenDays[j] = args[i]; j++; }

	sHtml  = '<table border=0 cellspacing=1 cellpadding=0 class="CALCSSTABLE" width=100%>\n';
	sHtml += '<tr>\n';
	sHtml += '<th id="calPrevMonth" onclick="calSelectPrev()" class="CALCSSHeadline" onselectstart="return false;">&nbsp;</th>\n';
	sHtml += '<th colspan=5 id="calMonth" class="CALCSSHeadline" onselectstart="return false;">&nbsp;</th>\n';
	sHtml += '<th id="calNextMonth" onclick="calSelectNext()" class="CALCSSHeadline" onselectstart="return false;">&nbsp;</th>\n';
	sHtml += '</tr>\n';
	sHtml += '<tr>';

	for( var i=0; i<7; i++ )
	{
		sHtml += '<td class="CALCSSDATE">' + sCalDayShortNames[i] + '</td>';
	}
	sHtml += '</tr>\n';

	for( i=0; i<6; i++ ) {
		sHtml += '<tr>';
		for( j=0; j<7; j++ ) {
			sHtml += '<td id="calBlock' + i + j + '" onclick="calSelectDay(this.innerHTML,this.className)" onmouseover="calMouseOver(this)" onmouseout="calMouseOut(this)">x</td>';
		}
		sHtml += '</tr>\n';
	}
	sHtml += '</table>\n';
	sHtml += '<input type="hidden" id="hiddendate" name="iSelectedDate" value="' + dateToday + '">\n';
	sHtml += '<input type="hidden" id="hiddentab" name="iTab" value="">\n';
	document.write( sHtml );
	document.close();
	calDrawToday( dateToday );
}

function calDrawToday( dateToday ) {
	iSelectedDay = new Number( dateToday.substr(6,2) );
	oFirstDay = new Date( dateToday.substr(0,4), Math.floor(dateToday.substr(4,2))-1, 1 );
	oLastDay = new Date( dateToday.substr(0,4), dateToday.substr(4,2), 0 );
	var iCurDay = oFirstDay.getDate();
	var iLastDay = oLastDay.getDate();
	var iFirstWeekDay = new Number( oFirstDay.getDay() - 1 );
	if( iFirstWeekDay == -1 ) { iFirstWeekDay = 6; }

	sTodayYear = Math.floor(dateToday.substr(0,4));
	sTodayMonth = Math.floor(dateToday.substr(4,2))-1;

	sFromYear = Math.floor(sDateFrom.substr(0,4));
	sFromMonth = Math.floor(sDateFrom.substr(4,2))-1;
	sFromDay = Math.floor(sDateFrom.substr(6,2));

	sToYear = Math.floor(sDateTo.substr(0,4));
	sToMonth = Math.floor(sDateTo.substr(4,2))-1;
	sToDay = Math.floor(sDateTo.substr(6,2));

	for( j=0; j<iFirstWeekDay; j++ ) { calSetBlock( 0, j, 'CALCSSFooter', '&nbsp;' ); }

	var i = new Number( 0 );
	var j = new Number( iFirstWeekDay );

	if( sTodayYear == sFromYear && sTodayMonth == sFromMonth && iSelectedDay < sFromDay ) { iSelectedDay = sFromDay; }
	if( sTodayYear == sToYear && sTodayMonth == sToMonth && iSelectedDay > sToDay ) { iSelectedDay = sToDay; }

	while( iCurDay <= iLastDay ) {
		// checker om dagen er tilladt
		var bForbidden = false;
		var bForceOpen = false;
		for(k=0; k<calForbiddenDays.length; k++ ) {
			//txt += 'checker ' + Math.floor(calForbiddenDays[k].substr(0,4)) + ' = ' + sTodayYear + ' og ' + Math.floor(calForbiddenDays[k].substr(4,2)) + ' = ' + sTodayMonth + ' og ' + Math.floor(calForbiddenDays[k].substr(6,2)) + ' = ' + iCurDay + '<br>\n';
			if( Math.floor(calForbiddenDays[k].substr(0,4)) == sTodayYear && Math.floor(calForbiddenDays[k].substr(4,2))-1 == sTodayMonth && Math.floor(calForbiddenDays[k].substr(6,2)) == iCurDay ) {
				bForbidden = true;
			}
		}
		// check for om der er specielt lukket paa denne dag
		for( a=0; a<sDayOpenAlt.length; a+=2 ) {
			if( Math.floor(sDayOpenAlt[a].substr(0,4)) == sTodayYear && Math.floor(sDayOpenAlt[a].substr(4,2))-1 == sTodayMonth && Math.floor(sDayOpenAlt[a].substr(6,2)) == iCurDay ) {
				if( sDayOpenAlt[a+1] == 0 ) { bForbidden = true; }
				if( sDayOpenAlt[a+1] == 1 ) { bForceOpen = true; }
			}
		}
		if( bForceOpen == false ) { bForbidden = true; }

		
		// hvis indenfor range og ikke forbidden dag
		if( sTodayYear >= sFromYear && sTodayYear <= sToYear )
		{
			if( ( sTodayMonth >= sFromMonth || sTodayYear > sFromYear ) && ( sTodayMonth <= sToMonth || sTodayYear < sToYear ) && bForbidden == false )
			{
				// we start and end in the same month
				if( sTodayMonth == sFromMonth && sTodayMonth == sToMonth )
				{
					// We are on today or a future day in this month
					if( iCurDay >= sFromDay && iCurDay <= sToDay )
					{
						calSetBlock( i, j, 'CALCSSLocation', iCurDay );
					}
					else
					{
						calSetBlock( i, j, 'CALCSSFooter', iCurDay );
					}
				}
				else
				{
					// We are within the first month of the period
					if( sTodayMonth == sFromMonth && iCurDay >= sFromDay )
					{
						calSetBlock( i, j, 'CALCSSLocation', iCurDay );
					}
					else
					{
						// we are within the last month of the period
						if( sTodayMonth == sToMonth && iCurDay <= sToDay )
						{
							calSetBlock( i, j, 'CALCSSLocation', iCurDay );
						}
						else
						{
							// We are in a month between the first and the last of the period
							if( sTodayMonth > sFromMonth && sTodayMonth < sToMonth )
							{
								calSetBlock( i, j, 'CALCSSLocation', iCurDay );
							}
							else
							{
								calSetBlock( i, j, 'CALCSSFooter', iCurDay );
							}
						}
					}
				}
			} else {
				calSetBlock( i, j, 'CALCSSFooter', iCurDay );
			}
		}
		iCurDay++; j++;
		if( j == 7 ) { i++; j = 0; }
	}

	while( i<6 ) {
		calSetBlock( i, j, 'CALCSSFooter', '&nbsp;' );
		j++;
		if( j == 7 ) { i++; j = 0; }
	}

	obj = document.getElementById( 'calMonth' );
	obj.innerHTML = '<nobr>' + sCalMonthNames[Math.floor(dateToday.substr(4,2))-1] + ' ' + dateToday.substr(0,4) + '</nobr>';
	
	if( dateOffset > 0 )
	{
		obj = document.getElementById( 'calPrevMonth' );
		obj.style.cursor = 'pointer';
		obj.innerHTML = "&lt;&lt;";
	}
	
	if( dateOffset < 12 )
	{
		obj = document.getElementById( 'calNextMonth' );
		obj.style.cursor = 'pointer';
		obj.innerHTML = "&gt;&gt;";
	}
	
}

function calSetBlock( i, j, sClass, sHtml ) {
	obj = document.getElementById( 'calBlock' + i + j );
	obj.className = sClass;
	obj.innerHTML = sHtml;
}

function calSelectDay( iDay, sClass ) {
	if( sClass != 'CALCSSFooter' ) {
		for( i=0; i<6; i++ ) {
			for( j=0; j<7; j++ ) {
				obj = document.getElementById( 'calBlock' + i + j );
				if( obj.className == 'CALCSSLocation' ) {
					obj.className = 'CALCSSLocation';
				}
				if( obj.innerHTML == iDay ) {
					obj.className = 'CALCSSLocation';
				}
			}
		}

		iSelectedDay = iDay;
		uri = new String( sCalShowOnPage + '?offsetdate=' + String(oFirstDay.getFullYear()) + (oFirstDay.getMonth()+1<10?'0':'') + String(oFirstDay.getMonth()+1) + (iSelectedDay<10?'0':'') + String(iSelectedDay) );
		window.location.href = uri;
	}
}

function calMouseOver( obj ) {
	if( obj.className != 'CALCSSFooter' ) {
		obj.className = 'CALCSSDescription';
	}
}

function calMouseOut( obj ) {
	if( obj.className != 'CALCSSFooter' ) {
		if( obj.innerHTML == iSelectedDay ) {
			obj.className = 'CALCSSLocation';
		} else {
			obj.className = 'CALCSSLocation';
		}
	}
}

function calSelectNext() {
	if( dateOffset < 12 )
	{
		uri = new String( '?dateOffset=' + (Math.floor(dateOffset)+1) );
		window.location.href = uri;
	}
}

function calSelectPrev() {
	if( dateOffset > 0 )
	{
		uri = new String( '?dateOffset=' + (Math.floor(dateOffset)-1) );
		window.location.href = uri;
	}
}
