
function check_subscribe_agreement(checkbox) {
  if ($(checkbox).checked) {
    $('subscribe_button').disabled = false;
    Element.removeClassName('subscribe_button', 'faded');
  } else {
    $('subscribe_button').disabled = true;
    Element.addClassName('subscribe_button', 'faded');
  }
}

function delete_image_confirm(media_id) {
  var yes_delete = confirm('Are you sure you want to delete this photo?');
  if (yes_delete) {
     location.href = '/resume/action/delete_image?media_id=' + media_id;
  }
}

function save_the_form() {
    // there might be multiples on the page
    var forms = document.getElementsByName("the_form");
    if (forms.length > 0)
        forms[0].submit();  // just submit the first one
}

function update_choices(tcat) {
  var num_other_selected = parseInt($('num_other_selected_' + tcat).value);
  var choices_id = 'num_choices_' + tcat;
  if (num_other_selected >= 1) {
    $(choices_id).innerHTML = "(" + num_other_selected + " selected)";
    Element.show(choices_id);
  } else {
    Element.hide(choices_id);
  }
}

function update_checkbox(item, tcat, is_other) {
  if (is_other) {
    var current_value = parseInt($('num_other_selected_' + tcat).value);
    if ($(item).checked) {
        current_value++;
    } else {
        current_value--;
    }
    $('num_other_selected_' + tcat).value = current_value;
  }

  var delete_field = $(item).name + "_del";
  // if the item is checked...
  if ($(item).checked) {
      // and there is a "delete" field for it (they had unchecked it)
      if ($(delete_field)) {
          // remove the delete field
          Element.remove($(delete_field));
      }
  }
  // else, the item is unchecked, and we should add a "delete" field
  else {
    var hidden_field_code =
      ('<input id="' + delete_field + '" type="hidden" ' + 
       'name="' + $(item).name + '" value="|delete|" />');
    new Insertion.Before('hidden_form_fields_placeholder', hidden_field_code);
  }
}

var num_other_options = 0;

function generate_other_option(tcat, form_input) {

    // I heart security audits.
    var new_option_string = null;
    new_option_string = $(form_input).value.unescapeHTML();
    new_option_string = new_option_string.replace(/"/gi, '');
    new_option_string = new_option_string.escapeHTML();

    if (! new_option_string)
        return;

    $(form_input).value = "";

    num_other_options++;
    var checkbox_form_name =
        ("d_new_" + (900 + num_other_options) + "_" + tcat);
                              
    var insertion_code = ('<input checked type="checkbox" ' + 
                          'onclick="update_checkbox(this,' + "'" + tcat + "'" +
                          ', true)" name="' + checkbox_form_name + '" ' + 
                          'value="' + new_option_string + '" ' +
                          ' /><b>'+ new_option_string + '</b><br />');

    var num_other_selected = parseInt($('num_other_selected_' + tcat).value);
    num_other_selected++;
    $('num_other_selected_' + tcat).value = num_other_selected;

    Element.show('other_input_selections_' + tcat);
    new Insertion.After('other_input_selections_' + tcat, insertion_code);
}

