function checkPasswordStrength() {
	/* DISPLAYS A DYNAMIC "PROGRESS BAR" WHEN THE USER NEEDS TO TYPE IN A NEW PASSWORD */
	/*
	## Opprinnelige regler:
	For Kort = 	under 6 tegn
	Ikke godkjent = 	alle bokstaver tall av samme, eks 111111, 123456789 eller qwerty
	Bra = kun bokstaver eller tall
	Sterkt	=	Både tall og bokstaver
	
	## Olas regler:
	For kort: under 4 tegn
	Bra: over 4 tegn
	Sterkt: over 8 tegn
	
	*/
	var pw = document.getElementById("password").value;
	
	if(pw.length > 8) {
		document.getElementById("password_strength").src = "/design/images/passord/strong.gif";
	} else if(pw.length > 4) {
		document.getElementById("password_strength").src = "/design/images/passord/weak.gif";
	} else if(pw.length > 0) {
		document.getElementById("password_strength").src = "/design/images/passord/too_short.gif";
	} else {
		document.getElementById("password_strength").src = "/design/images/passord/none.gif";
	}
}

function noIEObjectActivate() {
	/* DISABLE "CLICK TO ACTIVATE OBJECT" IN EXPLORER. UNNEDED FOR FLASH - USE SWFOBJECT INSTEAD! */
	//edit v2
	var n = navigator.userAgent;
	var w = n.indexOf("MSIE");

	if ((w>0) && (parseInt(n.charAt(w + 5)) > 5)) {
		var T = ["object", "embed", "applet"];
		var E, P, H;

		for(var j = 0; j < 2; j++) {
			E = document.getElementsByTagName(T[j]);

			for(var i = 0; i < E.length; i++) {
				P = E[i].parentNode;
				H = P.innerHTML;
				P.removeChild(E[i]);
				P.innerHTML=H;
			}
		}
	}
}

var HTMLDOMUtil = {
	createElement : function(tagName, className, id, content, extraAttributes) {
		className = className || null;
		id = id || null;
		content = content || null;
		var attr = extraAttributes || null;

		var el = document.createElement(tagName);

		if (className != null) el.className = className;
		if (id != null) el.id = id

		if (content != null)
			if (typeof(content) == 'string')
				el.innerHTML = content;
			else
				el.appendChild(content);

		if (attr != null)
			for (var i = 0; i < attr.length; i += 2)
				el[attr[i]] = attr[i+1];

		return el;
	},

	getElementsByClassName: function(className, tagName, rootNode) {
		if (tagName == null) tagName = '*';
		if (rootNode == null) rootNode = document;
		var elements = (tagName == "*" && rootNode.all)? rootNode.all : rootNode.getElementsByTagName(tagName);
		var nodes = new Array();
		var regexp = new RegExp('^(.*?\\s)?' + className + '(\\s.*)?$');
		for (var i = 0; i < elements.length; i++)
			if (regexp.exec(elements[i].className))
				nodes[nodes.length] = elements[i];
		return nodes;
	},

    /** 
     * Check if an element exists, and remove it if it does 
     * 
     * id     - The id of the element 
     */ 
    removeIfExists: function(id) { 
          var el = $(id); 
 
          if (el != null) 
               el.parentNode.removeChild(el); 
     },

	isVisible: function(id) {
		var el = $(id);

		while (el.tagName.toLowerCase() != 'body') {
			if (el.style.display == 'none')
				return false;

			el = el.parentNode;
		}

		return true;
	}
};

/**
 * General utility functions
 */
