// IE6 doesn't grok 'const'
var tooltip_ofsX = 10;
var tooltip_ofsY = -20;
var tooltip_time = 700;

var gTooltipTimer = null;

if( typeof top.gStatusMsgTimer == "undefined" )
    top.gStatusMsgTimer = null;

function getEvtX(e)
{
    if(!e) e = event;
    if(e.pageX) return e.pageX;

    // Hack for IEs
    var X = e.clientX;
    if(document.body && document.body.scrollLeft)
        X += document.body.scrollLeft;
    if(document.documentElement && document.documentElement.scrollLeft)
        X += document.documentElement.scrollLeft;
    return X;
}

function getEvtY(e)
{
    if(!e) e = event;
    if(e.pageY) return e.pageY;

    // Hack for IEs
    var Y = e.clientY;
    if(document.body && document.body.scrollTop)
        Y += document.body.scrollTop;
    if(document.documentElement && document.documentElement.scrollTop)
        Y += document.documentElement.scrollTop;
    return Y;
}

/*
 * Override window.open so we can center it ourselves - bug 8145
 *  NOTE! This doesn't work in IE5Mac.  We're just changing it "for show"
 *  here, so it's not a big deal, but don't put anything important
 *  in here that could break something if it doesn't get called.
 * -davidp 2004-03-15
 */
var _stock_open;

if( ! _stock_open )
{
    _stock_open = window.open;
    window.open = function( url, name, args )
    {
        try
        {
            var width = args.match( /width=([0-9]+)/ )[1];
            var height = args.match( /height=([0-9]+)/ )[1];
            if( width && height && !args.match( /(screenX|screenY|left|top)=/ ) )
            {
                var sx = (screen.availWidth/2) - (width/2);
                var sy = (screen.availHeight/2) - (height/2);
                args += ',screenX='+sx+',screenY='+sy
                    +',left='+sx+',top='+sy;
            }
        }
        catch(ex) { }
        return _stock_open( url, name, args );
    };
}

function popupwindow(url, height, width, resizable )
{
    if( height == 0 )
        height = "400";

    if( width == 0 )
        width = "600";

    if( resizable != "no" )
        resizable = "yes";

    var mywindow = window.open(url, 'Tracker', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable='+resizable+',width='+width+',height='+height+'');
    if( mywindow )
    {
        mywindow.location = url;
        mywindow.focus();
    }
}

function showTooltip( aId, e )
{
    if(!e) e = event;
    var el = document.getElementById(aId);
    if(!el) return;

    if(gTooltipTimer)
        clearTimeout(gTooltipTimer);
    gTooltipTimer = setTimeout( "realShowTooltip('"+aId+"', "+getEvtX(e)+", "+
        getEvtY(e)+")", tooltip_time);
    return;
    /*
    el.style.display = "";
    el.style.left = (getEvtX(e)+tooltip_ofsX) + "px";
    el.style.top = (getEvtY(e)+tooltip_ofsY) + "px";
    return;
    */
}

function realShowTooltip( aId, aX, aY )
{
    if( !(aId && aX && aY) ) return;
    var el = document.getElementById(aId);
    if(!el) return;

    gTooltipTimer = null;

    var tX = Math.max( aX+tooltip_ofsX, 0 );
    var tY = Math.max( aY+tooltip_ofsY, 0 );

    el.style.display = "";
    el.style.left = tX + "px";
    el.style.top = tY + "px";
    //alert( "tX="+tX+" tY="+tY );
}

function hideTooltip( aId )
{
    var el = document.getElementById(aId);
    if(!el) return;

    clearTimeout(gTooltipTimer);
    gTooltipTimer = null;
    
    el.style.display = "none";
    return;
}

function showTooltipText( aText, e )
{
    if(!aText) return;
    if(!e) e = event;

    var el = document.getElementById("textTooltip");
    if(!el) return;

    while(el.hasChildNodes())
        el.removeChild(el.firstChild);

    var arr = aText.split(/\n/);
    for( var i=0; i<arr.length; i++ )
    {
        t = document.createTextNode(arr[i]);
        el.appendChild(t);
        if( i+1 < arr.length )
            el.appendChild( document.createElement("br") );
    }
    
    showTooltip("textTooltip", e);
}

function hideTooltipText()
{
    hideTooltip("textTooltip");
}

function showStatusMsg( aMsg )
{
    if( !(top && top.topbar && top.topbar.document) )
        return;

    // Don't let the status message get wiped out too soon
    if( !aMsg && top.gStatusMsgTimer )
        return;

    var MsgEl = top.topbar.document.getElementById('StatusMessage');
    if( !MsgEl )
        return;

    // If the current message isn't stale, just append to it
    if( !top.gStatusMsgTimer )
        while( MsgEl.hasChildNodes() )
            MsgEl.removeChild(MsgEl.lastChild);
    if( aMsg )
    {
        aMsg = aMsg.replace( /\\n/g, ' ' );

        /* IE5Mac/5.5Win don't grok appendChild... rrrgh */
        try
        {
            /* and IE5Mac doesn't even raise an exception on appendChild!
               Double rrrgh. */
            if( window.clientInformation && window.clientInformation.platform.match(/mac/i) )
                RaiseAnException;

            MsgEl.appendChild(document.createTextNode(aMsg));
        }
        catch(ex)
        {
            MsgEl.innerHTML = MsgEl.innerHTML + aMsg;
        }
        top.gStatusMsgTimer = setTimeout( "unblockStatusMsg()", 5000 );
    }
}

function unblockStatusMsg()
{
    top.gStatusMsgTimer = null;
}

/*** onclick handler for radio buttons ***
- assumes one single form, and that 'subordinate' inputs have the attribute 'for' set on them.
  Eg: <radio name="connection" value="foo">  <input for="connection.foo"> 
  When that radio is selected, anything for connection.foo is enabled,
  and anything for connection.* (for * != foo) is disabled.

  inputs for nested radios should look something like for="connection.foo ip.bar" (ie, space-separated).

  Copied from admin.js from oe-admin. Any change here should be duplicated to there.
*/
function radioClick(aEl)
{
    if( aEl.getAttribute("disabled") ) return;

    var name = aEl.name;

    var value;
    if (aEl.getAttribute("type") == "checkbox") {
        if (aEl.checked) 
            value = aEl.value;
        else
            value = "";
    } else 
        value = aEl.value;

    for(var i=0; i < document.forms[0].elements.length; i++)
    {
        var el = document.forms[0].elements[i];
        if(!el) continue;

        var elFor = el.getAttribute("for");
        if(!elFor) continue;

        var elForArr = elFor.split(" ");

        for(var j=0; j<elForArr.length; j++)
        {
            if(elForArr[j] == name+"."+value)
            {
                if(elForArr.length == 1)
                {
                    el.removeAttribute("disabled");
                }
                else
                {
                    var conds = 0;
                    for(var k=0; k<elForArr.length; k++)
                    {
                        if(k==j) continue;
                        var arr = elForArr[k].split(".");
                        var radios = document.getElementsByName(arr[0]);
                        for(var l=0; l<radios.length; l++)
                        {
                            if(radios[l].value == arr[1] && radios[l].checked)
                            {
                                conds++;
                            }
                        }
                    }
                    if(conds == elForArr.length-1)
                    {
                        el.removeAttribute("disabled");
                    }
                }
            }
            else if(elForArr[j].match( new RegExp("^"+name+"\.") ))
            {
                el.setAttribute("disabled", "true");
            }
        }
    }
}
