﻿
function addEvent(obj, evType, fn, useCapture)
{
	window.alert("ja");
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, useCapture); 
		return true; 
	} 
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on" + evType, fn); 
		return r; 
	}
	else
	{ 
		obj['on' + evType] = fn;
		return false; 
	} 
}

function getElementsByClass(searchClass, node, tag)
{
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (var i = 0, j = 0; i < elsLen; ++i)
	{
		if (pattern.test(els[i].className))
		{
			classElements[j] = els[i];
			++j;
		}
	}
	return classElements;
}

function resizeIframeById(frameId)
{
	return resizeIframe(document.getElementById(frameid));
}

function getDocHeight(doc)
{
	var docHt = 0, sh, oh;
	if (doc.height)
		docHt = doc.height;
	else if (doc.body)
	{
		if (doc.body.scrollHeight)
			docHt = sh = doc.body.scrollHeight;
		if (doc.body.offsetHeight)
			docHt = oh = doc.body.offsetHeight;
		if (sh && oh)
			docHt = Math.max(sh, oh);
	}
	return docHt;
}

function resizeIframe(frameName)
{
	var frameWin = window.frames[frameName];
	if (frameWin)
	{
		var frameElem = document.getElementById ? document.getElementById(frameName) : document.all ? document.all[frameName] : null;
		if (frameElem)
		{
			frameElem.style.height = "auto";
			var h = 0;
			try
			{
				h = parseInt(getDocHeight(frameWin.document) + 30);
			}
			catch (err)
			{
				h = parseInt(getDocHeight(frameElem.document) - 260);
			}
			if (parseInt(h) > 0)
				frameElem.style.height = h + "px";
		}
	}
}

function initiateSizesIE()
{
	var mains = getElementsByClass("main");
	for (var i = 0; i < mains.length; ++i)
	{
		var mw = 730;
		var protections = getElementsByClass("box-overflow-protection", mains[i]);
		for (var ii = 0; ii < protections.length; ++ii)
		{
			var p = protections[ii];
			var w;
			with (p.style)
			{
				w = parseInt(Math.max(Math.max(p.scrollWidth, p.clientWidth), p.offsetWidth)
					+ borderLeft + borderRight + paddingLeft + paddingRight);
			}
			mw = Math.max(mw, w);
		}
		if (mw > 0)
		{
			mw = parseInt(mw + 157 + 31) + "px";
			document.all.page.style.minWidth = mw;
			document.all.page_top.style.width = mw;
		}
	}
}

var reinitiatingSize = 0;
var lastWidth = -1;
var lastHeight = -1;

function reinitiateSizesIE7()
{
	if (reinitiatingSize == 1)
		return;
	reinitiatingSize = 1;
	var h = document.documentElement.scrollHeight + "px";
	document.all.page.style.minHeight = h;
	document.all.page_body.style.minHeight = h;
	document.all.page_middle.style.minHeight = h;
	reinitiatingSize = 0;
}

function reinitiateSizesIE8()
{
	if (reinitiatingSize == 1)
		return;
	reinitiatingSize = 1;

	if((lastWidth != document.documentElement.clientWidth) ||  (lastHeight != document.documentElement.clientHeight))
	{
		var h = document.documentElement.scrollHeight + "px";
		document.all.page.style.minHeight = h;
		document.all.page_body.style.minHeight = h;
		document.all.page_middle.style.minHeight = h;
	}
	lastHeight = document.documentElement.clientHeight;
	lastWidth = document.documentElement.clientWidth;
	reinitiatingSize = 0;
}


function isIE()
{
	if ((navigator.appVersion.indexOf("MSIE") != -1) && (document.all.page != null))
	{
		return true;
	}
	return false;
}

function isIE7()
{
	if (navigator.appVersion.indexOf("MSIE 7.") != -1)
	{
		return true;
	}
	return false;
}

function isIE8()
{
	if (navigator.appVersion.indexOf("MSIE 8.") != -1)
	{
		return true;
	}
	return false;
}

function initiate()
{
	if (isIE())
	{
		initiateSizesIE();
		if (isIE7())
		{
			reinitiateSizesIE7();
			window.onresize = function() { handleResizeIE7();}
		}
		if (isIE7())
		{
			reinitiateSizesIE8
			window.onresize = function() { handleResizeIE8();}
		}
	}
	setupPanes();
	if (typeof replaceRequiredFieldValidator == "function")
	{
		replaceRequiredFieldValidator();
	}
}

