// ----------------------------------------------------------------------------
// Lauyan Software Javascript Search Engine 1.1
//
// 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_SCORE = "Score";
STR_TERMS = "Termes";
STR_SEARCH_SIZE = "20";
MIN_WORD_LEN = 2;
DISPLAY_SRCH_INFO = 1;
USE_UTF8 = 0;
REMOVE_ACCENTS = 1;
BOLD_LINKS = 1;
ONLINE_WEBSITE = 1;
ALLWORDSONLY = 0;
namesArray = new Array("  ",
" titre groupe saisissez message d'introduction rubriques ",
"  ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
"  ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" ferte 2009  ",
" ferte c'est bond dans passe ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
" saisissez description image ici ",
"  ",
" saisissez description image ici ",
" habillez pc's ici bientot petite collections photos fonds d'ecrans n'y pas taille rayons demandez ",
" sur celle-ci petit filtre fait sous toshop image issue meeting ferte alais 2011  ",
" meme filtre meeting  ",
" inaugurer cette rubrique propose petit traitement hdr image faite lors meeting niergnies marge ntm 2011 cambrai  ",
" seconde bel oiseau  ",
" saisissez contenu panneau ici ",
" ici toutes infos techniques sur machines volantes non j'ai photographier ",
"",
" saisissez description article ici ",
" saisissez description article ici ",
"  ",
" liens  ",
" ici notre petit coin reserve ",
"");
urlsArray = new Array("../topic/index.html",
"../groupederubrique/index.html",
"../ferte2011/index.html",
"../ferte2011/page1.html",
"../ferte2011/page2.html",
"../ferte2011/page3.html",
"../ferte2011/page4.html",
"../ferte2011/page5.html",
"../ferte2011/page6.html",
"../ferte2011/page7.html",
"../ferte2011/page8.html",
"../ferte2011/page9.html",
"../ferte2011/page10.html",
"../ferte2011/page11.html",
"../ferte2011/page12.html",
"../ferte2011/page13.html",
"../ferte2011/page14.html",
"../ferte2011/page15.html",
"../ferte2011/page16.html",
"../ferte2011/page17.html",
"../ferte2011/page18.html",
"../ferte2011/page19.html",
"../ferte2011/page20.html",
"../ferte2010/index.html",
"../ferte2010/page1.html",
"../ferte2010/page2.html",
"../ferte2010/page3.html",
"../ferte2010/page4.html",
"../ferte2010/page5.html",
"../ferte2010/page6.html",
"../ferte2010/page7.html",
"../ferte2010/page8.html",
"../ferte2010/page9.html",
"../ferte2010/page10.html",
"../ferte2010/page11.html",
"../ferte2010/page12.html",
"../ferte2010/page13.html",
"../ferte2010/page14.html",
"../ferte2010/page15.html",
"../ferte2010/page16.html",
"../albumphoto/index.html",
"../albumphoto/page1.html",
"../albumphoto/page2.html",
"../albumphoto/page3.html",
"../albumphoto/page4.html",
"../albumphoto/page5.html",
"../albumphoto/page6.html",
"../albumphoto/page7.html",
"../albumphoto/page8.html",
"../albumphoto/page9.html",
"../albumphoto/page10.html",
"../albumphoto/page11.html",
"../albumphoto/page12.html",
"../albumphoto/page13.html",
"../albumphoto/page14.html",
"../albumphoto/page15.html",
"../albumphoto/page16.html",
"../albumphoto/page17.html",
"../albumphoto/page18.html",
"../albumphoto/page19.html",
"../albumphoto/page20.html",
"../albumphoto/page21.html",
"../albumphoto/page22.html",
"../albumphoto/page23.html",
"../albumphoto/page24.html",
"../albumphoto/page25.html",
"../albumphoto/page26.html",
"../albumphoto/page27.html",
"../albumphoto/page28.html",
"../ferle2008/index.html",
"../ferle2008/index.html",
"../habillezvospcs/index.html",
"../habillezvospcs/index.html",
"../habillezvospcs/index.html",
"../habillezvospcs/index.html",
"../habillezvospcs/index.html",
"../habillezvospcs/index.html",
"../wallpaper/index.html",
"../wallpaper/page1.html",
"../wallpaper/page2.html",
"../wallpaper/page3.html",
"../livredor/index.html",
"../topic1/index.html",
"../famille/index.html",
"../famille/index.html");
titlesArray = new Array("Chasseur de Pixels",
"Titre du groupe",
"Chasseur de Pixels",
"Stearman_A-75N1",
"PBY",
"PBY",
"PBY",
"PBY",
"PBY",
"PBY",
"P51",
"P40B",
"P40B",
"P40B",
"P40B",
"P40B",
"P40B",
"P40B",
"Bronco",
"Avenger",
"Avenger",
"Avenger",
"Avenger",
"Chasseur de Pixels",
"zero_004.jpg",
"zero_005.jpg",
"zero_001.jpg",
"zero_000.jpg",
"WWII_001.jpg",
"T6_001.jpg",
"Skyraider_001.jpg",
"Rafale_003.jpg",
"Rafale_002.jpg",
"Rafale_001.jpg",
"P40_002.jpg",
"Fockewulf_001.jpg",
"Extra_300_005.jpg",
"Extra_300_004.jpg",
"Extra_300_003.jpg",
"Extra_300_001.jpg",
"Ferté 2009",
"Ambiance",
"Corsair_007.jpg",
"Corsair_010.jpg",
"Corsair_011.jpg",
"Corsair_012.jpg",
"Corsair_060.jpg",
"Corsair_070.jpg",
"Breitling_010.jpg",
"Breitling_020.jpg",
"Breitling_030.jpg",
"Breitling_040.jpg",
"Breitling_050.jpg",
"Breitling_060.jpg",
"Breitling_070.jpg",
"EC145_010.jpg",
"HAWKEYE_010.jpg",
"HAWKEYE_020.jpg",
"HAWKEYE_030.jpg",
"RED_040.jpg",
"RED_050.jpg",
"RED_060.jpg",
"Skyraider_010.jpg",
"Skyraider_020.jpg",
"Spitfire_001.jpg",
"Spitfire_002.jpg",
"Spitfire_02.jpg",
"Spitfire_004.jpg",
"Spitfire_003.jpg",
"Chasseur de Pixels",
"B17_Pink Lady",
"Habillez vos Pc's",
"Consolidated PBY Catalina",
"Curtiss P40B",
"SeaFury",
"SeaFury",
"Titre du panneau",
"Chasseur de Pixels",
"EC145",
"Panavia Tornado",
"Hawker SeaFury",
"Chasseur de Pixels",
"Liens",
"Chasseur de Pixels",
"Fête de la galette 2011");
descArray = new Array("",
"Saisissez le message d'introduction de votre groupe de rubriques",
"",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"&",
"",
"&",
"Ici bientôt une petite collections de photos pour vos fonds d'écrans si il n'y a pas votre taille en rayons demandez.",
"&",
"&",
"&",
"&",
"&",
"Ici toutes les infos techniques sur des machines volantes ou non que j'ai pu photographier",
"&",
"&",
"&",
"",
"",
"Ici notre petit coin réservé",
"&");
sublinksArray = new Array(85);
var linksCount = 85;


