function utf8_decode ( str_data ) {
     // Converts a UTF-8 encoded string to ISO-8859-1
     //
     // version: 810.1317
     // discuss at: http://phpjs.org/functions/utf8_decode
     // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
     // +      input by: Aman Gupta
     // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
     // +   improved by: Norman "zEh" Fuchs
     // +   bugfixed by: hitwork
     // +   bugfixed by: Onno Marsman
     // *     example 1: utf8_decode('Kevin van Zonneveld');
     // *     returns 1: 'Kevin van Zonneveld'
     var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;

     str_data += '';

     while ( i < str_data.length ) {
         c1 = str_data.charCodeAt(i);
         if (c1 < 128) {
             tmp_arr[ac++] = String.fromCharCode(c1);
             i++;
         } else if ((c1 > 191) && (c1 < 224)) {
             c2 = str_data.charCodeAt(i+1);
             tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
             i += 2;
         } else {
             c2 = str_data.charCodeAt(i+1);
             c3 = str_data.charCodeAt(i+2);
             tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
             i += 3;
         }
     }

     return tmp_arr.join('');
}

function setCookie(name, value)
{
    var arg_value = setCookie.arguments;
    var arg_length = setCookie.arguments.length;
    var expires = (arg_length > 2) ? arg_value[2] : null;
    var path = (arg_length > 3) ? arg_value[3] : null;
    var domain = (arg_length > 4) ? arg_value[4] : null;
    var secure = (arg_length > 5) ? arg_value[5] : false;
    document.cookie = name + "=" + encodeURIComponent(value) +
        ((expires == null) ? "" : ("; expires=" +
           expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}
function getCookie(name)
{
    name += "=";
    var length = name.length;
    var cookie_length = document.cookie.length;
    var i = 0;
    while (i < cookie_length)
    {
        var j = i + length;
        if (document.cookie.substring(i, j) == name)
            return getCookieValue(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
}
function getCookieValue(position)
{
    var end = document.cookie.indexOf(";", position);
    if (end == -1)
        end = document.cookie.length;
    return decodeURIComponent(
        document.cookie.substring(position, end));
}

var messages = 0;
var loaded = 0;
var sounds = getCookie("ShoutboxSounds");
function initShoutbox()
{
    var chkBox = document.getElementById("chkSounds");
    if (sounds == undefined)
    {
        sounds = 0;
    }
    if (sounds == 0)
    {
        chkBox.checked = true;
    }
    else
    {
        chkBox.checked = false;
    }
    var topumenu = getCookie("TopUserMenu");
    if (topumenu == undefined)
    {
        topumenu = 0;
    }
    if (topumenu == 1)
    {
        Element.setHeight('topusermenu', 32);
    }
}

function changeSounds()
{
    var chkBox = document.getElementById("chkSounds");
    if (sounds == 0)
    {
        sounds = 1;
        chkBox.checked = false;
        
    }
    else
    {
        sounds = 0;
        chkBox.checked = true;
    }
    var now = new Date();
    var y = now.getFullYear() + 1;
    var then = new Date(y, 1, 1, 1, 0, 0);
    setCookie("ShoutboxSounds", sounds, then);
}

 

function shoutins(NewCode)
{
    if (document.shoutbox.shouttext.createTextRange && document.shoutbox.shouttext.caretPos) {
        var caretPos = document.shoutbox.shouttext.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
        document.selection.createRange().select();
    }
    else
    {
        if (document.shoutbox.shouttext.setSelectionRange && browser.isNS)
        {
            var pos1 = document.shoutbox.shouttext.selectionStart;
            var pos2 = document.shoutbox.shouttext.selectionEnd;
            var etext = document.shoutbox.shouttext.value;
            var textBefore = etext.substring(0, pos1);
            var textAfter = etext.substring(pos2, etext.length);
            var textIn = etext.substring(pos1, pos2);
            if (textIn != "")
            {
                NewCode = NewCode.replace("Text", textIn);
            }
            var newtext = textBefore + NewCode + textAfter;
            document.shoutbox.shouttext.value=newtext;
            document.shoutbox.shouttext.selectionStart = pos1;
            document.shoutbox.shouttext.selectionEnd = pos1;

        }
        else
        {
            document.shoutbox.shouttext.value+=NewCode;
        }
    }
}

function countChars(text)
{
    document.getElementById("cchars").innerHTML = text.length;
}

function getContent(uid)
{
    sendRequest("get", "", 0);
}

function shoutMessage(uid)
{
    var text = document.getElementById("shout_text").value;
    document.getElementById("shout_text").value = '';
    if (text.length > 0)
    {
        sendRequest("send", text, uid);
        document.getElementById("cchars").innerHTML = 0;
    }
    else
    {
        alert("Bitte geben Sie erst eine Nachricht ein.");
        document.getElementById("shout_text").focus();
    }
    messages++;
}

function getXMLHttpRequest()
{
    var httpReq = null;
    if (window.XMLHttpRequest)
    {
        httpReq = new XMLHttpRequest();
    }
    else if (typeof ActiveXObject != "undefined")
    {
        httpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return httpReq;
}

function sendRequest(method, shout, uid)
{
    req = getXMLHttpRequest();
    if (req)
    {
        switch (method)
        {
            case "get":
            req.onreadystatechange = handleContent;
            str = "method=1";
            break;

            case "send":
            str = "method=2&uid="+uid+"&shout="+encodeURIComponent(shout);
            break;
            
            case "empty":
            str = "method=3&uid="+uid;
            break;
        }
        req.open("post", "shoutbox.php", true);
        req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded");
        req.setRequestHeader("Content-length", str.length);
        req.setRequestHeader("Connection", "close");
        req.send(str);
    }
}

function handleContent()
{
    if (req.readyState == 4)
    {
        var xml = req.responseText;
        var xmlDOM;

        if (typeof ActiveXObject != "undefined")
        {
            xmlDOM = new ActiveXObject("Microsoft.XmlDom");
            xmlDOM.loadXML(xml);
        }
        else
        {
            var parser = new DOMParser();
            xmlDOM = parser.parseFromString(xml, "text/xml");
        }
        var array = xmlDOM.getElementsByTagName('row');
        var output = new Array(array.length);
        for(var a=0;a<array.length;a++)
        {
            output[a] = new Array(array[a].childNodes.length);
            for(var i=0;i<array[a].childNodes.length;i++)
            {
                var n = xmlDOM.getElementsByTagName(array[a].childNodes[i].nodeName)[a];
                output[a][i] = n.childNodes[0].nodeValue;
            }
        }
        createContent(output);
    }
}

function createContent(array)
{
    var div = document.getElementById("shoutbox_content");
    div.innerHTML = '';
    var a = 0;
    if (array.length < 10)
    {
        var z = array.length;
    }
    else
    {
        var z = 10;
    }
    for (var i = 0; i < z;i++)
    {
        var ti = 'shout' + i;
        var ntr = document.createElement("div");
        ntr.setAttribute("id", ti);
        if (a == 0)
        {
            a = 1;
            ntr.setAttribute("class", "shout1");
        }
        else
        {
            a = 0;
            ntr.setAttribute("class", "shout2");
        }
        div.appendChild(ntr);
        var tr = document.getElementById(ti);
        var str = '<strong>' + array[i][0] + ' ' + array[i][2] + ':</strong><br />';
        str += array[i][1];
        str = str.replace(/:-\)/, "<img src='images/icons/emo_3.gif' border='0px' alt=':-)' />");
        str = str.replace(/;-\)/, "<img src='images/icons/emo_7.gif' border='0px' alt=';-)' />");
        str = str.replace(/:-\(/, "<img src='images/icons/emo_9.gif' border='0px' alt=':-(' />");
        str = str.replace(/:-\|/, "<img src='images/icons/emo_6.gif' border='0px' alt=':-|' />");
        str = str.replace(/:-D/, "<img src='images/icons/emo_4.gif' border='0px' alt=':-D' />");
        str = str.replace(/:-P/, "<img src='images/icons/emo_12.gif' border='0px' alt=':-P' />");
        str = str.replace(/:-O/, "<img src='images/icons/emo_15.gif' border='0px' alt=':-O' />");
        str = str.replace(/:\?\?\?:/, "<img src='images/icons/emo_16.gif' border='0px' alt='???' />");
        str = utf8_decode(str);
        tr.innerHTML = str;
    }
    if (messages < array.length)
    {
        if (loaded == 1 && sounds == 0)
        {
            parent.messageSound.location.href='shoutboxSound.php';
        }
        messages = array.length;
    }
    loaded = 1;
}

//administrative functions

function emptyShoutbox(uid)
{
    sendRequest("empty", "", uid);
    messages = 0;
}