var HTMLUtil = {
	getPageSize: function() {
		var scroll = {};

		if (window.innerHeight && window.scrollMaxY) {	
			scroll.x = document.body.scrollWidth;
			scroll.y = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			scroll.x = document.body.scrollWidth;
			scroll.y = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			scroll.x = document.body.offsetWidth;
			scroll.y = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		// for small pages with total height less then height of the viewport
		if (scroll.y < windowHeight) {
			pageHeight = windowHeight;
		} else { 
			pageHeight = scroll.y;
		}

		// for small pages with total width less then width of the viewport
		if (scroll.x < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = scroll.x;
		}

		return { pageWidth: pageWidth, pageHeight: pageHeight,
				 windowWidth: windowWidth, windowHeight: windowHeight };
	},

	getScrollTop: function() {
		var pos = 0;

		if (window.pageYOffset)	pos = window.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop) pos = document.documentElement.scrollTop;
		else if (document.body) pos = document.body.scrollTop;

		return pos;
	},

	linkRelEvents: function(linkRelEvents) {
		var link;

		for (var i = 0; (link = document.getElementsByTagName('a')[i]); i++) {
            for (var j = 0; j < linkRelEvents.length; j += 2) {
				if (link.rel == linkRelEvents[j]) {
				    if (linkRelEvents[j+1] instanceof Array) {
				        link.onclick = linkRelEvents[j+1][0];
				        linkRelEvents[j+1][1](link);
				    } else {
    					link.onclick = linkRelEvents[j+1];
					}

					break;
				}
            }
		}
	},

	scrollToAnchor: function(anchor) {
		if (window.location.href.indexOf('#') >= 0)
			window.location = window.location.href.split('#')[0] + '#' + anchor;
		else
			window.location = window.location + '#' + anchor;
	}
}


/* for highlighting the information about the active input box (TODO should be made more generic) - Øyvind Smestad */
function highlightInfo(inputNode){
	var help_info = inputNode.parentNode.getElementsByTagName('div');
	if (help_info.length > 0) {	
		var helpText = help_info[0].getElementsByTagName('p');
		if (helpText.length > 0) {
			helpText[0].style.color='#000';
		}
	}
}

function dimInfo(inputNode){
	var help_info = inputNode.parentNode.getElementsByTagName('div');
	if (help_info.length > 0) {	
		var helpText = help_info[0].getElementsByTagName('p');
		if (helpText.length > 0) {
			helpText[0].style.color='#999';
		}
	}
}

function highlightPasswordInfo(){
	var node = document.getElementById('password');
	if (node) {
		highlightInfo(node);
	}
}

function dimPasswordInfo(){
	var node = document.getElementById('password');
	if (node) {
		dimInfo(node);
	}
}

function radioSelect(inputNode){
	var radionode = inputNode.parentNode.getElementsByTagName('input')[0];
	if (radionode){
		radionode.checked = 'checked';
	}
}

Object.extend(Event, {
	_domReady : function() {
		if (arguments.callee.done) return;
			arguments.callee.done = true;

		if (this._timer)  clearInterval(this._timer);

		this._readyCallbacks.each(function(f) { f() });
		this._readyCallbacks = null;
	},

	onDOMReady : function(f) {
		if (!this._readyCallbacks) {
			var domReady = this._domReady.bind(this);

			if (document.addEventListener)
				document.addEventListener("DOMContentLoaded", domReady, false);

			/*@cc_on @*/
			/*@if (@_win32)
				document.write("<script id=__ie_onload defer src='" + self.location.protocol + "//0'><\/script>");
				document.getElementById("__ie_onload").onreadystatechange = function() {
					if (this.readyState == "complete") domReady(); 
				};
			/*@end @*/

			if (/WebKit/i.test(navigator.userAgent)) { 
				this._timer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) domReady(); 
				}, 10);
			}

			Event.observe(window, 'load', domReady);
			Event._readyCallbacks =  [];
		}
		Event._readyCallbacks.push(f);
	}

});

URL = {
	getParameter: function(name, url) {
		url = url || window.location.href;
		var res = url.match(new RegExp('(\\?|\\&)' + name + '=([^&]*)'));
		return res != null && res.length > 2 ? res[2] : null;
	}
};

var adslSimTime = 0; // in tenth's of a second

var adslSimTimer;

