function plone4radiocorePopupShow(sender, id, kwargs)
{
    if (kwargs)
    {
        for (key in kwargs)
        {
            var elem = document.getElementById(key);
            elem.value = kwargs[key];
        }
    }

    var popup = document.getElementById(id);

    // Position
    var posX = corePopupFindPosX(sender);
    var posY = corePopupFindPosY(sender);
    popup.style.left = posX + 'px';
    popup.style.top = posY + sender.offsetHeight + 1 + 'px';

    // Make visible
    popup.className = 'plone4radiocore-popup-show';
    
    // Now that it is visible we can check if it is out of bounds and reposition.
    /*
    xxx: buggy and needs work
    var width = coreGetWindowWidth();
    var height = coreGetWindowHeight();

    if ((posX + popup.clientWidth) > width)
    {
        posX = width - popup.clientWidth - 50;
        popup.style.left = posX + 'px';
    }
    if ((posY + popup.clientHeight) > height)
    {
        posY = height - popup.clientHeight - 50;
        popup.style.top = posY + 'px';
    }
    */
}

function corePopupFindPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function corePopupFindPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function _coreGetWindowDimensions(type)
{
    var myHeight = 0;
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE     
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;        
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'      
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible       
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    if (type == 'height')
       return myHeight;
    else
        return myWidth;
}

function coreGetWindowWidth()
{
    return _coreGetWindowDimensions('width');
}

function coreGetWindowHeight()
{
    return _coreGetWindowDimensions('height');
}

function getElementsByName_iefix(tag, name) 
{    
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}


