var background = null;
var dialog = null;
var okButton = new button('OK', 'hideDialog(); showBackground();');
var cancelButton = new button('Cancel', 'hideDialog(); showBackground();');

window.onresize = sizeFix;

function button(caption, event)
{
  this.caption = caption;
  this.event = event;
}

function fadeBackground(colour, alpha)
{
  tag = document.getElementsByTagName('select');
  for (i=tag.length-1; i>=0; i--)
    tag[i].style.visibility = 'hidden';
    
  if (background == null)
  {  
    background = document.createElement('div');
    background.style.visibility = 'hidden';
    background.style.background = colour;
    background.style.opacity = alpha; // firefox/safari
    if (background.style.setAttribute) background.style.setAttribute('filter', 'alpha(opacity='+alpha*100+')'); // internet explorer
    background.style.position = 'absolute';
    document.body.appendChild(background);
  }
  background.style.top = '0px';
  background.style.left = '0px';
  background.style.height = Math.max(getPageHeight(), getWindowHeight()) + 'px';
  background.style.width = Math.max(getPageWidth(), getWindowWidth()) + 'px';
  background.style.visibility = 'visible';
}

function showBackground()
{
  background.style.visibility = 'hidden';
  
  tag = document.getElementsByTagName('select');
  for (i=tag.length-1; i>=0; i--)
    tag[i].style.visibility = 'visible';
}

function isBackgroundFaded()
{
  return (background != null) && (background.style.visibility == 'visible');
}

function showDialog(title, contents, buttons)
{
  if (dialog == null)
  {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialog.className = 'dialog';
    document.body.appendChild(dialog);
  }
  if (buttons.length == 0)
    buttons.push(okButton);
  
  var window = '<table cellpadding="0" cellspacing="0" class="window">'+
                 '<tr class="header">'+ // style="cursor: move;" onMouseDown="dragStart(event, \'dialog\')"
                   '<td>'+title+'</td>'+
                   '<td align="right" valign="bottom"><img src="images/closeWindow.gif" height="21" width="21" style="cursor: pointer;" onClick="hideDialog(); showBackground();" /></td>'+
                 '</tr>'+
                 '<tr>'+
                   '<td colspan="2" class="content">'+contents+'</td>'+
                 '</tr>'+
                 '<tr>'+
                   '<td colspan="2" align="right" class="buttons">';
  for (var a=0; a<=buttons.length-1; a++)
    window += '<input type="button" class="button" value="'+buttons[a].caption+'" onclick="'+buttons[a].event+'" /> ';
  window +=        '</td>'+
                 '</tr>'+
               '</table>';
  dialog.innerHTML = window;
  dialog.style.top = getScrolledY() + (getWindowHeight() - dialog.clientHeight)/2 + "px";
  dialog.style.left = getScrolledX() + (getWindowWidth() - dialog.clientWidth)/2 + "px";
  dialog.style.visibility = 'visible';
}

function hideDialog()
{
  dialog.style.visibility = 'hidden';
  dialog.innerHTML = '';
}

function isShowingDialog()
{
  return (dialog != null) && (dialog.style.visibility == 'visible');
}

function sizeFix()
{
  if (background != null)
  {
    background.style.height = Math.max(getPageHeight(), getWindowHeight()) + 'px';
    background.style.width = Math.max(getPageWidth(), getWindowWidth()) + 'px';
  }
  if (dialog != null)
  {
    dialog.style.top = getScrolledY() + (getWindowHeight() - dialog.clientHeight)/2 + "px";
    dialog.style.left = getScrolledX() + (getWindowWidth() - dialog.clientWidth)/2 + "px";
  }
}