﻿// JScript File

var __capaWin=null;
var jsWindowzIndex=20000;


function jsWindow(title,e)
{
    //Propiedades
   this.top=100;
   this.left=20;
   this.width=0;
   this.height=0;
   this.posWin=0 //Posicion de la ventana Center=0 Top=1, Bottom=2, Left=3, Right=4
   this.divBox=null;
   this.title=title!=undefined?title:'Nueva ventana';
   this.frame=null;
   this.iframe=null;
   this.content=null; 
   this.selects=[];
   this.folderImages=''; 
   jsWindowzIndex++;
   jsWindowthis=this; 
   this.onPosition=function(e,obj){
        var doc=document.body;
        var y = ((doc.clientHeight - obj.height) / 2) + doc.scrollTop;
        var x = ((doc.clientWidth - obj.width) / 2) + doc.scrollLeft;
        y=y>0?y:0;
        x=x>0?x:0;
        obj.divBox.style.left = x + "px";
        obj.divBox.style.top = y + "px";
      };

 
   this.Event=e;
   this.onClose=function(){
    return;  
   }
   this.MostrarBtClose = true;
    
}

jsWindow.prototype.show=function(modal,mostrarClose)
{
    if (mostrarClose != undefined)
        this.MostrarBtClose = mostrarClose;
    else
        this.MostrarBtClose = true;

    __createWindow(this);

    if(modal)
    {
        __crearModal(this);
        DisableSelect(this.selects,true);
        $addHandler(window, 'resize',jsWindowResize );
    }
    
    
};


jsWindow.prototype.createContent=function()
{
  obj=null;
  
  if(this.divResultsPane.childNodes.length)
    obj=this.divResultsPane.childNodes[0];
  if(!obj||obj.tagName!='DIV')
  {
    if(obj)
      this.divReusltsPane.removeChild(obj);
    this.content=document.createElement('div');
    this.content.style.position='relative';
    this.divResultsPane.appendChild(this.content);
    
  }
  if(typeof(this.onPosition)=='function')
    this.onPosition(this.Event,this);
};

jsWindow.prototype.addContent=function(cont)
{
  this.createContent();
  if(cont){
    this.content.innerHTML=cont.innerHTML;
    pannel=this.divBox.childNodes[1];
    pannel.style.height=this.divResultsPane.offsetHeight+this.divHeading.offsetHeight +1+'px';
  }
  if(typeof(this.onPosition)=='function')
    this.onPosition(this.Event,this);
    
  setTimeout(this.recalculateIframe,10);
};


jsWindow.prototype.recalculateIframe=function()
{

  var h=0;
  if(!this.divBox)
    return;
  for(i=0;i<this.divBox.childNodes[0].childNodes.length;i++)
  {
    h+=this.divBox.childNodes[0].childNodes[i].offsetHeight;
  }  
  this.iframe.style.height=h+'px';
}

jsWindow.prototype.go=function(url)
{
    if(url!=undefined)
        this.url=url;
    obj=null
    if(this.divResultsPane.childNodes.length)
        obj=this.divResultsPane.childNodes[0];
    if(!obj || obj.tagName!='IFRAME')
    {
        if(obj)
            this.divResultsPane.removeChild(obj);
        this.frame=document.createElement('iframe');
        this.frame.style.width='100%';
        this.divResultsPane.appendChild(this.frame);
        this.frame.style.height=this.height-this.divHeading.offsetHeight+'px';
    }
    this.frame.src=url;
    if(typeof(this.onPosition)=='function')
      this.onPosition(this.Event,this);
}

jsWindow.prototype.close=function()
{
  $removeHandler(window, 'resize',jsWindowResize );
  obj=null
  if(this.divBox)
      obj=this.divBox;
  else
      obj=this.parentNode.parentNode.parentNode;
  document.body.removeChild(obj);
  div=this.modal
  if(div)
      document.body.removeChild(div);
  DisableSelect(this.selects,false);
  this.selects=null;
  return false;
}

function __crearModal(obj)
{
    div=document.createElement('div')
    div.id="__divmodal";
    obj.modal=div;
    div.className="jsWindowDivModal";
    div.style.zIndex=jsWindowzIndex;
    body=document.body;
    div.style.width=document.body.clientWidth -8;
    div.style.height=document.body.clientHeight-8;
    document.body.appendChild(div);
}

function jsWindowResize(){
      
      divmodal=document.getElementsByName('__divmodal')
      for(i=0;i<divmodal.length;i++)
      {
        divmodal[i].style.width=document.body.clientWidth -8;
        divmodal[i].style.height=document.body.clientHeight-8;
      }
    }

