/*

   -----------------------------------------------------------------------------------------------
   ** Original Copyright 
   -----------------------------------------------------------------------------------------------
   This Script was created by Satadip Dutta. 
   Email: sat_dutta@post1.com  / satadipd@inf.com 
   URL:http://dutta.home.ml.org
   Version 1.1-IExp 
   
   Please honor my hard work, if you use a variant of this in your page, 
   then please email me :) and keep these comments in the Script.

   This code is Copyright (c) 1997 Satadip Dutta all rights reserved.

   License is granted to user to reuse this code on their own Web site 
   if, and only if, this entire copyright notice is included. The Web Site
   containing this  script   must be a not-for-profit ( non-commercial ) web site. 
   Exclusive written permission must be obtained before using  this version of the search engine

   -----------------------------------------------------------------------------------------------
   ** Supplemental Copyright
   -----------------------------------------------------------------------------------------------
   Modifications to search logic, .js encapsulation, and cross-browser compatibility
   added by John Cokos, iWeb Inc.  May 2002.
      Copyright (x) 2002, iWeb Inc.   http://www.iwebtechnology.com / http://www.iwebtoolkit.com
   -----------------------------------------------------------------------------------------------
*/



    ttl = new Object();
    desc = new Object();
    keys = new Object();
    links= new Object();    
    matched= new Object();    
    kwords= new Object();
    found= new Object();
    temp=0;
    kwords[0]=0;
    found[0]=0;
    output = '';

    function search() {

        temp=0;
        kwords[0]=0;
        found[0]=0;
        output = '';
        do_search();
        show_output();


        return false;
    }


    function do_search(){

        var skeyword=document.searchengine.keywords.value.toLowerCase();
        var check=1;
        var pos=0;
        var i=0;
        var j=0;
        var itemp=0;
        var config='';

        while (true) {
            if (skeyword.indexOf("+") == -1 ) {
                kwords[check]=skeyword;
                break;
            }
            pos=skeyword.indexOf("+");
            if (skeyword !="+")	 {
                kwords[check]=skeyword.substring(0,pos);
                check++;
            }
            else {
                check--;
                break;
            }
            skeyword=skeyword.substring(pos+1, skeyword.length);	
            if (skeyword.length ==0) {
                check--;
                break;
            }

        }

        // the keywords have been put in keywords object.
        kwords[0]=check;

        // matching and storing the matches in matched
        for ( i=1; i<=kwords[0];i++) {
            for (j=1;j<=num_titles;j++) {
                if ( keys[j].toLowerCase().indexOf(kwords[i]) > -1 || ttl[j].toLowerCase().indexOf(kwords[i]) > -1 || desc[j].toLowerCase().indexOf(kwords[i]) > -1) {
                    matched[j]++;
                }
            }	
        }


        // putting all the indexes of the matched records  in found
        for (i=1;i<=num_titles;i++) {
            if (matched[i] > 0 ) {
                found[0]++;
                // increment the found 	
                found[found[0]]=i;
            }	
        }



        for (i=1;i<=found[0]-1;i++) {
            for(j=i+1;j<=found[0];j++) {
                if ( matched[found[i]]< matched[found[j]] ) {
                    temp= found[j];
                    found[j]=found[i];
                    found[i]=temp;
                }
            }
        }

        // end of sort

        // SEARCH HEADER //
        output = output + 'Search Results for: <B>';    
        for (i=1;  i<=kwords[0]; i++) {
            output = output +  kwords[i].bold() + "   ";
        }
        output = output + '</B>';

        // No Results //
        if (found[0]==0) {
            output = output + "<HR><BR><BR><b>0 Search results </b> <br>";
            output = output + "<br>No results could be found for your search query.</br>";
            output = output + "<br>No results could be found for your search query.</br>";
			output = output + "<br>Please try one of the following:<br/>";
			output = output + "<br>* Try different search terms.<br>";
			output = output + "    * Try a more general search, for example start with fewer search words and refine your search in a second step.<br>";
			output = output + "    * Try to do a search without quotations.<br>";
			output = output + "    * Check whether filters are set and undo the filtering if applicable.<br>";
     		output = output + "<br>* You can find more tips on improving your search results in our search help <br>";
     		output = output + "<br>* You can find more tips on improving your search results in our search help <br>";
        }

        // Results Found //
        else {
            
            output = output + "[" + found[0] + "  Matches  " . italics();
            output = output + "]<HR><BR><OL>";

            for (i=1; i<=found[0];i++) {
                output = output + "<LI>";
                itemp=found[i];
                output = output + ttl[itemp].bold() + "<br>";
                output = output + desc[itemp] + "<br>";
                output = output + links[itemp].link(links[itemp])+"<br>";
                temp= (matched[itemp]/kwords[0])*100
                output = output + "<i>" +temp+" %  </i><P>" ;
                matched[itemp]=0
            } 
            found[0]=0;
            output = output + "</OL>";
        }

    }

    function show_output() {

            var NS4 = (document.layers) ? 1 : 0;
            var IE = (document.all) ? 1 : 0;
            var DOM = 0; 
            var Opera = 0;

            if (parseInt(navigator.appVersion) >=5) {DOM=1};

            if ( (navigator.userAgent.indexOf("Opera 6")!=-1) || (navigator.userAgent.indexOf("Opera/6")!=-1) ) {
                 DOM = 0;
                 IE = 0;
                 Opera = 1;
            }

            target=('SEARCHOUT');
               
                if (DOM || IE) {
                    var out = document.getElementById(target);
                    out.innerHTML = output;
                }
                    
                else if (NS4 || Opera) {
                    config='height=400,width=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes'
                    win = window.open ("","outputwindow",config)
                    win.document.open();
                    win.document.write(output);
                    win.document.close();
                }
                    
                // && (version >=4)
                else {
                    alert("get a real browser");
                }


            }
