function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

var reInt = new RegExp(/^int_([0-9]*)$/);

var interrogantEvents = function () {
	var imgs = document.getElementsByTagName("img");
	for (i=0; i<imgs.length; i++) {
		if (reInt.test(imgs[i].getAttribute("id"))) {
			imgs[i].onclick = doInterrogant;
		}
	}
}

var doInterrogant = function () {
	if (reInt.test(this.id)) {
		var myId = RegExp.$1;
		// alert(myId);
		url = "showtext.php?id="+myId;
	 	ShowText = window.open(url,"ShowText",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=350');
	}
}

function showFitxa(field) {
	id = field.value;
	if (field.value != 0) {
		if (document.accesodirecto.sci.value == 0) {
			document.accesodirecto.sci.value = 1;
		} else {
			document.accesodirecto.sci.value = 0;
		}
		document.accesodirecto.submit();
	}
}

function avisLegal() {
	url = "showtext.php?id="+72;
 	ShowText = window.open(url,"ShowText",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=350');
}

function showNoms() {
	var scifield = $("scifield");
	var combo = $("comboespecie");
	var sp= combo.value;
	if (scifield.value == 1) {
		var sci = 1;
	} else {
		var sci = 0;
	}
	var indi = $("indicator");
	if (indi != null) {
		indi.src = "img/indicator.gif";
	}
	url = 'getspnames.php';
	var pars = 'sci=' + sci +"&sp="+sp;
	new Ajax.Request(url, {method: 'get', parameters: pars, onComplete:showSpNames});
}

var showSpNames = function (req) {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var xmldoc = req.responseXML;
			var spnodes = xmldoc.getElementsByTagName("especie");
			var scinode = xmldoc.getElementsByTagName("sci");
			if (scinode[0].firstChild.data == 0) {
				var noutext = "Veure noms científics";
				var showsci = 1;
			} else {
				var noutext = "Veure noms catalans";
				var showsci = 0;
			}

			if (spnodes.length > 0) {
				var combo = $("comboespecie");
				// borra las opciones del combo
				var num = combo.options.length;
				for (i=0; i<num; i++) {
					combo.options[1] = null;
				}
				// nuevos nombres
				for (j=0; j<spnodes.length; j++) {
					var nom = spnodes[j].firstChild.data;
					var id = spnodes[j].getAttribute("id");
					var sel = spnodes[j].getAttribute("selected");
					combo.options[j+1] = new Option(nom, id, false, false);
					if (sel == 1) {
						combo.options[j+1].selected = true;
					}
				}
				// cambia el nombre del enlace
				var link = $("getnames");
				var txt = link.firstChild.data;
				var childnode = link.firstChild;

				link.innerHTML = noutext;
				var scifield = $("scifield");
				scifield.value = showsci;
			} else {
				alert("No s'ha pogut obtenir la llista");
			}
		} else {
			alert("Problema de comunicació");
		}
		var indi = $("indicator");
		if (indi != null) {
			indi.src = "img/fletxa.png";
		}
	}
}