function CloseDialog()
{
    var lb,fd;
    lb = document.getElementById('_lightbox');
    fd = document.getElementById('_fade');

    if(lb == null)
    {
        var doc;
        doc = getDoc(window.frameElement);

        lb = doc.getElementById('_lightbox');
        fd = doc.getElementById('_fade');

        /* this works in IE */
        /*
        lb = window.frameElement.parentElement.document.getElementById('_lightbox');
        fd = window.frameElement.parentElement.document.getElementById('_fade');
        */
    }
    if(lb==null) alert("cannot find lightbox");
    if(fd==null) alert("cannot find fade");
    lb.style.display = 'none';
    fd.style.display = 'none';
}

function getDoc(node)
{
    return getParent(node, "#document");
}

function getParent(node, name)
{
    var el = node;
    while(el != null)
    {
        if(el.nodeName == name)
            return el;

        if(el.parentNode == null)
            return el;

        el = el.parentNode;
    }
    return null;
}

function defaultButtonClicked(event)
{
    var arr, keynum;
    if(window.event) // IE
    {
        keynum = event.keyCode;
    }
    else if(event.which) // Netscape/Firefox/Opera
    {
        keynum = event.which;
    }
    if(keynum == 13)
    {
        arr = document.getElementById("_dialogButton")
        if(arr != null)
        {
            if(arr.length == null)
            {
                if(info.name == "MSIE")
                    arr.click();
                else
                    arr.onclick();
            }
            else
            {
                if(info.name == "MSIE")
                    arr[0].click();
                else
                    arr[0].onclick();
            }
        }
    }
}

function getButtons(buttons)
{
    var arr,n,html,parts,img,onclick;
    html = "";
    arr = buttons.split('\t');
    for(n=0; n<arr.length; n++)
    {
        parts = arr[n].split('=');
        if(html != "")
            html += "&nbsp;&nbsp;";

        img = parts[0];
        if(parts.length >= 2)
            onclick = parts[1];
        else
            onclick = "";

        if(onclick.substr(onclick.length-1,1) != ";")
            onclick += ";";
        
        if(img.substr(0, 9) == "resources")
        {
            html += "<img id='_dialogButton' src=\"" + img + "\" onclick=\"" + onclick + "CloseDialog();\" class=\"button\"/>";
        }
        else
        {
            if(img.substr(img.length-4, 4).toLowerCase() == ".gif")
            {
                html += "<img id='_dialogButton' src=\"/travel/resources/com.wth.wicket.common.BaseImageButton.BaseImageButton/images/" + img + "\" onclick=\"" + onclick + "CloseDialog();\" class=\"button\"/>";
            }
            else
            {
                html += "<a id='_dialogButton' href=\"javascript:void(0)\" onclick=\"" + onclick + "CloseDialog();\">" + img + "</a>";
            }
        }
    }
    return html;
}

function OpenDialog(html, height, width, buttons)
{
    PopFrame(html, height, width, buttons);
}

function Alert(message, height, width, buttons)
{
    Alert(message, height, width, buttons, null)
}

function Alert(message, height, width, buttons, icon)
{
    var html;
    html = "";
    html += "<div class='height100pct' style='float:left'>";
    html += "  <table cellspacing='5' cellpadding='0' class='height100pct'><tr><td valign='middle'>";

    if(icon)
    {
        html += "    <img src='" + icon + "' alt='Error'>";
    }
    else
    {
        html += "    <img src='/travel/resources/com.wth.wicket.common.BaseImageButton.BaseImageButton/images/warning.gif' alt='Error'>";
    }
    html += "  </td></tr></table>";
    html += "</div>";
    html += "<div class='font11pt' style='overflow-y:auto;overflow-x:auto;height:" + (height - 75) + "px;'>";
    html += "        <table cellspacing='5' cellpadding='0' class='height100pct width100pct'><tr><td class='font11pt' valign='middle'>";
    html += message;
    html += "        </td></tr></table>";
    html += "</div>";

    PopFrame(html, height, width, buttons);
}

function PopFrame(html, height, width, buttons)
{
    var top,left;
    var lightbox, lightbox_content, curtain;
    lightbox = document.getElementById('_lightbox');
    lightbox_content = document.getElementById('_lightbox_content');
    curtain = document.getElementById('_fade');

    var pg;
    pg = document.getElementById("pageBody");
    curtain.style.top = "0px";
    curtain.style.left = "0px";
    curtain.style.width = window.top.document.body.clientWidth + "px";
    curtain.style.height = (pg.offsetHeight + 25) + "px";

    top = (window.document.body.scrollTop + (window.top.document.body.clientHeight / 2)) - (height / 2);
    if(top < 0) top = 0;
    left = (window.top.document.body.clientWidth / 2) - (width / 2);
    if(left < 0) left = 0;
    if(height > window.top.document.body.clientHeight)
        height = window.top.document.body.clientHeight;
    if(width > window.top.document.body.clientWidth)
        width = window.top.document.body.clientWidth;

    // make somre browser specific adjustments (info is set in browser.js)
    /*     this is being left here because i'm not sure if we will need it in the future
    if(info.name != "MSIE")
    {
        width -= 15;
        height -= 20;
    }
    */

    lightbox.style.top = top + "px";
    lightbox.style.left = left + "px";
    lightbox.style.width = width + "px";
    lightbox.style.height = height + "px";

    html = "<table id='_dialogTable' cellpadding='5' cellspacing='0' border='0' width='100%' height='100%' onkeypress='defaultButtonClicked(event);'><tr><td width='100%' height='100%'>" + html + "</td></tr><tr><td align='right' style='padding-top:5px;height:20px;'>" + getButtons(buttons) + "</td></tr></table>";
    lightbox_content.innerHTML = html;

    lightbox.style.display='block';
    curtain.style.display='block';
    document.getElementById("_dialogTable").focus();
}

