﻿// JScript Fill
//
// Sergio Muñoz Curto
//
// En este script se mantiene todas las funciones comunes para los rescalados
//
// Identificadores comunes para los formularios
//
// DivContentPage: Conetenedor del splitter derecho de la pantalla, aqui ubicaremos todos
//                 los datos.
// TbTitulo: tabla que contiene el titulo
//
// TbBarraHerramientas: Tabla que contiene los botones de las herramientas
//
// DivContentRegistro: contenedor que contiene los datos mas importantes del detalle, 
//                     para los formularios de detalle.
//
// DivRegistroPlus: contenedor que contine datos extra del detalle, por regla general es un TabPage.
//
// Se utiliza un input en cada pagina de detalle para indicar el alto del 
// DivRegistroPlus, este input se llama heightPanelInf
//
// DivContentBuscar: contenedor que guarda el filtro y el grid de los datos de busqueda
//
// divGridResultados: contenedor que guarda el grid de resultados de la busqueda
//
// DivFiltroBuscar: contenedor que esta en el control de usuario de filtro
//                           y contiene los parametros del filtro


var panneWidth  // guardamos el ancho del DivContentPage
var panneHeight // guardamos el alto del DivContentPage

var Filtro_Subir=false; // boolean para saber el estado del boton de expandir filtros

// function que trata el evento del lado derecho del splitter cuando se cambia de tamaño
function spliter_onResize(sender,e)
{
  panneWidth=e.get_pane().Width;
  panneHeight=e.get_pane().Height;
  
  ReCalcularPagina();
  CrearDivTrabajando();
}

function CrearDivTrabajando()
{
  divtbr=$get('DivTrabajando');
  if(!divtbr)
  {
    divtbr=document.createElement('div')
    divtbr.id="DivTrabajando";
    divtbr.className="jsWindowDivModal";
    divtbr.style.display='none';
    divtbr.style.zIndex=jsWindowzIndex;
    body=document.body;
    document.body.appendChild(divtbr);
   }
   divContenido=$get('DivContentPage');

   divtbr.style.width=divContenido.offsetWidth + 'px';
   divtbr.style.height=divContenido.offsetHeight + 'px';
   divtbr.style.top=ctsGetOffsetTop(divContenido);
   divtbr.style.left= 268;
}


function ReCalcularPagina()
{
  divContenidoBuscar=$get('DivContentBuscar');
  if (divContenidoBuscar)
  {
      altura=panneHeight - $get('TbTitulo').offsetHeight - $get('TbBarraHerramientas').offsetHeight - 8 
      altura=altura<300?300:altura;

      divContenido=$get('DivContentBuscar');
      divContenido.style.height= altura+ 'px';
      if(panneWidth>400)
      {
        divContenido.style.width=panneWidth-8 +'px';
        $get('TbTitulo').style.width=panneWidth-6 +'px';
      }
      CalcularContenidoBuscar();
  }
  else // es una pagina de detalle
  {
      objDivRegistroPlus=$get('DivRegistroPlus');
      objDivRegistroPlus.style.height = $get('heightPanelInf').value + 'px';
//      altura=panneHeight + 19 - $get('TbTitulo').offsetHeight - $get('TbBarraHerramientas').offsetHeight  -objDivRegistroPlus.offsetHeight-8 ;
      altura=panneHeight - $get('TbTitulo').offsetHeight  -objDivRegistroPlus.offsetHeight-8 ;
      altura=altura>60?altura:60;
      divContenido=$get('DivContentRegistro');
      divContenido.style.height= altura+ 'px';
      if (panneWidth > 400)
      {
        divContenido.style.width=panneWidth-8 +'px';
        $get('TbTitulo').style.width=panneWidth-6 +'px';
        objDivRegistroPlus.style.width=panneWidth-8 +'px';
      }


      RenderGridsPlus();
  }
}

