

//Inicio funciones validacion
function validarEmail(valor) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
	return (true)
} else {
	return (false);
}
}

function validarFormularioAltaAlumno(formulario){
	var flag = 0;
	
	//var sindicato = document.getElementById("sindicato");
	//sindicato.style.background = "#88FF66";
	var tipo_acceso = document.getElementById("tipo_acceso");
	tipo_acceso.style.background = "#88FF66";

	var dni = document.getElementById("input_dni");
	dni.style.background = "#88FF66";

	var nombre = document.getElementById("nombre");
	nombre.style.background = "#88FF66";

	var apellidos = document.getElementById("apellidos");
	apellidos.style.background = "#88FF66";

	var direccion = document.getElementById("direccion");
	direccion.style.background = "#88FF66";	
	var codigopostal = document.getElementById("codigopostal");
	codigopostal.style.background = "#88FF66";
	var ciudad = document.getElementById("ciudad");
	ciudad.style.background = "#88FF66";
	var provincia = document.getElementById("provincia");
	provincia.style.background = "#88FF66";

	var email = document.getElementById("email");
	email.style.background = "#88FF66";
	var remail = document.getElementById("remail");
	remail.style.background = "#88FF66";

	var username = document.getElementById("usernamep");
	username.style.background = "#88FF66";	
	var clave = document.getElementById("clave");
	clave.style.background = "#88FF66";

	var tlf_1 = document.getElementById("tlf_1");
	tlf_1.style.background = "#88FF66";
	
	//if ((formulario.sindicato.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		//document.getElementById("sindicato").style.background = "#FF6655";
		//flag = flag + 1;
	//}
	
	if (formulario.tipo_acceso.value == '-'){
		//alert('DNI no válido')
		//var aviso = document.getElementById("input_dni");
		document.getElementById("tipo_acceso").style.background = "#FF6655";		
		flag = flag + 1;
	}

	if ((formulario.dni.value.length < 9) || (nif(formulario.dni.value) == false)){
		//alert('DNI no válido')
		//var aviso = document.getElementById("input_dni");
		document.getElementById("input_dni").style.background = "#FF6655";		
		flag = flag + 1;
	}

	if ((formulario.nombre.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("nombre").style.background = "#FF6655";
		flag = flag + 1;
	}

	if ((formulario.apellidos.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("apellidos").style.background = "#FF6655";
		flag = flag + 1;
	}
	/////////////////////////////////////////////
	if ((formulario.direccion.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("direccion").style.background = "#FF6655";
		flag = flag + 1;
	}
	if ((formulario.codigopostal.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("codigopostal").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.ciudad.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("ciudad").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.provincia.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("provincia").style.background = "#FF6655";
		flag = flag + 1;
	}	
	///////////////////////////////////////////
	
	if ((formulario.email.value == '') || !(validarEmail(formulario.email.value))) {
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("email").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.remail.value == '') || !(validarEmail(formulario.remail.value))) {
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("remail").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.email.value != formulario.remail.value)) {
		document.getElementById("remail").style.background = "#FF6655";		
		document.getElementById("email").style.background = "#FF6655";
		alert('Repita el mismo email en ambos campos');
		flag = flag + 1;
	}
	
	if ((formulario.usernamep.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("usernamep").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.clave.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("clave").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.tlf_1.value) == ''){
		//alert('Al menos un teléfono es obligatorio.');
		//var aviso = document.getElementById("apellidos");
		document.getElementById("tlf_1").style.background = "#FF6655";
		flag = flag + 1;
	}

	
	if (flag > 0) {
		var aviso = document.getElementById('aviso').innerHTML = '<font color=red>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;¡Revisa los campos en rojo!</font>';		
		return (false);
	}else{
		return (true);
	}
}

function nif(dni) {
  numero = dni.substr(0,dni.length-1);
  let = dni.substr(dni.length-1,1);
  numero = numero % 23;
  letra='TRWAGMYFPDXBNJZSQVHLCKET';
  letra=letra.substring(numero,numero+1);
  let = let.toUpperCase();
  if (letra!=let) 
    return (false);
}
//Fin funciones AltaAlumno

function validarFormularioAltaCurso(formulario){
	var flag = 0;
		
	var nombre_curso = document.getElementById("nombre_curso");
	nombre_curso.style.background = "#88FF66";

	var input_codigo = document.getElementById("input_codigo");
	input_codigo.style.background = "#88FF66";
	
	var tipo = document.getElementById("tipo");
	tipo.style.background = "#88FF66";
	
	var anio_ini = document.getElementById("anio_ini");
	anio_ini.style.background = "#88FF66";
	var dia_ini = document.getElementById("dia_ini");
	dia_ini.style.background = "#88FF66";
	var mes_ini = document.getElementById("mes_ini");
	mes_ini.style.background = "#88FF66";
	
	var anio_fin = document.getElementById("anio_fin");
	anio_fin.style.background = "#88FF66";
	var dia_fin = document.getElementById("dia_fin");
	dia_fin.style.background = "#88FF66";
	var mes_fin = document.getElementById("mes_fin");
	mes_fin.style.background = "#88FF66";

	var precio_matricula = document.getElementById("precio_matricula");
	precio_matricula.style.background = "#88FF66";
	var pago_regular = document.getElementById("pago_regular");
	pago_regular.style.background = "#88FF66";
	var horas_curso = document.getElementById("horas_curso");
	horas_curso.style.background = "#88FF66";
	var plazas = document.getElementById("plazas");
	plazas.style.background = "#88FF66";	
	var convocatorias = document.getElementById("convocatorias");
	convocatorias.style.background = "#88FF66";
	
	if ((formulario.input_codigo.value == '') && (formulario.sin_codigo.checked == false)){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("input_codigo").style.background = "#FF6655";
		flag = flag + 1;
	}

	if ((formulario.tipo.value == '0')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("tipo").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.nombre_curso.value == '')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("nombre_curso").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	var str_nombre_curso = formulario.nombre_curso.value;
	
	for(var i=0;i<str_nombre_curso.length;i++)
	{ 
		if ((str_nombre_curso.charCodeAt(i) != 32) && 
			(str_nombre_curso.charCodeAt(i) != 225) &&
     		(str_nombre_curso.charCodeAt(i) != 237) &&
			(str_nombre_curso.charCodeAt(i) != 233) &&
			(str_nombre_curso.charCodeAt(i) != 243) &&
			(str_nombre_curso.charCodeAt(i) != 250) &&
			((str_nombre_curso.charCodeAt(i) < 48) || (str_nombre_curso.charCodeAt(i) > 57)) &&
			((str_nombre_curso.charCodeAt(i) < 65) || (str_nombre_curso.charCodeAt(i) > 90)) && 
			((str_nombre_curso.charCodeAt(i) < 97) || (str_nombre_curso.charCodeAt(i) > 122)))
		{
			alert('El nombre del curso sólo puede contener letras y números.\nNo poner acentos en las letras mayúsculas.');
			nombre_curso.style.background = "#FF6655";
			flag = flag + 1;
		}  
	}
	 if (formulario.tipo.value == 'curso')
	 { 	
		if ((formulario.dia_ini.value == '') || (formulario.dia_ini.value == 'Dia')){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("dia_ini").style.background = "#FF6655";
			flag = flag + 1;
		}
		if ((formulario.mes_ini.value == '') || (formulario.mes_ini.value == 'Mes')){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("mes_ini").style.background = "#FF6655";
			flag = flag + 1;
		}
		if ((formulario.anio_ini.value == '') || (formulario.anio_ini.value == 'Año') || (formulario.anio_ini.value.length < 4 )){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("anio_ini").style.background = "#FF6655";
			flag = flag + 1;
		}
		if ((formulario.dia_fin.value == '') || (formulario.dia_fin.value == 'Dia')){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("dia_fin").style.background = "#FF6655";
			flag = flag + 1;
		}
		if ((formulario.mes_fin.value == '') || (formulario.mes_fin.value == 'Mes')){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("mes_fin").style.background = "#FF6655";
			flag = flag + 1;
		}
		if ((formulario.anio_fin.value == '') || (formulario.anio_fin.value == 'Año')){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("anio_fin").style.background = "#FF6655";
			flag = flag + 1;
		}
	}
	if (formulario.precio_matricula.value == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("precio_matricula").style.background = "#FF6655";
		flag = flag + 1;
	}
	if ((formulario.pago_regular.value == '')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("pago_regular").style.background = "#FF6655";
		flag = flag + 1;
	}
	 if (formulario.tipo.value == 'curso')
	 { 		
		if ((formulario.horas_curso.value == '') || (formulario.horas_curso.value == '0')){
			//alert('Debe introducir un nombre')
			//var aviso = document.getElementById("nombre");
			document.getElementById("horas_curso").style.background = "#FF6655";
			flag = flag + 1;
		}
	 }
	if ((formulario.plazas.value == '')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("plazas").style.background = "#FF6655";
		flag = flag + 1;
	}

	if (flag > 0) {
		var aviso = document.getElementById('aviso').innerHTML = '<font color=red>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;¡Revisa los campos en rojo!</font>';		
		return (false);
	}else{
		return (true);
	}
	
}
//////////////////////////////////////
//////////////////////////////////////
//////////////////////////////////////
function validarDatosTest(formulario){
	var flag = 0;
	var restr_ini = document.getElementById("restriccion_ini");
	restr_ini.style.background = "#88FF66";
	var restr_fin = document.getElementById("restriccion_fin");
	restr_fin.style.background = "#88FF66";
	
	var sel_timeout = document.getElementById("sel_timeout");
	sel_timeout.style.background = "#88FF66";
	
	var sel_opciones_pregunta = document.getElementById("sel_opciones_pregunta");
	sel_opciones_pregunta.style.background = "#88FF66";
	
	var punto_correcta = document.getElementById("punto_correcta");
	punto_correcta.style.background = "#88FF66";
	var punto_fallada = document.getElementById("punto_fallada");
	punto_fallada.style.background = "#88FF66";
	var punto_aprobado = document.getElementById("punto_aprobado");
	punto_aprobado.style.background = "#88FF66";

	var sel_temas_curso = document.getElementById("sel_temas_curso");
	sel_temas_curso.style.background = "#88FF66";
	
	var titulo_test = document.getElementById("titulo_test");
	titulo_test.style.background = "#88FF66";
	
	if ((formulario.punto_correcta.value != 'x') && (formulario.punto_fallada.value == 'x')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("punto_fallada").style.background = "#FF6655";
		flag = flag + 1;
	}
	if ((formulario.punto_fallada.value != 'x') && (formulario.punto_correcta.value == 'x')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("punto_correcta").style.background = "#FF6655";
		flag = flag + 1;
	}
	if ((formulario.punto_correcta.value != 'x')  && (formulario.punto_aprobado.value == 'x')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("punto_aprobado").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.punto_aprobado.value != 'x')  && ((formulario.punto_correcta.value == 'x') || (formulario.punto_fallada.value == 'x'))){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("punto_aprobado").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.punto_fallada.value != 'x')  && ((formulario.punto_correcta.value == 'x') || (formulario.punto_aprobado.value == 'x'))){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("punto_aprobado").style.background = "#FF6655";
		flag = flag + 1;
	}	
	
	if ((formulario.id_tema.value == 'x')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		sel_temas_curso.style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.titulo_test.value == '')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		titulo_test.style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.restriccion_ini.value == 'FALSE') && (formulario.restriccion_fin.value != 'FALSE')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("restriccion_ini").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.restriccion_ini.value != 'FALSE') && (formulario.restriccion_fin.value == 'FALSE')){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("restriccion_fin").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if (((formulario.restriccion_ini.value) == (formulario.restriccion_fin.value)) && ((formulario.restriccion_ini.value != 'FALSE') && (formulario.restriccion_ini.value != 'FALSE'))){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("restriccion_fin").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.restriccion_ini.value) > (formulario.restriccion_fin.value)){
		//alert(formulario.restriccion_ini.value+''+formulario.restriccion_fin.value);
		//var aviso = document.getElementById("nombre");
		document.getElementById("restriccion_fin").style.background = "#FF6655";
		flag = flag + 1;
	}	

	if (flag > 0) {
		return (false);
	}else{
		return (true);
	}
}

