var curpopup = "";
function togglePopup(popup, btn, state) // 1 visible, 0 hidden
{
    var popupobj = document.getElementById(popup);
    var btnobj = document.getElementById(btn);

    if (state == 1)
    {
        var wantleft = 0;
        var wanttop = 0;
        var obj = btnobj;

        if (curpopup != "")
        {
            togglePopup(curpopup, "", 0);
        }

        curpopup = popup;

        if (obj.offsetParent)
        {
            do
            {
                wantleft += obj.offsetLeft;
                wanttop += obj.offsetTop;
            }
            while (obj = obj.offsetParent);
        }
    
        wantwidth = 300;
    
        if (wantleft + wantwidth > document.width)
        {
          if (document.width < wantwidth)
          {
              wantwidth = document.width;
              wantleft = 0;
          }
          else
          {
              wantleft = document.width - wantwidth;
          }
        }
    
        popupobj.style.width = wantwidth;
        popupobj.style.top = wanttop;
        popupobj.style.left = wantleft;
        popupobj.style.visibility = "visible";
    }
    else
    {
        curpopup = "";
        popupobj.style.visibility = "hidden";
        // popupobj.style.width = "0px";
    }
}