function SubmitSearch(n)
{
	var p = document.getElementsByName("jse_query");
	var idx = n;  if( p < 0) return false;
	if( p && p[idx] && p[idx].value != "" ) {
	if( (p[idx].value).replace(/^\s+|\s+$/g, '') != "" ) {
		p = document.getElementsByName("jse_form");
		if( p && p[idx] )
			p[idx].submit();
		return true;
	  }
	}
	return false;
}
function switchSearchLang(lang,emptycart)
{
	var params = "";
	var currURL = document.location.href;
	var newURL = (currURL.lastIndexOf('_frame/search2') >= 0) ? "search2":"search";
	if( lang != "" )
		newURL += "-"+lang;
	newURL += ".html";
	var paramIndex = currURL.lastIndexOf('?');
	if( paramIndex > -1 )
		newURL += "?" + currURL.substr( paramIndex + 1 );
	if( emptycart && typeof(CleanShopcart) != "undefined" )
		CleanShopcart();
	document.location.href = newURL;
	return 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 getQueryParam()
{
  var query = getParam("jse_query");
  query = query.replace(/[\++]/g, " ");  // replace any '+' with spaces
  query = query.replace(/\</g, "&lt;");
  query = query.replace(/[\"+]/g, " ");
	return query;
}

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 = getQueryParam();

  // 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>");
  }
  */
  if( internal == 1 ) {
	 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( "<span class='wg-paragraph'>" + STR_SEARCH_QUERY_MSG + " : <strong>" +query+"</span></strong><br>");
  }
  // init result table information
  res_table = new Array(linksCount);
  for( t=0; t<linksCount; t++ )
  {
    res_table[t] = new Array(4);
    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 )
			{
				var idx = urlsArray[t].indexOf('@');
				if( idx > 0 ) 
				{
					var lnk = urlsArray[k].substr(0, idx)
					if( res_table[t][3].indexOf( lnk ) == -1 )
						res_table[t][3] += "<tr><td width='35'>&nbsp;</td><td><a href=\"" + urlsArray[k].substr(idx+1) + "\" name=\"" + lnk + "\">" + titlesArray[k] + "</a></td></tr>";
				} else 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 class='wg-paragraph'><b>" + STR_NO_RESULT + "</b></p>");
  } 
  else 
  {
/*	if( ALLWORDSONLY )
	{
		//  if all words only then we recount and filter amoung the results to get the total of found items
		found = 0;
		for( var z=0; z<itemsCount; z++ )
			if( res_table[z][1] > 0 ) // display only results with a positive score
			if( !ALLWORDSONLY || (ALLWORDSONLY && res_table[z][2] == significantwords) )
				found++;
	}
  */
	// sort by words found and scores
    res_table.sort(SortCompare);	
    // display the results
	document.writeln( '<div class="wg-paragraph">' );
    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

		var lnk = urlsArray[t] + lnktarget;
		var idx = urlsArray[t].indexOf('@');
		if( idx > 0 ) 
			lnk = urlsArray[t].substr(idx+1);
			
		if( BOLD_LINKS ) {
		  document.writeln("<br><b>" + (q+1) + ". <a href=\"" + lnk + "\">" + titlesArray[t] + "</a></b>" );
		} else
		  document.writeln("<br><b>" + (q+1) + ".</b> <a href=\"" + lnk + "\">" + 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( urlsArray[t].indexOf('@') > 0 ) {
			  document.writeln(" - URL: 'TOWeb demonstration'</span>");
			} else 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 class="wg-paragraph">' + res_table[q][3] + '</table>' );
      	} else document.writeln("<br>");
      }
 	document.writeln( '</div>' );
  }
  document.writeln("<br>");
}

