// ----------------------------------------------------------------------------
// Lauyan Software Javascript Search Engine 1.0
//
// This file is the JavaScript search engine for a website created by TOWeb
//
// Copyright (C) LAUYAN SOFTWARE
// web: http://www.lauyan.com
// ----------------------------------------------------------------------------

STR_SEARCH_QUERY_MSG = "Résultats de la recherche pour";
STR_NO_RESULT = "Aucun résultat trouvé";
STR_SEARCH_BUTTON = "Rechercher";
STR_SCORE = "Score";
STR_TERMS = "Termes";
STR_SEARCH_SIZE = "40";
MIN_WORD_LEN = 2;
DISPLAY_SRCH_INFO = 1;
USE_UTF8 = 0;
REMOVE_ACCENTS = 1;
BOLD_LINKS = 0;
ONLINE_WEBSITE = 1;

namesArray = new Array(" bienvenue sur site ets ribery est marque deposee l'inpi 3547612 2008 tous droit reproduction interdit ",
" magasin  ",
" vetements ribery toujours disponible marque pantalons homme classique polyester velour jusque taille chemises 10 ",
" indispensable cet ete polo 50polyester manches courtes bord cotes bas col dos legerement plus long devant fente cote avec tresse proprete empiecement interieur popeline pied patte boutonnage logo brode ribery sur poche marque deposee ",
" indispensable cet ete polo 50polyester manches courtes bord cotes bas col dos legerement plus long devant fente cote avec tresse proprete empiecement interieur popeline pied patte boutonnage logo brode ribery sur poche marque deposee ",
" indispensable cet ete polo 50polyester manches courtes bord cotes bas col dos legerement plus long devant fente cote avec tresse proprete empiecement interieur popeline pied patte boutonnage logo brode ribery sur poche marque deposee ",
" indispensable cet ete polo 50polyester manches courtes bord cotes bas col dos legerement plus long devant fente cote avec tresse proprete empiecement interieur popeline pied patte boutonnage logo brode ribery sur poche marque deposee ",
" indispensable cet ete polo 50polyester manches courtes bord cotes bas col dos legerement plus long devant fente cote avec tresse proprete empiecement interieur popeline pied patte boutonnage logo brode ribery sur poche marque deposee ",
" indispensable cet ete polo 50polyester manches courtes col bord cotes dos legerement plus long devant fente cote avec tresse proprete surpiqure double aiguilles base bas empiecement interieur logo brode ribery sur poitrine marque deposee ",
" indispensable cet ete polo 50polyester manches courtes col bord cotes dos legerement plus long devant fente cote avec tresse proprete surpiqure double aiguilles base bas empiecement interieur logo brode ribery sur poitrine marque deposee ",
" indispensable cet hiver sweat 50polyester manches longues bord cotes poignets base empiecement cote 2x2 devant dos col montant zippe ecusson brode ribery sur poitrine marque deposee ",
" destockage vetements issus catalogues vente correspondance nord emballes individuellement avec marque etiquette taille sur produit sachet sans defauts rouge retour repartition tailles optimale ente proportion plus importante dans grandes demande possibilite d'envisager creation d'une solderie creations reussies 2008 dont deux pleine crise gamme classique pas d'effet mode produits ayant subi tests qualite cahier charges fabrication chinoise visibles nos entrepots completer chaussures confort quantite limitee cours sont www daxon celaia edmee proposes 2007 ces prix representent seulement gros depart exemple pull revient etait sera facture 40€ ",
" saisissez texte paragraphe ici ",
" lot retirer sur place pas livraison 100 parkas 160 pulls gilets tee-shirts pantalons jupes chemisiers total 1000 pieces		 prix 500 ht ",
" lot retirer sur place pas livraison 100 vestes 160 pulls gilets tee-shirts pantalons jupes chemisiers total 1000 pieces prix 500 ht ",
" lot retirer sur place pas livraison 180 pulls 100 gilets tee-shirts	 pantalons jupes chemisiers total 1000 pieces prix 350 ht ",
" lot retirer sur place pas livraison parkas pulls gilets tee-shirts pantalons jupes chemisiers total 500 pieces prix 850 ht ",
" lot retirer sur place pas livraison vestes pulls gilets tee-shirts pantalons jupes chemisiers total 500 pieces prix 850 ht ",
" lot retirer sur place pas livraison pulls gilets tee-shirts pantalons jupes chemisiers total 500 pieces prix 750 ht ",
" formulaire contact ets ribery rue quesnel 80134 hangest santerre tel fax email ribery@etsribery com ",
" polo refence 50201 ",
" polo refence 50202 ",
" polo refence 50203 ",
" polo refence 50204 ",
" polo refence 50205 ",
" polo refence 50206 ",
" polo refence 50207 ",
" gilet refence 50100 ",
" produit vendu lot  ",
" velo ribery sport  ",
" velo ribery classic  ");
urlsArray = new Array("../topic/index.html",
"../groupederubrique/index.html",
"../topic2/index.html",
"../topic2/page1.html",
"../topic2/page2.html",
"../topic2/page3.html",
"../topic2/page4.html",
"../topic2/page5.html",
"../topic2/page6.html",
"../topic2/page7.html",
"../topic2/page8.html",
"../magasin1/index.html",
"../magasin1/page1.html",
"../magasin1/page2.html",
"../magasin1/page3.html",
"../magasin1/page4.html",
"../magasin1/page5.html",
"../magasin1/page6.html",
"../magasin1/page7.html",
"../topic4/index.html",
"../albumphoto1/index.html",
"../albumphoto2/index.html",
"../albumphoto3/index.html",
"../albumphoto4/index.html",
"../albumphoto5/index.html",
"../albumphoto6/index.html",
"../albumphoto7/index.html",
"../albumphoto8/index.html",
"../velo/index.html",
"../velosport/index.html",
"../veloriberyclassi/index.html");
titlesArray = new Array("Bienvenue sur le site des Ets Ribery ",
"Magasin",
"Vêtements Ribery",
"Polo Noir ref: 50201",
"Polo Bleue ref: 50202",
"Polo Gris ref: 50203",
"Polo Vert ref: 50204",
"Polo Rouge ref: 50205",
"Polo rayer vert, gris, bleue ref: 50206",
"Polo Rayer Bleue, marron, gris ref: 50207",
"Gilet ref: 50100",
"Déstockage",
"DESTOCKAGE",
"Lot 1",
"Lot 2",
"Lot 3",
"Lot 4",
"Lot 5",
"Lot 6",
"Formulaire de contact",
"Polo",
"Polo",
"Polo",
"Polo",
"Polo",
"Polo",
"Polo",
"Gilet",
"Produit vendu en Lot",
"Velo Ribery Sport",
"Velo Ribery Classic");
descArray = new Array("Ribery est une marque déposée à l'INPI (08/3547612) le 04/01/2008. Tous Droit de reproduction est interdit.",
"",
"Toujours Disponible La Marque RIBERY  Pantalons Homme Classique Polyester ou Velour   Jusque la Taille 70 Chemises Homme    De la Taille 4 à 10  ",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"Vêtements issus des catalogues de vente par correspondance du Nord  Emballés individuellement avec Marque , Etiquette de Taille sur le Produit et sur ...",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"Ets Ribery 6 Rue de Quesnel 80134 Hangest en Santerre  Tel:  06 76 42 37 06 Fax: 03 22 90 70 34  Email: ribery@etsribery.com ",
"Réfence : 50201",
"Réfence : 50202",
"Réfence : 50203",
"Réfence : 50204",
"Réfence : 50205",
"Réfence : 50206",
"Réfence : 50207",
"Réfence : 50100",
"",
"",
"");
sublinksArray = new Array(31);
var linksCount = 31;