function initiateIframe(frameName)
{
	return resizeIframe(frameName);
}

function handleResizeIE7()
{
	reinitiateSizesIE7();
}

function handleResizeIE8()
{
	reinitiateSizesIE8();
}

function initateArticleEquation(origImage, forPrinting)
{
	if (origImage.parentNode.getAttribute("class") != "article-equation-box")
	{
		if (!forPrinting && isIE7())
		{
			var prefixImage = document.createElement("img");
			origImage.parentNode.insertBefore(prefixImage, origImage);
			prefixImage.className = "equation-img-pusher";
			prefixImage.src = "images/pusher.png";
			prefixImage.style.height = (origImage.height + 18) + "px";
		}

		var containerBox = document.createElement("div");
		origImage.parentNode.insertBefore(containerBox, origImage);
		containerBox.className = "article-equation-container";

		var imageBox = document.createElement("div");
		containerBox.appendChild(imageBox);
		imageBox.className = "article-equation-box";
		
		var newImage = document.createElement("img");
		imageBox.appendChild(newImage);
		newImage.className = "equation-img-cloned";
		newImage.src = origImage.src;
		newImage.alt = origImage.alt;

		if (!forPrinting && isIE7())
		{
			var suffixImage = document.createElement("img");
			origImage.parentNode.insertBefore(suffixImage, origImage.nextSibling);
			suffixImage.className = "equation-img-pusher";
			suffixImage.src = "images/pusher.png";
			suffixImage.style.height = (origImage.height + 18) + "px";
		}

		origImage.parentNode.removeChild(origImage);
	}
	return true; 
}

function initateArticleImage(origImage, forPrinting)
{
	if (origImage.parentNode.getAttribute("class") != "article-img-box")
	{
		var href = null;
		if (origImage.parentNode.tagName.toLowerCase() == "a")
			href = origImage.parentNode;

		var containerBox = document.createElement("div");
		if (href)
			origImage.parentNode.parentNode.insertBefore(containerBox, origImage.parentNode);
		else
			origImage.parentNode.insertBefore(containerBox, origImage);
		containerBox.className = "article-img-container";
		
		var imageBox = document.createElement("div");
		containerBox.appendChild(imageBox);
		imageBox.className = "article-img-box";
		
		var newImage = document.createElement("img");
		if (href)
		{
			href.appendChild(newImage);
			imageBox.appendChild(href);
		}
		else
			imageBox.appendChild(newImage);
		newImage.className = "article-img-cloned";
		newImage.src = origImage.src;
		newImage.alt = origImage.alt;
		newImage.width = origImage.width;
		newImage.height = origImage.height;

		var textBox = document.createElement("div");
		containerBox.appendChild(textBox);
		textBox.className = "article-img-caption";
		var b = 0;
		if (origImage.title && (origImage.title.length > 0))
		{
			++b;
			textBox.appendChild(document.createTextNode(origImage.title));
		}
		if (origImage.alt && (origImage.alt.length > 0))
		{
			if (b > 0)
				textBox.appendChild(document.createTextNode(" \u2014 "));
			++b;
			textBox.appendChild(document.createTextNode(origImage.alt));
		}
		if (origImage.copyright && (origImage.copyright.length > 0))
		{
			++b;
			textBox.appendChild(document.createTextNode(" (" + origImage.copyright + ")"));
		}
		textBox.style.visible = b > 0;

		with (origImage.style)
		{
			var xtra = 0;
			if (href)
				xtra = 8;
			if (isIE7())
				containerBox.style.width = (parseInt(origImage.width + borderLeft + borderRight + paddingLeft + paddingRight) + xtra + 10) + "px";
			else if (navigator.appVersion.indexOf("MSIE 6.") != -1)
				containerBox.style.width = (parseInt(origImage.width + borderLeft + borderRight + paddingLeft + paddingRight) + xtra) + "px";
			else
				containerBox.style.width = (parseInt(origImage.width + borderLeft + borderRight + paddingLeft + paddingRight) + xtra) + "px";
		}

		origImage.parentNode.removeChild(origImage);
	}
	return true; 
}

function printUrl(url)
{
	return window.open(url, "print_win");
}

