// JavaScript Document
// JavaScript Document
//Get and format the last change date for the page
function documentUpdateDate(){
	datelastmod = new Date(document.lastModified)
	document.write(datelastmod.getDate() + "/" + (datelastmod.getMonth()+1) + "/" + datelastmod.getFullYear() +".") 
}

//These scripts combine to put the footer in the right place. The first measures the height of the window.
//The second measures the height of the relatively position divs and either positions the footer relatively, 
//if they're higher than the window height, or absolutely stuck to the bottom of the window id the page is short.
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function setPosition() {

	//Get the width of the header right
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		var windowWidth = getWindowWidth();

		//Sort out dimensions of header and left margin
		var headerMenu = document.getElementById('headerMenu');
		var bodyContent = document.getElementById('bodyContent');
		//Set width of top menu area
		/*if (windowWidth > 800) 

			document.images.headerBackground.width = windowWidth - 190; //190
		else 

			document.images.headerBackground.width = 610;*/
			
		if (windowWidth < 820) 
			document.images.headerBackground.width = 630;
		else if (windowWidth > 1290)
			document.images.headerBackground.width = 1100;
		else
			document.images.headerBackground.width = windowWidth - 190;
			
		headerMenu.style.width = (document.images.headerBackground.width - 80) + 'px';
		bodyContent.style.width = (document.images.headerBackground.width - 80) + 'px';
		//document.getElementById('topMenuTable').style.width = headerMenu.style.width - 100;
			
	}

	//Make sure footer sticks to bottom of page
	if (windowHeight > 0) {

//					var container = document.getElementById('container');
//					var mainContent = document.getElementById('mainContent');
		var header = document.getElementById('header');
		var bodyContent = document.getElementById('bodyContent');
		var footer = document.getElementById('footer');
		//var lNav = document.getElementById('lNav');
		var lNav = document.getElementById('leftMargin');
		//Add distance of lNav from top of page
		var lNavHeight = lNav.offsetHeight + 250;
//					var rightMargin = document.getElementById('rightMargin');
//					var leftMargin = document.getElementById('leftMargin');					
//					var contentHeight = header.offsetHeight + bodyContent.offsetHeight;
		var headerHeight = header.offsetHeight;
		var footerHeight = footer.offsetHeight;
		var contentHeight = headerHeight + bodyContent.offsetHeight + footerHeight;
//					var footerHeight = footer.offsetHeight;

//					var contentHeight = mainContent.offsetHeight;

		//Place the footer in the right place
		//If content fits within the page
//					if (contentHeight < windowHeight - footerHeight){
		if (contentHeight < windowHeight){	
			bodyContent.style.height = (windowHeight - headerHeight - footerHeight -120) + 'px';
			if ( bodyContent.offsetHeight < 500)
				bodyContent.style.height = 500;
//						rightMargin.style.height = windowHeight + 'px';
//						leftMargin.style.height = windowHeight + 'px';						
		// else if content is longer than the page						
		} 
		
		//Now calculate length of left navigation
		var minHeight = 600;
		//All page elements are too short to dictate navigation bar length
		if (windowHeight < minHeight && contentHeight < minHeight && lNavHeight < minHeight) 
		{
			document.images.lMarginBackground.height = 600;
		}
		//Window height is the longest
		else if (windowHeight > contentHeight && windowHeight > lNavHeight)
		{
			//document.images.lMarginBackground.height = windowHeight - 173;
			document.images.lMarginBackground.height = windowHeight - 100;
			
		}
		//Content height dictates navigation length
		else if (contentHeight > windowHeight && contentHeight > lNavHeight)					
		{
			document.images.lMarginBackground.height = contentHeight - 100;
		}
		//nb lNavHeight includes the height of the top of page banner
		else
		{
			document.images.lMarginBackground.height = lNavHeight - 200;
		}
	}
}

//Window.onload function is in stylesheetSwitcher which also uses that event - can't have 2 definitions of the same function in different files
function topMenuHoverOver(cellId)
{
	var labelId = cellId + "Label";
	
	if (labelId != liveTopLabel)
	{
		var cell = document.getElementById(cellId);
		var offset = cell.offsetLeft + 25;
		var hoverImage = document.getElementById('hoverImage');
		
		hoverImage.style.visibility = "visible";
	
//		document.getElementById('bwImage').style.marginLeft = -offset + "px";
		document.getElementById('bwImage').style.left = -offset + "px";
		hoverImage.style.left = offset  + 'px';
		hoverImage.style.width = cell.offsetWidth + 'px';
		
		document.getElementById(labelId).style.backgroundColor = "#657a2f";

	}

}
function leaveTopMenu(liveLabel)
{
	document.getElementById('hoverImage').style.visibility = "hidden";
	var label;
	for (var i = 1; i <= 4; i++)
	{
		label = "tItem" + i + "Label";
		
		if (!(label == liveLabel))
			document.getElementById(label).style.backgroundColor = "#799338";
	}
}
		
window.onresize = function(e) {
	setPosition();
}

//Switch header image according to vairable set by PublicPage
function headerImage(){
	
	var headerMenu = document.getElementById('headerMenu');
	headerMenu.style.backgroundImage = headerImageUrl;

	var highlightImage = document.getElementById('bwImage');
	highlightImage.src = headerHighlightUrl;

}
 
function checkAll(onOrOff) {
   var boxes = document.getElementsByName('medicine[]');
   for (i=0; i < boxes.length; i++) 
      boxes[i].checked = onOrOff;
}

function Form_Validator(theForm)
{
	var boxes = document.getElementsByName('medicine[]');
	var checkSelected = false;	
	for (i=0; i < boxes.length; i++)
	{
		if (boxes[i].checked)
			checkSelected = true;
	}

	if (!checkSelected)
	{
		alert("Kies tenminste 1 geneesmiddel a.u.b.");
		return (false);
	}
	
	return (true);
}

			