ALLWORDSONLY = false;

// getParam returns the value of the specified GET parameter
function getParam(paramName)
{
    paramStr = document.location.search;
    if( paramStr == "" )
        return "";
    if( paramStr.charAt(0) == "?" )
        paramStr = paramStr.substr(1);
    arg = (paramStr.split("&"));
    for( i=0; i<arg.length; i++ ) 
    {
      arg_values = arg[i].split("=")
      if( unescape(arg_values[0]) == paramName ) 
      {
         if( USE_UTF8 == 1 && self.decodeURIComponent ) // check if decodeURIComponent() is defined
            ret = decodeURIComponent(arg_values[1]);
         else
            ret = unescape(arg_values[1]);  // IE 5.0 and older does not have decodeURI
         return ret;
      }
    }
    return "";
}

function replaceAll(str,from,to) 
{
    var idx = str.indexOf(from);
    while( idx > -1 ) {
        str = str.replace(from, to); 
        idx = str.indexOf(from);
    }
    return str;
}

// lowercase, remove quotes and accents
function formatChars(str) 
{
    str = str.toLowerCase();
    if( REMOVE_ACCENTS ) 
    {
      var a = "àáâãäåòóôõöèéêëçìíîïùúûüÿñ";
      var b = "aaaaaaoooooeeeeciiiiuuuuyn";
      for( i=0; i<a.length; i++ )
        str = replaceAll( str, a.charAt(i), b.charAt(i) );
    }
    str = replaceAll( str, "'", " " );
    return str;
}

