﻿
DialogBoxDiv = function(isModal) {
  // CONSTRUCTOR para el objeto 'DialogBoxDiv'
  if (arguments.length==0) return;
  this.isModal = isModal;

  if (DialogBoxDiv.veilOverlay == null) { // una vez por página
    DialogBoxDiv.veilOverlay = document.createElement('div');
    DialogBoxDiv.veilOverlay.className = "DialogBoxDivVeil";
    DialogBoxDiv.veilOverlay.style.zIndex = DialogBoxDiv.veilZ;
    DialogBoxDiv.veilOverlay.innerHTML = "&nbsp;";
    DialogBoxDiv.setVeilWidth();
    DialogBoxDiv.addListener(window, "resize", DialogBoxDiv.setVeilWidth);
    document.body.appendChild(DialogBoxDiv.veilOverlay);

    DialogBoxDiv.closeIcon = new Image();
    DialogBoxDiv.closeIcon.src = DialogBoxDiv.imagePath + "window_close.gif";
    }

  this.container = document.createElement('div');
  this.container.className = DialogBoxDiv.className;
  this.container.dialogBox = this;

  var mainTable = document.createElement('table');
  mainTable.setAttribute('cellSpacing', '0');
  mainTable.setAttribute('cellPadding', '0');
  mainTable.setAttribute('border', '0');

  var tBodyM = document.createElement('tbody');
  var rowM = document.createElement('tr');
  var cellM = document.createElement('td');

  //*********** INICIO Titulo TABLA ***********
  var titleTable = document.createElement('table');
  titleTable.setAttribute('cellSpacing', '0');
  titleTable.setAttribute('cellPadding', '0');
  titleTable.setAttribute('border', '0');
  titleTable.setAttribute('width', '100%');

  var tBodyT = document.createElement('tbody');
  var rowT = document.createElement('tr');
  var cellT = document.createElement('td');
  cellT.className = "tbLeft";
  rowT.appendChild(cellT);

  this.titleCell = document.createElement('td');
  this.titleCell.className = "Title";
  rowT.appendChild(this.titleCell);

  cellT = document.createElement('td');
  cellT.className = "tbRight";

  var closeIcon = document.createElement('img');
  closeIcon.src = DialogBoxDiv.closeIcon.src;
  closeIcon.setAttribute('border','0');
  closeIcon.dialogBox = this;

  var aLink = document.createElement('A');
  aLink.setAttribute('href','#');
  aLink.appendChild(closeIcon);
  aLink.onclick = DialogBoxDiv.closeBox;

  cellT.appendChild(aLink);
  rowT.appendChild(cellT);

  tBodyT.appendChild(rowT);
  titleTable.appendChild(tBodyT);
  //*********** FIN Titulo TABLA ***********

  cellM.appendChild(titleTable);
  rowM.appendChild(cellM);
  tBodyM.appendChild(rowM);

  rowM = document.createElement('tr');
  cellM = document.createElement('td');
  cellM.className = "MainPanel";

  this.contentArea = document.createElement('div');
  this.contentArea.className = "ContentArea";
  cellM.appendChild(this.contentArea);

  rowM.appendChild(cellM);
  tBodyM.appendChild(rowM);
  mainTable.appendChild(tBodyM);
  this.container.appendChild(mainTable);
  document.body.appendChild(this.container);

  Drag.init(this.titleCell, this.container, 0, null, 0);
  }

/************ INICIO: Metodos Publicos ************/
DialogBoxDiv.prototype.show = function() {
  this.container.style.display = "block";
  this.topZ();
  divOnScrn(this.container);
  if (this.isModal) DialogBoxDiv.veilOverlay.style.display = "block";
  }

DialogBoxDiv.prototype.hide = function(ok) {
  this.container.style.display = "none";
  if (this.isModal) DialogBoxDiv.veilOverlay.style.display = "none";
  var posInList = this.listPos();
  if (posInList != -1) {
    DialogBoxDiv.openList[posInList] = DialogBoxDiv.openList[DialogBoxDiv.openList.length-1];
    DialogBoxDiv.openList.pop();
    }
  if (ok) {
    if (this.callOK)
      if (this.returnData) this.callOK(this.returnData);
      else this.callOK();
    }
  else if (this.callCancel) this.callCancel();
  }

DialogBoxDiv.prototype.boxmoveTo = function(x, y) {
  if (x == -1) x = Math.round((document.body.clientWidth - this.container.offsetWidth) / 2);
  if (y == -1) y = Math.round((document.body.clientHeight - this.container.offsetHeight) / 2) + document.body.scrollTop;
  this.container.style.left = x + "px";
  this.container.style.top = y + "px";
  }

DialogBoxDiv.prototype.setTitle = function(title) {
  this.titleCell.innerHTML = title;
  }

