﻿function addLoadHandler(handler) {
    
	if (window.addEventListener) {
		window.addEventListener("load", handler, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", handler);
	} else if(window.onload) {
		var oldHandler = window.onload;
		window.onload = function piggyback() {
			oldHandler();
			handler();
		};
	} else {
		window.onload = handler;
	}
}

function checkEnter(e,caller) //e is event object passed from function invocation
{
	var characterCode //literal character code will be stored in this variable

	if(e && e.which)
	{ 
		//if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else
	{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	if(characterCode == 13)//if generated character code is equal to ascii 13 (if enter key)
	{ 
		document.getElementById(caller).click();
		
		return false
	}
	else
	{
		return true
	}
}


// DROPDOWN 

function setLanguage(pUrl) {
	
	var href = location.href;
	
	if (href.indexOf("lang=") < 0) {
		if (location.href.indexOf("?") < 0) {
			location.href = location.href + "?lang=" + pUrl;
		}
		else {
			location.href = location.href + "&lang=" + pUrl;
		}
	}
	else {
		var replacedString = href.substr(href.indexOf("lang="),7);
		href = href.replace(replacedString, "lang=" + pUrl);
		location.href = href;
	}
}


function setCountryID(countryID) {
	var href = location.href;
	
	if (href.indexOf("cID=") < 0) {
		if (location.href.indexOf("?") < 0) {
			location.href = location.href + "?cID=" + countryID;
		}
		else {
			location.href = location.href + "&cID=" + countryID;
		}
	}
	else {
		var newUrl = href.substr(0,href.indexOf(location.search)) + "?cID=" + countryID;
		location.href = newUrl;
	}
}

function toggleMenu(show){
	if(document.getElementById("langDropDown").style.display != "block" || show){
		document.getElementById("langDropDown").style.display = "block";
	}
	else {
		document.getElementById("langDropDown").style.display = "none";
	}
}


function toggleContactDDL(input, show) {
	if(show){
		document.getElementById(input).style.display = "block";
	}
	else {
		document.getElementById(input).style.display = "none";
	}
}

// Funktion   : pageLoad eksekveres automatisk ved AJAX kald.
 function pageLoad() {
    var obj = document.getElementById("freezePage");
    if (obj && obj.style.display == "inline") {
        obj.style.display = 'none';
        obj.style.cursor = 'normal';
        if (typeof (PrivateFreezeFunction) == 'function') {
            PrivateFreezeFunction();
        }
    }

    if (typeof (PrivatePageLoadFunction) == 'function') {
        PrivatePageLoadFunction();
    }
}

function FreezePage(doValidate) {
    var isValid = true;
    if (doValidate) {
        if (typeof (Page_ClientValidate) == 'function') {
            // Validator tests
            isValid = Page_ClientValidate();
        }
    }

    if (isValid) {
        // div tag impl. i browser.aspx
        var obj = document.getElementById("freezePage");
        if (obj) {
            obj.style.display = 'inline';
            obj.style.cursor = 'wait';
        }
    }
}

function URLEncode(plaintext) {
    var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ" + "abcdefghijklmnopqrstuvwxyzæøå" + "-_.!~*'()";
    var HEX = "0123456789ABCDEF";

    var encoded = "";
    for (var i = 0; i < plaintext.length; i++) {
        var ch = plaintext.charAt(i);
        
        if (ch == " ") {
            encoded += "+";
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    }

    return encoded;
};

function SearchSite(inputfield, resultPage) {
    if (inputfield.value == 'Søg på alka.dk') {
        inputfield.value = '';
    }
    document.location.href = 'http://' + location.hostname + resultPage + '?cx=009775185140443979295:txhh2ofl0ys&cof=FORID%3A9;NB%3A1&ie=UTF-8&q=' + URLEncode(inputfield.value) + '&sa=Search';
}

location.querystring = (function () {
    var queryStringDictionary = {};
    var querystring = decodeURI(location.search);
    if (!querystring) {
        return {};
    }

    querystring = querystring.substring(1);
    var pairs = querystring.split("&");


    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        queryStringDictionary[keyValuePair[0]]
                = keyValuePair[1];
        
    }

    queryStringDictionary.toString = function () {

        if (queryStringDictionary.length == 0) {
            return "";
        }

        var toString = "?";

        for (var key in queryStringDictionary) {
            toString += key + "=" +
                queryStringDictionary[key];
        }

        
        return toString;
    };

    return queryStringDictionary;
})();


dw_Tooltip.defaultProps = {
    content_source: 'class_id',
    hoverable: true
};
dw_Tooltip.writeStyleRule();

