﻿// Funciones para mensajes de aviso.

window.onload = function(){
                    links_externos();
                };
                
/*
window.onbeforeunload = function(){
                    inhabilitar_contexto();
                };
*/

function inhabilitar_contexto(opc){

    if(arguments.length == 0){
        opc = "1";
    }
    
    if(! document.getElementById("capaFondo" + opc)){
        var capa = document.createElement("DIV");

        capa.id = "capaFondo" + opc;

        document.body.appendChild(capa);
        
        document.body.tag = "capaFondo" + opc;
    }
    
    $(capa.id).fade('hide');
    $(capa.id).fade(0.7); 
}

function habilitar_contexto(){
    
    var capa_contexto = document.getElementById(document.body.tag);
    
    if(capa_contexto){
        $(capa_contexto.id).fade('out'); 

        document.body.removeChild(capa_contexto);
    }

    var capa_mensaje = document.getElementById("idMensaje_aviso");

    if(capa_mensaje){
        document.body.removeChild(capa_mensaje);
    }
}

function crea_mensaje_aviso(top, left, width, height, texto, opc, num_icono, icono_cerrar){

    inhabilitar_contexto(opc);
    
    var capa = document.createElement("DIV");

    capa.id               = "idMensaje_aviso";
    capa.style.position   = "absolute";

    if(top >= 0){
        capa.style.top        = top + "px";
    }else{
        capa.style.bottom     = (top * -1) + "px";
    }

    capa.style.left       = left + "px";
    capa.style.padding    = "15px";
    capa.style.width      = width + "px";
    if(height = "auto"){
        capa.style.height     = "auto";
    }else{        
        capa.style.height     = height + "px";
    }
    capa.className        = "cuadro_mensaje";
    capa.style.zIndex     = 99;

    var capa_icono = document.createElement("SPAN");

    capa_icono.id = "icono_mensaje_aviso";
    
    if(! num_icono){
        icono = 1;
    }

    if(num_icono == 1){
        capa_icono.className = "icono_mensaje_aviso_espera";
    }else{
        capa_icono.className = "icono_mensaje_aviso_informe";
    }
    
    capa.appendChild(capa_icono);

    var capa_texto = document.createElement("SPAN");

    capa_texto.id              = "idTexto_mensaje_aviso";
    capa_texto.style.float      = "left";
    capa_texto.style.width      = "auto";
    capa_texto.style.height     = "auto";
    capa_texto.style.margin     = "0 0 auto 10px";
    capa_texto.innerHTML        = texto;
    
    capa.appendChild(capa_texto);

    if(icono_cerrar == 1){
        var capa_cerrar = document.createElement("A");

        capa_cerrar.className        = "cerrar";
        capa_cerrar.style.float      = "left";
        capa_cerrar.style.margin     = "0 0 auto 15px";
        capa_cerrar.title            = "Cerrar";
        capa_cerrar.onclick          = function(){
                                        habilitar_contexto();
                                       };
                                       
        capa.appendChild(capa_cerrar);
    }
    
    document.body.appendChild(capa);

}

function cambia_icono_mensaje_aviso(opc){
    
    if(document.getElementById("icono_mensaje_aviso")){

        var icono = "icono_mensaje_aviso_espera";
        
        if(opc == 1){
            icono = "icono_mensaje_aviso_espera";
        }else if(opc == 2){
            icono = "icono_mensaje_aviso_informe";
        }

        document.getElementById("icono_mensaje_aviso").className = icono;
    }
}

function cambia_texto_mensaje_aviso(texto){
    if(document.getElementById("idTexto_mensaje_aviso")){
        document.getElementById("idTexto_mensaje_aviso").innerText = texto;
    }
}

function cambia_texto_mensaje_aviso_formato(texto){
    if(document.getElementById("idTexto_mensaje_aviso")){
        document.getElementById("idTexto_mensaje_aviso").innerHTML = texto;
    }
}
function elimina_mensaje_aviso(){
    
    var capa_mensaje = document.getElementById("idMensaje_aviso");

    if(capa_mensaje){
        document.body.removeChild(capa_mensaje);
    }
}

function alltrim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

function control_elemento(elemento,valor,texto){
	
	var elementos = elemento.split("#");
	var cadena;
		
	cadena = "alltrim(document.getElementById('" + elementos[0] + "').value) == valor";
	
	for(var i=1;i<elementos.length;i++){
		cadena = cadena + " && alltrim(document.getElementById('" + elementos[i] + "').value) == valor";
	}	

	if(eval(cadena)){
		if(texto != ""){
		    alert(texto);
		    document.getElementById(elementos[0]).focus();
        }
	    return false;
	}else{
		return true;
	}
}

// Joseba
// 04/05/2007
// Sustituir target's para que pueda ser validados por W3C
function links_externos(){
	if (! document.getElementsByTagName) return;

	var anclas = document.getElementsByTagName("a");

	for (var i=0; i<anclas.length; i++){
		var ancla = anclas[i];
        
		if (ancla.getAttribute("href") && ancla.getAttribute("rel") == "external"){
			ancla.target = "_blank";
			ancla.title = ancla.title  + " [Este enlace se abrirá en una nueva ventana]";
	}
	}
}

function imprimir_resultado(mensaje, funcion){

    var ret = imprimir_detalle(this);
    
	var tiempo = 2000;
	
	if(ret){
	    if(! mensaje){
	        mensaje = "Imprimiendo informe";
	    }    

        parent.cambia_icono_mensaje_aviso(3);
	    parent.cambia_texto_mensaje_aviso(mensaje + ", espere por favor ...");
	    tiempo = 6000;

	}else{
        parent.cambia_texto_mensaje_aviso("Cancelando impresión, espere por favor ...");
	}
	
	setTimeout("parent.habilitar_contexto()", tiempo);
	
	if(funcion){
	    setTimeout(eval('funcion'), tiempo);
	}
}

function ver_capa(capa, texto){
	
    habilitar_contexto();

    if(texto){
        $("idTexto_gracias").set("html", texto);
    }

	//transition effect
    inhabilitar_contexto();
    
	$(capa).fade('hide');
	$(capa).fade(1); 
}

function cerrar_capa(capa, reload){
	//transition effect		
	$(capa).fade(0);

    habilitar_contexto();
    
    if(reload){
        parent.location.reload();
    }
}

function descargar(fichero, ruta){

    var formu = document.getElementById("frm_descargas");
    
    if(! formu){
    
        formu = document.createElement("FORM");
        
        formu.id     = "frm_descargas";
        formu.action = "/web/exec/descarga_fichero.asp"
        formu.method = "post"
        formu.target = "nm_descargas"

        campo = document.createElement("INPUT");
        campo.type  = "hidden";
        campo.id    = "Fichero";
        campo.name  = "Fichero";
        formu.appendChild(campo);

        campo = document.createElement("INPUT");
        campo.type  = "hidden";
        campo.id    = "Ruta";
        campo.name  = "Ruta";
        formu.appendChild(campo);
        
        document.body.appendChild(formu);
   }
    
    document.getElementById("Fichero").value = fichero;
    document.getElementById("Ruta").value    = ruta;
    formu.submit();
    
}