function CalcularContenidoBuscar()
{
  objContenido=$get('DivContentBuscar');
  objGridResultados=$get('divGridResultados');
  objFiltroBuscarRegistros=$get('DivFiltroBuscar');
  objGridResultados.style.height=objContenido.offsetHeight-objFiltroBuscarRegistros.offsetHeight -2 + 'px';
  if (panneWidth != null)
    objGridResultados.style.width=panneWidth-8 + 'px';
  Buscar_setTitle();
}

// lo heredo pero no se lo que hace
function Buscar_setTitle()
{
  try{
  div=$get('ctl00_ContentPlaceHolder1_gridResultados_dom');
  row=div.childNodes[0].childNodes[0].childNodes[0];
  row.childNodes[1].title='Mi papel sobre el registro';
  row.childNodes[2].title='Acción que se puede realizar';
  row.childNodes[5].title='Es borrador';  
    
  }
  catch(Exception){}
}


function RenderGridsPlus()
{
    // aqui renderizamos los posibles grid que hubiera en el DivRegistroPlus
}

function btnContraer_click()
{
    img= $get('ctl00_ContentPlaceHolder1_btnContraer');
    if(expandir==true)
    {
      $get('heightPanelInf').value=157;
      expandir=false
      CambiarImagen(img,imgContrOver.src,imgContrOut.src);
      img.title='Pulse para contraer';
    }
    else
    {
      $get('heightPanelInf').value=24;
      expandir=true;
      CambiarImagen(img,imgExpanOver.src,imgExpanOut.src);
      img.title='Pulse para expandir';
    }
    CalcularPagina();
}

// 
// function para tratar el funcionamiento de la ficha de busqueda
function Registro_ExpandirFicha()
{

  if(expandir==true)
    btnContraer_click();

}

//
// function que encapsula el funcionamiento del boton del filtro cuando lo pulsas
function Filtro_btnContraer_click()
{
    if(event)
      imgcontraerFiltro=event.srcElement; 
    var divAExpandir=$get('DivFiltroBuscar');
    
    // puede existir dos filtros, para el caso de mi gestor
    if (divAExpandir.childNodes.length > 1)
    {
        var filtroContacto = divAExpandir.childNodes[0];
        var filtroGrupo = divAExpandir.childNodes[1];
        if (filtroContacto.style.display == "none")
            var filtro = filtroGrupo;
        else
            var filtro = filtroContacto;
    }
    else
        var filtro = divAExpandir.childNodes[0];
    if(Filtro_Subir==true)
    {
      divAExpandir.style.height="";
      filtro.style.height="";
      if (divAExpandir.childNodes.length > 1)
      {
        filtro.childNodes[1].style.display = 'inline';
        if (filtro.childNodes.length >2)
        {
            filtro.childNodes[2].style.display = 'block';
            filtro.childNodes[3].style.display = 'block';
            filtro.childNodes[4].style.display = 'block';
            filtro.childNodes[5].style.display = 'block';
            filtro.childNodes[6].style.display = 'block';
            filtro.childNodes[7].style.display = 'block';
        }

      }
      Filtro_Subir=false
      CambiarImagen(imgcontraerFiltro,imgContrOver.src,imgContrOut.src);
      imgcontraerFiltro.title='Pulse para contraer';
    }
    else
    {
      divAExpandir.style.height=24 + 'px';
      filtro.style.height=24 + 'px';
      if (divAExpandir.childNodes.length > 1)
      {
        filtro.childNodes[1].style.display = 'none';
        if (filtro.childNodes.length >2)
        {
            filtro.childNodes[2].style.display = 'none';
            filtro.childNodes[3].style.display = 'none';
            filtro.childNodes[4].style.display = 'none';
            filtro.childNodes[5].style.display = 'none';
            filtro.childNodes[6].style.display = 'none';
            filtro.childNodes[7].style.display = 'none';
        }
      }
      Filtro_Subir=true;
      CambiarImagen(imgcontraerFiltro,imgExpanOver.src,imgExpanOut.src);
      
      imgcontraerFiltro.title='Pulse para expandir';
    }
    if(typeof(CalcularContenidoBuscar)=='function')
      CalcularContenidoBuscar();
}


