// busqueda_rapida.js
// $Id: busqueda_rapida.js,v 1.2 2007/08/23 19:09:43 jesteves Exp $

var by_button = false;

function get_selected_type() {

	// si el tipo del control "tipo" es "radio" quiere decir que solamente
	// hay un botón de radio (el de búsqueda en el catálogo) y no un
	// arreglo: se hay descativado todos los sitios)
	if (document.forma.tipo.type == "radio") {
		return "catalogo";
	}
	else {
		for (i = 0; i < document.forma.tipo.length; i++) {
			if (document.forma.tipo[i].checked) {
				return document.forma.tipo[i].value;
			}
		}
	}

}
	
function get_selected_option(S) {

	index = S.selectedIndex;
	option = S.options[index].value;

	return option;

}
	
function set_index_and_submit(C) {
	document.forma.indice.value = C;
			
	by_button = true;

	if (document.forma.buscar.value == "") {
		alert("No se ha ingresado un término a buscar");
		by_button = false;
	}
	else {
		switch (get_selected_type()) {

			case "catalogo" :
				if (br_validate()) {
					document.forma.submit();
				}

				break;

			case "otros_catalogos" :
				select = document.forma.otro_catalogo_escape;
				var catalogo = get_selected_option(select);

				if (catalogo == "seleccione") {
					alert("No ha seleccionado el catálogo\n" +
					      "en que desea realizar la búsqueda");
				}
				else {
					var criterio;
					criterio = 'palabra_clave';

					if (document.forma.criterio.type == "hidden") {
						criterio = document.forma.criterio.value;
					}
					else {
						if (document.forma.criterio[0].checked) {
							criterio = 'alfabeticamente';
						}
					}

					if ((criterio == 'alfabeticamente') && (catalogo.match(/Z39.?50/i))) {
						alert("Lo sentimos.  Esa operación no ha sido implementada");
					}
					else {
						document.forma.submit();
					}
				}

				break;

			case "web" :
				alert("La búsqueda en web se inicia " +
				      "con el botón [>]");

				break;

		}
	}
}

function search_the_web() {
	if (get_selected_type() == "web") {
		option = get_selected_option(document.forma.web);
					
		if (option != "seleccione") {
			if (document.forma.buscar.value == "") {
				alert("No se ha ingresado un término a buscar");
			}
			else {
				var q = escape(document.forma.buscar.value);

				param = "directories=no,menubar=no,toolbar=yes,location=no,scrollbars=yes,resizable=yes,width=" + 650 + ",height=" + 330;
				url = option + q ;
				Window = window.open('', "janium_search_the_web", param);
				Window.location.href = url ; 
				if (!Window.opener) {
					Window.opener = self;
				}
			}
		}
		else {
			alert("Primero seleccione un portal");
		}
	}
	else {
		alert("No se ha seleccionado la búsqueda en web");
	}
}

function br_validate() {
	if (document.forma.buscar.value == "") {
		alert("No se ha ingresado un término a buscar");
		return false;
	}
	else {
		//document.forma.buscar.value =
		//	document.forma.buscar.value.replace(/&/g, "");
		//document.forma.buscar.value =
		//	document.forma.buscar.value.replace(/\'/g, "");
		document.forma.buscar.value =
			document.forma.buscar.value.replace(/^[ ]+/g, "");
		document.forma.buscar.value =
			document.forma.buscar.value.replace(/[ ]+$/g, "");

		var v, va;
		v = document.forma.buscar.value;
		v = v.replace(/[()]+/g, "");
		v = v.replace(/[ ]+/g, " ");
		v = v.replace(/[-]+/g, "-");
		v = v.replace(/\/+/g, "/");
		v = v.replace(/[ ](and|or|not)[ ]/gi, " ");
		va = v.split(/[ -\/]/);

		if (RDBMS != 1) {
			if (va.length > LIMITE_TERMINOS) {
				alert("Por favor utilice menos términos significativos de búsqueda.\nEl número máximo de términos permitido es " + LIMITE_TERMINOS + ".");
				return false;
			}
		}
	}

	if (get_selected_type() == "web") {
		if (! by_button) {
			alert("La búsqueda en web se inicia " +
			      "con el botón [>]");
			return false;	
		}
	}

	// si la búsqueda no se inicia con un botón, entonces el índice
	// seleccionado implíctamente es "palabra clave"
	if (!by_button) {
		document.forma.indice.value = 1;
	}
	// resetea el tipo de búsqueda por si el usuario regresa al catálogo
	// usando el botón back y da enter
	by_button = false;

	return true;
}

function change_in_select(R) {
	for (i = 0; i < document.forma.tipo.length; i++) {
		if (document.forma.tipo[i].value == R) {
			document.forma.tipo[i].checked = true;
		}
	}
}
