var _background = null;
var _dialog = null;
var _dialog_c = null;

function dialog_show_background()
   {
   var p = document.createElement("DIV");
   var dim = getDocDim();

   p.id = 'dialog_background';
   p.style.width = dim[0];
   p.style.height = dim[1] + dim[2];

   if (document.all)   // ie
      {
      p.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/dialog_background.png', sizingMethod='scale')";
      }
   else         // non-ie
      {
      p.style.backgroundImage = 'url(images/dialog_background.png)';
      }

   document.body.appendChild(p);

   _background = p;

   self.onResize = function()
      {
      dialogueResize(dim[0], dim[1], p.id);
      }
   self.onscroll = function()
      {
      dialogueResize(dim[0], dim[1], p.id);
      }
   window.onscroll = function()
      {
      dialogueResize(dim[0], dim[1], p.id);
      }
   }

function dialog_tell(title, html, delay, container)
   {
   var html_n = "";
   html_n += '<DIV ID="dialog_close"><A onClick="dialog_hide()">X</A></DIV>';

   if (title)
      {
      html_n += '<SPAN CLASS="dialog_title">' + title + '</SPAN>';
      }

   html_n += '<DIV CLASS="dialog_rule"></DIV>';
   html_n += '<P>';
   html_n += html;
   html_n += '</P>';
   html_n += '<P>';
   html_n += '<span class="dialog_instruction">(This window will close automatically after ' + delay + ' seconds)<span>';
   html_n += '</P>';

   var p = document.createElement("DIV");
   p.id = 'dialog';
   p.innerHTML = html_n;

   p.style.visibility = 'hidden';

   if (container)
      {
      container.appendChild(p);

      container.style.height = p.clientHeight + 20;

      _dialog = p;
      _dialog_c = container;
      }
   else
      {
      dialog_show_background();

      document.body.appendChild(p);

      var dim = getDocDim();
      p.style.left = (dim[0] - p.clientWidth) / 2;
      p.style.top = ( (dim[1] - p.clientHeight) / 2) + dim[2];

      _dialog = p;
      }

   p.style.visibility = 'visible';

   setTimeout("dialog_hide();", delay * 1000);
   }

function dialog_ask(title, html, container, oncloseCode, elementToReceiveFocus)
   {
   var html_n = "";
   html_n += '<DIV ID="dialog_close"><A onClick="dialog_hide();' + oncloseCode + '">X</A></DIV>';

   if (title)
      {
      html_n += '<SPAN CLASS="dialog_title">' + title + '</SPAN>';
      }

   html_n += '<DIV CLASS="dialog_rule"></DIV>';
   html_n += '<P>';
   html_n += html;
   html_n += '</P>';

   var p = document.createElement("DIV");
   p.id = 'dialog';
   p.innerHTML = html_n;

   p.style.visibility = 'hidden';

   if (container)
      {
      container.appendChild(p);

      container.style.height = p.clientHeight + 20;

      _dialog = p;
      _dialog_c = container;
      }
   else
      {
      dialog_show_background();

      document.body.appendChild(p);

      var dim = getDocDim();
      p.style.left = (dim[0] - p.clientWidth) / 2;
      p.style.top = ( (dim[1] - p.clientHeight) / 2) + dim[2];

      _dialog = p;
      }

   // IE requires that the element be visible before attempting to set focus
   p.style.visibility = 'visible';

   // set focus on the given element (if any)
   if (elementToReceiveFocus)
      {
      var element = document.getElementById(elementToReceiveFocus);
      if (element)
         {
         element.focus();
         element.select();
         }
      }
   }

function dialog_hide(callback)
   {
   self.onResize = null;
   self.onscroll = null;
   window.onscroll = null;

   if (_dialog_c)
      {
      _dialog_c.removeChild(_dialog);
      _dialog_c.style.height = "0px";

      _dialog = null;
      _dialog_c = null;
      }

   if (_dialog)
      {
      document.body.removeChild(_dialog);
      _dialog = null;
      }

   if (_background)
      {
      document.body.removeChild(_background);
      _background = null;
      }

   document.body.style.overflow = "";

   if (callback)
      {
      eval(callback);
      }
   }

function getDocDim()
   {
   var a = new Array();
   // 0:x, 1:y, 2:yScroll
   if (self.innerHeight) // all except Explorer
      {
      a[0] = self.innerWidth;
      a[1] = self.innerHeight;
      }
   else if (document.documentElement && document.documentElement.clientHeight)   // Explorer 6 Strict Mode
      {
      a[0] = document.documentElement.clientWidth;
      a[1] = document.documentElement.clientHeight;
      }
   else if (document.body) // other Explorers
      {
      a[0] = document.body.clientWidth;
      a[1] = document.body.clientHeight;
      }
   if (self.pageYOffset)
      { // all except Explorer
      a[2] = self.pageYOffset;
      }
   else if (document.documentElement && document.documentElement.scrollTop)
      { // Explorer 6 Strict
      a[2] = document.documentElement.scrollTop;
      }
   else if (document.body)
      { // all other Explorers
      a[2] = document.body.scrollTop;
      }
   return a;
   }

function dialogueResize(idealWidth, idealHeight, soId)
   {
   var a = document.getElementById(soId);
   if (a.style.display != "none")
      {
      var dim = getDocDim();
      a = document.getElementById(soId);
      a.style.top = (dim[2] + Math.max((dim[1] - idealHeight) / 2, 5)) + "px";
      a.style.left = (dim[0] - idealWidth) / 2 + "px";
      }
   }