function ClickFiltroContraer(nombreFiltro)
{
    var divAExpandir=$get('DivFiltroBuscar');
    
    if (nombreFiltro == 'Contacto')
    {
        var filtro = divAExpandir.childNodes[0];
        var boton = $get('ctl00_ContentPlaceHolder1_FiltroContacto_btnContraer'); 
    }
    else   
    {
        var filtro = divAExpandir.childNodes[1];
        var boton = $get('ctl00_ContentPlaceHolder1_FiltroGrupo_btnContraer'); 
    }     
    Filtro_Subir = false;
    if (boton.src = "http://localhost:2389/PMS.Geco.WebUI/Images/Default/Buttons/BNSubirSeccion.gif")
    {
      CambiarImagen(boton,imgExpanOver.src,imgExpanOut.src);
      imgcontraerFiltro.title='Pulse para expandir';
    }
    boton.onclick();
//    if (filtro.style.height != 24)
//    {
//      divAExpandir.style.height=24 + 'px';
//      filtro.style.height=24 + 'px';
//      filtro.childNodes[1].style.display = 'none';
//      Filtro_Subir=true;
//      CambiarImagen(boton,imgExpanOver.src,imgExpanOut.src);
//      imgcontraerFiltro.title='Pulse para expandir';
//    
//    }   
    if(typeof(CalcularContenidoBuscar)=='function')
      CalcularContenidoBuscar();   
}


function PosicionarEnMedio(obj)
{

    var altoScreen = document.documentElement.clientHeight;
    var anchoScreen = document.documentElement.clientWidth;
    if (anchoScreen == 0)
        anchoScreen =window.screen.width;
    if (altoScreen == 0)
        altoScreen =window.screen.height;
        
    var anchoobj = obj.offsetWidth;
    var altoobj = obj.offsetHeight;
    if (altoScreen > altoobj && anchoScreen > anchoobj)
    {
        obj.style.top = ((((altoScreen - altoobj) * 2) - 2) / 2) /2;
        obj.style.left = ((((anchoScreen - anchoobj) * 2) - 2) / 2) /2;
    }
}

function crearModal(zindex)
{
    divSelector=document.createElement('div')
    divSelector.id="__divmodal";
    divSelector.className="jsWindowDivModal";
    divSelector.style.zIndex=zindex;
    divSelector.style.width= '100%';
    divSelector.style.height= '100%';
    document.body.appendChild(divSelector);
}

function eliminarModal()
{
  if(divSelector != null)
  {
      document.body.removeChild(divSelector);
      divSelector = null;
  }      
}

function textMaxLength(obj, maxLength, evt)
{
    var charCode=(evt.which) ? evt.which : event.keyCode
    var max = maxLength - 0;
    var text = obj.value;
    if(text.length > max)
    {
        var ignoreKeys = [8,46,37,38,39,40,35,36];
        for(i=0;i<ignoreKeys.length;i++)
        {
            if(charCode==ignoreKeys[i])
            {
            return true;
            }
        }
        return false;
    }
    else
    {
        return true;
    }
}
function SeleccionMenu(sender, e) 
{

}

function popup(dir)
{
    if (document.title == 'GeCo (Inicio)')
    {
        if (dir.indexOf('http:') == -1)
            dir = dir.substring(3, dir.length);
    }
    window.open(dir,'_blank');
}

function trim(s)
{
	return rtrim(ltrim(s));
}

function ltrim(s)
{
	var l=0;
	while(l < s.length && s.charAt(l) == " ")
	{	l++; }
	return s.substring(l, s.length);
}

function rtrim(s)
{
	var r=s.length -1;
	while(r > 0 && s.charAt(r) == " ")
	{	r-=1;	}
	return s.substring(0, r+1);
}


function QuitarScroll() {
    var obj = ctl00_SpliterMaster_pane_PanelInformacion.childNodes[0];
    obj.style.overflowY = 'auto';
}