/*
 * SYSTEM_NAME: Guentersberg
 *
 * COMPONENT_NAME: Shop
 *
 * FUNCTIONS: Catalogue search
 *
 * AUTHOR: HAN J. YU, LIPING DAI, Rainer Tessmann, Franz Fackelmann
 *
 * CREATED: 10.01.2010
 *
 */


var initTableDone = false;
var lang = getLang();                   // Sprache
var colDescLink = 1;                    // Tabellenspalte mit Artikel-Link (ab 0)
var colBuyLink = 5;                     // Tabellenspalte mit Kaufen-Link (ab 0)


//*****************************************************************************
// Initialisierung
//*****************************************************************************

function init()
{
  cleanLocation();

  window.name = "katalogwin";

  // Tabelle verzoegert initialisieren, um Fehlermeldung zu vermeiden
  window.setTimeout("initTable('katalog')", 1500);
}


function getLang()
{
  url = window.document.URL;
  return (url != null && url.indexOf("-en.")) >= 0 ? "en" : "de";
}


//*****************************************************************************
// Suchfunktion
//*****************************************************************************

// Suchergebnisseite und deren Anzeigeeigenschaften
var ergWindow;
var ergWindowProps  = 'width=810,height=400,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes';

// HTML-Schnipsel fuer Suchergebnisseite
// (Aufbau abstimmen mit $shopgt['de']['shopItem'] in shopgt.php)
var htmlErgDocStart   = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' +
                        '<HTML><HEAD>' +
                        '<TITLE>Edition Güntersberg</TITLE>' +
                        '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">' +
                        '<LINK REL="stylesheet" TYPE="text/css" HREF="../guentersberg2.css">' +
                        '<BASE HREF="' + window.document.URL + '">' +
                        '<BASE TARGET="katalogwin">' +
                        '</HEAD>' +
                        '<BODY BGCOLOR="#FFFFFF">';

var htmlErgDocEnd     = '</BODY></HTML>';

var htmlErgTblStartDe = '<TABLE BGCOLOR="#294273" WIDTH="770" CELLPADDING="10" FRAME="BOX" BORDER="0">' +
                        '<TR><TD VALIGN="BOTTOM" WIDTH="274" HEIGHT="95"><IMG SRC="../bilder/logolinks-leiste.gif" WIDTH="274" HEIGHT="76" BORDER="0"></TD>' +
                        '<TD ALIGN="CENTER" VALIGN="MIDDLE"><SPAN CLASS="leiste">Suchergebnis</SPAN></TD></TR>' +
                        '</TABLE>' +
                        '<TABLE BGCOLOR="#294273" WIDTH="770" CELLPADDING="2" FRAME="BOX" BORDER="0">' +
                        '<TR><TD BGCOLOR="#FFFFE8" ALIGN="CENTER">' +
                        '<TABLE ID="katalog" BGCOLOR="#FFFFE8" CELLPADDING="3" CELLSPACING="0">' +
                        '<TR ALIGN="LEFT" VALIGN="MIDDLE">' +
                        '<TH WIDTH="60">Ersch. Datum</TH>' +
                        '<TH WIDTH="460">Komponist, Titel</TH>' +
                        '<TH WIDTH="125">Besetzung</TH>' +
                        '<TH WIDTH="60">Bestell- num.</TH>' +
                        '<TH WIDTH="40">Preis EUR</TH>' +
                        '<TH WIDTH="20">&nbsp;</TH></TR>';

var htmlErgTblStartEn = '<TABLE BGCOLOR="#294273" WIDTH="770" CELLPADDING="10" FRAME="BOX" BORDER="0">' +
                        '<TR><TD VALIGN="BOTTOM" WIDTH="274" HEIGHT="95"><IMG SRC="../bilder/logolinks-leiste.gif" WIDTH="274" HEIGHT="76" BORDER="0"></TD>' +
                        '<TD ALIGN="CENTER" VALIGN="MIDDLE"><SPAN CLASS="leiste">Search Result</SPAN></TD></TR>' +
                        '</TABLE>' +
                        '<TABLE BGCOLOR="#294273" WIDTH="770" CELLPADDING="2" FRAME="BOX" BORDER="0">' +
                        '<TR><TD BGCOLOR="#FFFFE8" ALIGN="CENTER">' +
                        '<TABLE ID="katalog" BGCOLOR="#FFFFE8" CELLPADDING="3" CELLSPACING="0">' +
                        '<TR ALIGN="LEFT" VALIGN="MIDDLE">' +
                        '<TH WIDTH="60">publ. date</TH>' +
                        '<TH WIDTH="460">composer, title</TH>' +
                        '<TH WIDTH="125">instrumentation</TH>' +
                        '<TH WIDTH="60">order number</TH>' +
                        '<TH WIDTH="40">price EUR</TH>' +
                        '<TH WIDTH="20">&nbsp;</TH></TR>';