function adslSim() {
	adslSimTime++;
	if (adslSimTime < 213) {
   		adslSimTimer = setTimeout("adslSim()", 100);
		animateGraph(19,'adsl_mini','adsl_mini_text');
		animateGraph(31,'adsl_basis','adsl_basis_text');
		animateGraph(50,'adsl_pluss','adsl_pluss_text');
		animateGraph(75,'adsl_ekstra','adsl_ekstra_text');
		animateGraph(200,'adsl_max','adsl_max_text');
	}
}

function animateGraph(speed, graph, text) {
	var progress = parseInt((adslSimTime * speed)/40);
	if (progress <= 100) {
		document.getElementById(text).innerHTML = progress + "% av 4 MB";
		document.getElementById(graph).style.width = progress + '%';
	} else {
		document.getElementById(text).innerHTML = "100% av 4 MB";
		document.getElementById(graph).style.width = '100%';
	}
}

function startAdslSim() {
	document.getElementById("adsl_sim_button").innerHTML = "Start på nytt";
	adslSimTime = 0;
	adslSim();
}

/**
 * Loops through all cells (td) contained in an element and
 * adjusts their background color. Input amount can be a hex color
 * (#xxxxxx or #xxx) either positive or negative (-#xxxxxx or -#xxx)
 * or an rgb color (rgb(x, x, x) or rgb(-x, x, x) and so on).
 *
 * If the input is null (not provided) and the function has run
 * once, the original background color will be retored. If the amount
 * is null and the function has not been run before, nothing happens.
 */
function adjustCellsBg(element, amount) {
	var cell, el;

	for (var i = 0, td = null; (td = element.getElementsByTagName('td')[i]); i++) {
		if (amount == null) {
			if (td.originalBg) td.style.backgroundColor = td.originalBg;
			continue;
		}

		cell = td, el = td, style = $(el).getStyle('background-color');

		while (style == 'transparent' && el.parentNode != null) {
			el = el.parentNode;
			style = $(el).getStyle('background-color')
		}

		cell.originalBg = style;
		cell.style.backgroundColor = addColors(style, amount);
	}
}

function addColors(color, add) {
	var plus = true;

	if (/^\-/.test(add)) {
		plus = false;
		add = add.substring(1);
	}

	color = /^#/.test(color) ? hexToRGB(color.substring(1)) : rgbFromRGBString(color);
	add = /^#/.test(add) ? hexToRGB(add.substring(1)) : rgbFromRGBString(add);
	var output;

	if (plus) {
		output = rgbToHex([color[0] + add[0], color[1] + add[1], color[2] + add[2]]);
	} else {
		output = rgbToHex([color[0] - add[0], color[1] - add[1], color[2] - add[2]]);
	}

	return output;
}

function rgbFromRGBString(rgb) {
	var a = rgb.match(/rgb\((\-?\d+)\,\s?(\-?\d+)\,\s?(\-?\d+)\)/);
	return [parseInt(a[1], 10), parseInt(a[2], 10), parseInt(a[3], 10)];
}

function hexToRGB(color) {
	if (color.length == 3) {
		color = color.charAt(0) + color.charAt(0) +
				color.charAt(1) + color.charAt(1) +
				color.charAt(2) + color.charAt(2);
	}

	if (color.length != 6) return null;

	return [parseInt(color.substring(0, 2), 16),
			parseInt(color.substring(2, 4), 16),
			parseInt(color.substring(4, 6), 16)];
}

function rgbToHex(rgb) {
	rgb = /^rgb/.test(rgb) ? rgbFromRGBString(rgb) : rgb;

	var hex = '#', tmp;

	for (var i = 0; i < rgb.length; i++) {
		if (rgb[i] > 255) rgb[i] = 255;
		if (rgb[i] < 0) rgb[i] = 0;

		tmp = rgb[i].toString(16);
		hex += tmp.length == 1 ? '0' + tmp : tmp;
	}

	return hex;
}

