var xmlDoc;
/*
 * load indexfile
 */
function loadIndex() {
  // most current browsers support document.implementation
  
  if ((typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined')) {
    xmlDoc = document.implementation.createDocument('', '', null);
    xmlDoc.load('index.xml');
  }
  // MSIE uses ActiveX
  else if (window.ActiveXObject) {
    xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
    xmlDoc.async = 'false';
    xmlDoc.load('index.xml');
  }
}
/*
 *
 */
function searchIndex() { // search the index (duh!)
  if (typeof xmlDoc == "undefined") {
    loadIndex();
  }
  
  // get the search term from a form field with id 'searchme'
  var SearchTerm = document.getElementById("searchme").value;
  var AllIndexItems = xmlDoc.getElementsByTagName("item");
  ResultItems = new Array;
  if (SearchTerm.length < 3) {
    var SearchResultArea = document.getElementById("sresult");
    SearchResultArea.innerHTML = 'Wyszukiwana fraza jest krótsza niż 3 znaki';
    }
  else {
    for (var i = 0; i < AllIndexItems.length; i++) {
      var IndexItemValue = AllIndexItems[i].lastChild.nodeValue;
      var SearchExp = new RegExp(SearchTerm, "i");
    if (IndexItemValue.match(SearchExp) != null) {
      ResultItems.push(AllIndexItems[i]);
    }
  }
  // send the results to another function that displays them to the user
  showResults(ResultItems, SearchTerm);
  }
}
/*
 * Write search results to a table
 */
function showResults(results, SearchTerm) {
  if (results.length > 0) {
    // if there are any results, write them to a table
    var sresult = document.getElementById("sresult");
    var HTMLResult = '';
    HTMLResult += 'Wynik wyszukiwania dla słowa: <b><i>' + SearchTerm + '</i></b><br><br>';
    HTMLResult += '<table border="0" style="width: 300px">';
    for (var i = 0; i < results.length; i++) {
      HTMLResult += '<tr>';
      HTMLResult += '<td>';
      HTMLResult += '<b>' + results[i].getAttribute("slowo") + '</b>';
      HTMLResult += '<td>';
      HTMLResult += '</tr>';
      HTMLResult += '<tr>';
      HTMLResult += '<td>' + results[i].getAttribute("opis") + '</td>';
      HTMLResult += '</tr>';
      HTMLResult += '<tr>';
      HTMLResult += '<td>';
      HTMLResult += '<img src="/images/layout/icon-link.gif">';
      HTMLResult += '<a href="' + results[i].getAttribute("link") + '"><b> więcej</b></a>';
      HTMLResult += '</td>';
      HTMLResult += '</tr>';
    }
    HTMLResult += '<table>';
    sresult.innerHTML = HTMLResult;
  }
  else {
    var sresult = document.getElementById("sresult");
    sresult.innerHTML = 'Nie znaleziono słowa: <b>' + SearchTerm + '</b>';
  }
}
/*
 *
 */
function validKeyPress(strRegExp) {
  var objRegExp = new RegExp(strRegExp);
  if (!objRegExp.test(String.fromCharCode(window.event.keyCode)))
    window.event.returnValue = 0;
}
/*
*
*/
function IsReturnKeyPressed() {
  if (window.event.keyCode == 13) {
    window.event.keyCode = 0;
    searchIndex();
  }
  else return false;
}
