// -------------------------------------------------------------------
// DIALOG DEFINITIONS
// -------------------------------------------------------------------

// Tells if the popup is currently opened or closed
var popUpOpened = false;

// current width and height of the popup
var popUpHeight = null;
var popUpWidth = null;

// current return function for the popup
var popUpReturnFct = null;

// Popup object displayed on screen
var pi = null;

// Layer used to intercept user clicks
var pdo = null;

// Layer to display a 50% opacity  filter on the whole screen
var pwo = null;

// -------------------------------------------------------------------
// DIALOG LOW LEVEL FUNCTIONS
// -------------------------------------------------------------------

// Resets the popup position on screen
function dialog_resetPopUpPosition(){
  if(!popUpOpened){
    return;
  }

  var wHeight = 0;
  var wWidth = 0; 
  
  var sHeight = 0;
  var sWidth = 0;
  
  // We get screen information
  if (window.innerHeight){
      wHeight = window.innerHeight;
      wWidth = window.innerWidth;   
      sHeight = window.pageYOffset;
      sWidth = window.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop != null) {
      wHeight = document.documentElement.offsetHeight;
      wWidth = document.documentElement.offsetWidth;    
      sHeight = document.documentElement.scrollTop;
      sWidth = document.documentElement.scrollLeft;
  } else {
      wHeight = document.body.offsetHeight;
      wWidth = document.body.offsetWidth;   
      sHeight = document.body.scrollTop;
      sWidth = document.body.scrollLeft;  
  }
  
  // Get the popUp size information
  var pHeight = popUpHeight;
  var pWidth = popUpWidth;
  
  if(pHeight == null){
     pHeight = 110;
  } else if(pHeight > wHeight) {
     pHeight = wHeight;
  }
  
  if(pWidth == null){
     pWidth = 350;
  } else if(pWidth > wWidth) {
     pWidth = wWidth;
  }
  
  // Set the position properties
  pdo.style.width = ( pWidth + 2 ) + "px";
  pdo.style.height = ( pHeight + 2 ) + "px";
  pdo.style.top = ( sHeight + wHeight/2 - pHeight/2 - 1) + "px";
  pdo.style.left = ( sWidth + wWidth/2 - pWidth/2 - 1) + "px";
  
  pi.style.width = ( pdo.offsetWidth - 2) + "px";
  pi.style.height = ( pdo.offsetHeight - 2) + "px";
  pi.style.top = ( pdo.offsetTop + 1 ) + "px";
  pi.style.left = ( pdo.offsetLeft + 1 ) + "px";

  setTimeout("dialog_resetPopUpPosition();", 200);
}

// Empty return function if null is given for return values
function dialog_emptyReturnFunction(returnValue) {
}

// shows the DIV element în a pop-up which id is given as a pop-up
// @param elementId id of the HTML element to show as pop-up
// @param returnFunction optional return function that will get the
// @param width optional pop-up width
// @param height optional pop-up height
function dialog_showPopUpFromElementId(elementId, returnFunction, width, height){

  // we get the popup element we are going to use
  pi = elementFinder(elementId);

  if(pi==null) {
    alert("element not found !!! "+elementId);
    return; // element not found !
  }

  // we save the dimensions
  popUpWidth = width;
  popUpHeight = height;

  // We set an empty return function if none is provided
  if(returnFunction==null) {
     popUpReturnFct = dialog_emptyReturnFunction;
  }
  else {
     popUpReturnFct = returnFunction;
  }

  // we set the other layers we are going to use
  pwo = elementFinder('popUpWO');
  pdo = elementFinder('popUpDO');

  // save our new state
  popUpOpened = true;
  
  // Loop to disable all the input fields
  var i, j;

  for(i = 0; i < document.forms.length; i ++){
    for(j = 0; j < document.forms[i].elements.length; j ++){
      if(document.forms[i].elements[j].disabled != null && !document.forms[i].elements[j].disabled
               && document.forms[i].elements[j].id!='promptPopUpField'){
        document.forms[i].elements[j].popUpdisabled = true;
        document.forms[i].elements[j].disabled = true;

        if(document.forms[i].elements[j].type == "select-one"){
           document.forms[i].elements[j].style.visibility = "hidden";
        }
      }
    }
  }
    
  // Set opacity for both IE and DOM browsers
  pwo.style.MozOpacity = "0.5";
  pwo.style.opacity = "0.5";

  // Block out content DIV
  pwo.style.left = "0px";
  pwo.style.top = "0px";

  if (document.documentElement && document.documentElement.scrollTop != null) {
      pwo.style.height = document.documentElement.scrollHeight + "px";
      pwo.style.width = document.documentElement.scrollWidth + "px";
  } else {
      pwo.style.height = document.body.scrollHeight + "px";
      pwo.style.width = document.body.scrollWidth + "px"; 
  }

  pwo.style.visibility = "visible";
  
  // Get the size information used to display
  var wHeight = 0;
  var wWidth = 0; 
  
  var sHeight = 0;
  var sWidth = 0;
  
  if (window.innerHeight){
      wHeight = window.innerHeight;
      wWidth = window.innerWidth;   
      sHeight = window.pageYOffset;
      sWidth = window.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop != null) {
      wHeight = document.documentElement.offsetHeight;
      wWidth = document.documentElement.offsetWidth;    
      sHeight = document.documentElement.scrollTop;
      sWidth = document.documentElement.scrollLeft;
  } else {
      wHeight = document.body.offsetHeight;
      wWidth = document.body.offsetWidth;   
      sHeight = document.body.scrollTop;
      sWidth = document.body.scrollLeft;  
  }
  
  // Get the popUp size information
  var pHeight = height;
  var pWidth = width;
  
  if(pHeight == null){
     pHeight = 110;
  } else if(pHeight > wHeight) {
     pHeight = wHeight;
  }
  
  if(pWidth == null){
     pWidth = 350;
  } else if(pWidth > wWidth) {
     pWidth = wWidth;
  }
  
  // Set the properties
  pdo.style.width = ( pWidth + 2 ) + "px";
  pdo.style.height = ( pHeight + 2 ) + "px";
  pdo.style.top = ( sHeight + wHeight/2 - pHeight/2 - 1) + "px";
  pdo.style.left = ( sWidth + wWidth/2 - pWidth/2 - 1) + "px";
  pdo.style.visibility = 'visible';
  
  pi.style.width = ( pdo.offsetWidth - 2) + "px";
  pi.style.height = ( pdo.offsetHeight - 2) + "px";
  pi.style.top = ( pdo.offsetTop + 1 ) + "px";
  pi.style.left = ( pdo.offsetLeft + 1 ) + "px";
  pi.style.visibility = 'visible';
  pi.style.display = 'block';
  
  setTimeout("dialog_resetPopUpPosition();", 200);
}

