// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Ajax Responders
Ajax.Responders.register({
 onCreate: function() {
   if ($('universal_ajax_activity_indicator') && Ajax.activeRequestCount > 0)
     Effect.BlindDown('universal_ajax_activity_indicator', {duration: 0.25});
 }, 
 onComplete: function() { 
   if ($('universal_ajax_activity_indicator') && Ajax.activeRequestCount < 1) 
     $('universal_ajax_activity_indicator').hide();
 } 
});

var Offerings = {}
Offerings = {
  selectNewRegistrationProvider: function(provider_li_tag) {
    provider_id = provider_li_tag.id.match(/provider_([0-9]+)/)[1];
    $('provider_id').value = provider_id
    $('registration_submit').disabled = false;
    $('registration_submit').focus();
  },
  clearSearchField: function() {
    $('provider_name').value = '';
    $('registration_submit').disabled = true;
  },
  disableEnterKey: function(e) {
     var key;     
     if (window.event) {
       key = window.event.keyCode; //IE
     }
     else {
       key = e.which; //firefox     
     }
     return (key != 13);
  }
}

// Payments
var Payments = {}
Payments = {
  setVisibleForm: function() {
    $$('div.x_payment_form').each(function(e) {
      e.id == ($('payment_kind').value + '_fields') ? e.show() : e.hide()
    })
  },
  updateTotal: function(auth_token) {
    string = ''
    $$('input.x_registration_check_box').each(function(e) {
      if (string != '') { string += '&' }
      if (e.checked) { string += 'registration_ids[]='+e.value }
    })
    $$('input.x_forced_registration_check_box').each(function(e) {
      if (string != '') { string += '&' }
      if (e.checked) { string += 'registration_ids[]='+e.value }
    })
    string += '&authenticity_token='+auth_token;
		new Ajax.Request('/amount_due?'+string, {method: 'post', asynchronous:true, evalScripts:true});
  },
  confirmUseOfForce: function(cb,wait_listed,expired) {
    if (cb.checked == true) {
      if (
        (!wait_listed || confirm("Are you sure you want to do this? The class is currently full and this will guarantee the provider a seat."))
           &&
        (!expired || confirm("Are you sure you want to do this? The class has already started."))         
         ) {
        cb.checked = true;
      } else {
        cb.checked = false;
      }
    }
    return cb.checked
  }
}