var net = new Object();
net.eregister = new Object();
var popup_div;

net.eregister.PopupDiv = function(inner_div, top, left, is_background_disabled, is_dragable)
{
    popup_div = this;
    this.is_background_disabled = is_background_disabled;
    this.inner_div = inner_div;
    this.top = top;
    this.left = left;
    this.is_dragable = is_dragable;
    this.drag_approved = 0;
    
    if (this.is_dragable)
    {
        this.offsetx = null;
        this.offsety = null;
        this.x = 0;
        this.y = 0;
        document.onmousedown = dragDiv;
        document.onmouseup = function()
        {
            popup_div.drag_approved = 0;
        }
    }
}

net.eregister.PopupDiv.prototype =
{
    display:function()
    {
        // Find the original div and make it visible
        var content = document.getElementById(this.inner_div);
        content.style.display = "block";
    
        // Create the banner at the top allowing exit
        var banner = document.createElement("div");
        banner.innerHTML = "<a href='#' onclick='popup_div.hide();'>Close</a>";
        banner.style.background = "#333333";

        // Create the pop-up div and include content
        var widget = document.createElement("div");
        widget.setAttribute("id", "popup_wgt");
        widget.style.position = "absolute";
        widget.style.overflow = "auto";
        widget.style.background = "white";
        widget.style.top = this.top.toString() + "px";
        widget.style.left = this.left.toString() + "px";
        widget.style.border = "1px solid";
        widget.style.padding = "1em 1em 1em 1em";
        widget.style.border.color = "#AAAAB8 #AAAAB8 #AAAAB8 #AAAAB8";
        
        widget.appendChild(banner);
        widget.appendChild(content);
        
        if (this.is_background_disabled == true)
        {
            // Create a cover div for the entire page
            var cover = document.createElement("div");
            cover.setAttribute("id", "cover_wgt");
            cover.style.position = "absolute";
            cover.style.background = "#333333";
            cover.style.filter = "alpha(Opacity=75)";
            cover.style.opacity = "0.5";
            document.body.appendChild(cover);


            cover.style.display = "";
            if (document.body.style.overflow = "hidden") 
            {
                cover.style.top = "0px";
                cover.style.left = "0px";
                if (navigator.appName == "Microsoft Internet Explorer")
                {
                    cover.style.width = document.documentElement.clientWidth;
                    cover.style.height = document.documentElement.clientHeight;
                }
                else
                {
                    cover.style.width = "100%";
                    cover.style.height = "100%";
                }
            }
        }
        document.body.appendChild(widget);
        widget.style.display = "block";
        this.hideSelects();
        
    },
    
    hide:function()
    {
        var content = document.getElementById(this.inner_div);
        content.style.display = "none";
        document.body.appendChild(content);
        document.body.removeChild(document.getElementById('popup_wgt'));
        document.body.removeChild(document.getElementById('cover_wgt'));
        this.unhideSelects();
    },
    
    hideSelects:function()
    {
        var selects = document.body.getElementsByTagName("select");
        for (var i = 0; i < selects.length; i++)
        {
            selects[i].style.display = "none";
        }
    },
    
    unhideSelects:function()
    {
        var selects = document.body.getElementsByTagName("select");
        for (var i = 0; i < selects.length; i++)
        {
            selects[i].style.display = "";
        }
    }
};
    
function dragDiv(e)
{
    var evtobj = window.event? window.event : e;
    var targetobj = window.event ? event.srcElement : e.target;
    var target_popup_div = document.getElementById('popup_wgt');

    if (targetobj.id == popup_div.inner_div || targetobj.id == "popup_wgt")
    {
        popup_div.drag_approved = 1;
        popup_div.offsetx = parseInt(target_popup_div.style.left);
        popup_div.offsety = parseInt(target_popup_div.style.top);
        popup_div.x = evtobj.clientX;
        popup_div.y = evtobj.clientY;
        if (evtobj.preventDefault)
        {
            evtobj.preventDefault();
        }
        document.onmousemove = moveDiv;
    }
}
function moveDiv(e)
{
    var evtobj = window.event? window.event : e;
    if (popup_div.drag_approved == 1)
    {
        var targetobj = document.getElementById("popup_wgt");
        targetobj.style.left = popup_div.offsetx + evtobj.clientX - popup_div.x + "px";
        targetobj.style.top = popup_div.offsety + evtobj.clientY - popup_div.y + "px";
        return false;
    }
}
