/*
   ###############################################################################################
   #                     PROYECTO: CIBEROTECA - Bancaja                                          #
   #                     Funciones Javascript para la parte del buscador                         #
   #                     David Ortiz 30/10/2000                                                  #
   ###############################################################################################
*/
function initform(capa){
	if(document.all){
		defineForm = document.frmSearch;
	} else {
		if(document.capalectura) {
			defineForm = document.capalectura.document.frmSearch;
		} else {
			defineForm = document.frmSearch;
		}
	}
}

//Búsqueda
function Search(bAdvanced, bOrder){
	if (!bAdvanced){
		var sTexto = defineForm.txtTexto.value;
		if (sTexto.replace(/\s/gi,'')) {
			defineForm.submit();
		} else {
			alert('Ha d’escriure el text que vol buscar');
		}
	} else {
		var sFields = 'txtTitulo|txtAutor|txtTema|txtURL|lstIdioma';
		if (CheckFields(sFields.split('|'), defineForm)){
			defineForm.submit();
		} else {
			if (!bOrder) alert('Ha d´indicar alguna dada per a poder realitzar la recerca');
		}
	}
}

function UpdateControls(){
	initform(false)
	parent.menu.oculta()
	for (i=0; i < eval(defineForm+".radOrder.length"); i++) {
		if (String(eval(defineForm+".radOrder[i].value")) == sField) {
			eval(defineForm+".radOrder[i].checked ="+ true);
		}
	}
}

/* Evalua si los campos de Texto indicados no contienen ningún valor y si es así muestra un mensaje
    notificándolo */
	  	// for(al=0;al<10;al++) alert(oForm.elements[al].name);
	  	// alert(oForm.elements['txtAutor'].type)

function CheckFields(aFields, oForm){
	var bResult = false;
	for (var i=0; i < aFields.length; i++) {
		stype = oForm.elements[aFields[i]].type;
		switch(stype) {
			case 'text':
				if (oForm.elements[aFields[i]].value != "" && !isWhitespace(oForm.elements[aFields[i]].value))
				bResult = true;
				break;
			case 'textarea':
				if (oForm.elements[aFields[i]].value != "" && !isWhitespace(oForm.elements[aFields[i]].value))
				bResult = true;
				break;
			case 'select-one':
				var nIndex = oForm.elements[aFields[i]].selectedIndex;
				if (oForm.elements[aFields[i]].options[nIndex].value != 0)
				bResult = true;
				break;
			case 'checkbox':
				if (!oForm.elements[aFields[i]].checked) bResult = true;
				break;
		}
		if (bResult) {
			break;
		}
	}
	return bResult;
}

// Devuelve true si el string s está vacio o contiene espacios en blanco
function isWhitespace(s) {
	var whitespace = " \t\n\r";
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

// Comprueba si un string está vacio
function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}