/******************************************************************************
 *
 *                   INDIGEN SOLUTIONS CODE PROPERTY
 *       The present javascript code is property of Indigen Solutions. This 
 *     code can only be used inside Internet/Intranet web sites located on 
 *  *web servers*, as the outcome of a licensed Indigen Solutions application 
 *  only. Any unauthorized use, reverse-engineering, alteration, transmission, 
 * transformation, facsimile, or copying of any means (electronic or not) is 
 *     strictly prohibited and will be prosecuted. Removal of the present 
 *              copyright notice is strictly prohibited
 *         Copyright (c) 2004 Indigen Solutions. All Rights Reserved.
 *
 * RCS Id                       $Id: popup.js,v 1.6.4.1 2005/10/10 08:46:18 jerome Exp $
 * 
 ******************************************************************************/

/* Constants. */

var POPUP_DEFAULT_TYPE    = 'okCancel';
var POPUP_DEFAULT_CONTENT = 'EMPTY POPUP !';
var POPUP_DEFAULT_TITLE   = 'Alert box';

/* Classes. */

function Popup(description) {
  this.base = CIB;
  this.base();

  /* Attributes. */

  this.description = ( description ? description : {} );
  this.title       = ( (description && description.title)   ? description.title   : POPUP_DEFAULT_TITLE   );
  this.type        = ( (description && description.type)    ? description.type    : POPUP_DEFAULT_TYPE    );
  this.content     = ( (description && description.content) ? description.content : POPUP_DEFAULT_CONTENT );
  this.document    = ( (description && description.document) ? description.document : document );

  this._domPopup     = null;
  this._popupPanel   = null;
  this._contentPanel = null;
  this._id           = Popup._id++;

  // Build content panel in function on content.
  if (typeof this.content == "string") {
    var div = this.document.createElement('div');
    div.innerHTML = this.content;
    this._contentPanel = div;
  } else {
    this._contentPanel = this.content;
  }

  /* Methods. */
  
  this.show = function() {
    this._hideDOM();
    this._createPopupPanel();
    this.document.body.focus();
  };
  
  this.hide = function() {
    this._showDOM();
  };

  this._showDOM = function() {
    this._popupPanel.parentNode.removeChild(this._popupPanel);
    var child = this.document.body.firstChild;
    while ( child ) {
      if ( child.nodeType == "1") {
	if ( child.getAttribute("icms_popup_hidden") ) 
	  child.style.display = "";
      }
      child = child.nextSibling;
    }
    var popupPanel = this.document.getElementById("popup_panel");
    popupPanel.style.display = "none";
  };  

  this._hideDOM = function() {    
    var child = this.document.body.firstChild;
    while ( child ) {
      if ( child.nodeType == "1" && 
	   ( (!child.style.display) || (child.style.display == "") || (child.style.display == "inline")) ) {
	child.style.display = "none";
	child.setAttribute("icms_popup_hidden", 1);
      }
      child = child.nextSibling;
    } 
    var popupPanel = this.document.getElementById("popup_panel");
    popupPanel.style.display = "";
  };

  this._createPopupPanel = function() {
    var popupPanel = this.document.getElementById("popup_panel");
    var popupModel = this.document.getElementById("popup_" + this.type + "_model");    
    if ( !popupModel )
      popupModel = this.document.getElementById("popup_" + POPUP_DEFAULT_TYPE + "_model");
    var popup = popupModel.cloneNode(true);
    popup.style.display = "";
    popup.setAttribute("id", "___popup___");
    var table = this.document.createElement("table");
    table.setAttribute("width", "100%");
    table.setAttribute("height", "100%");
    var tr = this.document.createElement("tr");
    table.appendChild(tr);
    var td = this.document.createElement("td");
    td.setAttribute("align", "center");
    tr.appendChild(td);
    td.appendChild(popup);    
    popupPanel.appendChild(table);
    if ( util_isIE() ) {
      table.setAttribute("id", "___table___");
      popupPanel.innerHTML = table.outerHTML;     
      table = this.document.getElementById("___table___");
      popup = this.document.getElementById("___popup___");
    }
    this._attachEventsToPanel(popup); 
    this._setTitle(popup);
    this._setContent(popup);
    this._popupPanel = table;
  };
  
  this._attachEventsToPanel = function(panel) {
    var inputs = panel.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
      var input = inputs[i];
      var name  = input.getAttribute("name");
      if ( this.description[name] && (label = this.description[name].label) )
	input.setAttribute("value", label);
      util_attachEventHandlerToElement(input, 'click', new Function(this.getStrRef() + "._manageHandlers('" + name + "')"));
    }
  };
  
  this._setTitle = function(panel) {
    if ( this.title ) {
      var title = getElementById(panel, 'popup_title');
      title.innerHTML = this.title;
    }
  };


  this._setContent = function(panel) {    
    if ( this.content ) {
      var content = getElementById(panel, 'popup_content');
      content.innerHTML = this.content;
    }
    /*
    switch ( this.type ) {
    case "istruct" :
    case "istructOkCancel" :
    case "istructOkRemoveCancel" :
      if ( this.description.istruct_formals ) {
	this.istruct = new IStruct(this.description.istruct_formals);
	this.istruct.setData((this.description.istruct_data ? this.description.istruct_data : {}));
	this.istruct.start(panel);    
      }
      break;
    default :
      panel.appendChild(this._contentPanel);      
      break;
    } 
    */
  };

  this._manageHandlers = function(name) {
    switch ( this.type ) {
      case "istruct" :
      case "istructOkCancel" :
      case "istructOkRemoveCancel" :
        if ( this.description.istruct_formals )
  	  this.istruct.updateData();
        break;
      default :
        break;
    }    
    this.hide();
    if ( this.description[name] && (func = this.description[name].handler) ) {
      func(this); 
    }
  }; 
}

