

var timervar = null; 


/*
  	function to init 

NB - benytter denne istedet for cp2_init p.g.a problemer med changeclass funk i IE.

*/
function nho_init()	{
	// Mouse position capture
	ns4 = document.layers?true:false;
	ns5 = (document.getElementById && !document.all)?true:false;
	ie5 = document.all?true:false;
	ie4 = (!document.getElementById && document.all)?true:false;
	
	if(ns4 || ns5)
	 document.captureEvents(Event.MOUSEMOVE);
	
	//document.onmousemove=catchXY;

	// setting up keyword layer
	//cp2_keywordlayer = new ct_layer("cp2keywordlayer");

	// Tell that document is loaded.
	cp2_loaded = true;
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function printArticle( artUrl ){
    behind = window.open(artUrl ,'printwin','height=620,width=590,status=yes,toolbar=yes,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes');
}

function colCh(cellname,mOver) {
	if (document.all)
        document.all[cellname].style.background = (mOver)?"#A10000":"white";
	else if (document.getElementById)
		document.getElementById(cellname).style.background = (mOver)?"green":"white" ;
}

function getAbsolutePos(el){
	for (var lx=0,ly=0;el!=null;
		lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}


function ExpandMenu2 (catID) {
	
	// first, check if the children layer exists
	 if (layerNamesByID[catID]) {
	   	// get the height of the layer
		var startX = layerNamesByID[catID].getTop() + layerNamesByID[catID].getHeight();
		var moved = 0;
		var layersAlreadyMoved = new Array();
		
		// move all layers that have the same x and a higher y
		// than our layer
		
		  
		// we need to find all layers that have this as the parent, 
		// and show them
		for (i=0; i < parentMap.length; i++) {
		   	if (parentMap[i][1] == catID) {
				//alert(i + "parent er" + parentMap[i][1]);
				//alert(i + "id er " + parentMap[i][0]);
				
				layerNamesByID[parentMap[i][0]].setTop(startX);
				startX += layerNamesByID[parentMap[i][0]].getHeight();
				moved += layerNamesByID[parentMap[i][0]].getHeight();					
                layersAlreadyMoved[layersAlreadyMoved.length] = layerNamesByID[parentMap[i][0]].objLayName;
				
				if (openState[catID] == 0) {
                    layerNamesByID[parentMap[i][0]].show();
				} else {
					layerNamesByID[parentMap[i][0]].hide();
				}
			}
		}		  
		  
		// then walk through all layers with a getTop() higher than 
		// getTop() of the active layer....
		var largestHeight = 0;
		for (i=0; i< layerNames.length; i++) {
			if (layerNames[i].getTop() >  layerNamesByID[catID].getTop()) {
				// check if layer has already been moved..
				var alreadyMoved = false;
				for (j=0; j < layersAlreadyMoved.length; j++ ) {
                    if (layerNames[i].objLayName == layersAlreadyMoved[j]) {
						alreadyMoved = true;
					}
				}
				if (alreadyMoved == false) {
					if (openState[catID] == 0) {
						//alert("moving layers down by " + moved);
						layerNames[i].setTop(layerNames[i].getTop() + moved);
					} else {
						layerNames[i].setTop(layerNames[i].getTop() - moved);
					}
				}
				
                if (largestHeight < (layerNames[i].getTop() + layerNames[i].getHeight())) {
					largestHeight = (layerNames[i].getTop() + layerNames[i].getHeight())
				}
				
			}
		}

        // adjust for menu offset....
        largestHeight -= 140;
		
		// check if we are to set new height of the td
		if (getContentAreaHeight() < largestHeight) {
            //alert ("contentarea is " + getContentAreaHeight() + " , largestheight is " + largestHeight);
			expandTD(largestHeight);
		}

		
	 
		// set open state   
		if (openState[catID] == 1) {
            openState[catID] = 0 ;
			
			// if we are closing, we need to make sure to close all open
			// sub childs as well....
			
			for (i=0; i < parentMap.length; i++) {
				if (parentMap[i][1] == catID) {
					if (openState[parentMap[i][0]] == 1) {
						ExpandMenu2(parentMap[i][0]);
					}
				}
			}
			
        } else {
			openState[catID] = 1 ;
		}

        // try to change the expand gif...
        // alert(moved);
		if (moved != 0 && document.getElementById && document.getElementById("exp-"+catID))	{
			theImage = document.getElementById("exp-"+catID);
            if (theImage.src.indexOf("expand.gif") > 0) {
                theImage.src = imgroot + "collapse.gif" ;
            } else {                                     
                theImage.src = imgroot + "expand.gif" ;
            }

            // alert(theImage.src);

        }
    }	
}

		 
function expandTD(newheight) {
	if (document.all)
        document.all["menuheighholder"].height = newheight;
	else if (document.getElementById)
		document.getElementById("menuheighholder").height = newheight;
}
		
function getContentAreaHeight() {
    if (document.all) {
        var ret  =  document.all["menuheighholder"].height;
    } else if (document.getElementById) {
		var ret =  document.getElementById("menuheighholder").height;
    }

    /*
    if (window.innerWidth && window.innerHeight) {
        win_height = window.innerHeight;
        win_width = window.innerWidth;
     } else if (document.body.clientWidth && document.body.clientHeight) {
        win_height = document.body.clientHeight;
        win_width = document.body.clientWidth;
     } else if (window.screen) {
        win_height = screen.availHeight;
        win_width = screen.availWidth;
     } else {
        // assuming low resolution
        win_height = 600;
        win_width = 800;
     }  
     var ret  =win_height - 120 - 25;*/

     //alert('contentarea is ' + ret);
    return  ret;

}		
		 
function Login(string) {
    //alert("Du må være logget inn for å få tilgang til dette menypunktet");
    alert(string);
}



// functions for the top menu
function showMenuLayer(catID,level,object,parentID,isTop) {

	if (submenuLayers[catID]) {
        
		// preserve menulayers showing...
		keepMenuLayerOpen(catID);   // Current
        
        if (parentID > 0) {
			keepMenuLayerOpen(parentID);
		}
		
        //timerArray[catID] = null;
        
		// position the layer using a hidden image...
		//submnuimg-catID
//		if (document.getElementById && document.getElementById("submnuimg-"+catID))	{
			theImage = document.getElementById("submnuimg-"+catID);
			pos = getAbsolutePos(theImage);
			
            //alert(level);
			// submenu layers require different position...
			if (level == 5)  {
				// sub layers
				initX = pos.x + 138;
				initY = pos.y -1;
			} else if(level == 4)  {
				// sub layers
				initX = pos.x + 1;
				initY = pos.y + 17;
			} else {
				// main layer
				initX = pos.x + 1;
				initY = pos.y + 17;
			}
			// alert("x er " + initX + ", y er " + initY);
			// initY = y;
//		} else {
			// ns 4
//			initX = x;
//			initY = y;
//		}									  
				  
		// close active layer if exists
		closeActiveLayer(level);
						    					 
		submenuLayers[catID].moveTo(initX,initY);							   
		submenuLayers[catID].show();
		
		// set active id on this level
		activeID[level] = catID;
		
	} else {
		// hide active layer on this level....
		closeActiveLayer(level);
	}
}		 
					 
function hideMenuLayer(catID,level) {
//	timerArray[catID] = setTimeout("submenuLayers["+catID+"].hide()",1000);
//	timerArray[catID] = setTimeout("closeAllActiveLayers()",1000);
	timervar = setTimeout("closeAllActiveLayers()",500);
}	

function closeActiveLayer(level) {
	if (activeID[level] > 0 ) {
		submenuLayers[activeID[level]].hide();
	}				  
	
	// close parents
	for (x=level;x < 12 ; x++) {
		if (activeID[x] > 0 ) {
			// alert("hei");
			submenuLayers[activeID[x]].hide();
		}				  
	}
}

function closeAllActiveLayers() {
    //alert("Timer triggered...");
	closeActiveLayer(0);
}

function keepMenuLayerOpen(catID) {
//	clearTimeout(timerArray[catID]);
//  timerArray[catID] = null;
    if(timervar!=0) {
        clearTimeout(timervar);
        timervar = null;
    }
}				 

function ChangeClass(id, classname) {
//	if (document.all) {
		element = document.getElementById( id );
		element.className = classname;
//	}
}

/*
    function that loops trough a given set of form elements and validates its.
    
    useage:
    [inside js]
    var requiredFields = Array("firstname",
                           "lastname",
                           "adress",
                           "postnr",
                           "birthyear",
                           "birthmonth",
						   "birthday");

        // types can be 1:text, 2:integer.
        var requiredTypes = Array(1,1,1,2,2,2,2);
        var requiredAliases = Array("Fornavn","Etternavn","Adresse","Postnr","Fødselsår","F.måned","f.dag");

    [on form tag]
    <FORM method="post" onsubmit="return validateForm('form1',requiredFields,requiredTypes,requiredAliases);" action="handler.php" name="form1">
    
    @param formname: the form name
    @param fields: array of strings containing names of the fields to search.
    @param types: array determing type of the string above, 1 is string av 2 is integer
    @param aliases: the field aliases used in the error message.
*/
function nhoValidateForm(formname,fields,types,aliases) {
	for (i=0;i<fields.length;i++) {

        // First, get the value out
        var elem = document.forms[formname].elements[fields[i]];
        var val = "";

        // Then switch between different form elements.
        if (elem.type == "text" || elem.type == "password" || elem.type == "checkbox") {
            val = elem.value;
        } else if (elem.type == "select-one" || elem.type == "select-multiple") {
            val = elem.options[elem.selectedIndex].value;
        }  else {
            // build on here as we need more.
            val = elem.value;
        }

        // Check whether there are any content at all
		if (val.length ==0 ) {
			alert("Elementet " + aliases[i] + " er ikke fylt ut");
            elem.focus();
			return false;
		}

        // Check for valid integer.
        if(types[i]==2) {
            if (isNaN(val)) {
				alert("Elementet " + aliases[i] + " er ikke et tall.");
                elem.focus();
				return false;
            }
        }
    }
    return true;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
