document.observe('dom:loaded', init_country_select);

function init_country_select() {
  $$('.country_select').each(function(elem){
    $(elem).observe('change', function() {
      var container = $(this.getAttribute('id') + '_state');
      if(!container) return;
    
      var value = $F(this);
      if(value == '') {
        container.hide();
      } else {
        var select = container.down('select');
        var text_field = container.down('input');
        if(value == 'United States') {
          select.disabled = false;
          select.show();
          text_field.disabled = true;
          text_field.hide();
        } else {
          select.disabled = true;
          select.hide();
          text_field.disabled = false;
          text_field.show();
        }
        container.show();
      }
    });
  });
}
