//   This function calculates the time difference between the moment 
//   of submit and the moment the result page is loaded. The result will 
//   be rounded off to two digits. The result will be written in a div in the 
//   appropriate jsp or html page. The div must have an id named resultLoadTime.
///////
function calculateLoadTime() 
{
	currentDate = new Date();
	dateString = currentDate.toLocaleDateString();
	cookieName = "performance_start_"+dateString;
	//cookieName = cookieName.replace(/\s/g, '_');
	start = readCookie(cookieName);
	if(start != null && document.getElementById('resultLoadTime'))
	{
		end = currentDate.getTime();
		var duration = (end-start)/1000;
		var result = Math.round(duration*100)/100;
		document.getElementById('resultLoadTime').innerHTML="Het laden van deze pagina duurde : "+result +" seconden", "resultLoadTime";
		eraseCookie(cookieName, '/', '');
	}
}
//   This function creates a cookie with the time of submit for a given action.
//   The time value that is written to the cookie is a value of milliseconds 
//   since 01/01/1970 00:00:00. The cookie expires after 7 days.
///////
function createCookie(name,value,days)
{
	if(days)
	{
		date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = '; expires='+date.toGMTString();
	}
	else var expires = '';
	var ck = name+'='+value+expires+'; path=/';
	if (days != -1) 
	{
		document.cookie = ck;
	}
}
//   This function erases the time value in the the given cookie. 
//   After calculation of the load time the cookie is no longer needed.
///////
function eraseCookie(name, path, domain)
{
	if ( readCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
//   This function reads the value of the given cookie. This time value is 
//   a value of milliseconds since 01/01/1970 00:00:00. It will be used to
//   calculate the time difference between the moment of submit and the
//   moment the result page is loaded.
///////
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0; i<ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	
	return null;
}
//   This function is to be called on a submit or click of a link for a given action.
//   The time it takes for the action to be performed and for the full result page 
//   to be loaded will be measured. This function sets the time of submit in a cookie 
//   to be used for calculating the load time. 
///////
function setTimeSubmit() 
{
		startDate = new Date();
		dateString = startDate.toLocaleDateString();
		start = startDate.getTime();
		cookieName='performance_start_'+dateString;
		createCookie(cookieName, start, 7);
		cookieName='performance_start';
		createCookie(cookieName, start, 7);
}
//   This function writes the result to a div in the appropriate jsp or html page. 
//   The div must have an id named resultLoadTime. The time needed to load
//   the page will be displayed in the div.
///////
function writeLoadTime(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
}
