// *************************************************************************************
// Define global variables
// *************************************************************************************
var SEARCHANY     = 1;
var SEARCHALL     = 2;
var SEARCHURL     = 4;
var searchType  = '';
var showMatches   = 10;
var currentMatch  = 0;
var copyArray   = new Array();
var p = 'height=300,width=500,scrollbars=yes,resizable=yes';
var docObj      =  'dummy';

// *************************************************************************************
// Determine the type of search, and make
// sure the user has entered something
// *************************************************************************************
function validate(entry) {
  if (entry.charAt(0) == "+") {
    entry = entry.substring(1,entry.length);
    searchType = SEARCHALL;
    }
  else if (entry.substring(0,4) == "url:") {
    entry = entry.substring(5,entry.length);
    searchType = SEARCHURL;
    }
  else { searchType = SEARCHANY; }
  while (entry.charAt(0) == ' ') {
    entry = entry.substring(1,entry.length);
    document.forms[0].query.value = entry;
    }
  while (entry.charAt(entry.length - 1) == ' ') {
    entry = entry.substring(0,entry.length - 1);
    document.forms[0].query.value = entry;
    }
  if (entry.length < 3) {
    alert("Eine Suche solch kleiner Ausdrücke ist nicht möglich. Starten Sie einen neuen Versuch.");
    document.forms[0].query.focus();
    return;
    }
  convertString(entry);
  }

// *************************************************************************************
// Put the search terms in an array and
// and call appropriate search algorithm
// *************************************************************************************
function convertString(reentry) {
  var searchArray = reentry.split(" ");
  if (searchType == SEARCHALL) { requireAll(searchArray); }
  else { allowAny(searchArray); }
  }
// *************************************************************************************
// Define a function to perform a search that requires
// a match of any of the terms the user provided
// *************************************************************************************
function allowAny(t) {
  var findings = new Array(0);
  for (i = 0; i < profiles.length; i++) {
    var compareElement  = profiles[i].toUpperCase();
    if(searchType == SEARCHANY) { var refineElement  = compareElement.substring(0,compareElement.indexOf('| ')); }
    else { var refineElement = compareElement.substring(compareElement.indexOf('|'), compareElement.length); }
    for (j = 0; j < t.length; j++) {
      var compareString = t[j].toUpperCase();
      if (refineElement.indexOf(compareString) != -1) {
        findings[findings.length] = profiles[i];
        break;
        }
      }
    }
  verifyManage(findings);
  }


// *************************************************************************************
// Define a function to perform a search that requires
// a match of all terms the user provided
// *************************************************************************************
function requireAll(t) {
  var findings = new Array();
  for (i = 0; i < profiles.length; i++) {
    var allConfirmation = true;
    var allString       = profiles[i].toUpperCase();
    var refineAllString = allString.substring(0,allString.indexOf('| '));
    for (j = 0; j < t.length; j++) {
      var allElement = t[j].toUpperCase();
      if (refineAllString.indexOf(allElement) == -1) {
        allConfirmation = false;
        continue;
        }
      }
    if (allConfirmation) {
      findings[findings.length] = profiles[i];
      }
    }
  verifyManage(findings);
  }

// *************************************************************************************
// Determine whether the search was successful
// If so print the results; if not, indicate that, too
// *************************************************************************************
function verifyManage(resultSet) {
  if (resultSet.length == 0) { noMatch(); }
  else {
    copyArray = resultSet.sort();
    formatResults(copyArray, currentMatch, showMatches);
    }
  }

