﻿var g_BodyLoadFunctions = new Array();
var g_BodyClickFunctions = new Array();
var g_isBodyLoaded = false;

function OnLoad() {

    g_isBodyLoaded = true;
    
    for ( var i = 0; i < g_BodyLoadFunctions.length; i++ ) {
            eval( g_BodyLoadFunctions[ i ] );
        }
}

function OnClick()
{
    for ( var i = 0; i < g_BodyClickFunctions.length; i++ ) {
            eval(g_BodyClickFunctions[i]);
    }
}

      
function getKeyCode(e)
{
    if(window.event) // IE
    {
        IE = true;
        return window.event.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        IE = false;
        return e.which
    }
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}


function getWindowInnerSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

function getBodySize() {
    var myWidth = 0, myHeight = 0;
    if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    else if (typeof (window.innerWidth) == 'number') {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
    } 
    return [myWidth, myHeight];    
}

function getEventTarget(e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
    return targ;

}

function GetUrlParam(paramName) {
    var oRegex = new RegExp('[\?&]' + paramName + '=([^&]+)', 'i');
    var oMatch = oRegex.exec(window.top.location.search);

    if (oMatch && oMatch.length > 1)
        return decodeURIComponent(oMatch[1]);
    else
        return '';
}

function ShowHelp( strHelp ) {
    var objHelp = document.getElementById("help_content");
    if ( objHelp )
        objHelp.innerHTML = strHelp;

    var objHelpContainer = document.getElementById( "profile_help");
    if (objHelpContainer)
        objHelpContainer.style.visibility = "visible";
}

function ClearHelp() {
    var objHelpContainer = document.getElementById("profile_help")
    if ( objHelpContainer )
        objHelpContainer.style.visibility = "hidden";
}

function CenterPopup() {
}


function opacity(id, opacStart, opacEnd, millisec, strEval ) {
    //speed for each frame 
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens 
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "');" + (strEval && i == opacEnd ? strEval : ""), (timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers 
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

//returns the absolute position of some element within document
function GetElementAbsolutePos(element, strParentClassName) {
	var res = new Object();
	res.x = 0; res.y = 0;
	if (element !== null) {
		res.x = element.offsetLeft; 
		res.y = element.offsetTop; 
    	
		var offsetParent = element.offsetParent;
		var parentNode = element.parentNode;

		while (offsetParent !== null && offsetParent.className != strParentClassName) {
		    
			res.x += offsetParent.offsetLeft;
			res.y += offsetParent.offsetTop;

			if (offsetParent != document.body && offsetParent != document.documentElement) {
				res.x -= offsetParent.scrollLeft;
				res.y -= offsetParent.scrollTop;
            }
			
			//next lines are necessary to support FireFox problem with offsetParent
			if (__isFireFox) {
				while (offsetParent != parentNode && parentNode !== null) {
					res.x -= parentNode.scrollLeft;
					res.y -= parentNode.scrollTop;
					
					parentNode = parentNode.parentNode;
				}    
			}
			parentNode = offsetParent.parentNode;
			offsetParent = offsetParent.offsetParent;
			
			if ( strParentClassName != null && ( offsetParent == null || offsetParent.className == strParentClassName ) ) {
			    break;
			}
		}
    }

    return res;
}



function Confirm( e, strMessage )
{
    if ( confirm(strMessage) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function OnAccessChange(e) {
    e = (!e ? window.event : e);
    var objSrcElement = getEventTarget(e);
    if (objSrcElement && objSrcElement.selectedIndex == 1 || objSrcElement.selectedIndex == 2) {
        if (GetCookie("accesschange") == "1")
            return;

        document.getElementById("access_change_info").style.display = "block";
        SetCookie("accesschange", "1"); // Only show warning once per client
    }
}

function EnsureMaxLength(id, long) {
    var objSrc = document.getElementById( id );
    if (objSrc) {
        var maxlength = new Number(long); // Change number to your max length.
        if (objSrc.value.length > maxlength) {

            objSrc.value = objSrc.value.substring(0, maxlength);
            alert("Teksten må max være " + long + " tegn lang");
            objSrc.focus();
        }
    }
}

var g_strEnsureMaxLengthTimer = "";

function OnMaxRestrictedKeyDown(e) {
    if (g_strEnsureMaxLengthTimer != "") {
        clearTimeout(g_strEnsureMaxLengthTimer);
        g_strEnsureMaxLengthTimer = "";
    }
}

function OnMaxRestrictedKeyUp(e ) {
    var obj = getEventTarget(e);
    if (obj && g_strEnsureMaxLengthTimer == "" )
    {
        g_strEnsureMaxLengthTimer = setTimeout("EnsureMaxLength('" + obj.id + "'," + obj.getAttribute( "maxlength" ) +" )", 300);
    }
}

var g_objBSProxyFrames = new Array();
var g_nBSProxyBufferCount = 10;
var g_nCurrentProxyIndex = 0;

function BSPostBack(strProxyURL,strCommand, strTabID, strData) {

    // Start new proxy - so we increment index
    g_nCurrentProxyIndex++;
    
    // Maybe the index is now too large?
    if ( g_nCurrentProxyIndex == g_nBSProxyBufferCount )
        g_nCurrentProxyIndex = 0;

    var objProxyFrame = g_objBSProxyFrames[g_nCurrentProxyIndex];
    if ( !objProxyFrame )
    {
        objProxyFrame = document.createElement("IFRAME");
        objProxyFrame.style.display = "none";
        document.body.appendChild(objProxyFrame);
        g_objBSProxyFrames[g_nCurrentProxyIndex] = objProxyFrame;
    }

    if (!strData) strData = "";

    objProxyFrame.src = strProxyURL + "?command=" + escape(strCommand) + "&tabid=" + escape(strTabID) +
                                    "&data=" + escape(
        strData.replace(/æ/g, "#ae#").replace(/Æ/g, "#AE#").replace(/ø/g, "#oe#").replace(/Ø/g, "#OE#").replace(/å/g, "#aa#").replace(/Å/g, "#AA#"));
}


var __isFireFox = navigator.userAgent.match(/gecko/i);


var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