var htmlErgTblEnd     = '</TABLE></TD></TR></TABLE>';


function search()
{
    // Daten aus Tabelle besorgen
    if (!initTableDone) initTable("katalog");

    // bei fehlenden Daten abbrechen
    if (rowArray.length == 0) return;

    // bei leerem Suchbegriff abbrechen
    var suchstr = window.document.suchform.suchbegriff.value.toLowerCase();
    if (suchstr == "") return;

    // Array (Zähler) Fundstellen/Tabellenzeile
    var datenZeilen = nRow-1;
    var fundst = new Array(datenZeilen);
    for(i=0; i<datenZeilen; i++) fundst[i]=0;

    // OFFEN: Suchstring von umgebenden und mehrfachen Leerzeichen reinigen
    var suchBegr=suchstr.split(' ');
    var begriff = "";
    var wort = "";
    var gefunden;

    for (k=0; k<suchBegr.length; k++)
    {
        begriff = suchBegr[k];

        for(i=0; i<rowArray.length; i++)
        {
            var phrasen = rowArray[i].split(recDelimiter);

            gefunden = false;

            for (h=0; h<maxNCol; h++)
            {
                var worte=phrasen[h].split(' ');
                for(j=0; j<worte.length; j++)
                {
                    wort=worte[j].toLowerCase();
                    if((wort.lastIndexOf(begriff))>=0) gefunden = true;
                }
            }

            if (gefunden) fundst[i]++;
        }
    }

    // Suchergebnisse sammeln
    // (ALLE Suchbegriffe muessen in einer Tabellenzeile mindestens einmal
    // vorkommen, damit diese Zeile als Fundstelle zaehlt)
    var htmlErgRows = "";

    for (m=0; m<fundst.length; m++)
    {
        if (fundst[m]>=suchBegr.length)
        {
            var phrasen = rowArray[m].split(recDelimiter);

            // (Aufbau abstimmen mit $shopgt['de']['shopItem'] in shopgt.php)
            htmlErgRows += '<TR ALIGN="LEFT" VALIGN="TOP">' +
                           phrasen[maxNCol+2] +
                           '</TR>';
        }
    }
    if (htmlErgRows == "") return;

    // Testen: Ergebnisfenster offen?
    var reOpenErgSeite = false;
    if (typeof(ergWindow) == "object")
    {
        if (typeof(ergWindow.window) == "unknown")
            reOpenErgSeite = true;

        else if (typeof(ergWindow.window) == "undefined")
            reOpenErgSeite = true;

        else
        {
            if (ergWindow.window.closed)
                reOpenErgSeite = true;
            else
                ergWindow.document.open();
        }
    }
    else
        reOpenErgSeite = true;

    // neues Fenster fuer Suchergebnisse oeffnen, falls noetig
    if (reOpenErgSeite)
        ergWindow = window.open("", "suchErgebnis", ergWindowProps);

    // Dokument mit Suchergebnissen dynamisch aufbauen
    var htmlErgTblStart = (lang == "de" ? htmlErgTblStartDe : htmlErgTblStartEn);

    ergWindow.document.open();
    ergWindow.document.write(htmlErgDocStart +
                             htmlErgTblStart +
                             htmlErgRows +
                             htmlErgTblEnd +
                             htmlErgDocEnd);
    ergWindow.document.close();

    // Fokus auf Suchergebnisfenster setzen
    ergWindow.focus();

    // FIXME FireFox lädt die Styles im Ergebnisfenster nicht
    ergWindow.location.reload();
}


//*****************************************************************************
// Filename: sortTable.js
// Description: This javascript file can be applied to convert record tables
// in a HTML file to be client-side sortable by associating title columns with
// sort events.
//
// COPYRIGHT (C) 2001 HAN J. YU, LIPING DAI
// THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT
// UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE
// SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION)
// ANY LATER VERSION. THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE
// USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
// MERCHANTABILITY OF FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU GENERAL
// PUBLIC LICENSE FOR MORE DETAILS.
//
// YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE ALONG
// WITH THIS PROGRAM; IF NOT, WRITE TO:
//
// THE FREE SOFTWARE FOUNDATION, INC.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
//
// Bugs/Comments: han@velocityhsi.com
//
// Change History:
//
// 11-26-01:
//  o Made a few more settings configurable (i.e. data delimiter etc).
//  o Added a check for the browser type/version (>= IE 5.0 allowed).
// 11-27-01:
//  o Used document.getElementById method to retrieve object
//  o Now supports both IE 5.0 or greater and Netscape 6.0 or greater
// 11-28-01:
//  o Fixed the status display for Netscape.
//  o Fixed the cursor shape for Netscape. Used pointer instead of hand.
// 11-29-01:
//  o Fixed a cursor bug for IE 5.5
// 11-10-07:
// Script modified by Rainer Tessmann to meet the requirenemts of the
// guentersberg home page.
// 14-02-10: FF
//  o remove sort functions (sort done in PHP to keep sorting after PHP action)
//*****************************************************************************

