var allPreloadImages = new Object();
var isIE = ((!window.opera) && (navigator.userAgent.indexOf('MSIE') != -1));
var isIECSSCompat = ((document.compatMode && document.compatMode.indexOf('CSS1') >= 0) ? true : false);
var body = null;
var snabela = '@';
function addEvent(obj, evType, func, useCapture) {
	useCapture = (useCapture) ? useCapture : false;
	if (obj.addEventListener) { // DOM
		obj.addEventListener(evType, func, useCapture);
		return true;
	} else if (obj.attachEvent) { // IE
		var r = obj.attachEvent('on' + evType, func);
		return r;
	} else {
		// for IE/Mac, NN4, and older
		obj['on' + evType] = func;
	}
}
function addListeners() {
	if (!document.getElementsByTagName) return;
	body = document.getElementsByTagName('body')[0];
	if (!isIE) {
		var allLinks = document.getElementsByTagName('a');
		for (var i = 0; i < allLinks.length; i++) {
			var link = allLinks[i];
			addEvent(link, 'mouseup', aBlurListener, false);
		}
	}
	var allImages = document.getElementsByTagName('img');
	for (var i = 0; i < allImages.length; i++) {
		var theImg = allImages[i];
		if (theImg.className == 'hyperImage') {
			addEvent(theImg, 'mouseover', hyperImage, false);
			addEvent(theImg, 'mouseout', hyperImage, false);
			if (!isIE) addEvent(theImg, 'mouseup', hyperImage, false);
		}
	}
	var allSpans = document.getElementsByTagName('span');
	for (var i = 0; i < allSpans.length; i++) {
		var theSpan = allSpans[i];
		if (theSpan.className == 'hyperImage') {
			addEvent(theSpan, 'mouseover', hyperImage, false);
			addEvent(theSpan, 'mouseout', hyperImage, false);
		}
		if (theSpan.className == 'hyperText') {
			addEvent(theSpan, 'mouseover', hyperText, false);
			addEvent(theSpan, 'mouseout', hyperText, false);
		}
	}
}
function aBlurListener(ev) {
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	var el = ascendDOM(t, 'a');
	if (el == null) return;
	el.blur();
}
function ascendDOM(elemRef, target) {
	var elem = getEl(elemRef);
	while ((elem.nodeName.toLowerCase() != target) && (elem.nodeName.toLowerCase() != 'html'))
		elem = elem.parentNode;
	return (elem.nodeName.toLowerCase() == 'html') ? null : elem;
}
function checkEmail(email, errstmt) {
	var emailPattern = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
//	var emailPattern =  /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(emailPattern.test(email) == false) {
		if (errstmt) {
			alert(errstmt);
		}
		return false;
	} else {
		return true;	
	}
}
function doPrint() {
	winTop = screen.height-400;
	winTop = winTop/2;
	winTop = winTop-15;
	winLeft = 0;
	detailsWindow = window.open('', 'Print', 'dependent,left=' + winLeft + ',top=' + winTop + '');
	if (docForm == "docMasterDocument") {
		detailsWindow.location = ReplicaId + "(sysPrintViewDefault)/" + docID + "?opendocument&Start=1&Count=1000&ExpandView";
	} else {
		detailsWindow.location="../(sysPrintViewDefault)/" + docID;
	}
	detailsWindow.moveTo(winLeft, winTop);
	detailsWindow.focus();
}
function findPosX(elemRef) {
	var elem = getEl(elemRef);
	var curLeft = 0;
	if (elem.offsetParent) {
		do {
			curLeft += elem.offsetLeft;
		} while (elem = elem.offsetParent);
	}
	else if (elem.x) {
		curLeft += elem.x;
	}
	return curLeft;
}
function findPosY(elemRef) {
	var elem = getEl(elemRef);
	var curTop = 0;
	if (elem.offsetParent) {
		do {
			curTop += elem.offsetTop;
		} while (elem = elem.offsetParent);
	}
	else if (elem.y) {
		curTop += elem.y;
	}
	return curTop;
}
function getEl(elemRef) {
	if (typeof elemRef == 'string') {
		if (document.getElementById)
			return document.getElementById(elemRef);
		else if (document.all)
			return document.all(elemRef);
	}
	else
		return elemRef;
}
function getElementStyle(elemRef, CSSStyleProp) { // both are string values
	var elem = getEl(elemRef);
	var styleValue, camel;
	if (elem) {
		if (document.defaultView) {
			// W3C DOM version
			var compStyle = document.defaultView.getComputedStyle(elem, '');
			styleValue = compStyle.getPropertyValue(CSSStyleProp);
		} else if (elem.currentStyle) {
			// make IE style property camelCase name from CSS version
			var IEStyleProp = CSSStyleProp;
			var re = /-\D/;
			while (re.test(IEStyleProp)) {
				camel = IEStyleProp.match(re)[0].charAt(1).toUpperCase( );
				IEStyleProp = IEStyleProp.replace(re, camel);
			}
			styleValue = elem.currentStyle[IEStyleProp];
		}
	}
	return (styleValue) ? styleValue : null;
}
function hyperImage(ev) {
	var theHyperDiv = null;
	theHyperDiv = getEl('hyperDiv');
	if (theHyperDiv)
		body.removeChild(theHyperDiv);
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	if (e.type == 'mouseover') {
		theImage = document.createElement('img');
		theImage.src = t.getAttribute('hyperImage');
		theDiv = document.createElement('div');
		theDiv.id = 'hyperDiv';
		theDiv.style.position = 'absolute';
		var theMouseLeft = (e.pageX) ? e.pageX : e.clientX;
		if (t.nodeName.toLowerCase() == 'img')
			theDiv.style.left = (findPosX(t) + t.width + 3) + 'px';
		else
			theDiv.style.left = theMouseLeft + 16 + 'px';
		if (theImage.height == 0)
			theDiv.style.top = (findPosY(t) - 60) + 'px';
		else
			theDiv.style.top = parseInt(findPosY(t) - (theImage.height/2)) + 'px';
		theDiv.style.borderWidth = '1px';
		theDiv.style.borderStyle = 'solid';
		theDiv.style.borderColor = '#000';
		theDiv.appendChild(theImage);
		body.appendChild(theDiv);
	} else if ((e.type == 'mouseout') || (e.type == 'mouseup')) {
		theHyperDiv = getEl('hyperDiv');
		if (theHyperDiv)
			body.removeChild(theHyperDiv);
	}
}
function hyperText(ev) {
	var theHyperDiv = null;
	theHyperDiv = getEl('hyperDiv');
	if (theHyperDiv)
		body.removeChild(theHyperDiv);
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	var theHyperDiv = null;
	theHyperDiv = getEl('hyperDiv');
	if (theHyperDiv)
		body.removeChild(theHyperDiv);
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	var theMouseLeft = (e.pageX) ? e.pageX : e.clientX;
	var theDivTop = findPosY(t);
	var theDivTopMod = parseInt((t.getAttribute('hypVariable').length)/40) + 1;
	if (e.type == 'mouseover') {
		t.style.cursor = 'help';
		theDiv = document.createElement('div');
		theDiv.id = 'hyperDiv';
		theDiv.style.position = 'absolute';
		theDiv.style.left = theMouseLeft + 40 + 'px';
		theDiv.style.top = theDivTop -  ( theDivTopMod * 9) + 'px';
		body.appendChild(theDiv);
		var theInsideDiv = document.createElement('div');
		theInsideDiv.className = 'hyperTextBox';
		theInsideDiv.style.textDecoration = 'none';
		theDiv.appendChild(theInsideDiv);
		theInsideDiv.innerHTML = t.getAttribute('hypVariable');
		body.appendChild(theDiv);
	} else if (e.type == 'mouseout') {
		theHyperDiv = getEl('hyperDiv');
		if (theHyperDiv)
			body.removeChild(theHyperDiv);
	}
}
function OakSendMail(f) {
	if (!f.checkThis.checked) {
		alert(f.CheckAlert.value);
		return false;
	}
	var elm, type, name, val;
	var output = "";
	for(i = 0; i < f.elements.length; i++) {
		elm = f.elements[i];
		type = elm.type;
		name = '%0D%0A%0D%0A'+elm.name+ ':%0D%0A------------------------------------------%0D%0A';
		value = "";
		switch (type) {
			case "hidden":
			case "submit":
			case "button": break;
			case "select-one":
			case "select-multiple":	
				for(var j = 0; j < elm.options.length; j++)
					if (elm.options[j].selected) {
						value += (value==""?"":";");
						value += elm.options[j].value;
					}
				break;
			case "radio":
			case "checkbox": if(elm.checked) value = elm.value; break;
			default: value = elm.value; break;
		}
		if(value != "") {
			output += output==""?"":"&";
			output += escape(name) + escape(value);
		}
	}
	document.FormHandler.FormData.value = output;
	if(f.MailTo.value) document.FormHandler.MailTo.value = f.MailTo.value;
	if(f.ThankYou.value) alert(f.ThankYou.value); else alert("Thank you for your submission");
	document.FormHandler.submit();
}
function preloadImage(strURL, iWidth, iHeight) {
	allPreloadImages[strURL] = new Image(iWidth, iHeight);
	allPreloadImages[strURL].src = strURL;
}
function replaceString(mainStr, searchStr, replaceStr) {
	regexp = eval("/" + searchStr + "/g")
	return mainStr.replace(regexp, replaceStr)
}
function resizeHandler() {
//	setTimeout('location.replace(location.href)', 500);
}
function sendUserInfo(form) {
	var req = null;
	if (window.XMLHttpRequest) req = new XMLHttpRequest( );
	else if (window.ActiveXObject) req = new ActiveXObject("Msxml2.XMLHTTP");
	if (req == null) req = new ActiveXObject("Microsoft.XMLHTTP");
	if (req == null) {return;}
	req.open('POST', ReplicaId + 'CreateLoginEntry?OpenAgent', true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(escape(form.Username.value)); // Use escape to get the danish characters right! Part 1.
}
function User(ac) {
	width = 350;
	height = 450;
	winTop = (screen.height - height) / 2;
	winLeft = (screen.width - width) / 2;
	newUserWindow = window.open('', "newUser", 'dependent, resizable=yes, scrollbars=yes, height=' + height + ', width=' + width + ',left=' + winLeft + ',top=' + winTop + '');
	if (ac == "FORGOT")
		newUserWindow.location = "/" + HRrepID + "/forgotPassword.HTML?OpenForm" + (varConfRepId == ""?"":"&CONF=" + varConfRepId) + (varLang == ""?"":"&LANG=" + varLang);
	else if (ac == "EDIT")
		newUserWindow.location = "/" + HRrepID + "/EditUserDataOnWeb/" + varSessionUser + "?EditDocument&login"
	else
		newUserWindow.location = "/" + HRrepID + "/docUser?Openform" + (varConfRepId == ""?"":"&CONF=" + varConfRepId) + (varLang == ""?"":"&LANG=" + varLang) + (varSiteInfo == ""?"":"&SiteInfo=" + varSiteInfo) + (varkwlLastMessage == ""?"":"&LastMessage=" + varkwlLastMessage);
		newUserWindow.moveTo(winLeft, winTop);
		newUserWindow.focus();
}
function winWidth() {
	var wW = 0;
	if (window.innerWidth) { // Mozilla
		wW = window.innerWidth;
	} else if (isIECSSCompat) { // IE7
		wW = document.body.parentElement.clientWidth;
	} else if (document.documentElement.clientWidth) {
		wW = document.documentElement.clientWidth;
	} else if (document.body.clientWidth) {
		wW = document.body.clientWidth;
	} else if (screen.availWidth) {
		wW = screen.availWidth;
	}
	return wW;
}
// Cookie Stuff Begin
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}
function getCookieVal(offset) {
    var endstr = window.document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = window.document.cookie.length;
    }
    return unescape(window.document.cookie.substring(offset, endstr));
}
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = window.document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (window.document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = window.document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}
function setCookie(name, value, expires, path, domain, secure) {
    window.document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function deleteCookie(name,path,domain) {
    if (getCookie(name)) {
        window.document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
// Cookie Stuff End
// String Handling *******
// extract front part of string prior to searchString
function getFront(mainStr, searchStr) {
	var foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) return null;
	return mainStr.substring(0,foundOffset);
}
// extract back end of string after searchString
function getEnd(mainStr, searchStr) {
	var foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) return null;
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}
// extract middle part of string between strFront and strEnd
function getMiddle(mainStr, strFront,strEnd) {
	var front = getEnd(mainStr,strFront);
	if (front != null) return getFront(front,strEnd);
	return null;
}
// insert insertString immediately before searchString
function insertString(mainStr, searchStr, insertStr) {
	var front = getFront(mainStr,searchStr);
	var end = getEnd(mainStr,searchStr);
	if (front != null && end != null)
		return front + insertStr + searchStr + end;
	return null;
}
// remove deleteString
function deleteString(mainStr, deleteStr) {
	return replaceString(mainStr,deleteStr,'');
}
// globally replace searchString with replaceString
function replaceString(mainStr, searchStr, replaceStr) {
	var regexp = eval('/' + searchStr + '/g');
	return mainStr.replace(regexp, replaceStr);
}
// test if searchString is contained in mainString
function isInMainstring(mainStr, searchStr) {
	var re = new RegExp(searchStr, 'g');
	return mainStr.match(re) ? true : false;
}
// Check Login User *******
var req;
function checkLoginUser() {
	if (initRequest() == false) {return;}
	req.onreadystatechange = processRequest;
	req.open('POST', ReplicaId + 'CheckLoginUser?OpenAgent', true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(escape(varSessionUser)); // Use escape to get the danish characters right! Part 1.
}
function initRequest() {
	req = null;
	if (window.XMLHttpRequest) req = new XMLHttpRequest( );
	else if (window.ActiveXObject) req = new ActiveXObject("Msxml2.XMLHTTP");
	if (req == null) req = new ActiveXObject("Microsoft.XMLHTTP");
	if (req == null) {
		return false;
	} else {
		return true;
	}
}
function processRequest() {
	if ((req.readyState == 4) && (req.status == 200)) {
		var msg = req.responseText;
		// alert(req.responseText);
		// alert(msg.indexOf('Not found!'));
		if (msg.indexOf('Not found!') > -1) {
//			alert("Some message");
			location.href = '/w-site/leo-es/docs-es.nsf?logout&redirectto=/C1256AD9005120BB/LogoutUser?OpenAgent';
		}
	}
}

// StartUp Stuff
addEvent(window, 'load', addListeners, false);
addEvent(window, 'resize', resizeHandler, false);
