Galeon = new Object();

Galeon.getEngine= function() {
	if (typeof this.engine == "undefined") {
		this.engine = new Ace.Engine();
	}
	return this.engine;
}

Galeon.setUrlFichaLibro = function(url) {
	this.urlFichaLibro = url;
}

Galeon.getUrlFichaLibro = function() {
	return this.urlFichaLibro;
}

Galeon.EnlaceFichaLibro = function(nodoEnlace) {
	this.nodo = nodoEnlace;
	nodoEnlace.enlace = this;
	
	this.codigoLibro = nodoEnlace.getAttribute("codigoLibro");
	
	xAddEventListener(nodoEnlace, "click", this.navegar);
}

Galeon.EnlaceFichaLibro.prototype = {
	getCodigoLibro: function() {
		return this.codigoLibro;
	},
	
	navegar: function(e) {
		var evt = new xEvent(e);
		
		var target = evt.target;
		while (target != null) {
			var enlace = target.enlace;
			if (enlace != null) {
				window.location.href = Galeon.getUrlFichaLibro() + "?codigoLibro=" + enlace.getCodigoLibro();
				return;
			}
			target = target.parentNode;
		}
	}	
}

Galeon.FormularioBusquedaAvanzada = function(formulario) {
	this.formulario = formulario;
	formulario.busquedaAvanzada = this;
}

Galeon.FormularioBusquedaAvanzada.prototype = {
	submit : function() {
		if (this.verificarParametros(this.formulario)) {
			this.formulario.submit();
		}
	},
	
	reset : function() {
		this.formulario.reset();
	},
	
	verificarParametros: function(f) {
		if ((f.generico.value      == null || f.generico.value      == '') &&
			(f.titulo.value        == null || f.titulo.value        == '') &&
			(f.autor.value         == null || f.autor.value         == '') &&
			(f.editorial.value     == null || f.editorial.value     == '') &&
			(f.palabrasClave.value == null || f.palabrasClave.value == '') &&
			(f.materia.value       == null || f.materia.value       == '') &&
			(f.precio.value        == null || f.precio.value        == '')) {
			alert('No ha especificado ningún criterio de búsqueda.');
			return false;
		}
		return true;
	}
}

Galeon.BotonBuscar = function(elem, formulario) {
	this.elem = elem;
	
	elem.boton = this;
	elem.formulario = formulario;
	
	xAddEventListener(elem, "click", function(e) {
		var evt = new xEvent(e);
		
		var target = evt.target;
		while (target != null) {
			var formulario = target.formulario;
			if (formulario != null) {
				formulario.submit(formulario);
				return;
			}
			target = target.parentNode;
		}
	});
}

Galeon.BotonLimpiar = function(elem, formulario) {
	this.elem = elem;
	
	elem.boton = this;
	elem.formulario = formulario;
	
	xAddEventListener(elem, "click", function(e) {
		var evt = new xEvent(e);
		
		var target = evt.target;
		while (target != null) {
			var formulario = target.formulario;
			if (formulario != null) {
				formulario.reset(formulario);
				return;
			}
			target = target.parentNode;
		}
	});
}

Galeon.BotonBiestable = function(elem) {
	this.elem = elem;
	elem.boton = this;
	
	xAddEventListener(elem, 'mouseover', function() { xRemoveClass(elem, "apagado"); xAddClass(elem, "encendido"); });
	xAddEventListener(elem, 'mouseout',  function() { xRemoveClass(elem, "encendido"); xAddClass(elem, "apagado");});
	
}

Galeon.FormularioSolicitudLibro = function(formulario) {
	this.formulario = formulario;
	formulario.solicitudLibro = this;
}

Galeon.FormularioSolicitudLibro.prototype = {
	submit : function() {
		if (this.verificarParametros(this.formulario)) {
			this.formulario.submit();
		}
	},
	
	reset : function() {
		this.formulario.reset();
	},
	
	verificarParametros: function(f) {
		if (f.nombre.value      == null || f.nombre.value      == '') {
			alert('Tu nombre es indispensable para la solicitud.');
			return false;
		}
		
		if (f.email.value        == null || f.email.value        == '' || !f.email.value.match(/.+@.+\..+/)) {
			alert('Tu dirección de correo electrónico es indispensable para la solicitud.');
			return false;
		}
		if ((f.autor.value         == null || f.autor.value         == '') &&
			(f.editorial.value     == null || f.editorial.value     == '') &&
			(f.titulo.value        == null || f.titulo.value        == '') &&
			(f.materia.value       == null || f.materia.value       == '')) {
			alert('Debes indicar algún dato del libro (titulo, autor, editorial o materia) para cursar la solicitud.');
			return false;
		}
		return true;
	}
}

Galeon.FormularioRealizarPedido = function(formulario) {
	this.formulario = formulario;
	formulario.realizarPedido = this;
	
	this.inputNumeroVisa = xGetElementById('inputNumeroVisa');
	this.inputCaducidadVisa = xGetElementById('inputCaducidadVisa');
	this.inputCodigoVisa = xGetElementById('inputCodigoVisa');
	this.gastosEnvio = xGetElementById('gastosEnvio');
	this.formaPago = xGetElementById('formaPago');
	this.formaEnvio = xGetElementById('formaEnvio');
	this.pais = xGetElementById('pais');
	this.provincia = xGetElementById('provincia');
	
	this.formaPago.formulario = this;
	xAddEventListener(this.formaPago, 'change', this.onChangeFormaPago);
	this.formaEnvio.formulario = this;
	xAddEventListener(this.formaEnvio, 'change', this.onChangeFormaEnvio);
	this.pais.formulario = this;
	xAddEventListener(this.pais, 'change', this.onChangePais);
	this.provincia.formulario = this;
	xAddEventListener(this.provincia, 'change', this.onChangeProvincia);
	
}

