<script type="text/javascript">
//<![CDATA[
  var ConfirmedUnique = 0;
  var ConfirmedDeps = 0;
  /* Calls the argument with all ID's on the page; typical usage:
   * allUpdates(showDetails);
   */
  function allUpdates (what) {
    var elems = document.getElementsByTagName('div');
    var ids = [];
    for (var i = 0; i < elems.length; i++) {
      if (!elems[i].id.match(/^show-hide(\d+)$/)) continue;
        what(RegExp.$1);
    }
  }
  function toggleDetails (id) {
    var e = document.getElementById('details' + id);
    if (!e.style.display || e.style.display == 'none')
      showDetails(id);
    else
      hideDetails(id);
  }
  function showDetails (id) {
    document.getElementById('details' + id).style.display = 'block';
    document.getElementById('show-hide' + id).childNodes[0].nodeValue = 'Hide details';
  }
  function hideDetails (id) {
    document.getElementById('details' + id).style.display = 'none';
    document.getElementById('show-hide' + id).childNodes[0].nodeValue = 'Show details';
  }
  /* Toggles an update - takes a numeric ID, which may optionally contain
   * non-numeric values.  The second argument, if passed and true, indicates
   * that the check has already occured - the actions associated with
   * checking a box will be performed, but the check not actually done.
   */
  function toggleUpdate (id, already, opts) {
    if (/\D/.test(id) && /(\d+)/.test(id)) id = RegExp.$1;
    var check = document.getElementById('check' + id);
    if (!already) {
      check.checked = !check.checked;
    }
    setBanner(id, check.checked);

    var installbox = document.getElementById("<%if update_mode eq 'history'%>uninstall<%else%><%update_mode%><%endif%>-updates");
    if (check.checked) {
      forceUnique(id, document.getElementById("unique" + id).value == "1");
      if (installbox && installbox.style.display == 'none')
        installbox.style.display = 'block';
    }
    else if (installbox && installbox.style.display != 'none') {
      // If no updates are selected, hide the Install box
      var check = document.getElementsByName('<%if update_mode eq 'history'%>uninstall<%else%><%update_mode%><%endif%>');
      var hide = true;
      for (var i = 0; i < check.length; i++) {
        if (check[i].checked) {
          hide = false;
          break;
        }
      }
      if (hide)
        installbox.style.display = 'none';
    }

    if (!opts || !opts['ignore_deps'])
      checkDeps(id, check.checked);
  }
  function setBanner (id, status) {
    /(\d+)/.test(id);
    id = RegExp.$1;
    document.getElementById('sel' + id).style.display = status ? 'block' : 'none';
  }
  function forceUnique (id, on) {
    var e = document.getElementById('check' + id);
    var checkboxes = document.getElementsByTagName('input');
    var confirmwas = ConfirmedUnique;
    for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].type != 'checkbox' || checkboxes[i].id == e.id || !checkboxes[i].checked || !checkboxes[i].id.match(/^check(\d+)$/)) continue;

      var checkid = RegExp.$1;

      if (!on && checkboxes[i].checked && document.getElementById('unique' + checkid).value != "1") continue;

      if (!ConfirmedUnique) {
        var c = confirm(on ?
            'The update you have selected cannot be installed simultaneously with other updates.  Click OK to deselect the other updates, or Cancel to deselect this update.' :
            'Another selected update cannot be installed simultaneously with other updates.  Click OK to deselect the other update, or Cancel to deselect this update.'
        );
        ConfirmedUnique = c ? 2 : 1;
        if (!c) break;
      }
      toggleUpdate(checkid, false, { 'ignore_deps' : true });
    }
    if (ConfirmedUnique == 1) e.checked = false;
    ConfirmedUnique = confirmwas;
  }
  function checkDeps (id, on) {
    /* If on is true, we need to make sure this update depends on no others.
     * Otherwise, we need to make sure that this update wasn't depended upon
     * by other updates.
     * Both of these need to be recursive - if A depends on B, and B depends
     * on C, checking A should check both B and C.
     */

    var confirmwas = ConfirmedDeps;

    if (<%if update_mode eq 'history'%>!<%endif%>on) {
      var deps = document.getElementById('<%if update_mode eq 'history'%>deps<%else%>requires<%endif%>' + id).value;
      deps = deps.length > 0 ? deps.split(/\s+/) : [];
      if (deps.length == 0) return;

      for (var i = 0; i < deps.length; i++) {
        var e = document.getElementById('check' + deps[i]);
        if (<%unless update_mode eq 'history'%>!<%endunless%>e.checked) {
          if (!ConfirmedDeps) {
            var okay = confirm('
                <%~if update_mode eq 'history'~%>
                  The update that you have deselected depends on other selected updates.  Press OK to deselect the other updates as well, or Cancel to reselect this update.
                <%~else~%>
                  The update that you have selected depends on other updates.  Press OK to select the other updates, or Cancel to deselect this update.
                <%~endif~%>
            ');
            if (!okay) {
              toggleUpdate(id);
              break;
            }
            ConfirmedDeps = okay ? 2 : 1;
          }
          toggleUpdate(deps[i])
        }
      }
    }
    else {
      var checkboxes = document.getElementsByTagName('input');
      for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].type != 'checkbox' || <%unless update_mode eq 'history'%>!<%endunless%>checkboxes[i].checked || !checkboxes[i].id.match(/^check(\d+)$/)) continue;
        var eid = RegExp.$1;
        var deps = document.getElementById('<%if update_mode eq 'history'%>deps<%else%>requires<%endif%>' + eid).value;
        if (deps.match(new RegExp("\\b" + id + "\\b"))) {
          if (!ConfirmedDeps) {
            var okay = confirm('
                <%~if update_mode eq 'history'~%>
                  The update that you have selected is depended upon by other updates.  Press OK to select the other updates as well, or Cancel to deselect this update.
                <%~else~%>
                  The update that you have deselected is required for other updates.  Press OK to deselect the other updates as well, or Cancel to reselect this update.
                <%~endif~%>
            ');
            if (!okay) {
              toggleUpdate(id);
              break;
            }
            ConfirmedDeps = okay ? 2 : 1;
          }
          toggleUpdate(eid);
        }
      }
    }
    ConfirmedDeps = confirmwas;
  }
  <%if update_mode eq 'install'%>
  function checkInitial () {
    var preselect = document.getElementById('preselect');
    var sel;
    if (preselect) {
      var updates = preselect.value.split(/\s+/);
      for (var i = 0; i < updates.length; i++) {
        if (!sel) sel = {};
        sel[updates[i]] = true;
      }
    }
    ConfirmedDeps = 2;
    ConfirmedUnique = 2;
    var checkboxes = document.getElementsByTagName('input');
    for (var i = 0; i < checkboxes.length; i++) {
      var e = checkboxes[i];
      if (e.type != 'checkbox' || e.checked || !e.id.match(/^check(\d+)$/)) continue;
      var eid = RegExp.$1;
      if (preselect ? sel[''+eid] : document.getElementById('update_type' + eid).value == 'critical') {
        toggleUpdate(eid);
        showDetails(eid);
      }
    }
    ConfirmedDeps = 0;
    ConfirmedUnique = 0;
  }
  <%endif%>

  // Need to look for pre-checked (e.g. from Firefox's form caching) and handle
  // them accordingly
  function detectPreChecked () {
    var checkboxes = document.getElementsByTagName('input');
    ConfirmedDeps = 2;
    ConfirmedUnique = 2;
    for (var i = 0; i < checkboxes.length; i++) {
      var e = checkboxes[i];
      if (e.type != 'checkbox' || !e.checked || !e.id.match(/^check(\d+)$/)) continue;
      var eid = RegExp.$1;
      toggleUpdate(eid, true);
    }
    ConfirmedDeps = 0;
    ConfirmedUnique = 0;
  }
  window.onload = <%if update_mode eq 'install'%>function() { detectPreChecked(); checkInitial(); }<%else%>detectPreChecked<%endif%>;

//]]>
</script>
