var registered_popups = new Array();
function popupAt(obj, x, y, align_x, align_y) {
    if (typeof(obj) == "string")
        obj = document.getElementById(obj);

    // If given an element instead of coordinates, translate!
    if (getPageXY) {
        if (typeof(x) == "string")
            x = document.getElementById(x);
        if (typeof(y) == "string")
            y = document.getElementById(y);
        if (typeof(x) == "object")
            x = getPageXY(x).x;
        if (typeof(y) == "object")
            y = getPageXY(y).y;
    }
    else alert("getPageXY() not found! Was 'dhtml.js' included?");

    if (typeof(align_x) == "string") {
        if (align_x == 'right')
            x -= parseInt(obj.style.width);
        else if (align_x == 'center')
            x -= parseInt(obj.style.width)/2;
    }
    if (typeof(align_y) == "string") {
        if (align_y == 'bottom')
            y -= parseInt(obj.style.height);
        else if (align_y == 'middle' || align_y == 'center')
            y -= parseInt(obj.style.height)/2;
    }

    // hide previous popups, and look for current one
    var found = false;
    for (var i = 0; i < registered_popups.length; i++) {
        hide(registered_popups[i]);
        if (obj.id == registered_popups[i])
            found = true;
    }

    // if we haven't registered this popup, do it now!
    if (!found) registered_popups[registered_popups.length] = obj.id;

    // Don't extend past the end of the page!
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    // FIXME: if width/height are known, prevent from extending past
    // right/bottom of page also.

    setPageXY(obj, x, y);
    show(obj);
    focusToInput(obj);
}
