jQuery.noConflict();

jQuery(function() {
	jQuery('#tipo').change(function() {
		var tipo = jQuery(this);
		var opcion = tipo.find('option[value=' + tipo.val() + ']');
		var turnos = opcion.attr('turnos');
		var turno = jQuery('#divturno');
		jQuery('#turno').html('');

		if(turnos != undefined) {
			turno.show();

			turno = jQuery('#turno');
			var trozos = turnos.split(',');
			for(var i = 0; i < trozos.length; i++) {
				turno.append(jQuery('<option value="' + trozos[i] + '">' + trozos[i] + '</option>'));
			}
		} else {
			turno.hide();
		}
	});
});

window.addEvent('domready', ampliaFormulario);
function ampliaFormulario() {
	$$('.obligatorio').each(
		function(a) {
			var mensaje = document.createElement('span');
			mensaje.style.display = 'none';
			mensaje.style.position = 'absolute';
			mensaje.style.right = 0;
			mensaje.style.top = 0;
			mensaje.appendChild(document.createTextNode('El campo es obligatorio'));

			a.parentNode.appendChild(mensaje);

			a.addEvent('blur', function() {
				return compruebaValor(this);
			});
		}
	);

	$$('#email').each(
		function(a) {
			var mensaje = document.createElement('span');
			mensaje.style.display = 'none';
			mensaje.style.position = 'absolute';
			mensaje.style.right = 0;
			mensaje.style.top = 0;
			mensaje.appendChild(document.createTextNode('El correo no es válido'));

			a.parentNode.appendChild(mensaje);

			a.addEvent('blur', function() {
				return compruebaEmail(this);
			});
		}
	);

	$$('#movil').each(
		function(a) {
			var mensaje = document.createElement('span');
			mensaje.style.display = 'none';
			mensaje.style.position = 'absolute';
			mensaje.style.right = 0;
			mensaje.style.top = 0;
			mensaje.appendChild(document.createTextNode('El móvil no es válido'));

			a.parentNode.appendChild(mensaje);

			a.addEvent('blur', function() {
				return compruebaTelefono(this);
			});
		}
	);

	$$('#abonado').each(
		function(a) {
			var mensaje = document.createElement('span');
			mensaje.style.display = 'none';
			mensaje.style.position = 'absolute';
			mensaje.style.right = 0;
			mensaje.style.top = 0;
			mensaje.appendChild(document.createTextNode('El número de abonado no es válido'));

			a.parentNode.appendChild(mensaje);

			a.addEvent('blur', function() {
				return compruebaAbonado(this);
			});
		}
	);

	$('registro').onsubmit = enviaFormulario;
}

function compruebaValor(a) {
	if(a.value == '') {
		a.getNext().style.display = 'block';
		return false;
	} else {
		a.getNext().style.display = 'none';
		return true;
	}
}

function compruebaEmail(a) {
	var Regex =/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if(Regex.test(a.value) || a.value == '') {
		a.getNext().getNext().style.display = 'none';
		return true;
	} else {
		a.getNext().getNext().style.display = 'inline';
		return false;
	}
}

function compruebaTelefono(a) {
	var Regex =/^6([0-9]{8})$/;

	if(Regex.test(a.value) || a.value == '') {
		a.getNext().style.display = 'none';
		return true;
	} else {
		a.getNext().style.display = 'inline';
		return false;
	}
}

function compruebaAbonado(a) {
	var Regex =/^\d{1,5}$/;

	if(Regex.test(a.value) || a.value == '') {
		a.getNext().style.display = 'none';
		return true;
	} else {
		a.getNext().style.display = 'inline';
		return false;
	}
}

function enviaFormulario() {
	var obligatorios = $$('.obligatorio');

	if(obligatorios.length == 0) {
		return true;
	} else {
		var correcto = false;

		obligatorios.every(
			function(a) {
				var retorno = compruebaValor(a);
				if(!retorno) {
					a.focus();
				}
				correcto = retorno;
				return retorno;
			}
		);
		
		return correcto && compruebaTelefono($('movil')) && compruebaEmail($('email')) && compruebaAbonado($('abonado'));
	}
}