{% extends "base.html" %}

{% block head %}
  <script type="text/javascript">
    function onChangeTreatment() {
        if (document.getElementById("id_treatment").value == 3) {
            document.getElementById("id_period").disabled = false;
            document.getElementById("id_dose").disabled = false;
        }
        else if (document.getElementById("id_treatment").value == 4) {
            document.getElementById("id_period").value = "";
            document.getElementById("id_period").disabled = true;
            document.getElementById("id_dose").disabled = false;
        } else {
            document.getElementById("id_period").value = "";
            document.getElementById("id_period").disabled = true;
            document.getElementById("id_dose").value = "";
            document.getElementById("id_dose").disabled = true;
        }
    }
    
    function onLoadBody() {
        document.getElementById("id_treatment").onchange = onChangeTreatment;
        onChangeTreatment();
    }
  </script>
  <script type="text/javascript">
    $(document).ready(function(){
    	
    		if (!$.isNumeric($( "#id_patient" )))
		$( "#id_patient" ).val($.cookie("patient"));

    	
    	
      $("#id_date").datepicker();
    });
  </script>
{% endblock %}

{% block title %}
  Treatment Form
{% endblock %}

{% block content %}
  {% if object %}
    {{object.patient}}
    <h2>Update prior treatment</h2>
  {% else %}
    {{patient}}
    <h2>Create prior treatment</h2>
  {% endif %}

  {% if form.errors %}
    <em>Please correct the following error{{ form.errors|pluralize }}:</em>
  {% endif %}

  <form action="." class="cssform" method="POST" name="priorTreatmentForm">{% csrf_token %}
    <p>{{form.patient.label_tag}}<input type="text" readonly="true" name="patient" id="id_patient"
      {% if object %}
        value = {{object.patient.id}}
      {% else %}
        value = {{patient_id}}
      {% endif %}
      />{{form.patient.errors}}</p>
    <p>{{form.date.label_tag}}{{form.date}}{{form.date.errors}}</p>
    <p>{{form.treatment.label_tag}}{{form.treatment}}{{form.treatment.errors}}</p>
    <p>{{form.period.label_tag}}{{form.period}}{{form.period.errors}}</p>
    <p>{{form.dose.label_tag}}{{form.dose}}{{form.dose.errors}}</p>
    <p>{{form.memo.label_tag}}{{form.memo}}{{form.memo.errors}}</p>
    <input type="submit" /> <input type="reset" />
  </form>

<!--
This is a lifesaver when debugging!
<p> {{ form.error_dict }} </p>
-->
  {% if object %}
    <a href='/patient/detail/{{object.patient.id}}/'>Return to patient</a>
  {% else %}
    <a href='/patient/detail/{{patient.id}}/'>Return to patient</a>
  {% endif %}

{% endblock %}