/////////////////////////////////////////////////
/////////////////////////////////////////////////
/////////////////////////////////////////////////
function validarFormularioAltaOnline(formulario){
	var flag = 0;
	
	//var sindicato = document.getElementById("sindicato");
	//sindicato.style.background = "#88FF66";
	
	var dni = document.getElementById("input_dni");
	dni.style.background = "#88FF66";

	var nombre = document.getElementById("nombre");
	nombre.style.background = "#88FF66";

	var apellidos = document.getElementById("apellidos");
	apellidos.style.background = "#88FF66";

	var direccion = document.getElementById("direccion");
	direccion.style.background = "#88FF66";	
	var codigopostal = document.getElementById("codigopostal");
	codigopostal.style.background = "#88FF66";
	var ciudad = document.getElementById("ciudad");
	ciudad.style.background = "#88FF66";
	var provincia = document.getElementById("provincia");
	provincia.style.background = "#88FF66";

	var email = document.getElementById("email");
	email.style.background = "#88FF66";
	var remail = document.getElementById("remail");
	remail.style.background = "#88FF66";

	var username = document.getElementById("usernamep");
	username.style.background = "#88FF66";	
	var clave = document.getElementById("clavep");
	clave.style.background = "#88FF66";

	var curso_matriculado = document.getElementById("curso_matriculado");
	curso_matriculado.style.background = "#88FF66";
	
	var tlf_1 = document.getElementById("tlf_1");
	tlf_1.style.background = "#88FF66";
	
	//if ((formulario.sindicato.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		//document.getElementById("sindicato").style.background = "#FF6655";
		//flag = flag + 1;
	//}

	if ((formulario.dni.value.length < 9) || (nif(formulario.dni.value) == false)){
		//alert('DNI no válido')
		//var aviso = document.getElementById("input_dni");
		document.getElementById("input_dni").style.background = "#FF6655";		
		flag = flag + 1;
	}

	if ((formulario.nombre.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("nombre");
		document.getElementById("nombre").style.background = "#FF6655";
		flag = flag + 1;
	}

	if ((formulario.apellidos.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("apellidos").style.background = "#FF6655";
		flag = flag + 1;
	}

	/////////////////////////////////////////////
	if ((formulario.direccion.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("direccion").style.background = "#FF6655";
		flag = flag + 1;
	}
	if ((formulario.codigopostal.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("codigopostal").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.ciudad.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("ciudad").style.background = "#FF6655";
		flag = flag + 1;
	}	
	if ((formulario.provincia.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("provincia").style.background = "#FF6655";
		flag = flag + 1;
	}	
	///////////////////////////////////////////	
	
	if ((formulario.email.value == '') || !(validarEmail(formulario.email.value))) {
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("email").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.remail.value == '') || !(validarEmail(formulario.remail.value))) {
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("remail").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.email.value != formulario.remail.value)) {
		document.getElementById("remail").style.background = "#FF6655";		
		document.getElementById("email").style.background = "#FF6655";
		alert('Repita el mismo email en ambos campos');
		flag = flag + 1;
	}
	
	if ((formulario.usernamep.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("usernamep").style.background = "#FF6655";
		flag = flag + 1;
	}

	if ((formulario.usernamep.length) > 10){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("usernamep").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.clavep.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("clavep").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.clavep.length) > 8){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("clavep").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.tlf_1.value) == ''){
		//alert('Al menos un teléfono es obligatorio.');
		//var aviso = document.getElementById("apellidos");
		document.getElementById("tlf_1").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if ((formulario.curso_matriculado.value) == 0){
		//alert('Al menos un teléfono es obligatorio.');
		//var aviso = document.getElementById("apellidos");
		document.getElementById("curso_matriculado").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if (flag > 0) {
		return (false);
	}else{
		document.getElementById('envia_matriculacion_online').disabled=true;
		return (true);
	}
}

/////////////////////////////////////////////////
/////////////////////////////////////////////////
/////////////////////////////////////////////////
function validarFormularioAniadeTema(formulario){
	var flag = 0;
	
	var nuevo_tema = document.getElementById("nuevo_tema");
	nuevo_tema.style.background = "#88FF66";

	var despues_tema = document.getElementById("despues_tema");
	despues_tema.style.background = "#88FF66";

	if ((formulario.nuevo_tema.value) == ''){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("nuevo_tema").style.background = "#FF6655";
		flag = flag + 1;
	}

	if ((formulario.despues_tema.value) == 'x'){
		//alert('Debe introducir un nombre')
		//var aviso = document.getElementById("apellidos");
		document.getElementById("despues_tema").style.background = "#FF6655";
		flag = flag + 1;
	}
	
	if (flag > 0) {
		return (false);
	}else{
		return (true);
	}
}
