//************************************************ 
//* Customizable region a file - this file can be 
//* modified to hold client page header.
//*
//* This region is the top of the html page.
//*
//* NOTE: DO NOT DELETE THIS FILE.
//*	  You may modify this file or you may
//*	  remove its functionality by deleting all
//*	  lines following the first line:
//*	  'document.write('<!--begin region_top-->');'
//*
//************************************************ 

if (typeof $=='function') {
  $('head').append('<style type="text/css">#headerBNav ul li.search .search-filter select { display: none !important; }</style>');
} else {
  document.write('<style type="text/css">#headerBNav ul li.search .search-filter select { display: none !important; }</style>');
} // End of else conditional: (!$('head'))

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Automatically clear input value if it is the default value
function inputAutoClear(input) {
  if (typeof SearchTerm!="undefined") { input.style.color = '#000'; input.value = SearchTerm; }
  input.onfocus = function() { if(this.value == this.defaultValue) { this.style.color = '#000'; this.value = '';}}
  input.onblur = function() { if(this.value == '') { this.style.color = '#6c7173'; this.value = this.defaultValue;}}
} // End of function inputAutoClear: (input)

// Automatically filter the search box at the top of the page based upon your current area
function autoFilterSearch() {
  var Filters = {
    'search': { 'pathfragment': '/kb/inc/custom/search.asp' },
    'files': { 'pathfragment': 'healthfiles/' },
    'med': { 'pathfragment': 'drugdetail/' },
    'test': { 'pathfragment': 'medicaltest/' },
    'ht': { 'pathfragment': 'kb/' },
    'ds': { 'pathfragment': 'dietitian/' }
  };
  var CurrentLocation = location.pathname;
  for (Filter in Filters) {
    var FilterHash = Filters[Filter];
    if (CurrentLocation.match(FilterHash['pathfragment'])!=null) {
      if (Filter=='search') {
        delete Filters['search'];
        Filters['hf'] = { 'pathfragment': '' };

      	var parts = location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,CurrentSearch) {
      		if (key=='filter') {
      		  CurrentSearch = CurrentSearch.toLowerCase();
      		  for (QueryFilter in Filters) {
              if (CurrentSearch.match(QueryFilter)) {
                document.getElementById('search-filter-all').selected = false;
                document.getElementById('search-filter-'+QueryFilter).selected = true;
                break;
              } // End of if conditional: (CurrentSearch.match(QueryFilter))
            } // End of for loop: (QueryFilter in Filters)
          } // End of if conditional: (key=='filter')
      	});
      } else {
        document.getElementById('search-filter-all').selected = false;
        document.getElementById('search-filter-'+Filter).selected = true;
      } // End of else conditional: !(Filter=='search')
      break;
    }
  }
} // End of function autoFilterSearch()

function replaceSearch() {
  if (document.getElementById('search-filter-select')!=null) {
    function selectReplacement() {
      var obj = document.getElementById('search-filter-select');
      // append a class to the select
      obj.className += ' replaced';
      // create list for styling
      var ul = document.createElement('ul');
      ul.className = 'selectReplacement';
      ul.id = 'search-filter-ul';
      var opts = obj.options;
      for (var i=0; i<opts.length; i++) {
        var selectedOpt;
        if (opts[i].selected) {
          selectedOpt = i;
          selectedOptInnerHTML = opts[i].innerHTML;
          break;
        } else {
          selectedOpt = 0;
        }
      }
      for (var i=0; i<opts.length; i++) {
        var li = document.createElement('li');
        var txt = document.createTextNode(opts[i].text);
        li.appendChild(txt);
        li.selIndex = opts[i].index;
        li.selectID = obj.id;
        li.onclick = function() {
          selectMe(this);
        }
        if (i == selectedOpt) {
          li.className = 'selected';
          li.onclick = function() {
            this.parentNode.className += ' selectOpen';
            this.onclick = function() {
              selectMe(this);
            }
          }
        }
        if (window.attachEvent) {
          li.onmouseover = function() {
            this.className += ' hover';
          }
          li.onmouseout = function() {
            this.className = 
              this.className.replace(new RegExp(" hover\\b"), '');
          }
        }
        ul.appendChild(li);
      }
      // add the input and the ul
      var filterLabel = obj.parentNode.getElementsByTagName('label')[0];
      filterLabel.innerHTML = 'Searching in';
      var currentFilter = document.createElement('div');
      currentFilter.className = 'current-filter';
      currentFilter.id = 'search-current-filter';
      currentFilter.innerHTML = selectedOptInnerHTML;
      currentFilter.onclick = function() {
        if (document.getElementById('search-filter-ul').className.indexOf('selectOpen')!=-1) {
          document.getElementById('search-filter-ul').className = 'selectReplacement';
        } else {
          document.getElementById('search-filter-ul').className = 'selectReplacement selectOpen';
        }
      }
      obj.parentNode.insertBefore(ul,filterLabel);
      obj.parentNode.insertBefore(currentFilter,filterLabel);
    }
    function selectMe(obj) {
      var lis = obj.parentNode.getElementsByTagName('li');
      for (var i=0; i<lis.length; i++) {
        if (lis[i] != obj) { // not the selected list item
          lis[i].className='';
          lis[i].onclick = function() {
            selectMe(this);
          }
       } else {
          setVal(obj.selectID, obj.selIndex);
          obj.className='selected';
          obj.parentNode.className = 
            obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
          obj.onclick = function() {
            obj.parentNode.className += ' selectOpen';
            this.onclick = function() {
              selectMe(this);
            }
          }
        }
      }
      document.getElementById('search-current-filter').innerHTML = obj.innerHTML;
    }
    function setVal(objID, selIndex) {
      var obj = document.getElementById(objID);
      obj.selectedIndex = selIndex;
    }
    function closeSel(obj) {
      // close the ul
    }
    
    // Replace the filter select with a nicer javascript dropdown menu 
    selectReplacement();
  } // End of if conditional: (document.getElementById('search-filter-select')!=null)
}

// Search input auto-blank and dropdown replacement
function enhancedSearch() {
  inputAutoClear(document.getElementById('top-searchterm'));
  autoFilterSearch();
  replaceSearch();
} // End of enhancedSearch function: ()


if (typeof $=='function') {
  enhancedSearch();
} else {
  addLoadEvent(enhancedSearch);
} // End of else conditional: !(typeof $=='function')