// Constant
var __divCoverDLG   = "__divCoverDLG";
var __divContentDLG = "__divContentDLG";
var __headerDLG     = "__headerDLG";
var __contentDLG    = "__contentDLG";
var __okDLG         = "__okDLG";
var __cancelDLG     = "__cancelDLG";
// Variables
var dlgBox = null;
var DialogBox = function(){};
var _fncOK = null;
var _fncCancel = null;
var dialog_ID = null;

DialogBox.prototype =
{
    // When OK button is pressed 
    clickOK: function() {
        // Hide dialog
        this.hideDialog(); 
        
        if (_fncOK){
            // Call function
            eval(_fncOK);
        }
    },

    // When Cancel button is pressed 
    clickCancel: function() {
        // Hide dialog
        this.hideDialog();
            
        if (_fncCancel){
            // Call function
            eval(_fncCancel);
        }
    },

    // Cover screen. Only use for dialog 
    screenCover: function(){
        // Get handle
        objCover = core.getObject(__divCoverDLG);
        if (objCover)
        {
            objCover.style.left = 0;
	        objCover.style.top  = 0;
            objCover.style.width = "100%";
            objCover.style.height = "100%";
            objCover.style.minHeight = "100%";
	        objCover.style.display = "block";            
        }
    },

    // UnCover screen. Only use for dialog 
    screenUnCover: function(){
        // Get handle
        objCover = core.getObject(__divCoverDLG);
        if (objCover)
        {
	        objCover.style.display = "none";  
	        objCover.style.height = "0px";          
        }
    },
    
    // Show dialog
    // fncOK: function to process when OK button is pressed
    // fncCancel: function to process when Cancel button is pressed
    // typeOfMessage: 1->OK & Cancel, 2->OK only, 3->none button
    showDialog: function(title, message, fncOK, fncCancel, typeOfMessage){
        // Get handle functions
        _fncOK = fncOK;
        _fncCancel = fncCancel;
        
        // Fill title and content
        core.getObject(__headerDLG).innerHTML = title; 
        core.getObject(__contentDLG).innerHTML = message;       
        
        // Show buttons
        core.getObject(__okDLG).style.display="inline";
        if (!typeOfMessage || typeOfMessage == 1)
        {
            core.getObject(__cancelDLG).style.display="inline";            
        }else if(typeOfMessage == 2){
            core.getObject(__cancelDLG).style.display="none";                        
        }else{
            core.getObject(__okDLG).style.display="none"; 
            core.getObject(__cancelDLG).style.display="none"; 
        }
        
        // Cover screen
        this.screenCover();
        // Show dialog
        core.getObject(__divContentDLG).style.display="block";
        core.getObject(__okDLG).focus();
        
        // Adjust to center
        adjustToCenterScreen(__divContentDLG);          
    },
    
    // Hide dialog. Only use with showDialog function    
    hideDialog: function(){
        // Uncover screen
        this.screenUnCover();
        // Hide dialog
        core.getObject(__divContentDLG).style.display="none";          
    },
    
    // Show dialog, which was designed already
    showDivAsDialog: function(dlgDiv, width, height){
        // Cover screen
        this.screenCover();
        // Show dialog
        core.getObject(dlgDiv).style.display="block";
        dialog_ID = dlgDiv;
        // Adjust to center
        adjustToCenterScreen(dlgDiv, width, height);   
    },
    
    // Hide dialog. Only use with showDivAsDialog function
    hideDivAsDialog: function(dlgDiv){
        // Uncover screen
        this.screenUnCover();
        // Hide dialog
        core.getObject(dlgDiv).style.display="none";        
    }
}

dlgBox = new DialogBox();
addEvent(document,"keydown",fnTrapESC);
function fnTrapESC(e)
{
  if(window.event)
    {
        key = window.event.keyCode; //IE
        if(key==27)
        {
           if (core.getObject(dialog_ID)==null || core.getObject(dialog_ID).style.display=='none')
           {
               dlgBox.clickCancel();
           }
           else
           {
              dlgBox.hideDivAsDialog(dialog_ID);
           }
		   
		}
    }
    else
    {
        key = e.which; //firefox
        if(key==27)
        {
		   if (core.getObject(dialog_ID)==null || core.getObject(dialog_ID).style.display=='none')
           {
                dlgBox.clickCancel();
           }
           else
           {
                dlgBox.hideDivAsDialog(dialog_ID);
           }
        }
    }
}