var dialog_object;

function dialog(title,url,body,images_directory) {
  dialog_object = this;

  this.borderColor = "#DF9E81";
  this.borderSize = "1";
  this.titleBackground = "#F5C9B6";
  this.titleForeground = "#000000";
  this.titleHeight = 20;
  this.titleText = "JavaScript dialog box";
  this.titleBorderColor = "#DF9E81";
  this.titleBorderSize = "1";
  this.background = "#F5C9B6";
  this.foreground = "#000000";
  this.bodyText = "JavaScript dialog box, place to put the body";
  this.left = eval(document.body.scrollLeft + (document.body.clientWidth - 350) / 2);
  this.top = eval(document.body.scrollTop + (document.body.clientHeight - 150) / 2);
  this.width = "350";
  this.height = "120";

  if ((title != undefined) && (title != "")) this.titleText = title;
  if ((body != undefined) && (body != "")) this.bodyText = body;
  if ((url != undefined) && (url != "")) this.url = url;
     else this.url = undefined;
     
  if (images_directory != undefined) this.dir = images_directory; else this.dir = "";

  this.back = document.createElement("div");
    this.back.style.position = 'absolute';
    this.back.style.left = 0;
    this.back.style.top = 0;
    if (document.body.scrollHeight > document.body.clientHeight) this.back.style.height = document.body.scrollHeight;
     else this.back.style.height = document.body.clientHeight;
    if (document.body.scrollWidth > document.body.clientWidth) this.back.style.width = document.body.scrollWidth;
     else this.back.style.width = document.body.clientWidth;
    this.back.style.backgroundImage = "url('"+this.dir+"/inactive.gif')";
  document.body.appendChild(this.back);

  this.div = document.createElement("div");
  this.div.style.position = 'absolute';
  this.titleDiv = document.createElement("div");
  this.div.appendChild(this.titleDiv);
  this.bodyDiv = document.createElement("div");
  this.div.appendChild(this.bodyDiv);
  document.body.appendChild(this.div);

  document.onmousemove = dialog_mousemove;
  this.titleDiv.onmousedown = dialog_mousedown;
  document.onmouseup = dialog_mouseup;

  this.Update = dialog_update;
  this.Close = dialog_close;

  this.Update();
}

function dialog_update() {
 this.div.style.left = this.left;
  this.div.style.top = this.top;
  this.div.style.width = this.width;
  this.div.style.border = "solid "+this.borderSize+"px "+this.borderColor;
  this.div.style.background = this.background;
  this.titleDiv.style.borderBottom = "solid "+this.titleBorderSize+"px "+this.titleBorderColor;
  this.titleDiv.style.background = this.titleBackground;
  this.titleDiv.style.height = this.titleHeight;
  this.bodyDiv.style.height = this.height;
  if ((this.url != undefined) && (this.url != ""))  {
      this.bodyDiv.innerHTML = "<center><iframe name='bodyFrame' id='bodyFrame' style='width:98%; height:"+this.height+"' marginwidth='0' frameborder='0' toolbar='no' scrolling='auto' src='"+this.url+"'></iframe></center>";
      if (document.all) doc = document.all('bodyFrame').document;
      else doc = document.getElementById('bodyFrame');
      
      doc.onmousemove = dialog_moveframe;
      doc.onload = dialog_moveframe;
  } else this.bodyDiv.innerHTML = "<div style='margin-left:5px; margin-top:5px;'>"+this.bodyText+"</div>";

  this.titleDiv.innerHTML = "<table cellspacing=0 cellpadding=0 style='cursor:pointer; width:100%; height:100%'>" +
                       "<tr><td>" + this.titleText + "</td><td style='width:10%;' align='right' valign='middle'>" +
                       "<img src='"+this.dir+"/close_icon.gif' style='cursor:pointer;' onclick='dialog_object.Close()'></td></tr></table>";
}

function dialog_mousemove(e) {
   if (!e) e = window.event;
   if (dialog_object.div.dialog_move) {
      if (e.pageX) {
          posx = e.pageX;
	  posy = e.pageY;
      } else {
	  posx = e.clientX + document.body.scrollLeft;
	  posy = e.clientY + document.body.scrollTop;
      }
      dialog_object.div.style.left = eval(posx + dialog_object.div.dialog_x);
      dialog_object.div.style.top = eval(posy + dialog_object.div.dialog_y);
   }
}

function dialog_moveframe() {
      document.onmousemove = dialog_mousemove;
      frames['bodyFrame'].document.onmousemove = function(e) {
          dl = dialog_object.div;
          if (dl.dialog_move) {
             if (!e) e = frames['bodyFrame'].event;
             if (e.pageX) {
                posx = e.pageX;
	        posy = e.pageY;
             } else {
	        posx = e.clientX + document.body.scrollLeft;
	        posy = e.clientY + document.body.scrollTop;
             }
             dl.style.top = eval(posy + dl.dialog_y + dialog_object.titleHeight+2);
             dl.style.left = eval(posx + dl.dialog_x);
          }
      }
}

function dialog_mousedown(e) {
    if (!e) e = window.event;
    dl = dialog_object.div;
    if (((document.all) && (e.button == 1)) || ((!document.all) && (e.button == 0))) {
        dl.dialog_move = true;
        if (e.pageX) {
          posx = e.pageX;
	  posy = e.pageY;
        } else {
	  posx = e.clientX + document.body.scrollLeft;
	  posy = e.clientY + document.body.scrollTop;
        }
        dl.dialog_x = eval(dl.style.left.substring(0,dl.style.left.length - 2) - posx);
        dl.dialog_y = eval(dl.style.top.substring(0,dl.style.top.length - 2) - posy);
    }
}

function dialog_mouseup(e) {
    if (!e) e = window.event;
    if (((document.all) && (e.button == 1)) || ((!document.all) && (e.button == 0))) dialog_object.div.dialog_move = false;
}

function dialog_close() {
    if (document.all)
       if (document.all("hide1") != null) {
          if (document.all("hide1").style != null)
              document.all("hide1").style.visibility = 'visible';
          else  {
              var obj = document.all("hide1");
              for(var i=0; i<obj.length; i++) {
                  if (obj[i].style != null)
                      obj[i].style.visibility = 'visible';
              }
          }
       }
    document.body.removeChild(this.div);
    document.body.removeChild(this.back);
}
