   /*
    * Funciones con DIVS
    * PARA USAR LA NAVEGACIÓN MEDIANTE AJAX (opción de volver), se necesita acceso a common.inc.php
    */

    // Si se utiliza el call_llamadaAjaxTransparenciaInicio, se puede
    // indicar a que función se llamará después de cerrar la ventana con el botón
    var sCallBackFunction = "";
       
    /**
    * function call_WindowAgenteInicio()
    * sTitle -> título de la ventana
    * phpFunction -> función de php a la que se llamará
    * sParams -> parámetros
    * iWidth -> width de la ventana
    * iHeight -> height de la ventana
    * bClose -> botón de cerrar true o false
    */
    function call_WindowAgenteInicio(sTitle, sLoadingMsg, phpFunction, sParams, iWidth, iHeight, bClose, bResizable)
    {            
        $('#div_magico_cuerpo').dialog('destroy');
            
        $("#div_magico_cuerpo").dialog({
            bgiframe    : false,
            autoOpen    : false,
            height      : iHeight,
            width       : iWidth,
            modal       : true,            
            title       : sTitle,
            resizable   : bResizable
        });         
        
        // Si no se quiere el botón de cerrar
        if(!bClose)
        {            
            $(".ui-dialog-titlebar-close").css("display","none");            
        }
                
        $('#div_magico_cuerpo').dialog('open');
        
        $('#div_magico_cuerpo').html("<br/>"+sLoadingMsg);
        
        if(phpFunction!="")
        {
            agent.call('', phpFunction, 'callback_WindowAgenteInicio', sParams);
        }
    }
    
    function callback_WindowAgenteInicio(str)
    {               
        $('#div_magico_cuerpo').html(str);
    }
    
    function call_WindowAgenteFin()
    {
        $('#div_magico_cuerpo').dialog('close');
    }
    
       
    function getScreenSize()
    {
        var dimensions = {width: 0, height: 0};
        if (document.documentElement) 
        {
            dimensions.width = document.documentElement.offsetWidth;
            dimensions.height = document.documentElement.offsetHeight;
        } 
        else if (window.innerWidth && window.innerHeight) 
        {
            dimensions.width = window.innerWidth;
            dimensions.height = window.innerHeight;
        }        
        return(dimensions);
    }
    
  
    /**
    * function vResizeDivsMagicosInternos(divContent, div)
    */
    function vResizeDivsMagicosInternos(divContent, sDiv)
    {                
        var iWidth = document.getElementById(divContent).style.width;
        var iHeight = document.getElementById(divContent).style.height;
        
        iWidth  = Number(iWidth.replace("px", ""));
        iHeight = Number(iHeight.replace("px", ""));        
                
        iWidth  = iWidth  - 20;
        iHeight = iHeight - 60;
            
        if(iWidth > 20)
        {
            document.getElementById(sDiv).style.width  = (iWidth) + "px";
        }
        if(iHeight > 60)
        {
            document.getElementById(sDiv).style.height = (iHeight) + "px";                
        }        
    }
  
    /**
    * function goBack
    * esta función permite realizar una llamada a una función de AJAX 
    * consulta el array de navegación y vuelve a una posición atrás
    */
    function goBack()
    {
        str        = agent.call('','common__getUrlBack','');      
        str        = removeChar(str);
        aValores   = str.split('|');
        // alert(str);            
        // alert("Función : "+aValores[0]);
        // alert("Param   : "+aValores[1]);        
        var sBackUrl = "";
        if(aValores[0]!="")
        {
            // Montamos la lista de parámetros que necesitará la función
            var sParams = "";
            aParams     = aValores[1].split(',');
            for(i=0; i<aParams.length; i++)
            {
                if(sParams=="")
                {
                    sParams = '"'+aParams[i]+'"';
                }
                else
                {
                    sParams = sParams+',"'+aParams[i]+'"';
                }
            }        
            if(sParams!=='""')
            {            
                var sEval = aValores[0]+'('+sParams+')';
            }
            else
            {         
                var sEval = aValores[0]+'()';
            }        
            eval(sEval);
        }
        else
        {
            call_llamadaAjaxTransparenciaFin();
        }
    }
 
    /**
    * function soloUnDiv(sDivName, sNameComun)
    * se utiliza para cerrar divs que pueden contener parte
    * de un nombre igual y que se pueden ocultar o 
    
    
    * para ocultar todos, se pasa un nombre base común a todos ellos
    * para poder cerrar los que estan abiertos.
    * por ejemplo : 'divDestinatarios_1' y 'divDestinatarios_2',
    * con este método se pueden cerrar todos los que tengan como nombre 'divDestinatarios_xxxxx'
    *
    */
    function soloUnDiv(sDivName, sNameComun)
    {
        if(document.getElementById(sDivName).style.visibility=="visible")
        {
            ocultarDiv(sDivName);
        }
        else
        {
            cierraDivsComunes(sNameComun);
            vOcultarMostrarDiv(sDivName);
        }
    }
 
    /**
    * function cierraDivsComunes(sName)
    * cierra todos los divs que en el nombre tengan una semejanza con sName
    */
    function cierraDivsComunes(sNameComun)
    {
        var aDivs = document.getElementsByTagName("div");        
        for(i=0; i<aDivs.length; i++)
        {            
            aValores = aDivs[i].id.split('_');                    
            
            if(aValores[0]===sNameComun)
            {                                
                ocultarDiv(aDivs[i].id);
            }
        }
    }
 
    /**
    * function mueveDivMagico()
    * mueve el div mágico para centrarlo cuando hay un scroll
    */
    function mueveDivMagico()
    {
        // divPantallaCompleta('div_transparente');
        centrarDiv('div_magico'); 
    }
 
    /**
    * function vResizeDivsMagicos()
    * si hay un resize del windows
    * hago un resize de los divs para que no queden trozos sin desactivar
    */
    function vResizeDivsMagicos()
    {
        // Ponemos el Div transparente a pantalla completa de nuevo
        divPantallaCompleta('div_transparente');

        // Centramos el div en la pantalla
        centrarDiv('div_magico');
    }

    /**
    * function OcultarMostrarDiv()
    * Oculta o muestra el div pasado en el parámetro object
    */
    function vOcultarMostrarDivAnim(sObject)
    {        
        //alert("Aqui");
        // if(document.getElementById(sObject).style.visibility != 'hidden')
        if ($("#"+sObject).is(":hidden"))
        {                     
            // mostrarDiv(sObject);        
            $("#"+sObject).css("visibility","visible");
            $("#"+sObject).slideDown("slow");
        }
        else
        {                                        
            // ocultarDiv(sObject);
            $("#"+sObject).slideUp("slow", function() { $("#"+sObject).css("visibility","hidden"); } );        
            // $("#"+sObject).slideUp("slow", function(){ ocultarDiv(sObject); } );        
        }            
    }
    
    /**
    * function vOcultarMostrarDiv(sObject)
    */
    function vOcultarMostrarDiv(sObject)
    {
        if(document.getElementById(sObject).style.visibility != 'hidden')
        {        
            ocultarDiv(sObject);
        }
        else
        {                                        
            mostrarDiv(sObject);                                
        }            
    }
 
    /**
    * function mostrarDiv()
    * muestra un div
    * sDiv -> nombre de la capa a mostrar
    */
    function mostrarDiv(sDiv)
    {    
        document.getElementById(sDiv).style.display     = 'block';
        document.getElementById(sDiv).style.visibility  = 'visible';        
    }

    /**
    * function ocultarDiv()
    * oculta un div
    * sDiv -> nombre de la capa a ocultar
    */
    function ocultarDiv(sDiv)
    {
        document.getElementById(sDiv).style.display     = 'none';
        document.getElementById(sDiv).style.visibility  = 'hidden';        
    }

    /**
    *
    */
    function call_llamadaProcesoInicio(sMensajeCargando)
    {
        divPantallaCompleta('div_transparente');
        mostrarDiv('div_transparente');
        toggleSelect('div_transparente');
        $("#div_magico_cuerpo").html("");
        $("#div_magico_cabecera").html("");
        $("#div_magico_titulo").html("");
        centrarDiv('div_proceso');
        $("#div_proceso_mensaje").html(sMensajeCargando);
        $("#div_transparente").fadeTo("slow", "0.8", function(){                     
            mostrarDiv('div_proceso');            
        });
    }
    
    function call_llamadaProcesoFin()
    {
        ocultarDiv('div_proceso');        
        $("#div_transparente").fadeTo("slow", 0.0, function() { 
                                                            // Reiniciamos el array de navegacion de AJAX                                                            
                                                            toggleSelect('div_transparente');
                                                            ocultarDiv('div_transparente');
                                                         });
    }
        
    /**
    * function llamadaAjaxTransparenciaInicio()
    * se utiliza para llamar a funciones de PHP de forma general
    * mostrando un mensaje de cargando y desactivando todas las capas que esten por debajo
    * mostrará el "div_magic"
    * sMensajeCargando -> mensaje que se muestra mientras se carga el div
    * sParams          -> string con los parametros que se pasan al php separados por '|'
    * iWidth           -> width del div mágico
    * iHeight          -> height del div mágico
    * bShowClose       -> indica si se muestra el botón de cerrar
    * sPath            -> path donde están las imágenes del botón de cerrar, etc...
    */
    function call_llamadaAjaxTransparenciaInicio(sMensajeCargando, sPhpFunction, sParams, iWidth, iHeight, bShowClose, sPath)
    {                
        divPantallaCompleta('div_transparente');
        
        // Mostramos la capa transparente para desactivar los controles que estén debajo
        mostrarDiv('div_transparente');    

        centrarDivVertical('div_magico_loading');
        mostrarDiv('div_magico_loading');
                
        $("#div_magico_cuerpo").html("");
        $("#div_magico_cabecera").html("");
        $("#div_magico_titulo").html("");    
        
        // Oculto los selects que están por debajo
        toggleSelect('div_principal');
                
        $("#div_transparente").fadeTo("slow", "0.8", function(){         
            mostrarDiv('div_magico');
            resizeWithTransparency('div_magico', iWidth, iHeight, sMensajeCargando, sPath, sPhpFunction, sParams, bShowClose);            
        } );        
    }
        
    /**
    * function callback_llamadaAjaxTransparenciaInicio()
    * recibe los resultados después de una llamada a Ajax con transparencia
    */
    function callback_llamadaAjaxTransparenciaInicio(str)
    {            
        ocultarDiv('div_magico_loading');        
        $("#div_magico_cuerpo").html(str);
    }

    /**
    * function call_llamadaAjaxTransparenciaFin()
    * finaliza la llamada a un ajax con transparencia, sólo oculta los divs
    */
    function call_llamadaAjaxTransparenciaFin()
    {
        // Mostramos los selects que están debajo
        toggleSelect('div_transparente');
        
        // Si se requiere llamar a alguna función
        // después de cerrar la ventana, se llama        
        if(sCallBackFunction!="")
        {
            eval(sCallBackFunction);
            sCallBackFunction="";
        }        
        ocultarDiv('div_magico');    
        ocultarDiv('div_magico_loading');    
    
        $("#div_transparente").fadeTo("slow", 0.0, function() { 
                                                            // Reiniciamos el array de navegacion de AJAX
                                                            agent.call('','common__sResetUrlBack','');                                                             
                                                            ocultarDiv('div_transparente');                                                            
                                                         });
    }

    /**
    * function divPantallaCompleta()
    * Pone un div en pantalla completa
    */
    function divPantallaCompleta(sDiv)
    {      
        // Obtengo el tamaño de la pantalla
        var arrPageSize = ___getPageSize();    
    
        // Cambio las propiedades del div para que ocupe toda la pantalla
        document.getElementById(sDiv).style.width   = arrPageSize[0] + "px";
        document.getElementById(sDiv).style.height  = arrPageSize[1] + "px";            
    }

    /**
    * function centrarDivVertical(sDiv)
    * centra un div verticalmente
    */
    function centrarDivVertical(sDiv)
    {        
        // Obtengo las propiedades de la scroll
        var arrPageScroll   = ___getPageScroll(); 
        var oDiv            = document.getElementById(sDiv);
        var alto_div        = oDiv.style.height;    
        alto_div            = alto_div.replace("px","");            
        // Centro el div    
        // document.getElementById(sDiv).style.marginLeft  = arrPageScroll[0] - (ancho_div/2) + "px";
        document.getElementById(sDiv).style.marginTop   = arrPageScroll[1] - (alto_div/2) + "px";    
    }

    /**
    * function centrarDiv(sDiv)
    * Centra un div en pantalla tanto vertical como horizontal
    */
    function centrarDiv(sDiv)
    {
        // Obtengo las propiedades de la scroll
        var arrPageScroll = ___getPageScroll();
    
        var oDiv        = document.getElementById(sDiv);
        var ancho_div   = oDiv.style.width;
        var alto_div    = oDiv.style.height;
    
        // Obtengo el tamaño del div
        // var ancho_div     = document.getElementById(sDiv).offsetWidth;    
        // var alto_div      = document.getElementById(sDiv).offsetHeight;

        ancho_div   = ancho_div.replace("px","");
        alto_div    = alto_div.replace("px","");    
        
        // alert(sDiv + '[' + ancho_div + ']');
        
        // Centro el div    
        document.getElementById(sDiv).style.marginLeft  = arrPageScroll[0] - (ancho_div/2) + "px";
        document.getElementById(sDiv).style.marginTop   = arrPageScroll[1] - (alto_div/2) + "px";            
    }

    /**
    * function ___getPageScroll()
    * obtiene las propiedades de la scroll
    * Sacado del código de jQuery
    */
    function ___getPageScroll() 
    {
        var xScroll, yScroll;
        if (self.pageYOffset)
        {
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
        } 
        else if (document.documentElement && document.documentElement.scrollTop) 
        {     // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
        } 
        else if (document.body) 
        {
            // all other Explorers
            yScroll = document.body.scrollTop;
            xScroll = document.body.scrollLeft;    
        }            
        arrayPageScroll = new Array(xScroll,yScroll);
        return arrayPageScroll;
    }

    /**
    * Obtiene el tamaño de la página
    * Sacado del código del jQuery
    * THIRD FUNCTION
    * getPageSize() by quirksmode.com
    *
    * @return Array Return an array with page width, height and window width, height
    */
    function ___getPageSize() 
    {
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY)
        {    
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        }
        else if (document.body.scrollHeight > document.body.offsetHeight)
        { 
            // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        }
        else
        {   
            // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        var windowWidth, windowHeight;
        if (self.innerHeight)
        {    
            // all except Explorer
            if(document.documentElement.clientWidth)
            {
                windowWidth = document.documentElement.clientWidth; 
            } 
            else
            {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } 
        else if (document.documentElement && document.documentElement.clientHeight) 
        { 
            // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }
        else if (document.body) 
        { 
            // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }    
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight)
        {
            pageHeight = windowHeight;
        }
        else
        { 
            pageHeight = yScroll;
        }
        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth)
        {    
            pageWidth = xScroll;        
        }       
        else
        {
            pageWidth = windowWidth;
        }
        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
        return arrayPageSize;
    }

    /**
    * function resize()
    */
    function resize(sDiv, toWidth, toHeight)
    {
        var oDiv    = document.getElementById(sDiv);
        var iWidth  = Number(oDiv.style.width.replace("px", ""));
        var iHeight = Number(oDiv.style.height.replace("px", ""));        
        if(iWidth=="")
        {
            iWidth = 0;
        }
        if(iHeight=="")
        {
            iHeight = 0;
        }
        
        if(iWidth<toWidth)
        {     
            if(iWidth+20 > toWidth)
            {
                iWidth = toWidth;
            }
            else
            {
                iWidth = iWidth+20;
            }
        }
        else
        {        
            if(iWidth-20 < toWidth)
            {
                iWidth = toWidth;
            }
            else
            {
            
                iWidth = iWidth-20;
            }
        }
        
        if(iHeight<toHeight)
        {                
            if(iHeight + 20 > toHeight)
            {
                iHeight = toHeight;
            }
            else
            {    
                iHeight = iHeight+20;
            }        
        }
        else
        {
            if(iHeight-20 < toHeight)
            {
                iHeight = toHeight;
            }
            else
            {
                iHeight = iHeight-20;
            }
            
        }    
        oDiv.style.width =  (iWidth)+"px";
        oDiv.style.height = (iHeight)+"px";
        
        document.getElementById('div_magico_cuerpo').style.height = (iHeight-80)+"px";
                
        if( (iWidth != toWidth) || (iHeight != toHeight) )
        {
            centrarDiv(sDiv)
            setTimeout('resize("'+sDiv+'","'+toWidth+'","'+toHeight+'")', 1);     
        }
    }
    
    
    /**
    * function resize()
    */
    function resizeWithTransparency(sDiv, toWidth, toHeight, sMensajeCargando, sPath, sPhpFunction, sParams, bShowClose)
    {        
        var oDiv    = document.getElementById(sDiv);
        var iWidth  = Number(oDiv.style.width.replace("px", ""));
        var iHeight = Number(oDiv.style.height.replace("px", ""));        
        if(iWidth=="")
        {
            iWidth = 0;
        }
        if(iHeight=="")
        {
            iHeight = 0;
        }
        
        if(iWidth<toWidth)
        {     
            if(iWidth+20 > toWidth)
            {
                iWidth = toWidth;
            }
            else
            {
                iWidth = iWidth+20;
            }
        }
        else
        {        
            if(iWidth-20 < toWidth)
            {
                iWidth = toWidth;
            }
            else
            {
            
                iWidth = iWidth-20;
            }
        }
        
        if(iHeight<toHeight)
        {                
            if(iHeight + 20 > toHeight)
            {
                iHeight = toHeight;
            }
            else
            {    
                iHeight = iHeight+20;
            }        
        }
        else
        {
            if(iHeight-20 < toHeight)
            {
                iHeight = toHeight;
            }
            else
            {
                iHeight = iHeight-20;
            }
            
        }    
        oDiv.style.width =  (iWidth)+"px";
        oDiv.style.height = (iHeight)+"px";        
        // document.getElementById('div_magico_cuerpo').style.height = (iHeight-70)+"px";
        // alert('From width ['+iWidth+'] to ['+toWidth+'] and height from ['+iHeight+'] to ['+toHeight+']');
        // alert("["+bShowClose+"]");
        if( (iWidth != toWidth) || (iHeight != toHeight) )
        {
            centrarDiv(sDiv)
            setTimeout('resizeWithTransparency("'+sDiv+'","'+toWidth+'","'+toHeight+'","'+sMensajeCargando+'","'+sPath+'","'+sPhpFunction+'","'+sParams+'","'+bShowClose+'")', 1);
        }
        else
        {              
            document.getElementById('div_magico_cuerpo').style.height = (iHeight-80)+"px";
            
            // Ponemos la capa transparente a pantalla completa
            divPantallaCompleta('div_transparente');
            
            // Mostramos mensaje de cargando
            if( removeChar(bShowClose)==="true" || removeChar(bShowClose)=="true" )
            {                                           
                $("#div_magico_cabecera").html('<table cellspacing="0" cellpadding="4" width="100%" bgcolor="#213343"><tbody><tr><td align="left" style="border-bottom: 4px solid #2c4526;"></td><td align="right" style="border-bottom: 4px solid #213343; text-align:right"><a href="#null" onclick="call_llamadaAjaxTransparenciaFin()"><img border="0" title="Cerrar ventana" src="./img/cerrar_new.gif"/></a></td></tr></tbody></table>');                
            }                
            
            // Llamamos a la función de php correspondiente
            if (sPhpFunction != '')
            {
                $("#div_magico_cuerpo").html(sMensajeCargando+'<br />'+'<img src="'+sPath+'cargando.gif">');        
                agent.call('', sPhpFunction, 'callback_llamadaAjaxTransparenciaInicio', sParams);
            }
            else
            {
                $("#div_magico_cuerpo").html("<br /><b>"+sMensajeCargando+'</b><br /><br />'+'<img src="./img/procesando_informacion.gif">');
            }        
        }
    }