//Storlek
function storlek(id) {
if(document.getElementById("artikelnr")!=null) 
{
var artikel = id.split("####");
document.getElementById("artikelnr").innerHTML = artikel[0];
}
if(document.getElementById("Rubrik")!=null) 
{
var rubrik = id.split("####");
document.getElementById("Rubrik").innerHTML = rubrik[2] + ', ' + rubrik[1];
}


}

// Arrays for nodes and icons

var nodes = new Array();
var nodeObjects = new Array();
var openNodes = new Array();
var icons = new Array(14);

// Loads all icons that are used in the tree

function preloadIcons(strSkin) {

	icons[0] = new Image();
	icons[0].src = "tree/mp.gif";
	icons[1] = new Image();
	icons[1].src = "tree/mp.gif";
	icons[2] = new Image();
	icons[2].src = "tree/mp.gif";
	icons[3] = new Image();
	icons[3].src = "tree/mp.gif";
	icons[4] = new Image();
	icons[4].src = "tree/mm.gif";
	icons[5] = new Image();
	icons[5].src = "tree/mm.gif";
	icons[6] = new Image();
	icons[6].src = "tree/mm.gif";
	icons[7] = new Image();
	icons[7].src = "tree/mm.gif";
	icons[8] = new Image();
	icons[8].src = "tree/trans.gif";
	icons[9] = new Image();
	icons[9].src = "tree/trans.gif";
	icons[10] = new Image();
	icons[10].src = "tree/trans.gif";
	icons[11] = new Image();
	icons[11].src = "tree/folder.gif";
	icons[12] = new Image();
	icons[12].src = "tree/folder.gif";
	icons[13] = new Image();
	icons[13].src = "tree/trans.gif";
}

