function change_printmode() {
    // Hmmm, not sure what case to use here...
    var stylesheetcollection = document.styleSheets || document.stylesheets;

    // Mozilla and MSIE has these...
    if(stylesheetcollection) {
        var printstylesheet;
        for(var i=0;i<stylesheetcollection.length;i++) {
            var ss = stylesheetcollection[i];
            if(ss && ss.href && (ss.href.match(/\/print_local\.css/) || ss.href.match(/\/print_local_ff\.css/))) {
                printstylesheet = ss;
                break;
            }
        }

        if(! printstylesheet) {
            return false;
        }

        // Mozilla implements stylesheet.media as a list...
        if(printstylesheet.media.item) {
            var has_screen = 0;
            for(var i=0;i<printstylesheet.media.length;i++) {
                var media = printstylesheet.media.item(i);
                if(media == 'screen') {
                    has_screen = 1;
                    break;
                }
            }

            if(has_screen) {
                printstylesheet.media.deleteMedium('screen');
            } else {
                printstylesheet.media.appendMedium('screen');
            }

            // Mozilla 2 doesn't show the change unless we do something to
            // a style object with an empty media list. The first <style>-tag
            // on the page is such a style object. Re-setting it's mediaText
            // to an empty string causes the change to be shown.
            var trickStylesheet = document.getElementById("mozillastyletrick");
            if(trickStylesheet) {
                var ssObj = trickStylesheet.sheet;
                if(ssObj) {
                    ssObj.media.mediaText = "";
                }
            }
        } else {
            // And MSIE goes here:
            if(printstylesheet.media == 'print') {
                printstylesheet.media = 'screen,print';
            } else {
                printstylesheet.media = 'print';
            }
        }

        return false;
    }

    // Opera and others goes here.

    var elem = document.getElementById('printstylesheet');

    if(! elem)
        return false;

    var media = elem.media || 'print';

    if(media == 'print') {
        elem.media = 'screen,print';
    } else {
        elem.media = 'print';
    }

    return false;
}

function start_print() {
    window.print();
    return false;
}

function open_videresend(url) {
    var url = document.location.href;
    window.open("./?videresend=1&url=" + escape(url), "videresend", "width=450,height=530,scrollbars=yes,resizable=yes");
    return false;
}

function mark_searchwords() {
    // Get string to test for searchwords from either current url or referrer.
    var test_str = '';
    if(document.location.search && document.location.search.match(/do_search=1/)) {
        test_str = document.location.search;
    } else {
        var ref = document.referrer || '';
        if(ref.match(/do_search=1/)) {
            test_str = ref;
        }
    }

    if(! test_str) {
        return;
    }

    var searchwords = new Array();
    var matches = test_str.match(/q=([^&;=]+)/);
    if(matches && matches[1]) {
        searchwords = unescape(matches[1]).match(/([\wæøåÆØÅ\*]+)/g);
    }

    if(! searchwords.length) {
        return;
    }

    for(var i=0;i<searchwords.length;i++) {
        searchwords[i] = searchwords[i].replace("*", '[\\wæøåÆØÅ*]*');
    }


    var middle = searchwords.join("\|");
    // Argh, we can't use word boundries because of danish chars, so we
    // will have to make do with multiple regexps. One for matching the whole
    // string, one for mathing start of line, one for matching end of line and
    // one for mathing words in the middle of the line
    var regexps = new Array();
    regexps[0] = new RegExp("^(" + middle + ")$", "i");
    regexps[1] = new RegExp("^(" + middle + ")(\\W*\\s+)", "i");
    regexps[2] = new RegExp("(\\s+\\W*)(" + middle + ")$", "i");
    regexps[3] = new RegExp("(\\s+\\W*)(" + middle + ")(\\W*\\s+)", "gi");

    var centerElem = document.getElementById('content');
    if(centerElem) {
        mark_searchwords_dom_recursive(regexps, centerElem);
    }
}

var tmp_div = document.createElement("div");

function mark_searchwords_dom_recursive(regexps, node) {
    var children = node.childNodes;
    for(var i=0;i<children.length;i++) {
        var child = children[i];
        if(child.nodeType == 3) {
            var text = child.nodeValue;
            if(text) {
                var new_html = text.replace(regexps[0], '<span class="searchword">$1</span>');
                new_html = new_html.replace(regexps[1], '<span class="searchword">$1</span>$2');
                new_html = new_html.replace(regexps[2], '$1<span class="searchword">$2</span>');
                new_html = new_html.replace(regexps[3], '$1<span class="searchword">$2</span>$3');
                if(new_html != text) {
                    var new_span = document.createElement("span");
                    new_span.className="searchwordmodified";
                    new_span.innerHTML = new_html;
                    child.parentNode.replaceChild(new_span, child);
                }
            }
        } else {
            mark_searchwords_dom_recursive(regexps, child);
        }
    }
}

