// GenoPro 2007 - This file can be copied, modify and distributed as you wish, it's plain and simple Ajax!
// Ajax service to search the GenoPro web site
// Create by JcMorin April 2007

var divSectionAnswer = null; // updated section
var searchBoxReference = null; // reference to the textbox (event sender)
var timerID = null; // callback timer
var hashAnswer = null; // global cache for search answer
var requestUrl = null; // the url of the AJAX server


// add the trim function to all string
String.prototype.trim = function() {
  return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

// called from the textbox
function suggestQA(searchBox, url, idDivResult, event) {
  if (event.keyCode < 20) {
    // avoid flicker on control key such as alt, shift, ctrl
    return;
  }

  if (hashAnswer == null) {
    hashAnswer = new Object(); // hashtable to cache answers
    requestUrl = url;
    divSectionAnswer = document.getElementById(idDivResult);
  }
  searchBoxReference = searchBox;
  
  if (searchBoxReference.value.length < 2) {
    // hide the suggestion since the text typped is too short.
    divSectionAnswer.style.visibility = 'hidden';
    if(timerID != null) {
      clearTimeout(timerID);
    }
  } else {
    if(timerID != null) {
      // clear the previous timer to avoid multiple refresh/ficker!
      clearTimeout(timerID);
    }
    timerID  = setTimeout("performSuggestQA()", 500);
  }
  return true;
}

// perform the search for a question and answer
function performSuggestQA() {
  performSuggestQA2(searchBoxReference.value.trim(), divSectionAnswer);
} 

// perform the search for a question and answer
function performSuggestQA2(search, answer) {
  divSectionAnswer = answer;
  // check in the hashtable
  if (search.length <= 2) {
    divSectionAnswer.style.visibility = "hidden";
  } else {
    search = escape(search);

    if (hashAnswer[search] != null) {
      showResult(hashAnswer[search]);
    } else {
      ahah(requestUrl + "?q=" + search, search);
    }
  }
  timerID = null;
} 

// show the result
function showResult(results) {
  divSectionAnswer.innerHTML = results.replace('<a ', '<a target="blank" ');
  divSectionAnswer.style.visibility = "visible";
}

// AJAX request
function ahah(url,searchText) {
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = function() {ahahDone(searchText);};
    req.open("GET", url, true);
    req.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = function() {ahahDone(searchText);};
      req.open("GET", url, true);
      req.send();
    }
  }
} 

// AJAX request callback
function ahahDone(searchText) {
  // only if req is "loaded"
  if (req.readyState == 4) {
    // only if "OK"
    if (req.status == 200) {
      results = req.responseText;
      showResult(results);
      
      // add to the cash
      if (hashAnswer[searchText] == null) {
        hashAnswer[searchText] = results;  
      }
    } else {
      divSectionAnswer.innerHTML="Error: " + req.statusText;
    }
  }
} 