// Method to call to hide the pop-up and reenable the screen
// @param the returned value by the popup
function dialog_hidePopUp(returnValue){

  // method does nothing if no popup is opened
  if(!popUpOpened){
      return;
  }

  // we save the return function (required for support of chain
  // of multiple popups)
  var returnFunctionToCall = popUpReturnFct;

  // Loop to enable the input fields we had disabled
  var i, j;

  for(i = 0; i < document.forms.length; i ++){
    for(j = 0; j < document.forms[i].elements.length; j ++){
      if(document.forms[i].elements[j].popUpdisabled){
         document.forms[i].elements[j].popUpdisabled = null;
         document.forms[i].elements[j].disabled = false;
        
         if(document.forms[i].elements[j].type == "select-one"){
            document.forms[i].elements[j].style.visibility = "visible";
         }
      }
    }
  }

  // display cleaning
  pwo.style.visibility = 'hidden';
  pwo.style.MozOpacity = null;
  pwo.style.opacity = null;

  pdo.style.visibility = 'hidden';

  pi.style.visibility = 'hidden';
  pi.style.display = 'none';

  // javascript cleaning
  popUpOpened = false;
  popUpHeight = null;
  popUpWidth = null;
  popUpReturnFct = null;

  pi = null;
  pdo = null;
  pwo = null;

  // We call the callback function
  returnFunctionToCall(returnValue);
}

// -------------------------------------------------------------------
// DIALOG BOX DISPLAY
// -------------------------------------------------------------------

// Here are the different methods you can call to display the dialog boxes.

// Alert box dialog. Displays the message and displays a single return button 'close'
// The returned value given to the return function is 'true'.
// @param message message to display on screen
// @param returnFunction optional return function to be called back when the user hits the close button
// @param width optional pop-up width (default is 350px)
// @param height optional pop-up height (default is 110px)
function dialog_alert(message, returnFunction, width, height){

  // change the message in the popup
  var alertPopUpMessage = elementFinder('alertPopUpMessage');
  alertPopUpMessage.innerHTML = message;

  // display the popup
  dialog_showPopUpFromElementId('alertPopUp', returnFunction, width, height);
}

// Confirm box dialog. Displays the message and displays a button 'Yes' and a button 'Cancel'
// The returned value given to the return function is true if 'Yes' is pressed, null otherwise.
// @param message message to display on screen
// @param returnFunction optional return function to be called back when the user hits a button
// @param width optional pop-up width (default is 350px)
// @param height optional pop-up height (default is 110px)
function dialog_confirm(message, returnFunction, width, height){

  // change the message in the popup
  var confirmPopUpMessage = elementFinder('confirmPopUpMessage');
  confirmPopUpMessage.innerHTML = message;

  // display the popup
  dialog_showPopUpFromElementId('confirmPopUp', returnFunction, width, height);
}

// Yes No Cancel box dialog. Displays the message and display 'Yes', 'No' and 'Cancel' buttons.
// The returned value given to the return function is true if 'Yes' is pressed, false if it's 'No'
// and null otherwise.
// @param message message to display on screen
// @param returnFunction optional return function to be called back when the user hits a button
// @param width optional pop-up width (default is 350px)
// @param height optional pop-up height (default is 110px)
function dialog_yesNoCancel(message, returnFunction, width, height){

  // change the message in the popup
  var yesNoCancelPopUpMessage = elementFinder('yesNoCancelPopUpMessage');
  yesNoCancelPopUpMessage.innerHTML = message;

  // display the popup
  dialog_showPopUpFromElementId('yesNoCancelPopUp', returnFunction, width, height);
}

// Prompt box dialog. Displays the message and displays a button 'Confirm' and a button 'Cancel'.
// One can press the confirm button only if the 
// The returned value given to the return function is the prompted value if 'Confirm' is pressed, null otherwise.
// @param message message to display on screen
// @param returnFunction optional return function to be called back when the user hits a button
// @param initValue init value for the pop-up prompt
// @param width optional pop-up width (default is 350px)
// @param height optional pop-up height (default is 110px)
function dialog_prompt(message, returnFunction, initValue, width, height){

  // change the message in the popup
  var promptPopUpMessage = elementFinder('promptPopUpMessage');
  promptPopUpMessage.innerHTML = message;
  
  if(!initValue || initValue==null){
    initValue = "";
  }

  document.promptPopUpForm.field.value = initValue;

  // display the popup
  dialog_showPopUpFromElementId('promptPopUp', returnFunction, width, height);
}

// Private return function for the dialog prompt
function dialog_prompt_confirm() {

   if(document.promptPopUpForm.field.value!="") {
      dialog_hidePopUp(document.promptPopUpForm.field.value);
   }
}