var ie = 0;
var agent = navigator.userAgent.toLowerCase();
var isMac = (agent.indexOf('mac') > -1) ? true : false;

if (navigator.appName.indexOf("Explorer") > -1) {
    if (navigator.appVersion.indexOf("MSIE 5") > -1) {
        ie = 5
    }
    else {
        if (navigator.appVersion.indexOf("MSIE 3") > -1) {
            ie = 3
        }
        else {
            ie = parseInt(navigator.appVersion.substring(22, 23)) ?parseInt(navigator.appVersion.substring(22, 23)) : 0
        }
    }
}

var openedPopups = new Array();

var defaults = new Array();
defaults['url'] = "";
// Default-URL: leeres fenster (was z.B. mit Javascripts fenster.document.write() gefuellt werden kann
defaults['name'] = "popup";
// Default-Name des Popups
defaults['width'] = 600;
// Default-Breite
defaults['height'] = 400;
// Default-Hoehe

function openPopup(url, name, w, h, switches, x, y) {

    if (!url) {
        url = defaults['url']
    }

    if (!name) {
        name = defaults['name']
    }

    if (!w) {
        w = defaults['width']
    }
    else {
        w = parseInt(w)
    }
    // in Integer umwandeln
    if (isMac && ie && ie < 5) {
        w -= 20
    } // auf dem Mac zu gross

    if (!h) {
        h = defaults['height']
    }
    else {
        h = parseInt(h)
    }
    // in Integer umwandeln
    if (ie && ie < 5) {
        h -= 20
    } // der Explorer 4- macht das Fenster zu gross

    if (switches && switches.indexOf("f") > -1 && window.screen) { // wenn Schalter 'f' und screen-Objekt vorhanden
        w = window.screen.availWidth;
        h = window.screen.availHeight;
    }

    var xpos;
    // keine Defaultposition
    var ypos;
    var xInt = parseInt(x);
    // vermeidet Fehlermeldungen im IE3 wenn x und y nicht gesetzt
    var yInt = parseInt(y);
    if (xInt > -1 && yInt > -1) { // an best. Position x/y (Koordinaten linker oberer Fensterecke)
        xpos = xInt;
        ypos = yInt;
    }
    else {
        if (switches && window.screen) { // wenn Schalter und screen-Objekt vorhanden
            if (switches.indexOf("c") > -1) { // wenn Schalter 'c' = centered
                xpos = parseInt((window.screen.availWidth - w) / 2);
                ypos = parseInt((window.screen.availHeight - h) / 2);
            }
            if (switches.indexOf("f") > -1) { // wenn Schalter 'f' = fullscreen
                xpos = 0;
                ypos = 0;
            }
        }
    }

    var params = getPopupParams(w, h, switches, xpos, ypos);

    if (switches && switches.indexOf('a') > -1 && window.close) {
        closePopups();
        // alle offenen Popups schliessen
    }

    var newwin = window.open(url, name, params);

    if (switches && switches.indexOf('b') > -1 && window.moveTo && window.resizeTo && window.focus) { // wenn Schalter 'b' (borderless) sowie Methoden moveTo, resizeTo und focus vorhanden
        newwin.blur();
        // Fenster verlassen
        newwin.opener.focus();
        // Herkunftsfenster nach vorne bringen
        newwin.resizeTo(w, h)
        newwin.moveTo(xpos, ypos)
    }

    if (window.focus) {
        newwin.focus()
    }

    openedPopups[name] = newwin;

}

function getWindow(winname) {
    if (!winname || !openedPopups[winname] || openedPopups[winname].closed) {
        return false
    } // wenn parameter nicht vorhanden oder Array-Eintrag nicht existiert: mit falsch raus
    else {
        return openedPopups[winname]
    }
    // sonst: gebe Referenz auf Fenster zurueck
}

function showWindows() {
    var msg = "";
    for (var key in openedPopups) {
        if (openedPopups[key].closed) {
            msg += key + ": \n"
        }
        else {
            msg += key + ": " + openedPopups[key] + "\n"
        }
    }
    alert(msg)
}

function closePopups() {
    if (ie && ie < 4) {
        return
    } // raus wenn IE3, sonst Fehlermeldungen
    if (window.close) { // wenn close-Methode vorhanden
        var popup, win;
        for (popup in openedPopups) {
            win = openedPopups[popup];
            if (win.close && !win.closed) {
                win.close();
                // Popup schliessen
                openedPopups[popup] = "";
                // Referenz loeschen
            }
        }
    }
}

function getPopupParams(w, h, switches, xpos, ypos) {

    var width = "width=" + w;
    var height = ",height=" + h;
    var parent = "";
    // z.Zt. nur NN4+
    var dirbar = "";
    var fullscreen = "";
    // z.Zt. nur IE3+
    var hotkeys = "";
    // z.Zt. nur NN4+
    var locbar = "";
    var menubar = "";
    var resizable = "";
    var scrollbars = "";
    var statusbar = "";
    var toolbar = "";
    if (switches) {
        if (switches.indexOf("p") > -1) {
            parent = ",dependent"
        }
        if (switches.indexOf("d") > -1) {
            dirbar = ",directories"
        }
        if (switches.indexOf("h") > -1) {
            hotkeys = ",hotkeys=no"
        }
        if (switches.indexOf("l") > -1) {
            locbar = ",location"
        }
        if (switches.indexOf("m") > -1) {
            menubar = ",menubar"
        }
        if (switches.indexOf("r") > -1) {
            resizable = ",resizable"
        }
        if (switches.indexOf("s") > -1) {
            scrollbars = ",scrollbars"
        }
        if (switches.indexOf("u") > -1) {
            statusbar = ",status"
        }
        if (switches.indexOf("t") > -1) {
            toolbar = ",toolbar"
        }
        if (switches.indexOf("k") > -1 || switches.indexOf("b") > -1) {
            fullscreen = ",fullscreen=1"
        }
    }

    var pos = "";
    var xposInt = parseInt(xpos);
    // vermeidet Fehlermeldungen im IE3 wenn xpos und ypos nicht gesetzt
    var yposInt = parseInt(ypos);
    if (xposInt > -1 && yposInt > -1) {
        pos = ",left=" + xposInt + ",top=" + yposInt
    }

    return width + height + parent + dirbar + hotkeys + locbar + menubar + resizable + scrollbars + statusbar + toolbar + pos + fullscreen;
}