function cambiarImagenSeccion(seccion, accionUsuario){
    window.status = " ";
    var directorioImagenes = "./graficos/";
    var extensionImagenes = ".jpg";

    var indice;
    for (indice = 0; indice < document.images.length; indice++){
        if (document.images[indice].name == seccion){
            // Determinación del inicio del nombre del fichero .jpg visualizado en la imagen correspondiente a la sección
            var offset = 0;
            var posicionInicial = 0;
            
            offset = document.images[indice].src.lastIndexOf("/");
            if (offset != -1)
                posicionInicial = offset + 1;

            // Determinación de la posición final del nombre del fichero .jpg
            var posicionFinal = document.images[indice].src.lastIndexOf(".") - 1;

            var sufijoFichero = document.images[indice].src.substring(posicionFinal, posicionFinal + 1);
            var nombreFichero = document.images[indice].src.substring(posicionInicial, posicionFinal);

            switch (sufijoFichero) {
                case "n" :
                    document.images[indice].src = directorioImagenes + nombreFichero + "p" + extensionImagenes;
                    break;
                case "p" :
                    if (accionUsuario == 1){
                        document.images[indice].src = directorioImagenes + nombreFichero + "n" + extensionImagenes;
                    }
                    else {
                        restablecerBarraSecciones(directorioImagenes, extensionImagenes);
                        restablecerOpcionesI();
                        document.images[indice].src = directorioImagenes + nombreFichero + "s" + extensionImagenes;
                    }
                    break;
                case "s" :
                    break;
            }
            break;
        }
    }
    return;
}

function restablecerBarraSecciones(directorioImagenes, extensionImagenes){
    for (i = 2; i <= 12; i++){
        posicionInicial = 0;
        offset = document.images[i].src.lastIndexOf("/");
        if (offset != -1)
            posicionInicial = offset + 1;
        posicionFinal = document.images[i].src.lastIndexOf(".") - 1;

        var fichero = document.images[i].src.substring(posicionInicial, posicionFinal);
        document.images[i].src = directorioImagenes + fichero + "n" + extensionImagenes;
        
    }
    return;
}

function cargarContenido1(seccion){
    window.status = " ";
    if (seccion != 10)
        location.href="archivotoarchivetransfer.php?seccion=" + seccion;
    else
        location.href="categoriatocategorytransfer.php";
    return;
}

function cargarContenido(pagina1, pagina2, iframePrimario, iframeSecundario){
    window.status = " ";
    document.getElementById(iframePrimario).src=pagina1;
    document.getElementById(iframeSecundario).src=pagina2;
    return;
}

function opcionesIOverOutClick(opcionI, accion){
    window.status = " ";
    var elemento1 = opcionI + "td";
    var elemento2 = opcionI + "a";
    
    switch (accion){
        case 1 :
            if (document.getElementById(elemento2).className != "lsclick"){
                document.getElementById(elemento1).className = "tdsover";
                document.getElementById(elemento2).className = "lsover";
            }
            break;
        case 2 :
            if (document.getElementById(elemento2).className != "lsclick"){
                document.getElementById(elemento1).className = "tds";
                document.getElementById(elemento2).className = "ls";
            }
            break;
        case 3 :
            if (document.getElementById(elemento2).className != "lsclick"){
                restablecerBarraSecciones("./graficos/", ".jpg");
                restablecerOpcionesI();
                //document.getElementById(elemento1).className = "tdsover";
                document.getElementById(elemento2).className = "lsclick";
            }
            break;
    }
    return;
}

function restablecerOpcionesI(){
    window.status = " ";
    var columnas = document.getElementsByTagName('td');
    for (var i = 0; i < columnas.length; i++){
        if (columnas[i].name == "opcionesITd")
            columnas[i].className = "tds";
    }
    var enlaces = document.getElementsByTagName('a');
    for (i = 0; i < enlaces.length; i++){
        if (enlaces[i].name == "opcionesIA")
            enlaces[i].className = "ls";
    }
    return;
}

function resizeContenido(contenidoId){
    if (document.all){
        document.getElementById(contenidoId).style.height = window.frames.eval(contenidoId).document.body.scrollHeight;
        document.getElementById(contenidoId).style.width = window.frames.eval(contenidoId).document.body.scrollWidth;
    }
    else {
        document.getElementById(contenidoId).style.height = window.frames.eval(contenidoId).document.body.offsetHeight;
        document.getElementById(contenidoId).style.width = window.frames.eval(contenidoId).document.body.offsetWidth;
    }
    return;
}

