function normalizeString(s) {
  // this is a simplified normalize space function that replaces any newline, carriage returns with spaces,
  //  replaces all continuous multiple spaces occuring in the string to a single space, then trims the string.
  var S = new String(s);
  while (S.indexOf('\r') > -1) {
    S = S.substring(0, S.indexOf('\r')) + ' ' + S.substring(S.indexOf('\r')+1)
  }
  while (S.indexOf('\n') > -1) {
    S = S.substring(0, S.indexOf('\n')) + ' ' + S.substring(S.indexOf('\n')+1)
  }   
  while (S.indexOf('  ') > -1) {
    S = S.substring(0, S.indexOf('  ')) + ' ' + S.substring(S.indexOf('  ')+2)
  } 
  var i
  while (S.length > 0 && S.charAt(0) <= ' ') S = S.substring(1);
  while ((i=S.length) > 0 && S.charAt(i-1) <= ' ') S = S.substring(0, S.length - 1);
  return S;
}

function getSelectedText() {
  // Resets the selection on the page to a trimmed selection and returns a normalized string of selected text.
  // Will only return a non-empty string if the selected item is of type 'Text'
  var retval = '';
  if (document.selection) {
    var sel = document.selection;
    var range = sel.createRange()
    if (sel.type == 'Text' && range.text.length > 0) {
      textRangeTrim(range);
      range.select();  
      retval = normalizeString(range.text);  
    }
  }
  return retval;
}
function textRangeTrim(r) {
  while (r.text.length > 0 && r.text.charAt(0) <= ' ') r.moveStart("character");
  var i
  while ((i=r.text.length) > 0 && r.text.charAt(i-1) <= ' ') r.moveEnd("character",-1);
}

function popHelpWindow(sHelpKey, w, h, scroll) {
  var width = (w) ? w:350;
  var height = (h) ? h:375;
  var v_scrollbar = '';
  var v_scroll = (scroll) ? scroll:'';
  if (v_scroll.length) {
    v_scrollbar = ',scrollbars';
  }
  var url = '';
  switch (sHelpKey) {
    case 'searchhints':
      url = 'pages/searchhints.asp';
      break;
    case 'finditfast':
      url = 'pages/finditfast.asp';
      break;      
    default:
      break;
  }
  if (url.length) {
    window.open(url, sHelpKey, 'WIDTH=' + width + ',HEIGHT=' + height + v_scrollbar);
  }
}
function doToolbarClick(sParam) {
  // example function to show how to
  // 1. get the selected text
  // 2. prompt for input if there is no selected text
  // 3. conditionally process request based on 
  //    inputed/selected text (querytext), and the 
  //    function argument(s) passed in (sParams)
  var querytext = getSelectedText();
  if (querytext.length == 0) {
    var msg = 'What words do you want to search the ' + sParam + ' for?'
    if (querytext = prompt(msg, '')) {
      querytext = normalizeString(querytext);
    }
  }
  if (!querytext || querytext.length == 0) {
    alert('No Search will be performed');
  }
  else {
    switch (sParam) {
      case 'glossary':
//        alert('A search will be performed on the glossary with the keywords below\r----------------------------------------------------\r' + querytext);    
        window.location.href='default.asp?req=search/&sort=Hits&index=gedglossary&querytext='+querytext+'&floaters=1';
        break;      
      default:
//        alert('A search will be performed in the knowledge articles with the keywords below\r----------------------------------------------------\r' + querytext);    
//        alert('No handler setup for the doToolbarClick argument sParam = \'' + sParam + '\'\r' + 'the keywords are below\r----------------------------------------------------\r' + querytext );
        window.location.href='default.asp?req=search/&sort=Hits&index=gedknowledge&querytext='+querytext+'&floaters=1';
        break;
    }
  }
}

function disableFloater()
{
  var url = window.location.href;
  if (url.indexOf('?') == -1)
    url += '?floater=disabled';
  else
    url += '&floater=disabled';
  window.location.href = url;
}
