/*!
 * Master JavaScript Library v1.4
 *
 * Copyright 2010, Joseba Rodríguez
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Date: 09/04/2010 
 */

function descargar(fichero, ruta){

    var iframe = document.getElementById("id_descargas");

    if(! iframe){
    
        //iframe = document.createElement("<iframe id='id_descargas' name='nm_descargas'>");
        iframe = document.createElement("iframe");
        
        iframe.id       = "id_descargas";
        iframe.name     = "nm_descargas"
        iframe.style.witdth = "0px";
        iframe.style.height = "0px";

        document.body.appendChild(iframe);
   }

    var formu = document.getElementById("frm_descargas");
    
    if(! formu){
    
        formu = document.createElement("FORM");
        
        formu.id     = "frm_descargas";
        formu.action = "/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();
    
    return false;
}

// Funciones para mensajes de aviso.

window.onload = function(){
                    links_externos();
                };
                
/*
window.onbeforeunload = function(){
                    inhabilitar_contexto();
                };
*/

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();
    
}