DialogBoxDiv.prototype.setContent = function(htmlContent) {
  this.contentArea.innerHTML = htmlContent;
  }

DialogBoxDiv.prototype.setWidth = function(width) {
  this.contentArea.style.width = width + "px";
  }

DialogBoxDiv.prototype.setCallOK = function(callOK) {  
  this.callOK = callOK; //Puede ser establecido por de manera externa si se necesita
  }

DialogBoxDiv.prototype.setCallCancel = function(callCancel) {  
  this.callCancel = callCancel; //Puede ser establecido de manera externa si se necesita
  }
/************ FIN: Metodos Publicos ************/

/************ INICIO: Metodos Privados ************/
DialogBoxDiv.className = "DialogBoxDiv";
DialogBoxDiv.closeIcon = null;
DialogBoxDiv.veilOverlay = null;
DialogBoxDiv.veilZ = 10000;
DialogBoxDiv.openList = new Array();
DialogBoxDiv.maxDepth = 5; // Optimiza la busqueda de nodos padre

DialogBoxDiv.closeBox = function(e) {
  if (!e) e = window.event;
  var node = e.target ? e.target : e.srcElement;
  var count = 0;
  while ((node != null) && (count < DialogBoxDiv.maxDepth)) {
    if (node.dialogBox) {
      node.dialogBox.hide();
      return false;
      }
    node = node.parentNode;
    count++;
    }
  return false;
  }

DialogBoxDiv.prototype.listPos = function() {
  var posInList = -1;
  for (var i=0; i<DialogBoxDiv.openList.length; i++)
    if (DialogBoxDiv.openList[i] == this) {
      posInList = i;
      break;
      }
  return posInList;
  }

DialogBoxDiv.prototype.topZ = function() {
  var posInList = this.listPos();
  if (posInList == -1) DialogBoxDiv.openList[DialogBoxDiv.openList.length] = this; // agregar a la lista
  else if (posInList < DialogBoxDiv.openList.length-1) {
    for (var i=posInList; i<DialogBoxDiv.openList.length-1; i++) DialogBoxDiv.openList[i] = DialogBoxDiv.openList[i+1];
    DialogBoxDiv.openList[DialogBoxDiv.openList.length-1] = this; // mover al final
    var newZ = DialogBoxDiv.veilZ;
    for (var i=DialogBoxDiv.openList.length-1; i>0; i--) {
      newZ--;
      DialogBoxDiv.openList[i].style.zIndex = newZ;
      }
    }
  this.container.style.zIndex = DialogBoxDiv.veilZ+1;
  }

DialogBoxDiv.setVeilWidth = function() {
  DialogBoxDiv.veilOverlay.style.width = document.body.scrollWidth;
  DialogBoxDiv.veilOverlay.style.height = document.body.scrollHeight;
  }

DialogBoxDiv.addListener = function(obj, evType, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, false);
    return true;
    }
  else if (obj.attachEvent) return obj.attachEvent('on' + evType, fn);
  else return false;
  }

AppAlert = function(icon, okButtonImg) {
  // CONSTRUCTOR para el objeto 'AppAlert' - Llama al constructor base 'DialogBoxDiv'
  if (arguments.length==0) return;
  this.base = DialogBoxDiv;
  this.base(true);

  var dialogTable = document.createElement('table');
  dialogTable.setAttribute('cellSpacing', '0');
  dialogTable.setAttribute('cellPadding', '0');
  dialogTable.setAttribute('border', '0');

  var tBody = document.createElement('tbody');
  var row = document.createElement('tr');
  var cell = document.createElement('td');
  cell.setAttribute("vAlign", "top");

  this.iconImage = document.createElement('img');
  this.iconImage.style.margin = "0px 10px 0px 0px";
  this.setIcon(icon);  
  if (icon != "") cell.appendChild(this.iconImage);
  row.appendChild(cell);

  this.contentCell = document.createElement('td');
  this.contentCell.className = "ContentArea";
  row.appendChild(this.contentCell);
  tBody.appendChild(row);
  dialogTable.appendChild(tBody);
  this.contentArea.appendChild(dialogTable);

  this.buttonDIV = document.createElement('div');
  this.buttonDIV.setAttribute("align", "center");
  this.buttonDIV.style.margin = "10px 0px 0px 0px";
  this.contentArea.appendChild(this.buttonDIV);
  AppAlert.addButton(this, AppAlert.lblOK, 1, okButtonImg);
  }

AppAlert.prototype = new DialogBoxDiv();

/************ INICIO: 'AppAlert' Metodos Publicos ************/
//Propiedades con valores por defecto para usar en la llamada al constructor
AppAlert.Warning = "warning.gif"; 
AppAlert.Error = "error.gif";     
AppAlert.Info = "info.gif";       
AppAlert.lblOK = "Aceptar"; 
AppAlert.lblCancel = "Cancelar"; 

