var formEvents = function() {
	var form = document.fOferta;
	form.onsubmit = doSubmit;
	var tituloh1 = document.getElementById("titoferta");
	var titfield = document.getElementById("titulo");
	if ((tituloh1 != null) && (titfield != null)) {
		titfield.onkeyup = updateTitle;
	}
}

var updateTitle=function() {
	var valor = this.value;
	var titulo = document.getElementById("titoferta");
	if (valor != "") {
		titulo.innerHTML = valor;
	} else {
		titulo.innerHTML = "Nueva oferta";
	}
}

var doSubmit = function() {
	if (validaForm("fOferta", listaCampos, listaEtiquetas, "Por favor, rellene:\n", ",\n")) {
		// horas semanales debe ser numerico
		if (esNumerico(document.fOferta.horas_semanales.value)) {
			this.submit();
		} else {
			alert("El número de horas semanales sólo puede contener números.");
			return false;
		}
	}
	return false;
}

/** listados **/
var listaOfertasEvents = function () {
	var links = document.getElementsByTagName("A");
	for (var i=0; i<links.length; i++) {
		if (reToggle.test(links[i].id)) {
			links[i].onclick = Oferta.toggle;
		}
		if (reDel.test(links[i].id)) {
			links[i].onclick = Oferta.borrar;
		}
	}
}

var inscribeEvents = function() {
	var form = document.fInscripcion;
	if (form != null) {
		form.onsubmit = doInscribe;
	}
}

var doInscribe = function() {
	if (validaForm("fInscripcion", listaCampos, listaEtiquetas, "Por favor, rellena:\n", ",\n")) {
		if (validaEmail("fInscripcion", "ins_email", "Indica una dirección de correo válida.")) {
			this.submit();
		}
	}
	return false;
}

var Oferta={
	borrar:function(){
		if (reDel.test(this.id)) {
			var id = RegExp.$1;
			if (confirm("¿Confirma la eliminación de la oferta?")) {
				Indicator.show();
				new Ajax.Request('del_oferta.php?id='+id+'&ajax=1', {method: 'get', onComplete: Oferta.showDelete});
			}
		}
		return false;
	},
	showDelete:function(response){
		var json = eval(response.responseText);
		if (json.success == 1) {
			var row = document.getElementById("row_"+json.id);
			Element.remove(row);
		} else {
			alert("No se pudo borrar la oferta.");
		}
		Indicator.hide();
	},
	toggle:function(){
		if (reToggle.test(this.id)) {
			var id = RegExp.$1;
			Indicator.show();
			var ajax = new Ajax.Request('toggle_closed.php?id='+id+'&ajax=1', {method: 'get', onComplete: Oferta.showToggle});
		}
		return false;
	},
	showToggle:function(response) {
		var json = eval(response.responseText);
		if (json.success == 1) {
			if (json.closed == 0) {
				var src = "iconoopen.gif";
			} else {
				var src = "iconoclosed.gif";
			}
			var img = document.getElementById("status_"+json.id);
			img.src = "/img2/"+src;
		} else {
			alert("No se pudo modificar el estado de la oferta.");
		}
		Indicator.hide();
	}
}