Galeon.FormularioRealizarPedido.prototype = {
	submit : function() {
		if (this.verificarParametros(this.formulario)) {
			this.formulario.submit();
		}
	},
	
	reset : function() {
		this.formulario.reset();
	},
	
	onChangeFormaPago: function(e) {
		var evt = new xEvent(e);
		if (evt == null)
			return;
		var target = evt.target;
		if (target == null || target.formulario == null)
			return;
		var formulario = target.formulario;
		
		var formaPago = formulario.formaPago;
		if (formaPago.value == 'visa') {
			formulario.inputNumeroVisa.style.display = 'block';
			formulario.inputCaducidadVisa.style.display = 'block';	
			formulario.inputCodigoVisa.style.display = 'block';	
		} else {
			formulario.inputNumeroVisa.style.display = 'none';
			formulario.inputCaducidadVisa.style.display = 'none';	
			formulario.inputCodigoVisa.style.display = 'none';	
		}
		formulario.actualizarGastosEnvio();
	},

	onChangeFormaEnvio: function(e) {
		var evt = new xEvent(e);
		if (evt == null)
			return;
		var target = evt.target;
		if (target == null || target.formulario == null)
			return;
		var formulario = target.formulario;
		formulario.actualizarGastosEnvio();
	},

	onChangeProvincia: function(e) {
		var evt = new xEvent(e);
		if (evt == null)
			return;
		var target = evt.target;
		if (target == null || target.formulario == null)
			return;
		var formulario = target.formulario;
		formulario.actualizarGastosEnvio();
	},
	
	onChangePais: function(e) {
		var evt = new xEvent(e);
		if (evt == null)
			return;
		var target = evt.target;
		if (target == null || target.formulario == null)
			return;
		var formulario = target.formulario;
		formulario.actualizarGastosEnvio();
	},
	
	actualizarGastosEnvio: function() {
		var engine = Galeon.getEngine();
		engine.invoke(new Ace.Request(Ace.Method.Get, "calcularGastosEnvio.action?formaEnvio="+this.formaEnvio.value+"&formaPago="+this.formaPago.value+"&pais=" + encodeURIComponent(this.pais.value.replace('ñ', 'n')) + "&provincia=" + encodeURIComponent(this.provincia.value) + "&numLibros=" + this.numLibros, null, null, this.actualizarGastosEnvioCallback, null, Ace.CallbackOption.Always));
	},
	
	actualizarGastosEnvioCallback: function(response) {
		xInnerHtml('gastosEnvio', response.text);
		xGetElementById('lineaGastosEnvio').style.display = response.text != '' ? 'block' : 'none';
	},
	
	verificarParametros: function(f) {
		if (f.nombre.value == null || f.nombre.value == '') {
			alert('Tu nombre es indispensable para el pedido.');
			return false;
		}
		
		if (f.direccion.value == null || f.direccion.value == '') {
			alert('Tu dirección es indispensable para el pedido.');
			return false;
		}

		if (f.codigoPostal.value == null || f.codigoPostal.value == '') {
			alert('Tu código postal es indispensable para el pedido.');
			return false;
		}
		
		if (f.poblacion.value == null || f.poblacion.value == '') {
			alert('Tu población es indispensable para el pedido.');
			return false;
		}
		if (f.provincia.value == null || f.provincia.value == '') {
			alert('Tu provincia es indispensable para el pedido.');
			return false;
		}
		if (f.pais.value == null || f.pais.value == '') {
			alert('Tu país es indispensable para el pedido.');
			return false;
		}
		if (f.email.value == null || f.email.value == '' || !f.email.value.match(/.+@.+\..+/)) {
			alert('Tu dirección de correo electrónico es indispensable para el pedido.');
			return false;
		}
		if (f.telefono.value == null || f.telefono.value == '') {
			alert('Tu teléfono es indispensable para el pedido.');
			return false;
		}
		if (f.formaPago.value == null || f.formaPago.value == '') {
			alert('Debes indicar una forma de pago para el pedido.');
			return false;
		}
		if (f.formaEnvio.value == null || f.formaEnvio.value == '') {
			alert('Debes indicar una forma de envío para el pedido.');
			return false;
		}
		if (f.formaPago.value == 'visa' && f.numeroVisa.value == '') {
			alert('Debes indicar el número de VISA para el pedido.');
			return false;
		}
		if (f.formaPago.value == 'visa' && f.caducidadVisa.value == '') {
			alert('Debes indicar la caducidad de la VISA para el pedido.');
			return false;
		}
		if (f.formaPago.value == 'visa' && f.codigoVisa.value == '') {
			alert('Debes indicar el código de la VISA para el pedido.');
			return false;
		}
		return true;
	}
}

Galeon.FormularioRegistroBoletin = function(formulario) {
	this.formulario = formulario;
	formulario.registroBoletin = this;
}

Galeon.FormularioRegistroBoletin.prototype = {
	submit : function() {
		if (this.verificarParametros(this.formulario)) {
			this.formulario.submit();
		}
	},
	
	reset : function() {
		this.formulario.reset();
	},
	
	verificarParametros: function(f) {
		if (f.nombre.value == null || f.nombre.value == '') {
			alert('Tu nombre es indispensable para el boletín.');
			return false;
		}
		if (f.email.value == null || f.email.value == '' || !f.email.value.match(/.+@.+\..+/)) {
			alert('Tu dirección de correo electrónico es indispensable para el boletín.');
			return false;
		}
		for (var i = 0; i < f.materias.length; i++) {
			if (f.materias[i].checked)
				return true;
		}
		
		alert('Debe indicar alguna materia para el boletín.');
		return false;
	}
}

