$(document).ready ->
return unless $("form#appointment_data_search")
###### set the lab code in the appointment form ########
# using click handler cause it fires before the submit
# request
$("#find_appointments").bind('click', ->
selected_lab = $('#lab_list_container input:radio:checked')
lab_code = $('#lab_list_container input:radio:checked').parent('li').find('form input#lab_code').val()
$("form#appointment_data_search input#lab_code").val(lab_code)
)
###### handle ajax appointment data search submit ########
$("form#appointment_data_search")
.bind('ajax:beforeSend', (event, data)->
form = $(this)
Appointment.BeforeSendHandler(form)
)
.bind('ajax:success', (event, data)->
form = $(this)
Appointment.SuccessHandler(form, event, data)
)
.bind('ajax:complete', (event, data)->
form = $(this)
$('.appointment_spinner_elem').hide()
)
.bind('ajax:error', (xhr, status, error)->
form = $(this)
response = JSON.parse(status.responseText)
Appointment.ErrorHandler(form, status.status, response.message)
)
# override this proc to do your own before send handling
Appointment.BeforeSendHandler = (form)->
messages = []
date = form.find('input#date').val()
valid_date = Date.parseExact(date, "M/d/yyyy")
messages.push("Date is invalid, must be mm/dd/yyyy format") unless valid_date
# trying to set the lab code here did not work. so had to use click handler above
lab_code = form.find('input#lab_code').val()
messages.push("Labcode is missing") unless lab_code
if (messages.length > 0)
span = $("#{messages.join("
")}")
form.find('.error_message').append(span)
span.fadeOut(4000)
false
$('.appointment_spinner_elem').show()
# override this proc to do your own success handling
Appointment.SuccessHandler = (form, event, data) ->
if ( data.times.length == 0)
Appointment.ErrorHandler($(this), 200, "No times available for that date")
return
window.appointment_list.setList(data.times)
# set the first appointment time as selected
$('input:radio[name=appointment_time]:first').prop('checked',true)
# re enable searching for appointment times
$('form#appointment_data_search input[type=submit]').prop("disabled", false)
# override this proc to do your own error handling
Appointment.ErrorHandler = (form, status, message)->
if message.match(/Connection refused/i)
message = "Unable to make appointments at this time. Try walk-in instead"
# do your own handling here
span = $("#{message}")
form.find('.error_message').append(span)
span.fadeOut(5000)
###### handle toggle to show more appointments ########
$("#more_appointments_list_toggler").live("click", ->
window.appointment_list.setShowNumber(window.max_appointment_show_number)
$('input:radio[name=appointment_time]:first').prop('checked',true)
$("#more_appointments_list_toggler").hide()
)