function NodeItem(obj, idx)
{
	var vals = obj.split('|');

	this.id = idx;
	this.catid = vals[0];
	this.parentid = vals[1];
	this.name = vals[2];
	this.path = vals[3];
	this.isOpen = false;
	this.hasChildNode = Boolean(parseInt(vals[4]));
	this.lastSibling = Boolean(parseInt(vals[5]));
	this.level = parseInt(vals[6]);



	this.joinString = function(cssClass)
	{

var oppen = GetCookie ('safirennode' + this.id);
var oppen2 = GetCookie ('safirennode' + this.catid);

if (oppen2 == 1)
{
this.isOpen = true;
//alert(this.id + ' ' + this.catid)
}
		var css = cssClass[this.level];
		var cssLength = css.length;
		var result = "";
		
		// Write out line & empty icons
		
	
		//if (this.level > 1)
		//{
		//result += "<div class=\"" + cssClass[this.level] + "\" style=\"border: 1px solid #808080\">";
		
		//}
		//else
		//{
		result += "<div class=\"" + cssClass[this.level] + "\">";
		//}
		

		result += "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\"><tr><td>&nbsp;</td><td>";

		for (jdx = 2; jdx <= this.level; jdx++)
		{
			if (jdx == this.level)
			{
				//result += "<img src=\"" + icons[13].src + "\" align=\"absbottom\" alt=\"\" />";
			}
			else if (jdx >= 1)
			{
				//result += "<img src=\"" + icons[9].src + "\" align=\"absbottom\" alt=\"\" />";
			}
		}
			
		// Write out join icons
		if (this.hasChildNode) 
		{
			if (this.isOpen) 
			{
				if (this.lastSibling)
				{
					result += "<img id=\"join" + this.catid + "\" onclick=\"javascript:oc(" + this.catid + ", -1);\" src=\"" + icons[6].src + "\"";
				}
				else
				{
					result += "<img id=\"join" + this.catid + "\" onClick=\"javascript: oc(" + this.catid + ", -1);\" src=\"" + icons[4].src + "\"";
				}
			}
			else 
			{
				if (this.lastSibling)
				{
					result += "<img id=\"join" + this.catid + "\" onclick=\"javascript: oc(" + this.catid + ", -1);\" src=\"" + icons[2].src + "\"";
				}
				else
				{
					result += "<img id=\"join" + this.catid + "\" onclick=\"javascript: oc(" + this.catid + ", -1);\" src=\"" + icons[0].src + "\"";
				}
			}
			result += " align=\"absbottom\" style=\"cursor:pointer;\" alt=\"&#214;ppna/St&#228;ng\" /></a>";
		}
		else 
		{
			if (this.lastSibling)
			{
				result += "<img src=\"" + icons[8].src + "\" align=\"absbottom\" />";
			}
			else
			{
				result += "<img src=\"" + icons[10].src + "\" align=\"absbottom\" alt=\"\" />";				
			}
		}
		
		// Start link
		// Write out folder & page icons

		result += "</td><td>";

		if (this.hasChildNode) 
		{
			if (this.path.length > 0)
			{
				result += "<img id=\"icon" + this.catid + "\" ";
				if (this.isOpen) 
				{
					result += "src=\"" + icons[12].src + "\" align=\"absbottom\"  width=\"0\" />";
				}
				else
				{
					result += "src=\"" + icons[11].src + "\" align=\"absbottom\"  width=\"0\" />";				
				}

		result += "</td><td width=\"100%\">";
		

				
										
				if (cssLength > 0)
				{
					result += "<a href=\"" + this.path + "\" class=\"" + css + "\" target=\"_top\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
				else
				{
					result += "<a href=\"" + this.path + "\" target=\"_top\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";					
				}
			}
			else
			{
				result += "<img id=\"icon" + this.catid + "\" ";
				if (this.isOpen) 
				{
					result += "src=\"" + icons[12].src + "\" align=\"absbottom\"  width=\"0\" />";
				}
				else
				{
					result += "src=\"" + icons[11].src + "\" align=\"absbottom\"  width=\"0\" />";				
				}

		result += "</td><td width=\"100%\">";
	

				
				if (cssLength > 0)
				{
					result += "<a href=\"javascript:;\" class=\"" + css + "\" target=\"_top\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
				else
				{
					result += "<a href=\"javascript:;\" target=\"_top\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
			}
		}
		else 
		{
			if (this.path.length > 0)
			{
				result += "<img id=\"icon" + this.catid + "\" src=\"" + icons[11].src + "\" align=\"absbottom\" width=\"0\"  /></td><td width=\"100%\">";
				
				if (cssLength > 0)
				{		
					result += "<a href=\"" + this.path + "\"  class=\"" + css + "\" target=\"_top\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
				else
				{
					result += "<a href=\"" + this.path + "\" target=\"_top\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";				
				}
			}
			else
			{
				result += "<img id=\"icon" + this.catid + "\" src=\"" + icons[11].src + "\" align=\"absbottom\" width=\"0\"  /></td><td width=\"100%\">";
				
				if (cssLength > 0)
				{
					result += "<a href=\"javascript:;\" class=\"" + css + "\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
				else
				{
					result += "<a href=\"javascript:;\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
			}


		}
			
		// Write out node name
		var str = window.location.pathname + window.location.search;
		str = str.toLowerCase();
		str = str.replace("?","#");
		str = str + '&';
		
		//if ('produkter.aspx#id=' + this.catid == str | str.match('produkter.aspx#id=' + this.catid + '&') == 'produkter.aspx#id=' + this.catid + '&')
		//{
		//result +="<b>";
		//}
		result +='<font id="font' + this.catid + '">';
		result += this.name;
		// End link
		result +='</font>';
		result += "</a>";
			
		//if ('produkter.aspx#id=' + this.catid == str | str.match('produkter.aspx#id=' + this.catid + '&') == 'produkter.aspx#id=' + this.catid + '&')
		//{
		//result +="</b>";
		//}
			//if (this.level == 1)
			//{
			//}
			//else
			//{
			if (this.lastSibling)
			{
				result += "<br><img src=\"img/linje475.jpg\" width=\"100\" height=\"1\">" //"<hr width=\"90%\" noshade style=\"margin: 0;border-top-style: dotted; border-right-style: dotted; border-left-style: dotted;border-bottom-style: dotted\" color=\"#808080\" size=\"1\">";
			//result += "<br><br>";
			}
			//}
		result += "</td></tr></table>\n";
		
		result += "</div>";

		
		return result;
	};
}

function InitNodeObjects(arrName)
{
	for (idx=0; idx < arrName.length; idx++)
	{
		var item = new NodeItem(arrName[idx], idx);

		nodeObjects.push(item);
	}
}	

// Create the tree

function createTree(arrName, startNode, openNode, strSkin) 

{

	nodes = arrName;
	if (nodes.length > 0) 
	{
		preloadIcons(strSkin);
		if (startNode == null)
		{
			startNode = 0;
		}
		
		var recursedNodes = new Array();
		
		var currentIdx = 0;
		
		InitNodeObjects(arrName);
		
		while (currentIdx < nodeObjects.length)
		{
			currentIdx = addNode(nodeObjects[currentIdx], nodeObjects[currentIdx].level);
		}
	}
}

// Returns the position of a node in the array

function getArrayId(node) {
	for (i=0; i<nodes.length; i++) 
	{
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}

// Puts in array nodes that will be open

function setOpenNodes(openNode) 
{
	for (i=0; i<nodes.length; i++) 
	{
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) 
		{
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}


// Checks if a node is open

function isNodeOpen(node) 
{
	for (i=0; i<openNodes.length; i++)
	{
		if (openNodes[i]==node) 
			return true;
	}
	
	return false;
}

// Adds a new node in the tree

function addNode(parentNode, currentLevel) 
{
	while (parentNode.level == currentLevel)
	{
	
		
			var currentIdx = parentNode.id;
			
			document.write(parentNode.joinString(cssClass));
			
			// If node has children write out divs and go deeper
			if (parentNode.hasChildNode) 
			{
				//document.write("<div id=\"div" + parentNode.catid + "\" class=\"GroupLvl" + parentNode.level + "\"");
				document.write("<div id=\"div" + parentNode.catid + "\"");
				if (!parentNode.isOpen)
				{
					document.write(" style=\"display: none;\"");
				}
			
				document.write(">");
				
				
				currentIdx = addNode(nodeObjects[currentIdx + 1], nodeObjects[currentIdx + 1].level);
				
				document.write("</div>");
			}
			// remove last line or empty icon
			
			parentNode = nodeObjects[currentIdx + 1];
			
			if (parentNode != null)
			{
				if (parentNode.level != currentLevel)
				{
					return currentIdx;
				}
			}
			else
			{
				return currentIdx + 1;
			}
		}
	
	return currentIdx + 1;
}


// Opens or closes a node

function oc(node, type)
{
	oc2(node, type);
}

function oc2(node, type) 
{
if(document.getElementById("div" + node)!=null) 
{ 

	var theDiv = document.getElementById("div" + node);
	var theJoin = document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	if (theDiv.style.display == 'none') 
	{
		if (type == 0)
		{
			theJoin.src = icons[8].src;
		}
		else if (type == 1)
		{
			theJoin.src = icons[9].src;
		}
		else if (type == 2)
		{
			theJoin.src = icons[6].src;
		}
		else
		{
			theJoin.src = icons[4].src;
		}
				SetCookie('safirennode' + node, '1', exp);
//alert('safirennode' + node + ' skapades');

		theIcon.src = icons[12].src;
		theDiv.style.display = '';
	}
	else 
	{
		if (type == 0)
		{
			theJoin.src = icons[8].src;
		}
		else if (type == 1)
		{
			theJoin.src = icons[9].src;
		}
		else if (type == 2)
		{
			theJoin.src = icons[2].src;
		}
		else
		{
			theJoin.src = icons[0].src;
		}
		DeleteCookie('safirennode' + node);
//alert('safirennode' + node + ' togs bort');
		theIcon.src = icons[11].src;
		theDiv.style.display = 'none';
	}
	
	return true;
}


// Push and pop not implemented in IE(crap! donīt know about NS though)

if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
		this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}

if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}
}

function oc3(node, type) 
{
if(document.getElementById("div" + node)!=null) 
{ 

	var theDiv = document.getElementById("div" + node);
	var theJoin = document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	
		if (type == 0)
		{
			theJoin.src = icons[8].src;
		}
		else if (type == 1)
		{
			theJoin.src = icons[9].src;
		}
		else if (type == 2)
		{
			theJoin.src = icons[6].src;
		}
		else
		{
			theJoin.src = icons[4].src;
		}
				SetCookie('safirennode' + node, '1', exp);
//alert('safirennode' + node + ' skapades');

		theIcon.src = icons[12].src;
		theDiv.style.display = '';
		
	return true;
}


// Push and pop not implemented in IE(crap! donīt know about NS though)

if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
		this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}

if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}
}