var maxAncho; //define la anchura máxima de la imagen
var maxAlto; //define la altura máxima de la imagen
function mostrarImagenDeArchivo(ancho, alto, imagen, contenedorObjetivo) {
    if (ancho > alto) {forma = 'apaisada';}
    if (ancho < alto) {forma = 'retrato';}
    if (ancho == alto) {forma = 'cuadrado';}

    document.getElementById(contenedorObjetivo.name).src = imagen;

    if(forma == 'apaisada') {
        porcientoAnchura = parseInt(maxAncho/ancho*100);
		document.getElementById(contenedorObjetivo.name).width = maxAncho;
		document.getElementById(contenedorObjetivo.name).height = porcientoAnchura*alto/100;
	}
    else if (forma == 'retrato') {
        porcientoAltura = maxAlto/alto*100;
        document.getElementById(contenedorObjetivo.name).height = maxAlto;
        document.getElementById(contenedorObjetivo.name).width = porcientoAltura*ancho/100;
	}
    else if (forma == 'cuadrado') {
        document.getElementById(contenedorObjetivo.name).height = maxAlto;
		document.getElementById(contenedorObjetivo.name).width = maxAlto;
	}
} // FIN: mostrarImagenDeArchivo(ancho, alto, imagen)

function redimensionarImagenDeAnuncio(contenedor)
{
    maxAncho = 86;
    maxAlto = 60;
    document.write('<div id="wrapper" style="position:absolute; width:1px; height:1px; z-index:1; overflow: hidden; visibility: hidden;"><img src="' + document.getElementById(contenedor).src + '" name="contenedoroculto"  onload="mostrarImagenDeArchivo(this.width, this.height, this.src, ' + eval(contenedor).name + ')"></div>');
    return;
}

function redimensionarImagenDeArchivo(contenedor)
{
    maxAncho = 200;
    maxAlto = 140;
    document.write('<div id="wrapper" style="position:absolute; width:1px; height:1px; z-index:1; overflow: hidden; visibility: hidden;"><img src="' + document.getElementById(contenedor).src + '" name="contenedoroculto"  onload="mostrarImagenDeArchivo(this.width, this.height, this.src, ' + eval(contenedor).name + ')"></div>');
    return;
}

function redimensionarImagenDeNoticia(contenedor)
{
    maxAncho = 450;
    maxAlto = 315;
    document.write('<div id="wrapper" style="position:absolute; width:1px; height:1px; z-index:1; overflow: hidden; visibility: hidden;"><img src="' + document.getElementById(contenedor).src + '" name="contenedoroculto"  onload="mostrarImagenDeArchivo(this.width, this.height, this.src, ' + eval(contenedor).name + ')"></div>');
    return;
}

function albricia() {
    return;
}

function publicidad() {
    return;
}

function validarNif(nif) {
	var temp=nif.toUpperCase();
	var cadenaDNI="TRWAGMYFPDXBNJZSQVHLCKE";

	if (temp !== ''){
		//si no tiene un formato valido devuelve error
		if (!/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			return 0;
		}

		//comprobacion de NIFs estandar
		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = nif.substring(8,0) % 23;
			letra = cadenaDNI.charAt(posicion);
			var letraDNI=temp.charAt(8);
			if (letra == letraDNI)
			{
			   	return 1;
			}
			else
			{
				return -1;
			}
		}
    }
    return 0;
}

function validarEmail(email){
    // /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
    // /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3,4})+$/
    if(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/.test(email))
        return true;
    else
        return false;
}

function validarComentario(formulario){
    var mensajeValidacion = "Para enviar un comentario debe completar todos los campos del formulario.\nPor favor, teclee los siguientes datos:\n";
    var primerDatoIncompleto = 0;
    
    if(formulario.nombre.value == ""){
        mensajeValidacion += "   - Nombre\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 1;
    }
    if(formulario.nif.value == ""){
        mensajeValidacion += "   - NIF\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 2;
    }
    if(formulario.telefono.value == ""){
        mensajeValidacion += "   - Tel\u00e9fono\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 3;
    }
    if(formulario.email.value == ""){
        mensajeValidacion += "   - Correo electr\u00f3nico\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 4;
    }
    if(formulario.ciudad.value == ""){
        mensajeValidacion += "   - Ciudad\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 5;
    }
    if(formulario.titulo.value == ""){
        mensajeValidacion += "   - T\u00edtulo\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 6;
    }
    if(formulario.texto.value == ""){
        mensajeValidacion += "   - Texto\n";
        if(primerDatoIncompleto == 0)
            primerDatoIncompleto = 7;
    }

    if(primerDatoIncompleto == 0)
        if(validarNif(formulario.nif.value) != 1){
            alert ("El NIF no es correcto.");
            formulario.nif.focus();
            return false;
        }
        else if (!validarEmail(formulario.email.value)){
            alert ("El correo electr\u00f3nico no es correcto.");
            formulario.email.focus();
            return false;
        }
        else {
            return true;
        }
    else {
        alert (mensajeValidacion);
        switch (primerDatoIncompleto){
            case 1:
                formulario.nombre.focus();
                break;
            case 2:
                formulario.nif.focus();
                break;
            case 3:
                formulario.telefono.focus();
                break;
            case 4:
                formulario.email.focus();
                break;
            case 5:
                formulario.ciudad.focus();
                break;
            case 6:
                formulario.titulo.focus();
                break;
            case 7:
                formulario.texto.focus();
                break;
        }
        return false;
    }
}