AppAlert.prototype.setContent = function(htmlContent) {
  this.contentCell.innerHTML = htmlContent;
  }

AppAlert.prototype.setIcon = function(icon) {
  this.iconImage.src = DialogBoxDiv.imagePath + icon;
  }
/************ FIN: 'AppAlert' Metodos Publicos ************/

/************ INICIO: 'AppAlert' Metodos Privados ************/
AppAlert.addButton = function(parent, buttonText, buttonNum, buttonSrc) {
      if (buttonSrc == "") {
        var button = document.createElement("button");
        button.style.fontSize = "10pt";
        button.style.width = "60px";
        button.style.margin = "0px 5px";
        button.innerHTML = buttonText;
        button.linkNum = buttonNum;
        button.appDialog = parent;
        button.onclick = AppAlert.clickLink;
        parent.buttonDIV.appendChild(button);
      } else {
        var button = document.createElement("img");
        button.style.cursor = 'hand';
        button.src = buttonSrc;
        button.title = buttonText;
        button.setAttribute('border','0');
        button.style.margin = "0px 15px";
        button.linkNum = buttonNum;
        button.appDialog = parent;
        button.onclick = AppAlert.clickLink;
        parent.buttonDIV.appendChild(button);
      }
  }

AppAlert.clickLink = function(e) {
  if (!e) e = window.event;
  var node = e.target ? e.target : e.srcElement;
  var linkNum = node.linkNum;
  var count = 0;
  while ((node != null) && (count < DialogBoxDiv.maxDepth)) {
    if (node.appDialog) {
      switch (linkNum) {
        case 1: {
          node.appDialog.hide(true);
          break;
          }
        case 2: {
          node.appDialog.hide();
          break;
          }
        }
      return false;
      }
    node = node.parentNode;
    count++;
    }
  return false;
  }
/************ FIN: 'AppAlert' Metodos Publicos ************/

AppConfirm = function(icon, callOK, callCancel, okButtonImg, cancelButtonImg) {  
  // CONSTRUCTOR para el objeto 'AppConfirm' - Llama al constructor base 'AppAlert'
  if (arguments.length==0) return;
  this.base = AppAlert;
  this.base(icon, okButtonImg);
  this.callOK = callOK;
  this.callCancel = callCancel;
  AppAlert.addButton(this, AppAlert.lblCancel, 2, cancelButtonImg);
  }

AppConfirm.prototype = new AppAlert();

/************ INICIO: 'AppConfirm' Metodos Publicos ************/
AppConfirm.prototype.askUser = function(htmlContent) {
  this.setContent(htmlContent);
  this.show();
  }
/************ FIN: 'AppConfirm' Metodos Publicos ************/

AppPrompt = function(icon, callOK, callCancel, cssClass, okButtonImg, cancelButtonImg) {  
  // CONSTRUCTOR para el objeto 'AppPrompt' - Llama al constructor base 'AppConfirm'
  if (arguments.length==0) return;
  this.base = AppConfirm;
  this.base(icon, callOK, callCancel, okButtonImg, cancelButtonImg);
  this.returnData = new Object();
  this.fInput = document.createElement('input');
  this.fInput.type = "text";
  if (cssClass != "") this.fInput.className = cssClass;
  this.fInput.appDialog = this;
  this.fInput.onkeypress = AppPrompt.keyPress;
  }

AppPrompt.prototype = new AppConfirm();
AppPrompt.superClass = AppConfirm.prototype;

/************ INICIO: 'AppPrompt' Metodos Publicos ************/
AppPrompt.prototype.askUser = function(htmlContent, stDefault) {
  this.setContent(htmlContent);
  this.fInput.value = stDefault;
  this.contentCell.appendChild(this.fInput);
  this.show();  
  this.fInput.focus();
  }

AppPrompt.prototype.focus = function() {
  this.fInput.focus();
  }

AppPrompt.prototype.hide = function(ok) {
  if (ok) this.returnData.value = this.fInput.value;
  AppPrompt.superClass.hide.call(this, ok);
  }
/************ FIN: 'AppPrompt' Metodos Publicos ************/

AppPrompt.keyPress = function(e) {
  if (!e) e = window.event;
  var node = e.target ? e.target : e.srcElement;
  var key = e.keyCode ? e.keyCode : e.which;
  if (key == 13) node.appDialog.hide(true);
  if (key == 27) node.appDialog.hide();
  }

AppAlert.prototype.trace = function() {
  alert(objToString(this.contentArea));
  }

function fixE(ev) {
    var e = ev ? ev : window.event;
    return e;
}
