var contentElementIDs = new Array('headerContent', 'searchTop5', 'bodyContent');
var contentElementNoHitMarkerID = new Array('searchNoHits');
var lastIDIfFound = "searchTop5";


/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
    // calc searchTerm length
    var searchTermLen = searchTerm.length ;

    // the highlightStartTag and highlightEndTag parameters are optional
    if ((!highlightStartTag) || (!highlightEndTag))
    {
        highlightStartTag = "<span class=\"se_hilite\">" ;
        highlightEndTag = "</span>" ;
    }

    // find all occurences of the search term in the given text,
    // and add some "highlight" tags to them (we're not using a
    // regular expression search, because we want to filter out
    // matches that occur within HTML tags and script blocks, so
    // we have to do a little extra validation)
    var newText = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText = bodyText.toLowerCase();

    while (bodyText.length > 0)
    {
        i = lcBodyText.indexOf(lcSearchTerm, i+1);

        if (i < 0)
        {
            newText += bodyText;
            bodyText = "";
        }
        else
        {
            // skip anything inside an HTML tag
            if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i))
            {
                // skip anything inside a <script> block
                if (lcBodyText.lastIndexOf("/script>", i) >=
                        lcBodyText.lastIndexOf("<script", i))
                {
                    newText += bodyText.substring(0, i) + highlightStartTag +
                        this.encode_umlauts(bodyText.substr(i, searchTermLen)) + highlightEndTag;
                    bodyText = bodyText.substr(i + searchTermLen);
                    lcBodyText = bodyText.toLowerCase();
                    i = -1;
                }
            }
        }
    }

   // return the result
   return newText ;
}

/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, warnOnFailure, highlightStartTag,
                highlightEndTag)
{

   // if a no search hit marker exists, do not highlight anything
   if (contentElementNoHitMarkerID)
   {
       for (var k=0; k < contentElementNoHitMarkerID.length; k++)
       {
         if (document.getElementById(contentElementNoHitMarkerID[i])) return;
       }
   }

    var searchAndArray = new Array() ;
    var searchOrArray  = new Array() ;

    // validate search entry
    if (searchText == null || searchText == "")
    {
        searchText = window.location.search ;

        // get search entry
        searchText = this.get_search_entry(searchText) ;

        if (searchText != null && searchText != "")
        {
            // exact match
            var re = new RegExp("\%22(.*)\%22") ;
            var exact_match = searchText.match(re) ;

            if (exact_match != null && exact_match[1] != null)
            {
                exact_match = exact_match[1] ;

                // delete exact match string from search string
                searchText = searchText.replace(re, '') ;

                // replace '%20' chars with ' ' char
                re = new RegExp("\%20|\\+") ;
                re.global = true ;
                exact_match = exact_match.replace(re, ' ') ;
            }
            else { exact_match = "" ; }

            searchText     = this.strip_junk(searchText) ;
            searchAndArray = this.get_tokens(searchText, '+') ;
            searchOrArray  = this.get_tokens(searchText, '|') ;
        }
        else { return ; }
    }


    if (!document.body || typeof(document.body.innerHTML) == "undefined")
    {
        if (warnOnFailure)
        {
//            alert("Sorry, for some reason the text of this page is " +
//                "unavailable. Searching will not work.");
        }
        return false;
    }
    for (var k=0; k < contentElementIDs.length; k++)
    {
      var contentElem = document.getElementById(contentElementIDs[k]);

      if (contentElem && contentElem.innerHTML)
      {
        var bodyText = contentElem.innerHTML;


        // mark exact match
        if (exact_match != null && exact_match != "")
        {
            bodyText = this.doHighlight(bodyText, exact_match,
                        highlightStartTag, highlightEndTag);
        }

        // iterate through 'and' array
        for (var i = 0; i < searchAndArray.length; i++)
        {
            if (searchAndArray[i] && searchAndArray[i] != null && searchAndArray[i] != "")
            {
                bodyText = this.doHighlight(bodyText, searchAndArray[i],
                           highlightStartTag, highlightEndTag);
            }
        }

        // iterate through 'or' array
        for (var i = 0; i < searchOrArray.length; i++)
        {
            if (searchOrArray[i] && searchOrArray[i] != null && searchOrArray[i] != "")
            {
                bodyText = this.doHighlight(bodyText, searchOrArray[i],
                           highlightStartTag, highlightEndTag);
            }
        }

        contentElem.innerHTML = bodyText;

        if (lastIDIfFound && (lastIDIfFound == contentElementIDs[k])) break;
      }

    }
    return true;
}


/**
 * Deletes extra chars and formats a string in a valid search string.
 *
 * Abbr.: + - 'and', '-' - not, '|' - or
 *
 * Conversion rules:
 *  +- = -, -| = |, +| = |
 *
 * @param str - string to format
 */