//*****************************************************************************
// To enable search function, simply include this javascript source file and
// add an onLoad event to the <body> like below:
//
// <body onLoad='initTable("table1")' ...>
//
// Note that the table that is the source of the search must have an ID.
//*****************************************************************************

// Global variables
var rowArray = new Array();             // Data row array
var titleRowArray = new Array();        // Contains title texts
var titleRowCellArray = new Array();    // Dynamically constructed title cells
var nRow, actualNRow, maxNCol;          // Various table stats

// Configurable constants
var recDelimiter = '|';                 // Char used as a record separator


//*****************************************************************************
// Main function. This is to be associated with onLoad event in <BODY>.
//
// IMPORTANT: This is the only function that needs to be included in the pages
// to be sorted. The rest of the functions are simply called by this
// function.
//*****************************************************************************
function initTable(obj)
{
    var table;                              // Table object

    // Local variables
    var nChildNodes;
    var innerMostNode;

    // RT Beginn Variablen für Spalten mit Verweisen
    var htmlDescLink;
    var htmlDescLink;
    // RT Ende Variablen für Spalten mit Verweisen

    // RT Beginn Daten extrahieren nur einmal
    initTableDone = true;
    // RT Ende Daten extrahieren nur einmal

    // Initializing global table object variable
    if (obj.tagName == "TABLE")
    {
        // Assumes that the obj is THE OBJECT
        table = obj;
    }
    else
    {
        // Assumes that the obj is the id of the object
        table = document.getElementById(obj);
    }

    // Check whether it's an object
    if (table == null) return;

    // Check whether it's a table
    if (table.tagName != "TABLE") return;

    // Initializing the max col number with the size of last data row
    maxNCol = table.rows[table.rows.length-1].cells.length;

    // Initializing arrays
    rowArray = new Array();
    titleRowCellArray = new Array();
    titleRowArray = new Array();

    // Setting the number of rows
    nRow = table.rows.length;

    // Should have at least 1 row
    if (nRow < 1) return;

    // Initialization of local variables
    actualNRow = 0;         // Number of actual data rows

    // Loop through rows
    // skip first row (0), that is title row
    for (var i=1; i<nRow; i++)
    {
        // Setting up the data rows
        for (var j=0; j<table.rows[i].cells.length; j++)
        {
            var curString = "";

            // Can't have row span in record rows ...
            if (table.rows[i].cells[j].rowSpan > 1) return;
            nChildNodes =
                table.rows[i].
                cells[j].firstChild.childNodes.
                length;

            innerMostNode =
                table.rows[i].
                cells[j].firstChild;

            // RT Beginn Spalten mit Verweisen
            // (Aufbau abstimmen mit $shopgt['de']['shopItem'] in shopgt.php)
            if (j == colDescLink)
            {
              htmlDescLink = table.rows[i].cells[j].innerHTML;

              // aktuellen Knoten auf Verweistextknoten setzen
              innerMostNode = table.rows[i].cells[j].firstChild.nextSibling.firstChild;
              nChildNodes = 0;
            }
            if (j == colBuyLink)
            {
              htmlBuyLink = table.rows[i].cells[j].innerHTML;

              // aktuellen Knoten auf Verweistextknoten setzen
              innerMostNode = table.rows[i].cells[j].firstChild;
              nChildNodes = 0;
            }
            // RT Ende Spalten mit Verweisen

            while (nChildNodes != 0)
            {
                innerMostNode =
                    innerMostNode.
                    firstChild;
                nChildNodes =
                    innerMostNode.
                    childNodes.
                    length;
            }

            if (j == 0)
            {
                 rowArray[actualNRow] =
                     innerMostNode.data;
            }
            else
            {
                rowArray[actualNRow] += recDelimiter +
                    innerMostNode.data;
            }
        }

        // RT Beginn Verweise ans Ende der Zeile anhängen
        rowArray[actualNRow] += recDelimiter + htmlDescLink;
        rowArray[actualNRow] += recDelimiter + htmlBuyLink;
        rowArray[actualNRow] += recDelimiter + table.rows[i].innerHTML;
        // RT Ende  Verweise ans Ende der Zeile anhängen

        // Inconsistent col length for data rows
        if (table.rows[i].cells.length > maxNCol)
            return;
        actualNRow++;
    }

    // If the row number is < 1, no need to do anything ...
    if (actualNRow < 1) return;


    // Setting up the title cells
    // Using individual cell as an array element
    for (var j=0; j<table.rows[0].cells.length; j++)
    {
        titleRowCellArray[j] = table.rows[0].cells[j];
    }
}

