// Make a trim function for strings...
function strtrim() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.trim = strtrim;

    
//--- /trim func

var __docAll = document.all? true : false;
var __docgetE = document.getElementById? true : false;

function getByID(id)
{
	if (__docAll)
		return document.all[id];
	else if (__docgetE)
		return document.getElementById(id)
	else
		return "";
}

// this object can represent the id of a (clicked) node, document exref or whatever
function NodeId(HtmlId)
{
	var s = HtmlId.substring(2, HtmlId.length);
	// doc?
	if ( /^d\d{1,5}/.test(s) )	{
		this.type = "document";
		this.id = parseInt(s.substring(1, s.length));
		this.rights = true;
	} // unsafe doc
	else if ( /^s\d{1,5}/.test(s) )	{
		this.type = "document";
		this.id = parseInt(s.substring(1, s.length));
		this.rights = false;
	}// result doc
	else if ( /^r\d{1,5}/.test(s) )	{
		this.type = "resultdocument";
		this.id = parseInt(s.substring(1, s.length));
		this.rights = true;
	}//Reserved result document
	else if ( /^x\d{1,5}/.test(s) )	{
		this.type = "resultdocument";
		this.id = parseInt(s.substring(1, s.length));
		this.rights = true;
	}
	else if (s.charAt(0) == "n") { // node?
		this.type = "treenode";
		this.sid = /n([0-9]+)*/.exec(s)[1];
		this.nid = /m([0-9]+)*/.exec(s)[1];
		var rex = /p([0-9]+)f([0-9]+)*/;
		if (rex.test(s))
		{
				this.hasParent = true;
				var m = rex.exec(s);
				this.parentSid = m[1];
				this.parentMid = m[2];
		}
	}
	else if (s.charAt(0) == "y") { // resNode?
		this.type = "restreenode";
		this.sid = /y([0-9]+)*/.exec(s)[1];
		this.nid = /m([0-9]+)*/.exec(s)[1];
		var rex = /p([0-9]+)f([0-9]+)*/;
		if (rex.test(s))
		{
				this.hasParent = true;
				var m = rex.exec(s);
				this.parentSid = m[1];
				this.parentMid = m[2];
		}
	}
	else if (s.charAt(0) == "k") { // Doc placement node?
		this.type = "docplace";
	}
	else if (s.charAt(0) == "Q") { // Structore Selection node node?
		this.type = "structure";
	}
	else if (s.charAt(0) == "M") { // Menu node
		this.type = "menu";
	}
	else if (s.charAt(0) == "T") { // Search result node
		this.type = "searchresult";
	}
	else if (s.charAt(0) == "e") { // EksRefNode
		this.type = "eksRefNode";
	}
	
}

// on over
function ov(aobject){
	
	var node = new NodeId(aobject.id);
	switch (node.type)
	{		
		case "eksRefNode":
			if(aobject.id.substring(3).indexOf("__") > 0)
				aobject.className += ' nodeHover';
			break;
		case "resultdocument":
		case "document":
			if (node.rights == false) {
				var sRefNo = aobject.getAttribute('tt'); 
				sRefNo += (sRefNo) ? ' ' : '';
				aobject.title = 'Du har ikke rettigheter til ' + sRefNo + aobject.innerText;
			}
			else {
				var sRefNo = aobject.getAttribute('tt'); 
				sRefNo += (sRefNo) ? ' ' : '';
				aobject.title = sRefNo + aobject.innerText;
				aobject.className += ' docHover';
			}
			break;
		case "searchresult":
		case "structure":
		case "docplace":
		case "restreenode":
		case "treenode":
			aobject.className += ' nodeHover';
			break;
		case "menu":
			var element;
			element = document.getElementById(aobject.id);
			if(element != null)
			{
				if(element.className.indexOf("NotOver") >= 0)
					element.className = element.className.replace(" NotOver","");
				element.className += " Over";
			}
			//element.style.backgroundColor='#aaaaff';
			break;
	}
}

// on out
function ou(aobject){
	var node = new NodeId(aobject.id);
	switch (node.type)
	{
		case "eksRefNode":
			var s = aobject.className.replace(/nodeHover/, '');
			aobject.className = s.trim();
			break;
		case "resultdocument":
		case "document":
			var s = aobject.className.replace(/docHover/, '');
			aobject.className = s.trim();
			break;
		case "searchresult":
		case "structure":
		case "docplace":
		case "restreenode":
		case "treenode":
			var s = aobject.className.replace(/nodeHover/, '');
			aobject.className = s.trim();
			break;
		case "menu":
			var element;
			element = document.getElementById(aobject.id);
			if(element != null)
			{
				if(element.className.indexOf("Over") >= 0)
					element.className = element.className.replace(" Over","");
				element.className += " NotOver";
			}
			//element.style.backgroundColor='Transparent';
			break;
	}
}

//This function is used to retirve a url parameter from javascript. Retiurns "" if no parameter matching name was found
function GetUrlParameter(name)
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
		return "";  
	else    
		return results[1];
}