// Variables for sizes of inner content (border and padding)
var contentBorderTop="";
var contentBorderBottom="";
var contentBorder="";
var contentPaddingTop="";
var contentPaddingBottom="";
var contentPadding="";
var contentInnerOffset=0;
var submenuBorderTop="";
var submenuBorderBottom="";
var submenuBorder="";
var submenuPaddingTop="";
var submenuPaddingBottom="";
var submenuPadding="";
var submenuInnerOffset=0;
// 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.clientHeight);
	var submenuLength = (submenuDiv.offsetTop+submenuDiv.offsetHeight);
	var maxLength = (contentLength > containerLength) ? contentLength : containerLength;
	maxLength = (submenuLength > maxLength) ? submenuLength : maxLength;
	var newContentHeight = maxLength-contentDiv.offsetTop-contentInnerOffset;
	var newSubmenuHeight = maxLength-submenuDiv.offsetTop-submenuInnerOffset;	
	// Fix all elements to same length, taking into account innerOffset values.
	if (contentLength != maxLength) {
		if (contentDiv) contentDiv.style.height = newContentHeight+'px';
	}
	if (containerLength != maxLength) {
		if (containerDiv) containerDiv.style.height = maxLength+'px';
	}
	if (submenuLength != maxLength) {
		if (submenuDiv) {
			submenuDiv.style.height = newSubmenuHeight+'px';
			var contactDiv = document.getElementById('contact');
			if (contactDiv) {
				// Adjust min. height of UL to push contact down to right position
				var subMenuUL = document.getElementById('menu')
				var offset = submenuDiv.offsetHeight-contactDiv.offsetHeight-(contactDiv.offsetTop-subMenuUL.offsetHeight);	// Last element eliminates margins etc.
				if (subMenuUL) subMenuUL.style.height = offset+'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(document.styleSheets[i].rules){			// if IE
			cssrules = document.styleSheets[i].rules;
		} else {							// if FireFox
			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(isNaN(contentPadding)) contentPadding=0;
	if(isNaN(contentBorder)) contentBorder=0;
	if(isNaN(contentPaddingTop)) contentPaddingTop = contentPadding;
	if(isNaN(contentPaddingBottom)) contentPaddingBottom = contentPadding;
	if(isNaN(contentBorderTop)) contentBorderTop = contentBorder;
	if(isNaN(contentBorderBottom)) contentBorderBottom = contentBorder;
	contentInnerOffset = contentPaddingTop*1+contentPaddingBottom*1+contentBorderTop*1+contentBorderBottom*1;
	// Calculate the total inner offset of the subMenuArea (borders + padding)
	if(isNaN(submenuPadding)) submenuPadding=0;
	if(isNaN(submenuBorder)) submenuBorder=0;
	if(isNaN(submenuPaddingTop)) submenuPaddingTop = submenuPadding;
	if(isNaN(submenuPaddingBottom)) submenuPaddingBottom = submenuPadding;
	if(isNaN(submenuBorderTop)) submenuBorderTop = submenuBorder;
	if(isNaN(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(getLabel(3))){
		var ref=location.href.toLowerCase();
		var view = 'pages/';
		var v = (parent != null) ? '&parent='+parent : '';
		if(ref.indexOf('/picture/') >= 0) {
			view = 'picture/';
			v = (parent != null) ? '&category='+parent : '';			// Reference to deleted document's category
		}
		if(ref.indexOf('/news/') >= 0) {
			view = 'news/';
		}
		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();
}
// Get message - depends on language specific text.js
function getLabel(n) {
	try {
		return textLabels[n-1];
	} catch(e) {
		// Nothing
	}
	return '¤'+n+'¤';		// Default "not found" message...
}
function isValidDataType(obj, label, dt) {
	try {
		var v = '';
		var txt = '';
		if(obj && obj.value != '') v = obj.value;
		if(v != '') {
			v = v.trim();
			if(dt=='1') {					// Email
				if(v.search(/^([a-z]+)([a-z0-9\-\_\.]{1,100})([a-z0-9]+)\@([a-z0-9]+)([a-z0-9\-\.]*)([a-z0-9]+)\.([a-z]{2,6})$/) == -1) {
					txt = getLabel(6);
				 }
			} else if(dt=='2') {			// Number
				if(v.search(/[0-9\-\+\,\.]/) == -1) {
					txt = getLabel(7);
				 }
			} else if(dt=='3') {			// Phone number
				if(v.search(/[0-9\-\+\,\.\(\)]/) == -1) {				// mellemrum.......???
					txt = getLabel(8);
				 }
			}
			if(txt != '') {
				alert(txt.replace('%1', label));
				obj.focus();
				return false;
			}
		}
	}
	catch(e) {
		return false;
	}
	return true;
}
function isValid(obj, label, ghost, dt, opt) {
	try {
		if(obj) {
			var optional = false;
			var datatype = '';
			var ghostvalue = '';		// Label shown inline as value
			if(ghost && ghost != '') ghostvalue = ghost;
			if(opt) optional = true;
			if(dt && dt != '') datatype = dt;
			if( ! optional) {
				if(obj.value == '' || obj.value == ghostvalue) {
					var txt = getLabel(4);
					alert(txt.replace('%1', label));
					obj.focus();
					return false;
				}
			} else {
				if(datatype != '') {
					return isValidDataType(obj, label, datatype)
				}
			}
			return true;
		}
	}
	catch(e) { }
	return false;
}
function isValidSelect(obj, label, ghost) {
	try {
		if(obj) {
			var ghostvalue = '';		// Label shown inline as value
			if(ghost && ghost != '') ghostvalue = ghost;
			if(obj.selectedIndex < 0 || obj[obj.selectedIndex].text == ghostvalue) {
				var txt = getLabel(5);
				alert(txt.replace('%1', label));
				obj.focus();
				return false;
			} else {
				return true;
			}
		}
	}
	catch(e) { }
	return false;
}
function isValidCheck(obj, label) {
	try {
		if(obj) {
			var valid = false;
			for (i = 0;  i < obj.length;  i++) {
				if (obj[i].checked) return true;
			}
			var txt = getLabel(5);
			alert(txt.replace('%1', label));
			// obj.focus();		Cannot set focus on checkbox or radio....
			return false;
		}
	}
	catch(e) { }
	return false;
}

