function Calendar(element, x, y, startDate, showTime) {
	
	var norwegianMonths = new Array('Januar', 'Februar', 'Mars', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Desember');
	var norwegianDays = new Array('Man', 'Tir', 'Ons', 'Tor', 'Fre', 'Lør', 'Søn');
	var today = new Date();
	var selectedDate = startDate;
	
	var oldBackground = element.style.backgroundColor;
	var oldBorder = element.style.border;
	var oldColor = element.style.color;
	var oldOpacity = element.style.opacity; 
	
	element.style.backgroundColor = '#333';
	element.style.border = '1px solid black';
	element.style.color = 'white';
	element.style.opacity = 0.9;
	
	var root = document.createElement('div');
	root.style.position = 'absolute';
	root.style.left = x + 'px';
	root.style.top = y + 'px';
	root.style.width = '335px';
	root.style.opacity = 0.95;
	root.style.color = 'white';
	root.setAttribute('id', 'bprogKalender');
	root.className = 'calendarRoot';
	
	var toppdiv = document.createElement('div');
	var toppimg = document.createElement('img');
	toppimg.setAttribute('src', '/img/kalender/topp.png');
	toppdiv.style.margin = '0px';
	toppdiv.style.padding = '0px';
	toppdiv.style.height = '65px';
	toppdiv.appendChild(toppimg);
	root.appendChild(toppdiv);
	
	var cal_root = document.createElement('div');
	cal_root.style.backgroundColor = '#333';
	cal_root.style.width = '288px';
	//cal_root.style.height = '100px';
	cal_root.style.marginLeft = '45px';
	cal_root.style.marginTop = '0px';
	cal_root.style.borderLeft = '1px solid black';
	cal_root.style.borderRight = '1px solid black';
	root.appendChild(cal_root);
	
	var header = document.createElement('div');
	cal_root.appendChild(header);
	header.style.backgroundColor = '#111';
	header.style.width = '100%';
	header.style.height = '19px';
	header.style.padding = '2px 0px'
	
	/**
	 *		Måneder
	 */
	var prevMonth = document.createElement('input');
	prevMonth.type = 'button';
	prevMonth.value = '-';
	prevMonth.className = 'monthBtn';
	header.appendChild(prevMonth);
	prevMonth.onclick = function() {
			
		selectedDate.setFullYear(selectedDate.getFullYear(), selectedDate.getMonth()-1, selectedDate.getDate());
		update();
	}
	
	var months = document.createElement('input');
	months.type = 'text';
	months.style.width = '80px';
	header.appendChild(months);
	months.className = 'floatLeft';
	months.setAttribute('disabled', 'disabled');
	
	var nextMonth = document.createElement('input');
	nextMonth.type = 'button';
	nextMonth.value = '+';
	nextMonth.className = 'monthBtn';	
	header.appendChild(nextMonth);	
	nextMonth.onclick = function() {
		
		selectedDate.setFullYear(selectedDate.getFullYear(), selectedDate.getMonth()+1, selectedDate.getDate());
		update();
	}	
	
	/**
	 *		År
	 */
	var nextYear = document.createElement('input');
	nextYear.type = 'button';
	nextYear.value = '+';
	nextYear.className = 'yearBtn';	
	header.appendChild(nextYear);	
	nextYear.onclick = function() {
		
		selectedDate.setFullYear(selectedDate.getFullYear()+1, selectedDate.getMonth(), selectedDate.getDate());
		update();
	}			
	
	var years = document.createElement('input');
	years.type = 'text';
	years.style.width = '40px';
	header.appendChild(years);
	years.className = 'floatRight';
	years.setAttribute('disabled', 'disabled');
	
	var prevYear = document.createElement('input');
	prevYear.type = 'button';
	prevYear.value = '-';
	prevYear.className = 'yearBtn';
	header.appendChild(prevYear);	
	prevYear.onclick = function() {
			
		selectedDate.setFullYear(selectedDate.getFullYear()-1, selectedDate.getMonth(), selectedDate.getDate());
		update();
	}
	
	/**
	 *		Datoer
	 */
	var dates = document.createElement('div');
	dates.style.textAlign = 'center';
	cal_root.appendChild(dates);
	
	
	/**
	 *		Evt. klokkeslett
	 */
	if (showTime) {
		
		var timeDiv = document.createElement('div');
		cal_root.appendChild(timeDiv);
		timeDiv.style.textAlign = 'right';
		timeDiv.style.paddingRight = '10px';
		
		var info = document.createElement('b');
		info.appendChild(document.createTextNode('Klokkeslett: '));
		timeDiv.appendChild(info);
		
		var hour = document.createElement('select');
		timeDiv.appendChild(hour);
		var chour = selectedDate.getHours();	
		
		hour.onchange = function() {
			selectedDate.setHours(hour.value);
			update();
		}		
		
		for (var h=0; h<=23; h++) {
			var opt = document.createElement('option');
			hour.appendChild(opt);
			opt.appendChild(document.createTextNode(h));
			
			if (h == chour)
				opt.setAttribute('selected', 'selected');
		}
		
		var sep = document.createElement('b');
		sep.appendChild(document.createTextNode(':'));
		timeDiv.appendChild(sep);		
		
		var min = document.createElement('select');
		timeDiv.appendChild(min);
		var cmin = selectedDate.getMinutes();
		var satt = false;	
		min.onchange = function() {
			selectedDate.setMinutes(min.value);
			update();
		}

		for (var m=0; m<=55; m+=5) {
			var opt = document.createElement('option');
			min.appendChild(opt);
			opt.appendChild(document.createTextNode(m));
			
			if (!satt && m > cmin) {
				opt.setAttribute('selected', 'selected');
				satt = true;
			}
		}
		
	}
	
	var divbottom = document.createElement('div');
	var imgbottom = document.createElement('img');
	divbottom.appendChild(imgbottom);
	root.appendChild(divbottom);
	divbottom.style.height = '14px';
	divbottom.style.paddingLeft = '45px';
	divbottom.style.margin = '0px';
	divbottom.style.paddingTop = '0px';
	divbottom.style.marginTop = '-2px';
	imgbottom.setAttribute('src', '/img/kalender/bunn.png');
	
	document.getElementsByTagName('body').item(0).appendChild(root);
	

	/** Fjerner kalenderen */
	var kill = function() {
		
		element.style.backgroundColor = oldBackground;
		element.style.border = oldBorder;
		element.style.color = oldColor;
		element.style.opacity = oldOpacity;
		
		document.getElementsByTagName('body').item(0).removeChild(root);
	}
	
	/** Oppdaterer visningen */
	var update = function() {
				
		var month = selectedDate.getMonth()+1;
		var year = selectedDate.getFullYear();
		var max = daysInMonth(month, year);
		
		months.value = norwegianMonths[selectedDate.getMonth()];
		years.value = year;
		
		element.value = year + '-' + month + '-' + selectedDate.getDate();
		
		if (showTime)
			element.value += ' ' + selectedDate.getHours() + ':' + selectedDate.getMinutes();
		
		/** Oppdaterer datoer */
		dates.innerHTML = '';
		
		var table = document.createElement('table');
		dates.appendChild(table);
		
		var tbody = document.createElement('tbody');
		table.appendChild(tbody);
		
		var headerRad = document.createElement('tr');
		tbody.appendChild(headerRad);
		
		for (var d in norwegianDays) {
			var th = document.createElement('th');
			th.appendChild(document.createTextNode(norwegianDays[d]));
			headerRad.appendChild(th);
			th.style.width = '35px';
		}
		
		var firstDay = new Date();
		firstDay.setFullYear(year, month-1, 1);
		var firstDate = firstDay.getDay();
		if (firstDate == 0) firstDate = 7; 	// Idiotene har søndag som første ukedag
		firstDate--;						// Nå er mandag første dag (0)
		
		
		var tomTeller = 0;		// Teller antall tomme ruter i starten av måneden
		var trTeller = 0;
		var date = 1;
		var tr = document.createElement('tr');
		tbody.appendChild(tr);
		do {
			
			var celleInnhold = '';
			++trTeller;
			
			if (firstDate > tomTeller) {
				++tomTeller;
			} else {
				celleInnhold = date;
				++date;
			}
			
			var celle = document.createElement('td');
			tr.appendChild(celle);
			celle.appendChild(document.createTextNode(celleInnhold));
			celle.className = 'calDate';
			celle.onclick = function() {
				
				selectedDate.setFullYear(selectedDate.getFullYear(), selectedDate.getMonth(), parseInt(this.innerHTML));
				update();
				kill();
				
			}
			
			if (trTeller == 7) {
				tr = document.createElement('tr');
				tbody.appendChild(tr);
				trTeller = 0;
			}
			
		} while(date <= max);
				
	}

	
	var daysInMonth = function(month,year) {
		var dd = new Date(year, month, 0);
		return dd.getDate();
	}	
	
	update();
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
