// We want to provide hints to the user when they are trying an Author
// or Title search, since we have a list of "known" authors and titles.
// When they enter text into the keyword field, we should use that to
// query the database (via XMLHTTP) for possible suggestions, and display them.
//
// We don't want to hit the server too many times. We should trim a list
// locally if we can.  Query the server once each time we have a new 
// word of 3 or more letters.  When more letters than that are typed,
// just filter the results we have in-function. 


var sh_words = '';
var sh_response = '';
var sh_lines = 0;
var sh_type = '';
function searchhelp ( e ) { 
   if (window.event) { keynum = e.keyCode; }
   else if (e.which) { keynum = e.which; }

   // Get the new keypress
   key = String.fromCharCode(keynum);

   // Get the string in the form (before the keypress!)
   var keyword = document.getElementById('search_keyword').value;

   // If the new key is a regular letter, add it to our keyword string
   //if (keynum > 32 && keynum < 123) { keyword += key; }

   // If our string length is too small (less than 3 characters total)
   // clear the hint box, hide it, and return.
   var h = document.getElementById('search_hint');
   if (keyword.length < 3) { 
      h.style.display='none';
      sh_words    = '';
      sh_response = '';
      return 1; 
   }

   // We potentially have something to search on and display.
   // Make sure the search type is one we can provide hints for - 
   // only author or title. Other search types (keyword/subject) do not
   // get hints.
   var searchtype = document.searchform.searchtype;
   var type = 'author'
   for (var i = 0; i < searchtype.length; i++) {
      if (searchtype[i].checked) { type = searchtype[i].value }
   }
   if (type != 'author' && type != 'title') { return 1; }

   // ******* Kludge
   // We want to provide something useful for author "An Na", which can
   // also appear as "Na, An".  Below we ignore individual words less than
   // 3 letters and dump commas.  Intervene here for this special case.
   var kl = keyword.toLowerCase();
   if (kl == 'an na' || kl == 'na,an' || kl == 'na, an') { 
      h.innerHTML = '<b>Are you looking for...</b><br>\n' + 
         "<a href='/authoridsearch.cgi?id=4227'>Na, An</a><br>\n";
      h.style.display='block';
      return 1;
   }


   // Ignore commas as in "Last, First"
   keyword = keyword.replace(/\,/, " ");

   keyword = keyword.replace("?", " ");
   keyword = keyword.replace("-", " ");
   keyword = keyword.replace(":", " ");
   keyword = keyword.replace("(", " ");
   keyword = keyword.replace(")", " ");
   keyword = keyword.replace("!", " ");
   keyword = keyword.replace("'", " ");
   keyword = keyword.replace("\"", " ");


   // Count words >= 3 in length
   var words = keyword.split(' ');
   var numwords = 0;
   var shortwords = Array();
   for (i=0; i<words.length; i++) { 
      if (words[i].length >= 3) { 
         shortwords[i] = words[i].substr(0,3);
      }
   }
   var all_shortwords = shortwords.join(' ');

   // If we have more words than we used to, do a new query
   if ((all_shortwords != sh_words) || (type != sh_type) || (sh_lines >= 200)) {
      sh_words = all_shortwords;
      sh_type  = type;

      var xmlHTTP = getXMLHTTP();
      if (!xmlHTTP) { return; }
      var geturl = '/searchhelp.cgi?type='+type+'&key='+keyword;

      xmlHTTP.open("GET", geturl, false);
      xmlHTTP.send(null);
      data = xmlHTTP.responseText;

      sh_response = data;

   } else {
      data = sh_response;
   }

   if (!data) { 
      h.innerHTML = '';
      h.style.display = 'none';
      return;
   }

   // With data lines returned (HTML links with <br>\n between them)
   // filter by our words (which might be longer than when the data
   // was generated) for display.


   var shown = '';
   var lines = data.split('\n');
   sh_lines = lines.length;
   var debug = '';
   for(i=0; i<lines.length; i++) { 
      debug += i+')'+lines[i]+'<br>\n';

      var fields = lines[i].split('|');

      var field_type   = fields[0];
      var field_id     = fields[1];
      var field_name   = fields[2];
      var field_search = fields[3];

      var found=0;
      for (j=0; j<words.length; j++) { 
         //Compare a cleaned up word, since our search field is also cleaned up
         var thisword = words[j].toLowerCase();
         //if (field_search.toLowerCase().indexOf(words[j].toLowerCase()) >= 0) { 
         if (field_search.toLowerCase().indexOf(thisword) >= 0) { 
            debug += '...found '+words[j]+'('+thisword+')<br>\n';
            found++; 
         } else { 
            debug += '...not found '+words[j]+'('+thisword+')<br>\n';
         }
      }
      debug += 'found#='+found+', words.length='+words.length+'<br>\n';
      if (found == words.length) { 
         //shown += lines[i] + '\n';

         var url;
         if (field_type == 'author') {
            url = "authoridsearch.cgi";
         } else {
            url = "titleidsearch.cgi";
         }
         var option = "<a href='/"+url+"?id="+field_id+"'>"+field_name+"</a><br>\n";
         shown += option;

      }
   }

   h.innerHTML = '<b>Are you looking for...</b><br>\n' + shown;
   h.style.display='block';

   //h.innerHTML = sh_words + '<br>\n' + debug + '<br>\n' + shown;
      
   return 1;
}

function clear_searchhelp() { 
   var h = document.getElementById('search_hint');
   h.style.display='none';
}

