﻿/***********************************************
Javascript to help with usability of checkboxes in Advanced search form so
that 'All' and the rest are mutually exclusive.

This functionality is not available when Javascript is disabled 
but does not prevent the search operating correctly
***********************************************/

/**
 * Description: Bubbles window events
 * Author: Peter Hammans
 **/
function attachEvent(oFunction, sEvent) {
    var oOriginal = window[sEvent];
    if(typeof(oOriginal) != "function") {
        window[sEvent] = oFunction;
    } else {
        window[sEvent] = function() {
            oOriginal();
            oFunction();
        };
    };
};

var oAdvSearchBoxes = 
{
  init : function()
  {
    //Find the advanced search options
    var advancedSearchOptions = document.getElementById("advancedSearchOptions");
    
    var inputElements = advancedSearchOptions.getElementsByTagName("INPUT");
    
    //Get the child checkboxes and attach an event handler
    
    //Attach a specific handler to the 'All' check box
    inputElements[0].onclick = function()
    {
      //When 'All' is checked, all the sibling elements should be unchecked
      if (this.checked)
      {
        var siblings = this.parentNode.parentNode.getElementsByTagName("INPUT");
        for ( var siblingIndex = 1; siblingIndex < siblings.length; siblingIndex++ )
        {
          siblings[siblingIndex].checked = false;
        }
      }
    }
    
    //Attach another handler to the other checkboxes
    for ( var childInputIndex = 1; childInputIndex < inputElements.length; childInputIndex++)
    {
      inputElements[childInputIndex].onclick = function()
      {
        //When the other boxes are checked, the all check box should become unticked
        if ( this.checked )
        {
          var siblings = this.parentNode.parentNode.getElementsByTagName("INPUT");
          siblings[0].checked = false;
        }
      }
    }
  }
}

attachEvent(oAdvSearchBoxes.init, "onload")