/* Static. */

Popup._id = 1;

/* Public methods. */

var OK_CANCEL          = 0;
var YES_NO_CANCEL      = 1;
var OK_ONLY            = 2;
var YES_NO             = 3;
var ABORT_RETRY_IGNORE = 4;
var RETRY_CANCEL       = 5;

var CRITICAL           = 0;
var QUESTION           = 1;
var EXCLAMATION        = 2;
var INFORMATION        = 3;

var OK_BUTTON          = 1;
var CANCEL_BUTTON      = 2;
var ABORT_BUTTON       = 3;
var RETRY_BUTTON       = 4;
var IGNORE_BUTTON      = 5;
var YES_BUTTON         = 6;
var NO_BUTTON          = 7;

function messageBox(message, title, type, icon, handler){  
  var description = {};
  description.title = title;
  description.content = message;
  switch ( type ) {
  case OK_CANCEL : 
    description.type = "2button";
    description.button1 = { label : "OK" }
    description.button2 = { label : "Cancel" }
    description.button1.handler = function() {
      handler(OK_BUTTON);
    }
      description.button2.handler = function() {
	handler(CANCEL_BUTTON);
      }      
      break;
  case YES_NO_CANCEL : 
    description.type = "3button";
    description.button1 = { label : "Yes" }
    description.button2 = { label : "No" }
    description.button3 = { label : "Cancel" }
    description.button1.handler = function() {
      handler(YES_BUTTON);
    }
      description.button2.handler = function() {
	handler(NO_BUTTON);
      }      
      description.button3.handler = function() {
	handler(CANCEL_BUTTON);
      }      
      break;
  case OK_ONLY : 
    description.type = "1button";
    description.button1 = { label : "OK" }
    description.button1.handler = function() {
      handler(OK_BUTTON);
    }
      break;
  case YES_NO : 
    description.type = "2button";
    description.button1 = { label : "Yes" }
    description.button2 = { label : "No" }
    description.button1.handler = function() {
      handler(YES_BUTTON);
    };
    description.button2.handler = function() {
      handler(NO_BUTTON);
    };      
    break;
  case ABORT_RETRY_IGNORE : 
    break;
  case RETRY_CANCEL       : 
    break;
  };
  switch ( icon ) {
  case CRITICAL    : 
    description.icon = THEME_URL + '/image/icon/critical.gif'; 
    break;
  case QUESTION    : 
    description.icon = THEME_URL + '/image/icon/question.gif'; 
    break;
  case EXCLAMATION : 
    description.icon = THEME_URL + '/image/icon/exclamation.gif'; 
    break;
  case INFORMATION : 
    description.icon = THEME_URL + '/image/icon/information.gif'; 
    break;
  };
  var popup = new Popup(description);
  popup.show();

}


