﻿// 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;
    }
}

function habilitar_contexto(){
    
    var capa_contexto = document.getElementById(document.body.tag);
    
    if(capa_contexto){
        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";
    capa.style.top        = top + "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";
    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 0";
    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;
	}
}

function ocultar_selects(){

    if(navigator.appVersion.indexOf("MSIE 6") > 0){
	    for(var i=0; i<document.getElementsByTagName("SELECT").length; i++){
		    document.getElementsByTagName("SELECT")(i).style.visibility = "hidden";
	    }

        for (var j=0; j<document.frames.length; j++){
            var marco = document.frames[j];

            if(marco){
	            for(var i=0; i<marco.document.getElementsByTagName("SELECT").length; i++){
		            marco.document.getElementsByTagName("SELECT")(i).style.visibility = "hidden";
	            }
            }        
            
        }
    }
}

function mostrar_selects(){
    if(navigator.appVersion.indexOf("MSIE 6") > 0){
	    for(var i=0; i<document.getElementsByTagName("SELECT").length; i++){
		    document.getElementsByTagName("SELECT")(i).style.visibility = "visible";
	    }

        for (var j=0; j<document.frames.length; j++){
            var marco = document.frames[j];

            if(marco){
	            for(var i=0; i<marco.document.getElementsByTagName("SELECT").length; i++){
		            marco.document.getElementsByTagName("SELECT")(i).style.visibility = "visible";
	            }
	        }
            
        }
    }
}


// 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]";
		}
	}
}
