$(document).ready(function() {
  toggleBetweenStreetAndPackstation();
  $('input[name="selected_packstation"]').change(function() {
    toggleBetweenStreetAndPackstation();
  });
});

function toggleBetweenStreetAndPackstation() {
  if ($('#selected_packstation_yes').attr('checked')) {
    showPackstationHideStreet();
    makeCountryReadOnly();
  }
  if ($('#selected_packstation_no').attr('checked')) {
    showStreetHidePackstation();
    makeCountrySelectable();
  }
}

function showPackstationHideStreet() {
  $('#packstation').css('display','table-row');
  $('input[name="customers_street_address"]').parent('td').parent().css('display','none');
}

function showStreetHidePackstation() {
  $('#packstation').css('display','none');
  $('input[name="customers_street_address"]').parent('td').parent().css('display','table-row');
}

function makeCountryReadOnly() { // and select germany
  var selectElement = $('select[name="customers_country_code"]');
  selectElement.attr('name','customers_country_code_dummy');
  selectElement.attr('disabled','disabled');
  selectElement.children('option[value="DE"]').attr('selected','selected');
  selectElement.after('<input name="customers_country_code" type="hidden" value="DE" />');
}

function makeCountrySelectable() {
  var inputElement = $('input[name="customers_country_code"]');
  if (inputElement) {
    inputElement.remove();
  }
  var selectElement = $('select[name="customers_country_code_dummy"]');
  selectElement.attr('name','customers_country_code');
  selectElement.removeAttr('disabled');
  selectElement.children('option[value="DE"]').attr('selected','selected');
}