function strip_junk(str)
{
    // validate param
    if (str && str != null && str != "")
    {
        str = unescape(str) ;

        // strip extra spaces
        var re = new RegExp("\\s+") ;
        re.global = true;
        str = str.replace(re, ' ') ;

        // strip quotes
//        var re = new RegExp("\"") ;
//        re.global = true;
//        str = str.replace(re, '') ;

        // delete repeated control chars
        re = new RegExp("\\++") ;   // delete repeated 'and' chars
        re.global = true;
        str = str.replace(re, '+') ;
        re = new RegExp(" --+") ;     // delete repeated 'not' chars
        re.global = true;
        str = str.replace(re, ' -') ;
        re = new RegExp("\\|+") ;   // delete repeated 'or' chars
        re.global = true;
        str = str.replace(re, '-') ;
        re = new RegExp("\\|+") ;   // delete repeated 'or' chars
        re.global = true;
        str = str.replace(re, '|') ;

        // replace spaces with '+' (i.e. 'and')
        re = new RegExp("\\s") ;
        re.global = true;
        str = str.replace(re, "+") ;

        // replace '+-" or '-+' through '-' (i.e. 'not')
        re = new RegExp("\\+-") ;
        re.global = true;
        str = str.replace(re, "!") ;
        re = new RegExp("-\\+") ;
        re.global = true;
        str = str.replace(re, "!") ;

        // replace '-|' or '|-' or '+|' or '|+' through '|' (i.e. 'or')
        re = new RegExp("-\\|") ;
        re.global = true;
        str = str.replace(re, "|") ;
        re = new RegExp("\\|-") ;
        re.global = true;
        str = str.replace(re, "|") ;
        re = new RegExp("\\+\\|") ;
        re.global = true;
        str = str.replace(re, "|") ;
        re = new RegExp("\\|\\+") ;
        re.global = true;
        str = str.replace(re, "|") ;

        // strip off non alpha chars
        re = new RegExp("[^a-zA-Z0-9-&Uuml;&Ouml;&Auml;&uuml;&ouml;&auml;&szlig;\\s\\+]") ;
        re.global = true ;

	// fucking javascript
        while (str.match(re))
        {
            str = str.replace(re, "") ;
        }


        // add '+' for easy parsing
        str = '+' + str ;
    }

    // well, I hope it is what u want :)
    return str ;
}


/**
 * Returns all 'and' tokens found in a provided string
 * e.g. 'Hello+Servus-Ciao|Welcome' the function will return the following
 * tokens: 'Hello' and 'Servus'
 *
 * @param str     - string to process
 * @param divider - string distinguishing divider
 */
function get_tokens(str, divider)
{
    // array with 'and' tokens
    var tokens = new Array() ;


    // validate param
    if (str != null || str != "")
    {
        // otherwise, it sucks
        str += "\n" ;

        var re = new RegExp("\\" + divider + "([a-zA-Z0-9-&Uuml;&Ouml;&Auml;&uuml;&ouml;&auml;&szlig;]*)") ;
        re.global = true;

        var i = 0 ;

        while (matched_str = str.match(re))
        {
            // delete token
            var reg = new RegExp("\\" + divider + "([a-zA-Z0-9-&Uuml;&Ouml;&Auml;&uuml;&ouml;&auml;&szlig;]*)") ;

            // delete token
            str = str.replace(reg, "") ;
            tokens[i++] = matched_str[1] ;

            // safety procedure in order to avoid eternal loop
            if (i > 10) { break ; }
        }
    }
    return tokens ;
}


/**
 * Parses URI and returns back only search entry
 */
function get_search_entry(search_entry, search_token)
{
    // return var = only search entry
    // e.g.: search_entry == '?q=Hello' returns only 'Hello'
    var result = "" ;


    // validate search_token
    if (search_token == null || search_token == '')
    {
        search_token = 'q' ;
    }

    // validate params
    if (search_entry != null || search_entry != "")
    {
        var re = new RegExp("^\\?") ;

        // validate search_entry
        if (search_entry.match(re))
        {
            // get rid off uri chars
            re = new RegExp("^.*(\\?|&)" + search_token + "=([^&]+)") ;
            result = search_entry.replace(re, '$2') ;

            // get rid off additional params
            re = new RegExp("&.*") ;
            re.global = true ;
            result = result.replace(re, '') ;

            // get normal space char
            re = new RegExp("\\+") ;
            re.global = true ;
            result = result.replace(re, " ") ;

            // oe
            re = new RegExp("%F6") ;
            re.global = true ;
            result = result.replace(re, "&ouml;") ;

            // OE
            re = new RegExp("%D6") ;
            re.global = true ;
            result = result.replace(re, "&Ouml;") ;

            // ae
            re = new RegExp("%E4") ;
            re.global = true ;
            result = result.replace(re, "&auml;") ;

            // AE
            re = new RegExp("%C4") ;
            re.global = true ;
            result = result.replace(re, "&Auml;") ;

            // ue
            re = new RegExp("%FC") ;
            re.global = true ;
            result = result.replace(re, "&uuml;") ;

            // UE
            re = new RegExp("%DC") ;
            re.global = true ;
            result = result.replace(re, "&Uuml;") ;

            // ss
            re = new RegExp("%DF") ;
            re.global = true ;
            result = result.replace(re, "&szlig;") ;
        }
    }

    return result ;
}

function doHighlightSearchTerms()
{
   highlightSearchTerms();
}

/**
 * Encode umlauts
 */
function encode_umlauts(str)
{
    if (str && str != "")
    {
        // oe
        var re = new RegExp("&ouml;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&ouml;") ;

        // OE
        re = new RegExp("&Ouml;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&Ouml;") ;

        // ae
        re = new RegExp("&auml;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&auml;") ;

        // AE
        re = new RegExp("&Auml;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&Auml;") ;

        // ue
        re = new RegExp("&uuml;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&uuml;") ;

        // UE
        re = new RegExp("&Uuml;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&Uuml;") ;

        // ss
        re = new RegExp("&szlig;") ;
        re.global = true ;
        re.multiline = true ;
        str = str.replace(re, "&szlig;") ;
    }

    return str ;
}

if (document.all)
{
    try { attachEvent("onload", doHighlightSearchTerms); }
    catch (e) { window.onload = doHighlightSearchTerms; }
}
else
    window.addEventListener("load", doHighlightSearchTerms, true);
