
var initAutoConfirm = function () {

  $('.auto_confirm_unconfirmed').click(function() {
    $(this).html('Please wait...');
    var json_string = '"volunteer_id":"' + encodeURIComponent($('#volunteer_id').val()) + '"';
    json_string += ', "shift_id":"' + encodeURIComponent($(this).parent().parent().attr('id').substring(1)) + '"';
    json_string += ', "shift_volunteer_id":"' + encodeURIComponent($(this).attr('id').substring(1)) + '"';
    result = '';

    $.ajax({
      url: '/volunteer/auto_confirm.php?confirm&data={' + json_string + '}',
      type: 'GET',
      async: false,
      cache: false,
      timeout: 30000,
      error: function(){
        return true;
      },
      success: function(data){
        result = data;
      }
    });

    data = result;
    if (data) {
      var dataObject = jQuery.parseJSON(data);
      if (dataObject.success == 1) {
        $(this).html('Confirmed');
        $(this).addClass('auto_confirm_confirmed');
        $(this).removeClass('auto_confirm_unconfirmed');
      } else {
        $(this).html('(couldn\'t confirm)');
        $(this).css('cursor', 'default');
        $(this).unbind('click');
        alert('We were not able to confirm this shift. Bummer. Please refresh this page and try again.');
      }
    } else {
      alert('There was a problem with our website. Please refresh this page and try again.');
    }
  });

}

