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

// State/Region drop-down event handler
function bindRegionSelect(regionSelectId, countrySelectId, regionsMapping) {
  jQuery(function ($) {
    var region = $("#" + regionSelectId);
    var country = $("#" + countrySelectId);

    country.bind("change", function(e) {
      var countryId = country.val();
      var oldValue = region.val();

      region.html("");
      
      if (regionsMapping[countryId]) {
        region.append("<option value=\"\">-- Please select --</option>");
        $.each(regionsMapping[countryId], function(index, row){
          region.append("<option value=\"" + row[0] + "\"" + (row[0] + "" == oldValue ? " selected=\"selected\"" : "") + ">" + row[1] + "</option>");
        });
      } else {
        region.append("<option value=\"\">-- Other --</option>");
      }
    });
  });
}

document.observe("dom:loaded", function() {
  if ($('billing_info_bill_current_card_true') && $('billing_info_bill_current_card_false')) {
    toggler = function(event) {
        if ($("billing_info_bill_current_card_true").checked) {
            $$(".billing_fields > fieldset.left").each(function(element, index) { element.parentNode.hide(); })
        } else {
            $$(".billing_fields > fieldset.left").each(function(element, index) { element.parentNode.show(); })
        }
    }
    $('billing_info_bill_current_card_true').observe("click", toggler);
    $('billing_info_bill_current_card_false').observe("click", toggler);
    toggler(null);
  }
});

