// A way to find out the absolute position in the page of the element owning the tooltip is needed:

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


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


var aktiv = null;
function xstooltip_show(tooltipId, parentId, posX, posY)
{
    aktiv = window.setTimeout(function() { xstooltip_popup(tooltipId, parentId, posX, posY); } , 1000);
}

// These functions find the absolute X and Y position into the HTML page of
// the element passed as an argument, recursively examining the position of
// the element's parents.
function xstooltip_popup(tooltipId, parentId, posX, posY)
{
    var it = document.getElementById(tooltipId);
    if (!it) return;

    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0)) {

        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        var img = document.getElementById(parentId); 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > img.offsetWidth)
            posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0; 
        
        var x = xstooltip_findPosX(img) + posX;
        var y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    
    it.style.visibility = 'visible'; 
}

function xstooltip_hide(id)
{
    var it = document.getElementById(id); 
    if (it) { 
        it.style.visibility = 'hidden'; 
    }
    if (aktiv) {
        window.clearTimeout(aktiv);
        aktiv = null;
    }
}


// -------------------------------------

function checkInputIsNumber(val) 
{
    if (!val.match(/^[0-9]*$/)) {
        alert("Bitte eine Zahl angeben.");
    }
}
