if (typeof(functionsOnload) == 'undefined')
	functionsOnload = Array(0); // for page-specific onload functions, push into this array
var W3CDOM = false;
var innerHTMLWorks = false;




// these functions are sometimes used even before the page has loaded, onmouseover, onclick and onsubmit:

function wnstt(str) // WiNdow STaTus
{
	window.status = str;
	return true;
}

function cst(area) // Click STatistics
{
	var xhr;
	if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	if (xhr)
	{
		xhr.open("GET", "/clickstat.php?area=" + area, true);
		xhr.send(null);
	}
}
function cstr(prefix) // Click STatistics for Rotating placards
{
	cst(prefix + rotaNr);
}

function MagazineIssue(url)
{
	window.open(url, 'magazineissue', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=620,height=450');
	return false;
}

function AdvertisingInfo(url)
{
	window.open(url, 'advertise', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=620,height=450');
	return false;
}

function GRTVpopup(url)
{
	window.open(url, 'GRTVpopup', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=765,height=580');
	return false;
}

function Gullstikka()
{
	window.open('/gullstikka/', 'gullstikka', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=710,height=892');
	return false;
}

function SubmitWithoutXY(inputimage)
{
	// by using this function we can avoid the ugly x=11&y=13 in the querystring
	inputimage.form.submit();
	return false;
}

function SubmitForm(formId)
{
	// Kenneth created this to use <a> links as submit buttoms
	document.forms[formId].submit();
}

function Login()
{
	if (document.loginform)
	{
		if (!document.loginform.username.value)
		{
			document.loginform.username.focus();
			return false;
		}

		if (!document.loginform.password.value)
		{
			document.loginform.password.focus();
			return false;
		}
	}

	return true;
}

function expandBanner(exp_banner)
{
	document.getElementById('exp_banner').style.height = "360px";
}

function retractBanner(exp_banner)
{
	document.getElementById('exp_banner').style.height = "180px";
}




// these functions are called onload or later:

function TimeThisPage()
{
	var pageTimeEnd = new Date();
	if (!W3CDOM)
		return;
	var pageTimeStart = getCookie('jsPageTimer');
	if (pageTimeStart)
	{
		pageTimeEnd = pageTimeEnd.getTime();
		var pageLoadTime = pageTimeEnd - pageTimeStart; // difference in milliseconds
		if (!isNaN(pageLoadTime))
		{
			var jsPageTimer = document.getElementById('jsPageTimer');
			if (jsPageTimer && jsPageTimer.innerHTML)
				jsPageTimer.innerHTML = (pageLoadTime / 1000);
		}
		deleteCookie('jsPageTimer');
	}
}

function unload()
{
	if (typeof(exitscript) == "string") // exitscript is only defined on a few pages
	{
		var xhr;
		if (window.XMLHttpRequest)
			xhr = new XMLHttpRequest();
		else if (window.ActiveXObject)
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		if (xhr)
		{
			xhr.open("GET", exitscript, true);
			xhr.send(null);
		}
	}
	StartPageTimer();
}

function StartPageTimer()
{
	var expires = new Date();
	var expiresSeconds = 60;
	expires.setMilliseconds(expiresSeconds * 1000);
	expires = expires.toGMTString();

	var pageTimeStart = new Date();
	setCookie('jsPageTimer', pageTimeStart.getTime(), expires, '/', false, false);
}

function setCookie(name, value, expires, path, domain, secure)
{
	var myCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	document.cookie = myCookie;
}

function deleteCookie(name)
{
	var expires = new Date();
	expires.setTime(expires.getTime() - 1);
	document.cookie = name += "=; expires=" + expires.toGMTString();
}

function getCookie(name)
{
	var results = document.cookie.match(name + '=(.*)(;|$)');
	if (results)
		return unescape(results[1]);
	else
		return null;
}

/*
// from http://www.quirksmode.org/js/mouseov.html
var mouseOvers = new Array();
var mouseOuts = new Array();

function InitConsoleSwitch()
{
	var imgs = document.getElementById('topmenu');
	if (!imgs)
		return;
	imgs = imgs.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++)
	{
		if (imgs[i].src.indexOf('_on.gif') == -1) // no mouseover effect if it's already on!
		{
			mouseOuts[i] = new Image();
			mouseOuts[i].src = imgs[i].src;
			mouseOvers[i] = new Image();
			mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + '_on.gif';
			imgs[i].number = i;
			imgs[i].onmouseover = mouseGoesOver;
			imgs[i].onmouseout = mouseGoesOut;
		}
	}
}

function mouseGoesOver()
{
	if (typeof(mouseOvers) == "object")
		this.src = mouseOvers[this.number].src;
}

function mouseGoesOut()
{
	if (typeof(mouseOvers) == "object")
		this.src = mouseOuts[this.number].src;
}
*/

var oldMenuSelection = -1;

function Submenu(objA, intIndex)
{
	if (oldMenuSelection == -1)
	{
		// gotta loop through all menu items and deselect them all
		var objAllMenus = document.getElementById('topmenu');
		objAllMenus = objAllMenus.getElementsByTagName('img');
		for (var i=0;i<objAllMenus.length;i++)
			if (objAllMenus[i].src.indexOf('_on.gif') != -1)
			{
				objAllMenus[i].src = objAllMenus[i].src.replace('_on.gif', '.gif');
				oldMenuSelection = i;
				break;
			}
	}
	else
	{
		var objTemp = document.getElementById('topmenu_'+oldMenuSelection);
		objTemp.src = objTemp.src.replace('_on.gif', '.gif');
	}

	if (oldMenuSelection != -1)
	{
		var objTemp = document.getElementById('submenu_'+oldMenuSelection);
		if (objTemp)
			objTemp.style.display = 'none';
	}


	oldMenuSelection = intIndex;

	if (objA.firstChild)
	{
		var objImg = objA.firstChild;
		objImg.src = objImg.src.substring(0,objImg.src.lastIndexOf('.')) + '_on.gif';
	}

	document.getElementById('submenu_'+intIndex).style.display = 'inline';

	objA.blur();

	return false;
}

function FixExplorerLabelBug()
{
	if (!document.getElementsByTagName)
		return;

	// official bug page here: http://support.microsoft.com/default.aspx?scid=kb;en-us;314279&Product=iep
	// suggested workaround here: http://splintor.blogspot.com/2005/09/ie-bug-with-label-related-to-select.html
	// Microsoft might have fixed this bug in IE 7, I haven't tested.
	// Either way, this workaround doesn't break anything, so we don't have to give IE 7 special treatment.
	// find all <label> elements on the page, and add an onclick function.
	var labels = document.getElementsByTagName('label');
	var nrSearched = 0;
	var nrAdded = 0;
	for (var i = 0; i < labels.length; i++)
	{
		if (labels[i].htmlFor && !labels[i].onclick)
		{
			labels[i].onclick = FocusFor;
			nrAdded++;
		}

		nrSearched++;
	}
//	alert('Lade till onclick i ' + nrAdded + ' av ' + nrSearched + ' labels');
}

function FocusFor()
{
	document.getElementById(this.htmlFor).focus();
	return false;
}

function jshtmlspecialchars(str)
{
	return str.replace('&','&amp;').replace('"','&quot;').replace('<','&lt;').replace('>','&gt;');
}

function jsurlencode(strArg)
{
	return escape(strArg).replace(/%20/g, '+').replace(/\//g, '%2F');
}


function onDomLoaded()
{
	onDomLoaded.done = true;

	W3CDOM = (document.createElement && document.getElementsByTagName);

	if (W3CDOM)
	{
		var container = document.getElementById("loginbox");
		if (container && typeof(container.innerHTML) == "string") // test if the browser supports innerHTML
		{
			// set a cookie just to show the server that the client supports modern javascript
			if (!getCookie('w3'))
				setCookie('w3', '1', false, '/', false, false);
			innerHTMLWorks = true;

			if (typeof(html_Loginbox) == "string") // coming from /js/gr_se.js
			{
				container.innerHTML = html_Loginbox;

				container = document.getElementById("pollbox");
				container.innerHTML = html_Pollbox;
			}
		}

		//InitConsoleSwitch();
	}
}

function onWindowLoaded()
{
	if (!onDomLoaded.done)
		onDomLoaded();

	if (typeof(doJsTiming) == 'number')
	{
		TimeThisPage();
		window.onunload = unload;
	}
	else if (typeof(exitscript) == "string") // exitscript is only defined on a few pages
		window.onunload = unload;

	if (navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0)
	{
		FixExplorerLabelBug();

		try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
	}

	if (innerHTMLWorks && typeof(banners) == "object") // coming from multi_w3.php on the ad server
	{
		for (var i = 0; i < banners.length; i++)
		{
			var banner = banners[i];
			setad(banner[0],banner[1]);
		}
	}

	if (typeof(functionsOnload) == 'object')
		for (var i = 0; i < functionsOnload.length; i++)
		{
			var runFunction = functionsOnload[i];
			runFunction();
		}
}

if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", onDomLoaded, false);

window.onload = onWindowLoaded;
