﻿//FILTRO
function filtrarLista () {
	document.getElementById('cargando').style.display = '';

	var expresion = /^\s+|\s+$/g;
	var expA = /[á|à]/gi;
	var expE = /[é|è]/gi;
	var expI = /[í|ì]/gi;
	var expO = /[ó|ò]/gi;
	var expU = /[ú|ù|Ü]/gi;
	
	var busqueda = document.filtro.criterio.value.toUpperCase().replace(expresion,'');
		busqueda = busqueda.replace(expA,"A");
		busqueda = busqueda.replace(expE,"E");
		busqueda = busqueda.replace(expI,"I");
		busqueda = busqueda.replace(expO,"O");
		busqueda = busqueda.replace(expU,"U");
		
	var f = true;
	var fondo = "";
	var contador;
	var puntero;
	
	contador = 0;
	
	if (busqueda == "INGRESE UN CRITERIO") {
		document.getElementById('cargando').style.display = 'none';
		alert('No ha ingresado Ningun Criterio de Busqueda');
		document.filtro.criterio.focus();
		return false;
	}
	
	puntero = document.getElementById("rubrosContenedor").firstChild;
	while (puntero != null){
		if (puntero.tagName == "a" || puntero.tagName == "A"){
			
			var auxRubro = puntero.innerHTML.toUpperCase();
			auxRubro = auxRubro.replace(expA,"A");
			auxRubro = auxRubro.replace(expE,"E");
			auxRubro = auxRubro.replace(expI,"I");
			auxRubro = auxRubro.replace(expO,"O");
			auxRubro = auxRubro.replace(expU,"U");

			
			if (filtro(auxRubro,busqueda)) {
				puntero.style.display = "";
				f = !f;
				if (f) fondo = "rgb(245,245,245)";
				else fondo = "#FFFFFF";
				puntero.style.background = fondo;
				contador++;
			}else{
				puntero.style.display = "none";
			}
		}
		puntero = puntero.nextSibling;
	}
	
	document.getElementById('cargando').style.display = 'none';
	if (busqueda != "") {
		document.getElementById('buscador').style.display = 'none';
		document.getElementById('resumenBusqueda').innerHTML = 	'Su Busqueda: "'+document.filtro.criterio.value+'"<br>Se encontraron: '+contador+' coincidencias<br>';
		document.getElementById('resumenBusqueda').style.display = '';
		document.getElementById('nuevaBusqued').style.display = '';
	}
}//fin Funcion

function nuevaBusqueda(){
	document.filtro.criterio.value = '';
	filtrarLista();
	document.filtro.criterio.value = 'Ingrese un Criterio';
	document.getElementById('buscador').style.display = '';
	document.getElementById('resumenBusqueda').style.display = 'none';
	document.getElementById('nuevaBusqued').style.display = 'none';
}//fin Funcion

function filtro (nombre, busqueda) {
	return (nombre.indexOf(busqueda) != -1);
}

function limpiarCriterio() {
	if (document.filtro.criterio.value == "Ingrese un Criterio") {
		document.filtro.criterio.value = "";
	}
}