// For menu navigation in Lightboxes
function toggleMenu(menuId) {
	menuLevels = menuId.split('-');
	if (menuLevels[0]) {
		menuLevels[0] = 'menu-' + menuLevels[0];
	}
	if (menuLevels[1]) {
		menuLevels[1] = menuLevels[0] + '-' + menuLevels[1];
	}
	if (menuLevels[2]) {
		menuLevels[2] = menuLevels[1] + '-' + menuLevels[2];
	}
	if (menuLevels[3]) {
		menuLevels[3] = menuLevels[2] + '-' + menuLevels[3];
	}

	for (i=0; i < document.getElementById('terms_menu').getElementsByTagName('ul').length; i++) {
		idName = document.getElementById('terms_menu').getElementsByTagName('ul')[i].id;
		if (idName != null && idName.indexOf('menu-') == 0) {
			if (idName == menuLevels[0]) {
				document.getElementById(idName).style.display = '';
			} else if (idName == menuLevels[1]) {
				document.getElementById(idName).style.display = '';
			} else if (idName == menuLevels[2]) {
				document.getElementById(idName).style.display = '';
			} else if (idName == menuLevels[3]) {
				document.getElementById(idName).style.display = '';
			} else {
				document.getElementById(idName).style.display = 'none';
			}
		}
	}
	toggleContent(menuId);
}

// Set up Lightbox menu navigation
function initMenu() {
	if (document.getElementById('terms_menu')) {
		// Hide sub menus
		for (i=0; i < document.getElementById('terms_menu').getElementsByTagName('ul').length; i++) {
			idName = document.getElementById('terms_menu').getElementsByTagName('ul')[i].id;
			if (idName != null && idName.indexOf('menu-') == 0) {
				document.getElementById(idName).style.display = 'none';
			}
		}
		// Add "plus"
		for (i=0; i < document.getElementById('terms_menu').getElementsByTagName('li').length; i++) {
			liNode = document.getElementById('terms_menu').getElementsByTagName('li')[i];
			if (liNode != null) {
				if (liNode.getElementsByTagName('ul').length > 0) {
					liNode.getElementsByTagName('a')[0].addClassName('submenu');
				}
			}
		}
	}
}

// For content in Lightbox
function toggleContent(menuId) {
	// Hide all
	for (i=0; i < document.getElementById('terms_content').getElementsByTagName('div').length; i++) {
		idName = document.getElementById('terms_content').getElementsByTagName('div')[i].id;
		if (idName != null && idName.indexOf('content') == 0) {
			document.getElementById(idName).style.display = 'none';
		}
	}
	// Show selected branch
	menuLevels = menuId.split('-');
	if (menuLevels[0]) {
		menuLevels[0] = 'content-' + menuLevels[0];
	}
	if (menuLevels[1]) {
		menuLevels[1] = menuLevels[0] + '-' + menuLevels[1];
	}
	if (menuLevels[2]) {
		menuLevels[2] = menuLevels[1] + '-' + menuLevels[2];
	}
	if (menuLevels[3]) {
		menuLevels[3] = menuLevels[2] + '-' + menuLevels[3];
	}
	for (i=0; i < document.getElementById('terms_content').getElementsByTagName('div').length; i++) {
		idName = document.getElementById('terms_content').getElementsByTagName('div')[i].id;
		if (idName != null && idName.indexOf('content-') == 0) {
			if (idName == menuLevels[0]) {
				document.getElementById(idName).style.display = '';
			} else if (idName == menuLevels[1]) {
				document.getElementById(idName).style.display = '';
			} else if (idName == menuLevels[2]) {
				document.getElementById(idName).style.display = '';
			} else if (idName == menuLevels[3]) {
				document.getElementById(idName).style.display = '';
			}
		}
	}
	// Show all children of selected node
	activeNode = 'content-' + menuId;
	for (i=0; i < document.getElementById(activeNode).getElementsByTagName('div').length; i++) {
		idName = document.getElementById(activeNode).getElementsByTagName('div')[i].id;
		if (idName != null && idName.indexOf('content-') == 0) {
			document.getElementById(idName).style.display = '';
		}
	}
}

// Fix for flickering background images in IE6
function setIeImageCache() {
	/*@cc_on
	@if (@_win32)
		try {
	     	document.execCommand("BackgroundImageCache", false, true);
	    } catch(err) {};
	@end @*/
}