function getSelectionFromBrowser() {
    var txt = '';
    var foundIn = '';
    if (window.getSelection)
        {
            txt = window.getSelection();
        }
    else if (document.getSelection)
        {
            txt = document.getSelection();
        }
    else if (document.selection)
        {
            txt = document.selection.createRange().text;
        }
    else return;

    txt = processDate(txt);

    txt = minusToTil(txt);

    txt = spaceAfterColon(txt);

    // ToDo - call the URL for reading instead of alert
    alert("This will call VoiceASP with the text: " + txt);
}

function minusToTil(txt) {
    var content = new String(txt);

    txt = content.replace(/(\d+)-(\d+)/g, "$1 til $2");

    return txt;
}

function spaceAfterColon(txt) {
    var content = new String(txt);

    txt = content.replace(/(\d+): /g, "$1:  ");

    return txt;
}

function processDate(txt) {
    var content = new String(txt);

    // Remove spaces
    content = content.replace(/(\d{2,4}) *[.-] *(\d{2}) *[.-] *(\d{2,4})/g, "$1.$2.$3");

    // Now leading 0's from month day
    content = content.replace(/(\d{4}[.])0?(\d{1,2}[.])0?(\d{1,2})/g, "$1$2$3");

    txt = content.replace(/0(\d{1,2}[.])0?(\d{1,2}[.])(\d{4})/g, "$1$2$3");

    return txt;
}

function DHTML_Sound(url, pakningAttr, pakning, avsnittAttr, avsnitt) {
    return DHTML_Sound(url, pakningAttr, pakning, avsnittAttr, avsnitt, null);
}

function DHTML_Sound(url, pakningAttr, pakning, avsnittAttr, avsnitt, id) {
    try {
        var startSound = true;

        if (id) {
            startSound = toggleStop(id);
        }

        var hiddenSpan = document.getElementById('soundSpan');

        if (startSound) {

            var src = url + '?' + pakningAttr + '=' + pakning;
            if (avsnitt) {
                src = src + '&' + avsnittAttr + '=' + avsnitt;
            }
            // Needed to force IE to realise that this is a media file.
            src = src + '&doc=dummy.wav';

            // Object is the correct way to go. But IE throws a security wobbly on it.
            // src = '<object data="' + src + '" width="0" height="0" type="audio/wav"></object>';

            // Embed is illegal in XHTML - but appears to work
            src = '<embed id="voiceAspSound" src="' + src + '" width="0" height="0" autostart="true" hidden="hidden"></embed>';
 
            hiddenSpan.innerHTML = src;
        } else {
            hiddenSpan.innerHTML = "&nbsp;";
        }

        return false;
    } catch (err) {
        alert("Feil ved avspilling av lyd: " + err);
    }
}

function toggleStop(id) {
    var a = document.getElementById("voiceButton_" + id);

    var aText = a.innerHTML;

    // Be warned:
    // Stopp</span> is Les avsnitt
    // Stopp </span> is Les pakningsvedlegg
    if (aText.indexOf("novoice.gif") > -1) {
        aText = aText.replace(/novoice.gif/, "voice.gif");
        if (aText.indexOf('="Stopp ') > -1) {
          a.innerHTML = aText.replace(/Stopp /g, "Les pakningsvedlegg");
        } else {
          a.innerHTML = aText.replace(/Stopp/g, "Les avsnitt");
        }
        return false;
    } else {
        aText = aText.replace(/voice.gif/, "novoice.gif");
        aText = aText.replace(/Les avsnitt/g, "Stopp");
        a.innerHTML = aText.replace(/Les pakningsvedlegg/g, "Stopp ");
        return true;
    }
}
