{% extends "base.html" %}
{% block title %} Añadir alumno | {{super}}{%endblock %}

{%block escoge-asignatura%}
  {{informacion.asignatura.slug}}
{%endblock%}

{% block navbar%}
{%include "app/include/menu.html"%}
{%endblock%}

{%block content %} 


    <div class="span8">



  <h1>Añadir alumno</h1>
  <br>

               <form method="post" action="" class="form-horizontal ">
                  {% csrf_token %}
        {% for field in form %}


          <div class="control-group">
            <label class="control-label" for="input01">{{ field.label}}</label>
            {% if  field.errors %}
                          <div class="alert alert-error" style="margin-top:0.4em;width:10em;">
                              <a class="close" data-dismiss="alert">×</a>
                              <strong>ERROR</strong>{{ field.errors }}
                          </div>
                        {%endif%}
            <div class="controls">
              {%if field.label == 'Foto' %}
                  <img src="{{MEDIA_URL}}{{user.usuario.foto}}"width="100px" heigth="100px"><br/>
              {%endif %}              

              {{field}}


            </div>
          </div>

        {% endfor %}
        <br/>

          <div class="form-actions">
            <div class="pull-right">
                     <button type="submit" class="btn btn-primary btn-large btn-success" id="salvar">Crear alumno</button>
                     {%if informacion.atras %}
                      <a href="{{informacion.atras}}" class="btn btn-primary btn-large btn-danger">Volver</a>

                     {% else%}
                     <a href="{%url ver_asignatura asignatura.slug  %}" class="btn btn-primary btn-large btn-danger">Volver</a>
                     {%endif%}
            </div>
          </div>
        </form>  
    

  </div>
</div>


{%endblock%}


{% block extra_js%}

  {{super}}

  <script>
$(document).ready(function($) {
    $('button[type=submit]').bind('click', function(e) {
        e.preventDefault() // previene que sea enviado varias veces
        if(validar_formulario())
        {
          $('#salvar').attr('disabled','true'); // desactiva el boton
          // Envia la peticion
          var args = { type:"POST", url:'{%url aniadir_alumno_asignatura informacion.asignatura.slug %}', data:$('form').serialize()};
          $.ajax(args);
        }

    });

    
    function validar_formulario()
    {
 
      valida_Email();
        
      
      if ($('#id_first_name').val() == "") 
      {
          alert( "Por favor introduce el nombre" );
          $('#id_first_name').focus();
          return false ;
      }

      if ($('#id_last_name').val() == "") 
      {
          alert( "Por favor introduce los apellidos" );
          $('#id_last_name').focus();
          return false ;
      }

      if(valida_Dni() && valida_Email());
        return true;
    }

    function valida_Dni()
    {
      dni =$('#id_username').val();
      if (dni == "") 
      {
        alert( "Por favor introduce el dni" );
        $('#id_username').focus();
        return false ;
      }
      else
      {
        if(/^([0-9]{8})*[a-zA-Z]+$/.test(dni))
        {
            var numero = dni.substr(0,dni.length-1);
            var let = dni.substr(dni.length-1,1);
            numero = numero % 23;
            var letra='TRWAGMYFPDXBNJZSQVHLCKET';
            letra=letra.substring(numero,numero+1);
            if (letra==let.toUpperCase())
            {
              return true;
            }

            alert('Error: La letra no corresponde al Dni');
            return false;
        }

        else
        {
          alert('Por favor escriba un dni con el formato correcto');
          return false;
        } 
      } 
    }

    function valida_Email()
    {

      if ($('#id_email').val() == "") 
      {
          alert( "Por favor introduce una direccion de correo" );
          $('#id_email').focus();
          return false ;
      }
      else
      {
        email = $('#id_email').val();
        var emailFilter= /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/;

        if (emailFilter.test(email))
        {
          return true;
        }
        else
        {
          alert('Email incorrecto');
          $('#id_email').focus();
          return false
        }
      }
    }


    $('body').ajaxComplete(function(e, xhr, settings) {
          if (xhr.status == 278) {
              window.location.href = xhr.getResponseHeader("Location").replace(/\?.*$/, "?next="+window.location.pathname);
          }
      });

});
</script>
{% endblock %}