// The root URL of the catalogue, no trailing slash
// i.e. voyager.aber.ac.uk
var voyURL = "ctwweb.wesleyan.edu:7002";
// The message to be displayed when checking
// for suggestions
var lookMsg = "Looking for alternate words...";
// The text preceeding a suggestion
var didMsg = "Did you mean";
// The message when no suggestions are found
var noSuggMsg = "";
// BEGIN - TCL - added extra message
var librarianMsg = 'Would you like <a href="http://library.trincoll.edu/research/reference.cfm" target="_blank">help from a librarian</a>?';
/******************************/
/*                            */
/* Do no edit below this line */
/*                            */
/******************************/

http_spell = createRequestObject_spell();
var noHitsMsg;

function createRequestObject_spell() {
     // This creates the object that we use to get
     // the item information
     var ro;
     var browser = navigator.appName;
     if(browser == "Microsoft Internet Explorer"){
          ro = new ActiveXObject("Microsoft.XMLHTTP");
     } else {
          ro = new XMLHttpRequest();
     }
     return ro;
}

function doSpellCheck() {
   // First determine if we're on a page that is displaying
   // no search results. We can tell by looking for an element
   // with an id of noHits
   var searchArg;
   if (document.getElementById("noHits")) {
      noHitsMsg=document.getElementById("noHits").innerHTML;
      document.getElementById("noHits").innerHTML=noHitsMsg+" "+lookMsg+'<img src="/images/loading.gif" alt="Loading icon" />';
      // Get the search terms used
      var schURL=location.href;
      schURL=schURL.replace(/\+/g," ");
      var findMe=/searchArg=(.+)&searchCode/;
      if (result = schURL.match(findMe)) {
         searchArg = result[1];
      }
      
      // Send the query to the Perl script
	// BEGIN - TCL - modify URL for local installation
      http_spell.open('GET', 'http://'+voyURL+'/getSpell.cgi?searchArg='+searchArg);
   
      // When the page is ready, do something with it
      http_spell.onreadystatechange = handleResponse3;
      http_spell.send(null);
   }
}

function handleResponse3() {
   // When the page is ready, fill the div with it
   // and show it
   if(http_spell.readyState == 4){
      var response = http_spell.responseText;
      if (response!="ERROR") {
         if (document.getElementById("noHits")) {
            // Get the URL of the current page
            var currURL=location.href;
            // Replace the old searchArg with our spell checked one
            var newURL=currURL.replace(/searchArg=.+?&/,"searchArg="+response+'&');
// BEGIN - TCL - modify to include librarian msg
            document.getElementById("noHits").innerHTML = noHitsMsg+' '+didMsg+' <a href="'+newURL+'" title="Search for '+response+'">'+response+'</a>?<br />'+librarianMsg;
         }
      } else {
         if (document.getElementById("noHits")) {
// BEGIN - TCL - modify to include librarian msg
            document.getElementById("noHits").innerHTML=noHitsMsg+' '+noSuggMsg+'<br />'+librarianMsg;
         }
      }
   }
}
