var timervar = null; 

function westlabSetObjPosition(repositionObjId,posId){
    pos = getAbsolutePos(document.getElementById(posId));
    obj = document.getElementById(repositionObjId);
    obj.style.left = pos.x;
    //alert(pos.x);
}

function westlabChangeLocation(selectObject) {
    var string = selectObject.options[selectObject.selectedIndex].value;
    if(string == 'none') {
        return true;
    }
    var array = string.split('|');
    var url = array[0];
    var type = array[1];
    //Open in new window if target is _new
    if(type == 1) {
        window.open(url);
    } else {
        window.location=url;
    }
}

function printArticle( artUrl ){
    behind = window.open(artUrl ,'printwin','height=650,width=810,status=yes,toolbar=yes,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes');
}

function emailPage( artUrl ){
    behind = window.open(artUrl ,'printwin','width=425,height=320,status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no');
    behind.focus();
}

function getContentAreaHeight() {
    if (document.all) {
        var ret  =  document.all["menuheighholder"].height;
    } else if (document.getElementById) {
		var ret =  document.getElementById("menuheighholder").height;
    }

    return  ret;

}       

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}
}

// functions for the top menu
function showMenuLayer(catID,level,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 + 15;
				initY = pos.y -4;
            } else {
                // main layer
				initX = pos.x + 0;
				initY = pos.y + 11;
			}
			// 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()",1000);
}	

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 that opens a new window where the user can 
    read how to bookmark the site. 
*/
function howToBookmark() {
    behind = window.open('howtobookmark.php' ,'bookmark','height=280,width=425,status=yes,toolbar=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes');
}

function validateForm(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("The element " + aliases[i] + " is empty.");
            elem.focus();
                        return false;
                }

        // Check for valid integer.
        if(types[i]==2) {
            if (isNaN(val)) {
                                alert("The element " + aliases[i] + " is not an Integer.");
                elem.focus();
                                return false;
            }
        }
    }
    return true;
}

