// Variables for Content and SubMenu border and padding
var contentBorderTop="";
var contentBorderBottom="";
var contentBorder="";
var contentPaddingTop="";
var contentPaddingBottom="";
var contentPadding="";
var contentInnerOffset="";
var submenuBorderTop="";
var submenuBorderBottom="";
var submenuBorder="";
var submenuPaddingTop="";
var submenuPaddingBottom="";
var submenuPadding="";
var submenuInnerOffset="";
// Fix height of layout
function checkLength() {
	// Get border and padding values from stylesheet
	getstyle();
	var contentDiv = document.getElementById('contentArea');
	var containerDiv = document.getElementById('container');
	var submenuDiv = document.getElementById('subMenuArea');
	var contentLength = (contentDiv.offsetTop+contentDiv.offsetHeight);
	var containerLength = (containerDiv.offsetTop+containerDiv.offsetHeight);
	var submenuLength = (submenuDiv.offsetTop+submenuDiv.offsetHeight);
	var maxLength = (contentLength > containerLength) ? contentLength : containerLength;
	maxLength = (submenuLength > maxLength) ? submenuLength : maxLength;
	var newContentHeight = maxLength-contentDiv.offsetTop;
	var newSubmenuHeight = maxLength-submenuDiv.offsetTop ;	
	// Fix all elements to same length, taking into account innerOffset values.
	if (contentLength != maxLength) {
		if (contentDiv) contentDiv.style.height = newContentHeight-contentInnerOffset+'px';
	}
	if (containerLength != maxLength) {
		if (containerDiv) containerDiv.style.height = maxLength+'px';
	}
	if (submenuLength != maxLength) {
		if (submenuDiv) submenuDiv.style.height = newSubmenuHeight-submenuInnerOffset+'px';
	}
}
function getstyle()
{
  var i;  var j;  var cssrules;  var Style;
  // Itterate through available stylesheets, in the same order as they appear in the source code (and are read by browser)
  for (i=0; i< document.styleSheets.length; i++)
  {
     // if IE
    if(document.styleSheets[i].rules){
        cssrules = document.styleSheets[i].rules;
    }
     // if FireFox
    else {
          cssrules = document.styleSheets[i].cssRules;
    }
    // Itterate through rules on stylesheet
     for (j=0; j <  cssrules.length; j++)
      {
	// If contentArea rule, then get appropriate values from stylesheet, and replace strings, so we only get numbers
	if(cssrules[j].selectorText=="#contentArea"){		
		Style = cssrules[j].style;
		if(Style.borderWidth != "") contentBorder = Style.borderWidth.replace('pt','').replace('px','');
		if(Style.borderTopWidth!="") contentBorderTop = Style.borderTopWidth.replace('pt','').replace('px','');
		if(Style.borderBottomWidth!="") contentBorderBottom = Style.borderBottomWidth.replace('pt','').replace('px','');
		if(Style.padding != "") contentPadding = Style.padding.replace('pt','').replace('px','');
		if(Style.paddingTop != "") contentPaddingTop = Style.paddingTop.replace('pt','').replace('px','');
		if(Style.paddingBottom != "") contentPaddingBottom = Style.paddingBottom.replace('pt','').replace('px','');
     	}
	// If subMenuArea rule, then get appropriate values from stylesheet, and replace strings, so we only get numbers
	if(cssrules[j].selectorText=="#subMenuArea"){		
		Style = cssrules[j].style;
		if(Style.borderWidth != "") submenuBorder = Style.borderWidth.replace('pt','').replace('px','');
		if(Style.borderTopWidth != "") submenuBorderTop = Style.borderTopWidth.replace('pt','').replace('px','');
		if(Style.borderBottomWidth != "")	submenuBorderBottom = Style.borderBottomWidth.replace('pt','').replace('px','');
		if(Style.padding != "") submenuPadding = Style.padding.replace('pt','').replace('px','');
		if(Style.paddingTop != "") submenuPaddingTop = Style.paddingTop.replace('pt','').replace('px','');
		if(Style.paddingBottom != "") submenuPaddingBottom = Style.paddingBottom.replace('pt','').replace('px','');
    	 }
     }
}
// Calculate the total inner offset of the contentArea (borders + padding)
if(contentPadding == "") contentPadding=0;
if(contentBorder == "") contentBorder=0;
if(contentPaddingTop == "") contentPaddingTop = contentPadding;
if(contentPaddingBottom == "") contentPaddingBottom = contentPadding;
if(contentBorderTop == "") contentBorderTop = contentBorder;
if(contentBorderBottom == "") contentBorderBottom = contentBorder;
contentInnerOffset = contentPaddingTop*1+contentPaddingBottom*1+contentBorderTop*1+contentBorderBottom*1;
// Calculate the total inner offset of the subMenuArea (borders + padding)
if(submenuPadding == "") submenuPadding=0;
if(submenuBorder="") submenuBorder=0;
if(submenuPaddingTop == "") submenuPaddingTop = submenuPadding;
if(submenuPaddingBottom == "") submenuPaddingBottom = submenuPadding;
if(submenuBorderTop == "") submenuBorderTop = submenuBorder;
if(submenuBorderBottom == "") submenuBorderBottom = submenuBorder;
submenuInnerOffset = submenuPaddingTop*1+submenuPaddingBottom*1+submenuBorderTop*1+submenuBorderBottom*1;
}

function getDbRef() {
	var db = '/';
	var ref = location.href.toLowerCase();
	var host = location.protocol + '//' + location.host;
	return host+db;
}
function ViewDeleteDoc(id, parent) {
	if(confirm('Er du sikker på at du vil slette denne side?')){
		var ref=location.href.toLowerCase();
		var view = 'pages/';
		if(ref.indexOf('/picture/') >= 0) {
			view = 'picture/';
		}
		if(ref.indexOf('/news/') >= 0) {
			view = 'news/';
		}
		var v = (parent != null) ? '&parent='+parent : '';
		window.location.href=getDbRef() + view + id +'?DeleteDocument' + v;
	}
}

function getForm() {
	return document.forms[0]
}
function isFieldEmpty(fld, msg) {
	if (fld) {
		if (fld.value.toString().length == 0) {
			alert(msg);
			fld.focus();
			return true;
		}
	}
	return false;
}
function isAFAenabled() {
	return ('' == '1')
}
function loadAFA() {
	if (isAFAenabled()) {
		try {		// Catch failure, if js-lib not found (e.g. due to being edited)
			Create_PAFToolbar(window,10,250,getDbRef()+'helpafa?openpage','120','250','')
		} catch(e) {
			return false;
		}
	}
}
// Run this on page load..!
window.onload = function(){
	// Will "adjust" body element in IE to allow selecting text by the mouse... (sigh!)
	document.body.style.height = document.documentElement.scrollHeight+'px'; 
	loadAFA();
	if(runPageAdjust) checkLength();		// To cater for pictures of unknown height!
}
