/*
..........................................................................
:: Menus desplegables de n-niveles                                      ::
..........................................................................
*/
function eventAssign(obj,event,func){
	if( obj.addEventListener ) {
		obj.addEventListener(event,func,false);
	}else if (obj.attachEvent) {
		obj['e'+event+func]=func;
		obj[event+func]=function(){
		    obj['e'+event+func]( window.event );
		}
		obj.attachEvent('on'+event,obj[event+func]);
	}else {
		alert('Este navegador no es soportado!!');
	}
}

function preparaEventosMenu() {
    hijosLI = $$("#nav li");
    for (i = 0; i < hijosLI.length; i++) {
        if (hijosLI[i].getElementsByTagName("UL").length) {
            eventAssign( hijosLI[i], "mouseover", function() { despliega(this) });
            eventAssign( hijosLI[i], "mouseout",  function() { pliega(this)    });
        }
    }
}

function despliega(padre) {
    clearTimeout(padre.temporizador);
    hijos = padre.getElementsByTagName("UL");
    hijos[0].style.display = "block";
	hijos[0].style.zIndex = 100;
}

function pliega(padre) {
    padre.temporizador = setTimeout(function() {
        hijos = padre.getElementsByTagName("UL");
        hijos[0].style.display = "none";
		hijos[0].style.zIndex = 1;
    }, 400)
}

Event.observe ( window, "load", preparaEventosMenu );

/*
..........................................................................
:: Comprobar email válido                                               ::
..........................................................................
*/
function validarEmail(sTesteo) {
    var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
    return reEmail.test(sTesteo);
}

/*
..........................................................................
:: Validar fecha                                                        ::
:: Formato correcto: dd/mm/aaaa                                         ::
..........................................................................
*/
function validarFecha( sFecha ) {
    var reFecha = /\b(0?[1-9]|[12][0-9]|3[01])\/([1-9]|0[1-9]|1[0-2])\/(19|20\d{2})/;
    return reFecha.test( sFecha );
}

/*
..........................................................................
:: Animacion de capas                                                   ::
:: Para usar con transiciones.js                                        ::
..........................................................................
*/
function animacion(objeto, propiedad, puntoPartida, puntoFinal) { // Requiere el uso de la librería prototype.js!!!
    var avance = 1; // Inicializa la animación (no cambiar)
    var pasos = 20; // Número de pasos de la animación
    var intervalo = 0.05; // Segundos de retraso entre un paso y otro de la animación
    var distancia = puntoFinal - puntoPartida;
    new PeriodicalExecuter(function(periodica) {
        var pos = Math.easeInOutQuint(avance, puntoPartida, distancia, pasos) + "px";
        objeto.style[propiedad] = pos;
        avance++;
        if (avance > pasos) {
            periodica.stop();
        }
    }, intervalo);
}

