{% extends "base_menu.html" %}
{% load crispy_forms_tags %}

{% block title %}
    {% if object %} Update {{ object.title }}
    {% else %} Create Ad
    {% endif %}
{% endblock %}

{% block content %}
<p>
  <form action="" method="post" id="upload_form" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form|crispy }}
    <input type="submit" value="Submit">
    <input type="submit" value="Cancel" onclick="window.location.href='{% url 'ads:index' %}';return false;">
  </form>
</p>
<!-- https://stackoverflow.com/questions/2472422/django-file-upload-size-limit -->
<script>
$("#upload_form").submit(function() {
  console.log('Checking file size');
  if (window.File && window.FileReader && window.FileList && window.Blob) {
      var file = $('#id_{{ form.upload_field_name }}')[0].files[0];
      if (file && file.size > {{ form.max_upload_limit }} ) {
          alert("File " + file.name + " of type " + file.type + " must be < {{ form.max_upload_limit_text }}");
      return false;
    }
  }
});
</script>
{% endblock %}
