$(document).ready(function() {
	
	//formulario
	var emailValido = {
	       email: function(el) {return /^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test($(el).val());}
	}
	
	$("#userEmail").blur(function(){
		var v = $(this).attr("value");
		if (v) {
			$(this).removeClass("error");
			$($($(this).parents("p")[0]).children("span")[0]).removeClass("error").addClass("ok").text("Correcto");
			var er_email = /^(.+\@.+\..+)$/
			if(!er_email.test(v)) {
				$(this).addClass("error");
				$($($(this).parents("p")[0]).children("span")[0]).removeClass("ok").addClass("error").text("Email inválido");
			} else 	{
				$(this).removeClass("error");
				$($($(this).parents("p")[0]).children("span")[0]).removeClass("error").addClass("ok").text("Correcto");
			}
		}
		else {
			$(this).addClass("error");
			$($($(this).parents("p")[0]).children("span")[0]).removeClass("ok").addClass("error");
		}
	});
	
	
	//soyOrganizacion:
	$("#soyOrganizacion").hide();
	$("input.nosoyOrg").click(function(){
		$("#soyOrganizacion").hide();
	});
	$("input#soyOrg").click(function(){
		$("#soyOrganizacion").show();
	});
});

function isValidTlf ( tlf )
{
   if ( !/^\d{9}$/.test ( tlf ) && !/^\+\d{1,3}\ \d{9}$/.test ( tlf ) )
   {
      return false;
   }
   else
   {
      return true;
   }
}
function isValidEmail ( email )
{
   var re = /^[a-zA-Z0-9\-\_\.]+\@([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4})$/;
   if ( !re.test ( email ) )
   {
      return false;
   }
   else
   {
      return true;
   }
}

function checkFormularioFormacion()
{
  var f = document.formFormacion;
  if ( f.nombre.value == '' )
  {
     alert ( "Debe rellenar el campo nombre" );
     f.nombre.focus();
     return false;
  }
  if ( f.email.value == '' )
  {
     alert ( "Debe rellenar el campo email" );
     f.email.focus();
     return false;
  }
  if ( !isValidEmail ( f.email.value ) )
  {
     alert ( "El email no es válido" );
     f.email.focus();
     return false;
  }
  if ( f.provincia.value == '' )
  {
     alert ( "Debe rellenar el campo provincia" );
     f.provincia.focus();
     return false;
  }
  if ( f.profesion.value == '' )
  {
     alert ( "Debe rellenar el campo profesión" );
     f.profesion.focus();
     return false;
  }
}

function checkFormularioNorma()
{
  var f = document.formNorma;
  if ( f.nombre.value == '' )
  {
     alert ( "Debe rellenar el campo nombre" );
     f.nombre.focus();
     return false;
  }
  if ( f.email.value == '' )
  {
     alert ( "Debe rellenar el campo email" );
     f.email.focus();
     return false;
  }
  if ( !isValidEmail ( f.email.value ) )
  {
     alert ( "El email no es válido" );
     f.email.focus();
     return false;
  }
  if ( f.organizacion.value == '' )
  {
     alert ( "Debe rellenar el campo organización" );
     f.organizacion.focus();
     return false;
  }
  if ( f.direccion.value == '' )
  {
     alert ( "Debe rellenar el campo dirección" );
     f.direccion.focus();
     return false;
  }
  if ( f.telefono.value == '' )
  {
     alert ( "Debe rellenar el campo teléfono" );
     f.telefono.focus();
     return false;
  }
  if ( !isValidTlf ( f.telefono.value ) )
  {
     alert ( "El teléfono no es válido" );
     f.telefono.focus();
     return false;
  }
  if ( f.poblacion.value == '' )
  {
     alert ( "Debe rellenar el campo población" );
     f.poblacion.focus();
     return false;
  }
  if ( f.codigopostal.value == '' )
  {
     alert ( "Debe rellenar el campo Código postal" );
     f.codigopostal.focus();
     return false;
  }
  if ( f.provincia.value == '' )
  {
     alert ( "Debe rellenar el campo provincia" );
     f.provincia.focus();
     return false;
  }
  if ( f.comentarios.value == '' )
  {
     alert ( "Debe rellenar el campo comentarios" );
     f.comentarios.focus();
     return false;
  }

}