function redirectToUrl(url)
{
	if (navigator.appVersion.indexOf("MSIE") != -1)
		location.replace(url);
	else
		location.HREF = url;
}


function restartAnimatedGifs()
{
	if (navigator.appVersion.indexOf("MSIE") != -1)
		for (var i = 0; i < document.images.length; ++i)
		{
			var img = document.images[i];
			var name = img.src.toLowerCase();
			if (name.substring(name.length - 3, name.length) == "gif")
				img.src = img.src;
		}
}

var paneControls = new Array();
function setupPanes()
{
	var controls = getElementsByClass("pane-control");	
	for (var i = 0; i < controls.length; ++i)
	{
		var control = controls[i];
		if (control == null)
			continue;
		paneControls[i] = new Array();
		var maxHeight = 0;
		var maxWidth = 0;
		var panes = getElementsByClass("pane", control);
		if (panes.length > 0)
		{
			for (var j = 0; j < panes.length; ++j)
			{
				var pane = panes[j];
				if (pane == null)
					continue;
				if (pane.nodeType != 1)
					continue;
				paneControls[i][j] = pane;
				if (pane.offsetHeight > maxHeight)
					maxHeight = pane.offsetHeight;
				if (pane.offsetWidth > maxWidth)
					maxWidth = pane.offsetWidth;
				setPaneVisibility(pane, j == 0);
			}
			control.style.height = maxHeight + "px";
			control.style.width = maxWidth + "px";
			showPane(control.id, paneControls[i][0].id);
		}
	}
	return true;
}

function showPane(controlId, paneId)
{
	var controls = getElementsByClass("pane-control");	
	for (var i = 0; i < controls.length; ++i)
	{
		var control = controls[i];
		if (control == null)
			continue;
		if (control.id == controlId)
		{
			var panes = paneControls[i];
			for (var j = 0; j < panes.length; ++j)
			{
				var pane = panes[j];
				if (pane == null)
					continue;
				setPaneVisibility(pane, pane.id == paneId);
			}
			break;
		}
	}
	return false;
}

function setPaneVisibility(pane, visible)
{
	if (visible)
	{
		if (isIE())
		{
			if (isIE7() || isIE8())
			{
				pane.style.height = "auto";
				pane.style.overflow = "auto";
			}
			else
			{
				pane.style.height = "auto";
				pane.style.maxHeight = "auto";
				pane.style.overflow = "auto";
			}
		}
		else
			pane.style.visibility = "visible";
	}
	else
	{
		if (isIE())
		{
			if (isIE7() || isIE8)
			{
				pane.style.height = "0";
				pane.style.overflow = "hidden";
			}
			else
			{
				pane.style.height = "1px";
				pane.style.maxHeight = "1px";
				pane.style.overflow = "hidden";
			}
		}
		else
			pane.style.visibility = "hidden";
	}
}

function getFloatValue(origInput)
{
	var v;
	if (origInput == null)
	{
		v = 0;
	}
	else
	{
		var s;
		if (origInput.value)
		{
			s = origInput.value;
		}
		else
		{
			s = origInput.innerText;
		}
		if (s.length == 0)
		{
			v = 0;
		}
		else
		{
			var r = s.replace(/\,/, '.');
			var f = parseFloat(r);
	 		if (r != f)
	 		{
				window.alert('Värdet för "' + origInput.title + '" måste vara ett tal');
				origInput.focus();
				v = 0;
			}
			else if (f < 0)
			{
				window.alert('Värdet för "' + origInput.title + '" måste vara större eller lika med noll.');
				origInput.focus();
				v = 0;
			}
			else
			{
				v = f;
			}
		}
	}
	return v;
}

function setFloatValue(resInput, value)
{
	var v;
	if (value == null)
	{
		v = 0;
	}
	else
	{
		v = value;
	}
	if (resInput)
	{
		resInput.value = String(Math.round(v)).replace(/\./, ',');
	}
	return v;
}

function setFloatValueWithDecimals(resInput, value, decimalPlaces)
{

	var v;
	if (value == null)
	{
		v = 0;
	}
	else
	{
		v = value;
	}
	if (resInput)
	{
		resInput.value = String(Math.round(v * (Math.pow(10, decimalPlaces)))/Math.pow(10, decimalPlaces)).replace(/\./, ',');
	}
	return v;
}

