function chooseAddress() {
  if ($('lower_form_fields'))
    $('lower_form_fields').style.display = 'block';
  var pafSelect = $('user_paf_address_id');
  replaceChildNodes('address_display', ellipsisTruncate(pafSelect.options[pafSelect.selectedIndex].text));
  $('address_chosen').style.display = 'block';
  $('address_not_chosen').style.display = 'none';
};
function ellipsisTruncate(str) {
  var words = str.match(/(\S+)/g);
  if (words.length == 0) return '';
  var out = words.shift();
  while (words.length && (out + ' ' + words[0]).length < 48) {
    out += ' ' + words.shift();
  }
  if (words.length) {
    return out + '...';
  } else {
    return out;
  }
};
function reshowAddressSelector() {
  $('address_chosen').style.display = 'none';
  $('address_not_chosen').style.display = 'block';
}

function ajaxifyFindAddress(buttonId) {
  var regform = $("large");
  var button = $(buttonId);
  if (button == null) return;
  Event.observe(button, "click", function(e) {
    Event.stop(e);
    var loading = new Insertion.Top("address_chooser", '<img style="margin-left: 120px;" src="/images/icons/loading_bar.gif" width="220" height="19">');
    new Ajax.Updater("address_chooser", regform.action,
                     { method: "post",
                       parameters: Form.serialize(regform) });
  });
};
Event.onDOMReady(function() {
	ajaxifyFindAddress("find_address_button");
	ajaxifyFindAddress("change_address_button");
});

function prefsMonitor() {
  var heading = $("more_details_heading");
  var postcode_label = $("postcode_label");
  
  if (postcode_label == null) return;

  function updateForm() {
    if (existing_supporter.is_yes()) {
      heading.innerHTML = translations.existing_supporter_address;
      postcode_label.className = "required";
    } else if (phone_post_contact_opt_in.is_yes() || email_contact_opt_in.is_yes()) {
      heading.innerHTML = translations.keep_in_touch_address;
      postcode_label.className = "required";
    } else {
      heading.innerHTML = translations.please_let_us_know_details;
      postcode_label.className = "";
    };
  };
  
  var radios = [ "existing_supporter", "email_contact_opt_in", "phone_post_contact_opt_in" ];
  for (var i = 0; i < radios.length; i++) {
    // Fancy way to build up useful objects for each of the radios
    var name = radios[i];
    var radio = {
      items: { yes: $(name + "_yes"), no: $(name + "_no") },
      is_yes: function() { return this.items.yes.checked; }
    };
    eval("var " + name + " = radio");
    
    // Then attach the click event to them
    Event.observe(radio.items.yes, "click", updateForm);
    Event.observe(radio.items.no, "click", updateForm);
  };
};
Event.onDOMReady(prefsMonitor);

function showHidePassword() {
  var updating_password = $("user_updating_password");
  if (!updating_password) return;
  
  var password_fields = $("password_fields");
  
  function update() {
    if (updating_password.checked) {
      password_fields.show();
    } else {
      password_fields.hide();
    };
  };
  
  Event.observe(updating_password, "change", update);
  update();
};
Event.onDOMReady(showHidePassword);

function showHideUniversityOptions() {
  var isStudentOption = $('relation_to_university_student');
  if (!isStudentOption) return;
  var studentFields = $("university_student_options");
  var isStaffOption = $('relation_to_university_staff');
  var staffFields = $("university_staff_options");
  var isNeitherOption = $('relation_to_university_neither');
  
  var extraOptionsContainer = $('university_extra_options');
  appendChildNodes(extraOptionsContainer, studentFields, staffFields);

  function updateFields() {
    if (isStudentOption.checked) {
      studentFields.show();
    } else {
      studentFields.hide();
    }
    if (isStaffOption.checked) {
      staffFields.show();
    } else {
      staffFields.hide();
    }
  }
  updateFields();
  Event.observe(isStudentOption, 'change', updateFields);
  Event.observe(isStaffOption, 'change', updateFields);
  Event.observe(isNeitherOption, 'change', updateFields);
}
Event.onDOMReady(showHideUniversityOptions);

// If the address chooser gets hidden this modifies the size of the page
// And the automatic moving to the #community_profile anchor is thrown out
// This function fixes that
function moveToCommunityProfile() {
  if (window.location.hash == "#community_profile") {
    window.scrollTo(0, Position.cumulativeOffset($("community_profile"))[1]);
  };
};
addLoadEvent(moveToCommunityProfile);

