/***********************************************************************
* Programmed by...
*   Keith Eric Grant
*
* Date...
*   17 April 2007
*      Adapted for MCDs from "Note Glue" script
*   20 March 2005
*
* Purpose...
*   These routines implement a search capability in the "Note Glue"
*   journaling framework. Because the Javascript search() method
*   includes regular expressions, the search can use re's also.
*
***********************************************************************/

function SearchMCDs (form)
{
  var str   = form.SearchString.value;
  var divs  = document.getElementsByTagName('div');
  var ndivs = divs.length;

  var idiv;

  str = str.toLowerCase();  // Case insensitive search

  // Firefox would get the value directly, but to get this working
  // with IE, we had to get the index of the selected search option
  // and then get the text associated with that index.

  // var type = form.getElementsByTagName('select').item(0).value;
  var sel  = form.getElementsByTagName('select').item(0);
  var type = sel.options[sel.selectedIndex].text;

  // Notes are found by looking for 'div' elements with
  // the class name of 'competency'.

  for ( idiv=0; idiv<ndivs; idiv++ ) {
    var  node = divs.item(idiv);
    if ( node.className && node.className == 'competency' ) {

      // Turn off display as the starting point
      node.style.display = 'none';

      // Set if search is "all text" or "Search Titles" or ...
      // This intentionally uses drop through.

      var text = '';
      switch (type)  {

        case 'Search Titles+Descriptions+Comments':

          // Comments are an optional field
          if ( node.getElementsByTagName('p').item(1) ) {
            text = getObjText(node.getElementsByTagName('p').item(1));
          }

        case 'Search Titles+Descriptions':
          text += ' ' + getObjText(node.getElementsByTagName('p').item(0));

        case 'Search Titles':
          text += ' ' + getObjText(node.getElementsByTagName('h3').item(0));
          break;

        default:
          text = getObjText(node);
          break;
      }

      text = text.toLowerCase();  // Case insensitive search


      // If we find a match, display is turned on

      if ( text.search(str) != -1 ) {
        node.style.display = '';
      }

    }
  }
  return false; // Don't want the form to reload
}


// Reset to display all massage competency definitions

function DisplayAllMCDs (form)
{
  var divs  = document.getElementsByTagName('div');
  var ndivs = divs.length;
  var idiv;

  // Clear any previous search string
  form.SearchString.value = '';

  // Reset all of the style display properties

  for ( idiv=0; idiv<ndivs; idiv++ ) {
    var  node = divs.item(idiv);
    if ( node.className && node.className == 'competency' ) {
      node.style.display = '';
    }
  }
}

function getObjText (obj) {

  // The W3C DOM uses textContent. IE uses innerText

  return ( obj.textContent ) ? obj.textContent :
    ( obj.innerText ) ? obj.innerText : "";

}


