function trim(text) {
	while(text.charAt(0) == ' ' || text.charAt(0) == '\n' || text.charAt(0) == '\t')
		text = text.substring(1, text.length);
	while(text.charAt(text.length - 1) == ' ' || text.charAt(text.length - 1) == '\n' || text.charAt(text.length - 1) == '\t')
		text = text.substring(0, text.length - 1);
	return text;
}

function getCookie(key) {
	if(document.cookie.length > 0) {
		var start = document.cookie.indexOf(key + "=");
		if(start > -1) {
			start += key.length + 1;
			var end = document.cookie.indexOf(";", start);
			if(end < 0)
				end = document.cookie.length;
			return unescape(document.cookie.substring(start, end));
		}
	}
	return null;
}

function setCookie(key, value, expiredays) {
	var expireDate = new Date();
	expireDate.setDate(expireDate.getDate() + expiredays);
	document.cookie = escape(key) + "=" + escape(value) + ";expires="+expireDate.toGMTString(); + ";path=/;domain=.krunk.dk";
}

function setSessionCookie(key, value) {
	document.cookie = escape(key) + "=" + escape(value) + ";expires=;path=/;domain=.krunk.dk";
}

function deleteCookie(key) {
	setCookie(key, "", -1);
}

function openCloseMenu(headerDivID, contentDivID) {
	var header = document.getElementById(headerDivID);
	var content = document.getElementById(contentDivID);
	
	// if contentDiv was found
	if(content != null && content != "undefined") {
		// If content is hidden, show it
		if(content.style.visibility == "hidden") {
			content.style.visibility = "visible";
			content.style.display = "block";
			if(header != null && header != "undefined")
				changeOpenCloseImage(header, false);
			setCookie(headerDivID, "open", 365);
		}
		// else hide it
		else {
			content.style.visibility = "hidden";
			content.style.display = "none";
			if(header != null && header != "undefined")
				changeOpenCloseImage(header, true);
			setCookie(headerDivID, "closed", 365);
		}
	}
}

function closeMenu(headerDivID, contentDivID) {
	var header = document.getElementById(headerDivID);
	var content = document.getElementById(contentDivID);
	
	// if contentDiv was found
	if(content != null && content != "undefined") {
		content.style.visibility = "hidden";
		content.style.display = "none";
		if(header != null && header != "undefined")
			changeOpenCloseImage(header, true);
		setCookie(headerDivID, "closed", 365);
	}
}

function openMenu(headerDivID, contentDivID) {
	var header = document.getElementById(headerDivID);
	var content = document.getElementById(contentDivID);
	
	// if contentDiv was found
	if(content != null && content != "undefined") {
		// If content is hidden, show it
		content.style.visibility = "visible";
		content.style.display = "block";
		if(header != null && header != "undefined")
			changeOpenCloseImage(header, false);
		setCookie(headerDivID, "open", 365);
	}
}

function changeOpenCloseImage(element, contentHidden) {
	if(contentHidden)
		element.style.backgroundImage = "url(graphics/open.gif)";
	else
		element.style.backgroundImage = "url(graphics/close.gif)";
}