var dlg_zindex = 100;
var dlg_active_ids = new Array();
var dlg_active_id = "";
var dlg_wait = false;
var dX = 200; 
var dY = 150; 
var X = 0; 
var Y = 0; 

document.onmousedown = dlg_mousedown;
function dlg_mousedown(e) {
   if (!e)
       e = window.event;
} 

document.onkeydown = dlg_keydown;
function dlg_keydown(e) {
   if (!e)
       e = window.event;
   if(e.which) {    
	   if(e.which == 27 && dlg_active_id.length > 0 && dlg_wait == false) 
         dlg_hide(dlg_active_id); 
   }
	else if(e.keyCode) {
	   if(e.keyCode == 27 && dlg_active_id.length > 0 && dlg_wait == false)    
         dlg_hide(dlg_active_id);
   }         
} 


function show_dlg(dlg_id, width, height) {
  if(!document.getElementById(dlg_id))
      return false;
   dlg_zindex += 1;
   document.getElementById(dlg_id).style.zIndex = dlg_zindex;
   document.getElementById(dlg_id).style.width = width + "px";
   document.getElementById(dlg_id).style.height = height + "px";
   var top = dY;
   if(window.innerWidth) {
     document.getElementById(dlg_id).style.left = Math.round((window.innerWidth / 2) - (width / 2)) + "px";
     document.getElementById(dlg_id).style.top = Math.round((window.innerHeight / 2) - (height / 2)) + "px";
     top = Math.round((window.innerHeight / 2) - (height / 2));
   }
   else {
      if(document.documentElement.clientWidth) {
         if(document.documentElement.clientWidth > 0) {
            document.getElementById(dlg_id).style.left = Math.round((document.documentElement.clientWidth / 2) - (width / 2)) + "px";
            document.getElementById(dlg_id).style.top = Math.round((document.documentElement.clientHeight / 2) - (height / 2)) + "px";
            top = Math.round((document.documentElement.clientHeight / 2) - (height / 2));
		   }
		   else {
           document.getElementById(dlg_id).style.left = dX + "px";
           document.getElementById(dlg_id).style.top = dY + "px";
		   }
      }
	   else {
	      if(document.body.clientWidth) {
           document.getElementById(dlg_id).style.left = Math.round((document.body.clientWidth / 2) - (width / 2)) + "px";
           document.getElementById(dlg_id).style.top = Math.round((document.body.clientHeight / 2) - (height / 2)) + "px";
           top = Math.round((document.body.clientHeight / 2) - (height / 2));
         }
		   else {
            document.getElementById(dlg_id).style.left = dX + "px";
            document.getElementById(dlg_id).style.top = dY + "px";
		   }
	   }
   }
   if((Y - Math.round(height / 2)) > top) 
      document.getElementById(dlg_id).style.top = (Y - Math.round(height / 2)) + "px";
   dlg_active_ids.push(dlg_id); 
   dlg_active_id = dlg_id;
   document.getElementById(dlg_id).style.display = "block";
   document.getElementById('body').className = "body_dlg";
   return true;
}

function show_dlg_and_hide(dlg_id, width, height, hide_after) {
   show_dlg(dlg_id, width, height);
   dlg_wait = true;
   document.getElementById(dlg_id).style.cursor = "wait";
   window.setTimeout('dlg_hide("' + dlg_id + '")', hide_after);
}

function dlg_hide(dlg_id) {
   if(!document.getElementById(dlg_id))
      return false;
   if(dlg_active_ids.length > 0) {
      del_i = dlg_active_ids.length - 1;
      for(i = 0; i < dlg_active_ids.length; i++)
         if(dlg_active_ids[i] == dlg_id)
		    del_i = i;
      dlg_active_ids.splice(del_i, 1);
   }	    
   dlg_active_id = "";
   dlg_wait = false;
   if(dlg_active_ids.length == 0)
      document.getElementById('body').className = "body_normal";
   else
      document.getElementById('body').className = "body_dlg";
   document.getElementById(dlg_id).style.cursor = "auto";
   document.getElementById(dlg_id).style.display = "none";
}
