// ----------------------------------------------------------------------------
// 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(" nadine morano ump depute meurthe moselle toul toulois sarkozy nocolas nicolas sarko gouvernement assemblee nationale blog est republicain lorrain philippe morenvillier velaine-en-haye ",
" depute 5eme circonscription meurthe-et-moselle  ",
" depute 5eme circonscription meurthe-et-moselle l'equipe parlementaire ",
" eme circonscription  ",
"  ",
" nmtv  ",
" blog philippe morenvillier  ",
"  ",
"  ",
"  ",
"  ",
" revue presse  ",
" commentaires  ",
" questions ecrites mandat  ",
" propositions loi resolution  ",
" clef d'actu  ",
" questions d'actualite gouvernement interventions seance publique philippe morenvillier cours 13e legislature  ",
" deputee 5eme circonscription meurthe-et-moselle  ",
" liens favoris  ",
"  ",
" mars 2008 moment charge d'emotions apres annees passees conseil municipal depuis 1995 mandats devenir maire fut moi extremement fort cette election represente l'aboutissement d'un investissement personnel familial important mes collegues comprendront surtout confiance population d'une equipe m'est chere considere travail dirige totalement vers l'action locale nos concitoyens peut s'accomplir qu'avec l'addition competences l'investissement entiere qualites sont complementaires delegue beaucoup colistiers afin responsabiliser motiver prendre plaisir travailler efficacement ensemble nom notre liste velaine prefigurait deja commun deployons etre est ancrage local tres complementaire parlementaire tant qu'elu ans sens proche elus 181 communes j'appuye quotidien depute ",
" velaine-en-haye avril  ",
" amis natalie velaine-en-haye  ",
" toul 2008  ",
" toul juin  ",
" juin 2008  ",
" thorey-lyautey 74eme anniversaire mort mal lyautey juillet colonel pierre geoffroy conservateur chateau president l'association nationale marechal invitait ceux restent fideles memoire recueilllir comme chaque annee l'occasion l'anniversaire celui fut premier commissaire resident general maroc 1912 1925 monsieur abderrahman balagh vice-consul pres consulat royaume strasbourg honorait presence cette manifestation dans propos loue caractere perenne l'amitie franco-marocaine souligne l'importance l'oeuvre faveur aimait marocains aimaient philippe morenvillier depute 5eme circonscription meurthe-et-moselle lieutenant-colonel jacques dudreuilh delegue militaire departemental adjoint representants conseil mairie nancy gendarmerie parmi nombreuses autres personnalites etait egalement presents rappelons fete trone lieu  ",
" place concorde paris  ",
" jardins l'elysee canicule forte 4500 personnes invitees lieu 6000 2007 voilure ete reduite  ",
" moment historique juillet 2008  ",
"  ",
"  ",
" newsletter  ",
" inscription newsletter <html><font size= 5px <p> bien ete prise compte <br> merci font>< html> ");
urlsArray = new Array("../topic1/index.html",
"../mapage/index.html",
"../lequipeparlement/index.html",
"../lacirconscriptio/index.html",
"../laprocedurelegis/index.html",
"../nmtv/index.html",
"../monblog/index.html",
"../mapage4/index.html",
"../mapage5/index.html",
"../video/index.html",
"../mapage1/index.html",
"../revuedepresse/index.html",
"../temoignages/index.html",
"../questionsecrites/index.html",
"../propositionsdelo/index.html",
"../clefdactu/index.html",
"../questionsecrite1/index.html",
"../leroledudepute/index.html",
"../topic2/index.html",
"../albumphoto/index.html",
"../albumphoto3/index.html",
"../albumphoto4/index.html",
"../voeux2007/index.html",
"../albumphoto5/index.html",
"../albumphoto6/index.html",
"../albumphoto2/index.html",
"../albumphoto7/index.html",
"../defile14juillet0/index.html",
"../gardenpartyelyse/index.html",
"../albumphoto1/index.html",
"../albumphoto8/index.html",
"../albumphoto9/index.html",
"../contact/index.html",
"../formulaire/index.html");
titlesArray = new Array("Député de la 5ème Circonscription de Meurthe-et-Moselle",
"Député de la 5ème Circonscription de Meurthe-et-Moselle",
"Député de la 5ème Circonscription de Meurthe-et-Moselle",
"La 5 eme circonscription",
"Site de Philippe Morenvillier",
"NMTV",
"Blog de Philippe Morenvillier",
"Site de Philippe Morenvillier",
"Site de Philippe Morenvillier",
"Site de Philippe Morenvillier",
"Site de Philippe Morenvillier",
"Revue de presse",
"Commentaires",
"Questions écrites du Mandat",
"Propositions de loi et de résolution",
"Clef d'actu",
"Questions d'actualité au gouvernement",
"Députée de la 5ème Circonscription de Meurthe-et-Moselle",
"Liens favoris",
"Site de Philippe Morenvillier",
"15 mars 2008 Un moment chargé d'émotions ",
"Velaine-en-Haye 24 avril 08",
"« Les amis de Natalie » 23/05/08 à Velaine-en-Haye ",
"Toul 08/06/2008",
"Toul 18 juin 08",
"Juin 2008",
"Thorey-Lyautey. Le 74ème anniversaire de la mort du Mal Lyautey - 6 juillet 08",
"Place de la Concorde - PARIS",
"Jardins de l'Elysée",
"Moment historique le 21 juillet 2008",
"Site de Philippe Morenvillier",
"Site de Philippe Morenvillier",
"Newsletter",
"Inscription à la Newsletter");
descArray = new Array(" ",
"",
"    L'équipe Parlementaire",
"",
"",
"",
"%tw-search%",
"",
"",
"",
"",
"",
"",
"%tw-search%",
"%tw-search%",
"",
"Interventions en séance publique de Philippe Morenvillier  au cours de la 13e législature %tw-search% ",
"%tw-search%",
"",
"",
"Après 13 années passées au Conseil Municipal depuis 1995 (donc 2 mandats), devenir Maire fût pour moi un moment extrêmement fort. Cette élection repré...",
"",
"",
"",
"",
"",
"Le Colonel Pierre Geoffroy, Conservateur du Château de Thorey-Lyautey et Président de l'Association Nationale Maréchal Lyautey, invitait ceux qui rest...",
"",
"Une canicule forte, 4500 personnes invitées (Au lieu de 6000 en 2007, la voilure a été réduite !).",
"",
"",
"",
"",
"<html><font size=5px> <P> Votre inscription à la Newsletter a bien été prise en compte !!! <BR> MERCI.  </font></html>   ");
sublinksArray = new Array(34);
var linksCount = 34;


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>");
}