function __createWindow(obj)
{
  
//Caja principal
    obj.divBox= document.createElement("div");
    obj.divBox.style.zIndex=jsWindowzIndex+1;
    obj.divBox.className = "jsWindowBox";
    obj.divBox.style.cursor='default'; 
    obj.divBox.id='jsWindowBox';
//Cabecera
    divHeading = document.createElement("div");
    divHeading.className = "jsWindowHeading";
    obj.divHeading=divHeading;
    //divHeading.style.height="22px";
//Panel de resultados    
    divResultsPane = document.createElement("div");
    //$addHandler(divResultsPane, 'resize',obj.recalculateIframe );


    if (obj.MostrarBtClose)
    {
    // imagelink para cerrar la ventana
        var CloseLink = document.createElement("img");   
        CloseLink.src=obj.folderImages+ 'CerrarPopUp.png';
    //    CloseLink.onmouseover="this.src='" + obj.folderImages+ 'CerrarPopUpOver.png\';';
    //    CloseLink.onmouseout="this.src='" + obj.folderImages+ 'CerrarPopUp.png\';';
        othis=this;
        CloseLink.onmouseover=function(){this.src=othis.folderImages+ 'CerrarPopUpOver.png';}
        CloseLink.onmouseout=function(){this.src=othis.folderImages+ 'CerrarPopUp.png';}
        
        CloseLink.onclick=function(){
          obj.close(); 
          obj.onClose();
          return false;
        }
        CloseLink.style.cursor='pointer';
    }
    
    divTitle=document.createElement("div");
    divTitle.style.cursor='default';
    divTitle.className='jsWindowHeadingContent';
    divTitle.onmouseover=function(){this.style.cursor='move';};
    divTitle.onmouseout=function(){this.style.cursor='default';};
    divTitle.onmousedown=obj.InicioArrastre;
    divTitle.appendChild(document.createTextNode(obj.title))
    
    tableHeading=document.createElement('table');
    tableHeading.cellPading='0';
    tableHeading.cellsPacing='0';
    tableHeading.style.width='100%';
    tbodyHeading=document.createElement('tbody');
    tableHeading.appendChild(tbodyHeading);
    
    trHeading=document.createElement('tr');
    trHeading.className="JsWindowsHeading";
    tbodyHeading.appendChild(trHeading);
    
    tdHeading=document.createElement('td');
    tdHeading.style.width='100%';
    trHeading.appendChild(tdHeading);
    tdHeading.appendChild(divTitle);
    tdHeading.className='JsWindowsHeading';
    
    
    tdHeading=document.createElement('td');
    tdHeading.className='JsWindowsHeading';
    trHeading.appendChild(tdHeading);
    if (obj.MostrarBtClose)
        tdHeading.appendChild(CloseLink);
    
    divHeading.appendChild(tableHeading);
    //divHeading.appendChild(tableHeading);

    
    divWindow=document.createElement('div');
    divWindow.className='jsWindowWindow';

    
  
    obj.divBox.style.top = obj.top + "px";
    obj.divBox.style.left = obj.left + "px";
    
    
    divResultsPane.className = "jsWindowResults";
    obj.divResultsPane=divResultsPane;
    divWindow.appendChild(divHeading);
    divWindow.appendChild(divResultsPane);
    obj.divBox.appendChild(divWindow);
    iframe=document.createElement('iframe');
    iframe.style.position='absolute';
    iframe.style.top=1+'px';
    iframe.style.left=1+'px';
    iframe.style.width = obj.width +1+ "px";
    iframe.style.height = obj.height + 1+"px";
    iframe.className='jsWindowframe';
    obj.iframe=iframe;
    obj.divBox.appendChild(iframe);
    document.body.appendChild(obj.divBox);
    iframe.style.height=obj.divBox.offsetHeight;
    if(this.height)
      divResultsPane.style.height=iframe.offsetHeight-this.divHeading.offsetHeight-2+'px';
    othis=obj;
    divWindow.onresize=function()
    {
      othis.recalculateIframe();
    }
    if(obj.width!=0)
      divWindow.style.width = obj.width + "px";
    if(obj.height!=0)
      divWindow.style.height = obj.height + "px";
}



var IE_X, IE_Y, AntX, AntY, RatonPulsado = false;

  jsWindow.prototype.InicioArrastre=function (ev) {
    evento=ev?ev:event;
    boton=ev?ev.MOUSEDOWN:evento.button;
    if (boton == 1) {
      
      divbox=this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
      divbox.AntX = divbox.offsetLeft ;
      divbox.AntY = divbox.offsetTop;
      divbox.IE_X = evento.clientX;
      divbox.IE_Y = evento.clientY;
      RatonPulsado = true;
      __capaWin=divbox;

      document.onmousemove=Arrastrar;
      document.onmouseup=FinArrastre;
      return false;
      }
    }
  FinArrastre=function (ev) {
  
    evento=ev?ev:event;
     boton=ev?ev.MOUSEDOWN:evento.button;
    if (boton== 1) {
      RatonPulsado= false;   
      return false;
      
      }
    }
  Arrastrar=function (ev) {
     
     evento=ev?ev:event;
     
     if (RatonPulsado==1) {
      __capaWin.style.posLeft = __capaWin.AntX
        + evento.clientX - __capaWin.IE_X;
      __capaWin.style.posTop = __capaWin.AntY
        + evento.clientY - __capaWin.IE_Y;
      if (__capaWin.style.posTop <= 0) __capaWin.style.posTop = 0;
      if (__capaWin.style.posLeft <= 0) __capaWin.style.posLeft = 0;
      return false;
      }
   
    }
    
    function DisableSelect(col,value)
    {
      var f=document.getElementsByTagName('SELECT');
		  if(value)
		  {
		    for(var i=0; i<f.length; i++) 
		    {
		      if(!f[i].disabled)
		      {
		        f[i].disabled=value;
		        col[col.length]=f[i];
		      }
		    }
		  }
		  else
		  {
		    for(i=0;i<col.length;i++)
		    {
		      col[i].disabled=value;
		    }
		  }
		  
    }