// *************************************************************************************
// Define a function that indicates that the search returned no results
// *************************************************************************************
function noMatch() {
  docObj=window.open('','',p);
  docObj.focus();
  docObj.document.writeln('<HTML>\n<HEAD>\n<TITLE>Orgellexikon: Suchergebnisse</TITLE>\n</HEAD>' +
    '<BODY BGCOLOR=WHITE TEXT=BLACK>' +
    '<TABLE WIDTH=90% BORDER=0 ALIGN=CENTER><TR><TD VALIGN=TOP><FONT FACE=Arial><B><DL>' +
    '<HR NOSHADE WIDTH=100%>"' + document.forms[0].query.value +
    '" ergab keine Ergebnisse.<HR NOSHADE WIDTH=100%></TD></TR></TABLE></BODY></HTML>');
    docObj.document.close(); 
    document.forms[0].query.select();
  }

// *************************************************************************************
// Define a function to print the results of a successful search
// *************************************************************************************
function formatResults(results, reference, offset) {
  var currentRecord = (results.length < reference + offset ? results.length : reference + offset);
  docObj=window.open('','',p);
  docObj.focus();
  docObj.document.writeln('<HTML>\n<HEAD>\n<TITLE>Orgellexikon: Suchergebnisse</TITLE>\n</HEAD>' +
    '<BODY BGCOLOR=WHITE TEXT=BLACK>' +
    '<TABLE WIDTH=90% BORDER=0 ALIGN=CENTER CELLPADDING=3><TR><TD>' +
    '<HR NOSHADE WIDTH=100%></TD></TR><TR><TD VALIGN=TOP><FONT FACE=Arial><B>' +
    'Suche nach: <I>' + window.document.forms[0].query.value + '</I><BR>\n' +
    'Suchergebnisse: <I>' + (reference + 1) + ' - ' +
    currentRecord + ' von ' + results.length + '</I><BR><BR></FONT>' +
    '<FONT FACE=Arial SIZE=-1><B>' + '\n\n<!-- Begin result set //-->\n\n\t<DL>');
  if (searchType == SEARCHURL) {
    for (var i = reference; i < currentRecord; i++) {
      var divide = results[i].split("|");
      docObj.document.writeln('\t<DT>' + '<A HREF="' + divide[2] + '" target="_new">' + divide[2] + '</A>' +
        '\t<DD>' + '<I>' + divide[1] + '</I><P>\n\n');
      }
    }
  else {
    for (var i = reference; i < currentRecord; i++) {
      var divide = results[i].split('|');
      docObj.document.writeln('\n\n\t<DT>' + '<A HREF="' + divide[2] + '" target="_new">' + divide[0] + '</A>' +
        '\t<DD>' + '<I>' + divide[1] + '</I><P>');
      }
    }
  docObj.document.writeln('\n\t</DL>\n\n<!-- End result set //-->\n\n');
  prevNextResults(results.length, reference, offset);
  docObj.document.writeln('<HR NOSHADE WIDTH=100%>' +
    '</TD>\n</TR>\n</TABLE>\n</BODY>\n</HTML>');
  docObj.document.close(); 
  document.forms[0].query.select();
  }

// *************************************************************************************
// Define a function to dynamically display Prev and Next buttons
// *************************************************************************************
function prevNextResults(ceiling, reference, offset) {
  docObj.document.writeln('<CENTER><FORM>');
  if(reference > 0) {
    docObj.document.writeln('<INPUT TYPE=BUTTON VALUE="Letzte ' + offset + ' Ergebnisse" ' +
      'onClick="window.opener.formatResults(window.opener.copyArray, ' +
      (reference - offset) + ', ' + offset + ')">');
    }
  if(reference >= 0 && reference + offset < ceiling) {
    var trueTop = ((ceiling - (offset + reference) < offset) ? ceiling - (reference + offset) : offset);
    var nextButtonCaption = (trueTop > 1 ? 'N&auml;chste ' + trueTop + ' Ergebnisse' : 'N&auml;chstes Ergebnis');
    docObj.document.writeln('<INPUT TYPE=BUTTON VALUE="' + nextButtonCaption + '" ' +
      'onClick="window.opener.formatResults(window.opener.copyArray, ' +
      (reference + offset) + ', ' + offset + ')">');
    }
  docObj.document.writeln('</CENTER>');
  }
