var id = "";
var month = "";
var year = "";
var curmonth = "";
var curyear = "";
function leapYear(year) {
  if (year % 4 == 0) {
    return true;
  }
  return false;
}

function getDays(month, year) {
	var ar = new Array(12);
	ar[0] = 31; // January
	ar[1] = (leapYear(year)) ? 29 : 28; // February
	ar[2] = 31; // March
	ar[3] = 30; // April
	ar[4] = 31; // May
	ar[5] = 30; // June
	ar[6] = 31; // July
	ar[7] = 31; // August
	ar[8] = 30; // September
	ar[9] = 31; // October
	ar[10] = 30; // November
	ar[11] = 31; // December
	
	return ar[month];
}
function getMonthName(month) {
	var ar = new Array(12);
	ar[0] = "Januar";
	ar[1] = "Februar";
	ar[2] = "Marts";
	ar[3] = "April";
	ar[4] = "Maj";
	ar[5] = "Juni";
	ar[6] = "Juli";
	ar[7] = "August";
	ar[8] = "September";
	ar[9] = "Oktober";
	ar[10] = "November";
	ar[11] = "December";
	
	return ar[month];
}
function setCal(form_id, get_month, get_year) {
	// standard time attributes
	id = form_id;
	month = get_month;
	year = get_year;
	var now = new Date();
	if (month == undefined) {
		var month = now.getMonth();
		var curmonth = now.getMonth();
	}
	if (year == undefined) {
		var year = now.getFullYear();
		var curyear = now.getFullYear();
	}
	var monthName = getMonthName(month);
	var date = now.getDate();
	now = null;
	
	// create instance of first day of month, and extract the day on which it occurs
	var firstDayInstance = new Date(year, month, 1);
	var firstDay = firstDayInstance.getDay();
	firstDayInstance = null;
	
	// number of days in current month
	var days = getDays(month, year);
	
	// call function to draw calendar
	drawCal(firstDay + 1, days, date, monthName, year);
}

function drawCal(firstDay, lastDate, date, monthName, year) {
	// constant table settings
	//var headerHeight = 50 // height of the table's header cell
	var border = 0; // 3D height of table's border
	var cellspacing = 2; // width of table's border
	var headerColor = "midnightblue"; // color of table's header
	var colWidth = 20; // width of columns in table
	var dayCellHeight = 18; // height of cells containing days of the week
	var dayColor = "darkblue"; // color of font representing week days
	var cellHeight = 18; // height of cells representing dates in the calendar
	var todayColor = "#CCCCCC"; // color specifying today's date in the calendar
	var otherColor = "#EEEEEE"; // color specifying not today's date in the calendar
	var timeColor = "purple"; // color of font representing current time
	var now = new Date();
	var curMonthName = getMonthName(now.getMonth());
	if (month == '' && month.length == 0) {
		month = now.getMonth();
	}
	if (year == '') {
		year = now.getYear();
	}
	now = null;
	var colorswitch = "";
	var nextYear = year;
	var prevYear = year;
	var nextMonth = month+1;
	if (month == 11) {
		nextMonth = 0;
		nextYear = year+1
	}
	var prevMonth = month-1
	if (month == 0) {
		prevMonth = 11;
		prevYear = year-1;
	}
	
	
	// create basic table structure
	var text = "<style>td {font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;font-size: 10px;}th {font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;font-size: 10px;} A:link {	font-size:11px; text-decoration: none;	color: #000000;}A:visited {font-size:11px; text-decoration: none; color: #000000; } A:active {font-size:11px; text-decoration: none; color: #000000; } A:hover {font-size:11px; text-decoration: none; color: #000000; }</style>"; // initialize accumulative variable to empty string
	text += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + ' ALIGN=CENTER STYLE="BORDER: 1px solid #666666;">'; // table settings
	text += '<TR><TD><a href="javascript: void(0);" onclick="window.opener.month = '+prevMonth+';window.opener.year = '+prevYear+';window.opener.setCal(\''+id+'\', '+prevMonth+', '+prevYear+');">&lt;&lt;</a></TD><TD COLSPAN=5 HEIGHT=' + 10 + ' ALIGN=CENTER>'; // create table header cell
	text += '<FONT COLOR="' + headerColor + '">'; // set font for table header
	text += monthName + ' ' + year;
	text += '</FONT></TD><TD ALIGN=RIGHT><a href="javascript: void(0);" onclick="window.opener.month = '+nextMonth+';window.opener.year = '+nextYear+';window.opener.setCal(\''+id+'\', '+nextMonth+', '+nextYear+');">&gt;&gt;</a> '; // close table header's font settings
	text += '</TD></TR>'; // close header cell

	// variables to hold constant settings
	var openCol = '<TD BGCOLOR="#DDDDDD" WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '>';
	openCol += '<FONT COLOR="' + dayColor + '">';
	var closeCol = '</FONT></TD>';

	// create array of abbreviated day names
	var weekDay = new Array(7);
	weekDay[0] = "S";
	weekDay[1] = "M";
	weekDay[2] = "T";
	weekDay[3] = "O";
	weekDay[4] = "T";
	weekDay[5] = "F";
	weekDay[6] = "L";

	// create first row of table to set column width and specify week day
	text += '<TR ALIGN="center" VALIGN="center">';
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += openCol + weekDay[dayNum] + closeCol;
	}
	text += '</TR>';

	// declaration and initialization of two variables to help with tables
	var dayOfMonth = 1;
	var curCell = 1;

	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		text += '<TR ALIGN="right" VALIGN="top">';
		for (var col = 1; col <= 7; ++col) {
			if ((curCell < firstDay) || (dayOfMonth > lastDate)) {
				text += '<TD BGCOLOR="#999999"> </TD>';
				curCell++
			} else {
				if (dayOfMonth == date) { // current cell represents today's date
					colorswitch = todayColor;
				} else {
					colorswitch = otherColor;
				}
				text += '<TD BGCOLOR="'+colorswitch+'" ALIGN=CENTER>' + '<FONT COLOR="#000000"><a href="javascript: void(0);" onclick="window.opener.document.getElementById(\''+id+'\').value = \''+year+'-'+(month+1)+'-'+dayOfMonth+'\';window.opener.month = \''+curmonth+'\';window.opener.year = \''+curyear+'\';';
				if (id == "start") {
    				text += 'window.opener.document.getElementById(\'slut\').value = \''+year+'-'+(month+1)+'-'+dayOfMonth+'\';window.opener.document.getElementById(\'start_time\').focus();'
    			}
    			if (id == "slut") {
    				text += 'window.opener.document.getElementById(\'slut_time\').focus();'
    			}
				text += 'window.close();">' + dayOfMonth + '</a></TD>';
				dayOfMonth++;
			}
		}
		text += '</TR>';
	}

	// close all basic table tags
	text += '<TR><TD COLSPAN=7 ALIGN=CENTER><A HREF="javascript: window.close();">Luk</A></TD></TR></TABLE>';

	// print accumulative HTML string
	calWin = window.open("","cal","toolbars=0, scrollbars=0, location=0, statusbars=0, menubars=0, resizable=0, width=175, height=170");
	calWin.document.open();
    calWin.document.write(text);
    calWin.document.close();
}
