{% extends "new_request.html" %}
{% load sekizai_tags %}

{% block extra_scripts %}
{{ block.super }}
{% addtoblock "endscripts" %}
<script>
  var licences = { {% for licence in  wizard.form.fields.licence.queryset.all %} 
    {{licence.id}}: {
      name: "{{licence.name}}",
      free: {% if licence.only_free_documents %}true{% else %}false {% endif %},
    },
  {% endfor %}
  };
  var $licence = $('#id_document_copyrights-licence');
  function disable_licences(truth) {
    $('option', $licence).each(function (i, v) {
      var val = $(v).attr('value');
      if (val == "") {
        return true;
      }
      val = parseInt(val);
      if (licences[val].free) {
        $(v).attr('disabled', truth);
        if (truth) {
          $('<span>(interdit si le document contient des extraits protégés)</span>').appendTo(v);
        } else {
          $('span', v).remove();
        }
      }
    });
    if (truth) {
      var val = $licence.val();
      if (val != "") {
        val = parseInt(val);
        if (licences[val].free) {
          $licence.val('');
          $licence.trigger('change');
        }
      }
    }
  }
  $('#id_document_copyrights-copyright').change(function () {
    disable_licences($(this).val() == 'True');
  });
</script>
{% endaddtoblock %}
{% endblock %}
