/**** Add application wide javascripts below this point  ******/
$(document).ready(function() {
  $("#treatment_options").tabs();
  
  $(".treatment_select").click(function(){
    $("#treatment_time").empty();
    $("#selected_treatment").val($(this).val());
    $.get('/populate_weeks', function(response){
      $("#treatment_week").html(response);
      $("#treatment_week").removeAttr("disabled");
      populate_days();
    });
    return true;
  });
  
  $("#treatment_week").change(function(){
    populate_days();
  });
  
  $("#check_link").click(function() {
    if(!$("#selected_treatment").val() || !$("#treatment_week").val()) { 
      alert("Please choose a treatment and time before checking availability");
      return false;
    } else {
      feedback_start("Checking Availability");
      populate_appointments();
    }
  });
  
  $("#appointment_form").validate();
  $("#contact_form").validate();
  $("#treatment_day").change(function() { 
    $("#treatment_time").empty();
  });
});


function populate_days() {
  var treat = $("#selected_treatment").val();
  var week = $("#treatment_week").val();
  $.get('/populate_days/',{week:week }, function(response){
    $("#treatment_day").html(response);
    $("#treatment_day").removeAttr("disabled");
  });
}

function populate_appointments() {
  var treat = $("#selected_treatment").val();
  var week = $("#treatment_week").val();
  var day = $("#treatment_day").val();
  $.get('/populate_appointments/',{treatment:treat, treatweek:week, treatday:day }, function(response){
    if(!response) {
      alert("No appointments are available, try another time");
    } else {
      $("#treatment_time").html(response);
      $("#treatment_time").removeAttr("disabled");
      $("#name").removeAttr("disabled");
      $("#email").removeAttr("disabled");
      $("#telephone").removeAttr("disabled");
      $("#booking_button").removeAttr("disabled");
      $("#booking_button").attr("src", "images/book_active.gif");
    }
    feedback_end();
  });
}


function feedback_start(msg) {
  $.blockUI({ 
    css: { 
      border: 'none', 
      padding: '15px', 
      backgroundColor: '#000', 
      '-webkit-border-radius': '10px', 
      '-moz-border-radius': '10px', 
      opacity: '.5', 
      color: '#fff',
      fontSize: '150%'
    },
    message: msg
  });
}

function feedback_end() {
  $.unblockUI();
}