function insertMessage(type, msgText) {

    var titleText;

    if (type == "error") {
        titleText = "Fehler";
    } else if (type == "warning") {
        titleText = "Warnung";
    } else if (type == "hint") {
        titleText = "Tipp";
    } else if (type == "help") {
        titleText = "Hilfe";
    } else {
        titleText = "Info";
    }

    var div = document.createElement("div");
    div.setAttribute("className", "message");
    div.setAttribute("class", "message");


    var table = document.createElement("table");
    table.setAttribute("cellspacing", "0");
    table.setAttribute("cellpadding", "0");
    table.setAttribute("width", "100%");
    table.setAttribute("className", type);
    table.setAttribute("class", type);

    var tbody = document.createElement("tbody");

    var row = document.createElement("tr");

    var td1 = document.createElement("td");
    td1.setAttribute("className", "icon");
    td1.setAttribute("class", "icon");

    var td2 = document.createElement("td");
    /*td2.setAttribute("className", "message");
    td2.setAttribute("class", "message");*/

    var span = document.createElement("span");
    span.setAttribute("className", "title");
    span.setAttribute("class", "title");

    var title = document.createTextNode(titleText + ": ");
    //var message = document.createTextNode(msgText);

    div.appendChild(table);
    table.appendChild(tbody);
    tbody.appendChild(row);
    row.appendChild(td1);
    row.appendChild(td2);
    td1.appendChild(span);
    span.appendChild(title);
    //td2.appendChild(message);
    setInnerHTML(td2, msgText);

    if (type == "dialog") {
        var main = document.getElementsByTagName("body")[0];

        var overlappingDiv = document.createElement("div");
        overlappingDiv.setAttribute("id", "overlappingDivId");
        overlappingDiv.style.position = "absolute";
        if (navigator.userAgent.indexOf("Opera") == -1) {
            overlappingDiv.style.backgroundColor = '#fff';
        }


        overlappingDiv.style.left = "0px";
        overlappingDiv.style.top = "0px";

        if (document.all) {
            overlappingDiv.style.width = document.body.offsetWidth + 10 + "px";
            overlappingDiv.style.height = document.body.offsetHeight + 20 + "px";
        } else {
            overlappingDiv.style.width = "100%";
            overlappingDiv.style.height = "100%";
        }
        
        overlappingDiv.style.filter = "alpha(opacity=30)";
        overlappingDiv.style.opacity = 0.3;
        overlappingDiv.style.display = 'block';
        overlappingDiv.style.zIndex = "0";

        div.style.zIndex = "2";
        main.style.zIndex = "0";

        main.appendChild(overlappingDiv);
        main.appendChild(div);


    } else {
        var pos = document.getElementById("messages");
        if (pos == null) {
            pos = document.getElementById("list");
        }
        if (pos == null) {
            pos = document.getElementById("form");
        }
        if (pos == null) {
            pos = document.getElementsByTagName("body")[0];
        }

        if (pos.firstChild) {
            pos.insertBefore(div, pos.firstChild);
        } else {
            pos.appendChild(div);
        }
    }
}

function checkFirefoxVersion() {
    var r = new RegExp(/Firefox\/((\d)(.\d)*)/);
    if (r.test(navigator.userAgent)) {
        var version = parseFloat(RegExp.$1);
        if (version * 100 < 150) {
            return true;
        }
    }
    return false;
}

var innerHtmlBroken = innerHtmlBroken = checkFirefoxVersion();

function setInnerHTML(element, string) {
    string = string.replace(/\$\$\$nl\$\$\$/g, '\r\n');
    if (innerHtmlBroken && element.namespaceURI == "http://www.w3.org/1999/xhtml") {
        if (document.importNode && window.DOMParser) {
            while (element.childNodes.length > 0) {
                element.removeChild(element.firstChild);
            }
            element.appendChild(document.importNode(getAsDOM(string), true));
            return;
        }
    }
    element.innerHTML = string;
}

function getAsDOM(string) {
    var e = unserializeDocument("<x>" + string + "</x>");
    return e.documentElement.firstChild;
}

function unserializeDocument(xml)
{
    var dom;
    var parser = new DOMParser();
    dom = parser.parseFromString(xml, "text/xml");

    if (!dom.documentElement || dom.documentElement.tagName == "parsererror")
    {
        var message = dom.documentElement.firstChild.data;
        message += "\n" + dom.documentElement.firstChild.nextSibling.firstChild.data;
        throw message;
    }

    return dom;
};