function SortCompare(a, b)
{
  if( a[2] == b[2] )
  {  
    if (a[1] < b[1]) return 1;
    else if (a[1] > b[1]) return -1;
    else return 0;
  }
  else if (a[2] < b[2]) return 1;
    else return -1;
}

function jseSearch( internal )
{
  // get params
  var rootURL = '';
	var SelfURL = document.location.href;
  var paramIndex = SelfURL.indexOf("?");    
  if (paramIndex > -1)
     SelfURL = SelfURL.substr(0, paramIndex);
  paramIndex = SelfURL.indexOf("#");
  if (paramIndex > -1)
    SelfURL = SelfURL.substr(0, paramIndex);        
	if (ONLINE_WEBSITE) 
	{
		paramIndex = SelfURL.lastIndexOf('/');
		if (paramIndex > -1) 
		{
			rootURL = SelfURL.substr(0, paramIndex);
			paramIndex = rootURL.lastIndexOf('/');
			if (paramIndex > -1) {
				rootURL = SelfURL.substr(0, paramIndex+1);
			} else
			  rootURL = '';
		}
	}	
  SelfURL = SelfURL.replace(/\</g, "&lt;");
  SelfURL = SelfURL.replace(/\"/g, "&quot;");

  var query = getParam("jse_query");
  query = query.replace(/[\++]/g, " ");  // replace any '+' with spaces
  query = query.replace(/\</g, "&lt;");
  query = query.replace(/[\"+]/g, " ");

  // display the search form
  var lnktarget = "";
	if( internal == null || internal != 1 )
	{
    document.writeln("<form method=\"get\" action=\"" + SelfURL + "\">");
    document.writeln("<input type=\"text\" name=\"jse_query\" size=\"" + STR_SEARCH_SIZE + "\" value=\"" + query + "\" />");
    document.writeln("<input type=\"submit\" value=\"" + STR_SEARCH_BUTTON + "\" />");
    document.writeln("</form>");
  }	
	else
	{
	 lnktarget = "\" target=\"_blank";
  }
	query = formatChars(query);
  if( query.length == 0 ) return;
  
  var found=0, t=0, k=0, score=0, subscore=0, i=0;
  var keyword = "", tmp = "";
  var searchWords = new Array();

  // split search query by spaces
  searchWords = query.split(" ");
	if( internal == null || internal != 1 ) {
		document.writeln( "" + STR_SEARCH_QUERY_MSG + " : <strong>" +query+"</strong><br>");
  }
  // init result table information
  res_table = new Array(linksCount);
  for( t=0; t<linksCount; t++ )
  {
    res_table[t] = new Array(3);
    res_table[t][0] = 0;  // index
    res_table[t][1] = 0;  // score
    res_table[t][2] = 0;  // words found
    res_table[t][3] = ""; // sublinks (output string)
  }
  var significantwords = 0;
  
  // begin search ...
  for( i=0; i<searchWords.length; i++ )
  {
    keyword = searchWords[i];
    if( keyword.length > MIN_WORD_LEN ) // skip "small" words
	{
	  significantwords++;
	  for(var q=0; q<linksCount; q++ )
	  {
	    t = q;
	    score = 0;
	
	    // search for keywords (an exact word doubles the score)
		  tmp = formatChars(descArray[t]);
	    if( tmp.indexOf(keyword) != -1 ) score++;
	    if( tmp.indexOf(' '+keyword+' ') != -1 ) score+=2;
		  tmp = formatChars(titlesArray[t]);
	  	if( tmp.indexOf(keyword) != -1 ) score++;
		  if( tmp.indexOf(' '+keyword+' ') != -1 ) score+=2;
	    if( namesArray[t].indexOf(keyword) != -1 ) score++;
	    if( namesArray[t].indexOf(' '+keyword+' ') != -1 ) score+=2;
	
		// search for keywords in sublinks (i.e. bookmarks)
	    k = q+1;
		while( k <= linksCount )
		{
			tmp = descArray[k];
			if( tmp == null || tmp.charAt(0) != '&' ) break;
			subscore = 0;
			tmp = formatChars(descArray[k]);
			if( tmp.indexOf(keyword) != -1 ) subscore++;
			if( tmp.indexOf(' '+keyword+' ') != -1 ) subscore+=2;
			tmp = formatChars(titlesArray[k]);
			if( tmp.indexOf(keyword) != -1 ) subscore++;
			if( tmp.indexOf(' '+keyword+' ') != -1 ) subscore+=2;
			if( namesArray[k].indexOf(keyword) != -1 ) subscore++;
			if( namesArray[k].indexOf(' '+keyword+' ') != -1 ) subscore+=2;
			
			if( subscore > 0 )
			{
				if( res_table[t][3].indexOf( "\"" + urlsArray[k] + "\"" ) == -1 )
					res_table[t][3] += "<tr><td width='35'>&nbsp;</td><td><a href=\"" + urlsArray[k] + lnktarget + "\">" + titlesArray[k] + "</a></td></tr>";
//	           		if( B OLD_LINKS )  res_table[t][3] += "<tr><td width='50'>&nbsp</td><td><b><a href='" + urlsArray[k] +"'>" + titlesArray[k] + "</a></b></td></tr>";
				score += subscore;
			}	
			k++;
		}
		q = k-1;
		
	    if( score > 0 )
	    {
	      res_table[t][0] = t;
	      res_table[t][1] += score;
	      res_table[t][2]++;
	      found++;
	    }
	  }
	}
  }
  if( found == 0 ) 
  {
    document.write("<p><b>" + STR_NO_RESULT + "</b></p>");
  } 
  else 
  {
	 // sort by words found and scores
    res_table.sort(SortCompare);	
	
    // display the results
    for( q=0; q<found; q++ )
      if( res_table[q][1] > 0 ) // display only results with a positive score
	  if( !ALLWORDSONLY || (ALLWORDSONLY && res_table[q][2] == significantwords) )
	    {
        t = res_table[q][0];    // get page index
        if( BOLD_LINKS ) {
          document.writeln("<br><b>" + (q+1) + ". <a href=\"" + urlsArray[t] + lnktarget + "\">" + titlesArray[t] + "</a></b>" );
        } else
          document.writeln("<br><b>" + (q+1) + ".</b> <a href=\"" + urlsArray[t] + lnktarget + "\">" + titlesArray[t] + "</a>" );
        if( descArray[t].length > 2 )
          document.writeln("<br>" + descArray[t]);
        if( DISPLAY_SRCH_INFO ) 
		{
			document.write("<br><span style='font-size: 80%; font-style: italic;'>");
			if( !ALLWORDSONLY )
				document.write("" + STR_TERMS + ": " + res_table[q][2] + " - " );
			document.write("" + STR_SCORE + ": " + res_table[q][1] );
			if (ONLINE_WEBSITE) {
			  document.writeln(" - URL: " + rootURL + urlsArray[t].substr(3) + "</span>");
			}	else
			  document.writeln(" - URL: " + urlsArray[t] + "</span>");
      	}
				if( res_table[q][3].length > 0 ) { 
      	  document.writeln( '<table>' + res_table[q][3] + '</table>' );
      	} else document.writeln("<br>");
      }
  }
  document.writeln("<br>");
}
