var choix = "AERODROME";


// Fonction pour remplacer, dans string, tous les text par by. 
 function replace(string,text,by) {
     // Replaces text with by in string
     var strLength = string.length, txtLength = text.length;
     if ((strLength == 0) || (txtLength == 0)) return string;

     var i = string.indexOf(text);
     if ((!i) && (text != string.substring(0,txtLength))) return string;
     if (i == -1) return string;

     var newstr = string.substring(0,i) + by;

     if (i+txtLength < strLength)
         newstr += replace(string.substring(i+txtLength,strLength),text,by);

     return newstr;
 }



// Fonction pour remplacer les caracteres desires dans chaine.
 function remplacerCar( chaine )
 {
   chaine = replace( chaine, '&', '%26' );
   chaine = replace( chaine, '#', '%23' );

   return chaine;
 }


function rechercheCaracteres(x)
{
  var valid="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+()-,.&+`'/"

  for (var i=0; i< x.length; i++) 
  {
    if (valid.indexOf(x.charAt(i)) < 0) 
      return false;
  }

  return true;
}


function contientDesPlus(x)
{
  for( var i = 0; i < x.length; i++ )
  {
    if( x.charAt(i) == '+' )
      return true;
  }
  return false;
}


// Fonction pour remplacer tous les espaces par le caractere +. 
 function stripSpaces(x) 
 {
   // Remove spaces at the end:   
   var i = x.length - 1;
   var j = 0;
   var tab;
   for( ; i >= 0 && x.charAt(i) == ' '; i-- );

   // i now points to last non-blank character:
   x = x.substring( 0, i+1 );

   // Remove spaces at the beginning:
   while (x.substring(0,1) == ' ') 
     x = x.substring(1);

   // Replace spaces by '+':
   x = replace( x, ' ', '+' );

   return x;
 }



// Fonction pour ouvrir la page HTML.
// Methode utilisee: GET
// Contexte: AERODROME / WEATHER 
//
// info sur parametres entres 
//          chaine    - ce qui est entre par l'usager dans l'entry box
//          loc       - la location ou sera retournee la reponse par afficher.cgi. En fait
//                       c'est le nom de l'entry box ou sera retourne le ID trouve par
//                       le lookup.
//          le_choix  - WEATHER ou AERODROME (selon que nous voulons dans la liste
//                       les aeordrome ID pour les stations qui ont un WX ID ou alors
//                       la listes de tous les aerodrome ID.
//          la_langue - ANGLAIS / FRANCAIS ou anglais / francais (qui arrive de la page HTML)


 function launchLookup( chaine, loc, le_choix, la_langue )
 {
   choix = loc; 

   var langue = la_langue.toUpperCase();
// convertir en majuscule ce qui arrive dans la variable la_langue. A partir de 
// ce moment peut importe si on pass des majuscules ou minuscules, ca marchera.
   
   var empty_msg = "Please enter a search criteria.";
   var msg_vide  = "Veuillez entrer un critère de recherche.";

//   var char_msg = "You may enter only alphanumeric characters.";
//   var msg_car  = "You pouvez seulement entrer des caractères alphanumériques.";

   var char_msg = "Only alphanumeric characters allowed.";
   var msg_car  = "Seuls les caractères alphanumériques sont permis.";

 

   if( contientDesPlus( chaine ) )
   {
     if( langue == 'ANGLAIS' )
        alert( char_msg );
     else
        alert( msg_car );
     return;
   }


// remplacerCar va aller remplacer (voir **) les espaces blancs par des +
// pour que le CGI convertisse les + en espace. CGI et les blancs ne font pas bon menage ! 
// ** une fois que la chaine de caractere entree par l'usager a ete validee.

   var valeur = stripSpaces( chaine );


   if( !rechercheCaracteres( valeur ) )
   {
     if( langue == "ANGLAIS" )
       alert( char_msg );
     else
       alert( msg_car );
     return;
   }


   valeur = remplacerCar( valeur );

   if( valeur == '' )
   {
     if( langue == "ANGLAIS" ) 
       alert(empty_msg);
     else
       alert(msg_vide);
     return;
   }


   if( le_choix == 'WEATHER' )
   {
     if( langue == 'ANGLAIS' )

       var vLoc = '/cgi-bin/afficher.cgi?location=' +
           valeur + '&choix=weather' + '&forme=Produit' + '&language=anglais'; 
		   
     else if ( langue == 'FRANCAIS' )

	   var vLoc = '/cgi-bin/afficher.cgi?location=' +
           valeur + '&choix=weather' + '&forme=Produit' + '&language=francais'; 
		   
	 else	   

	   var vLoc = '/cgi-bin/afficher.cgi?location=' +
           valeur + '&choix=weather' + '&forme=Produit' + '&language=langueInvalide'; 
   }

   else
   {
     if( langue == 'ANGLAIS' )

      var vLoc = '/cgi-bin/afficher.cgi?location=' +
           valeur + '&choix=aerodrome' + '&forme=Produit' + '&language=anglais'; 
	 
	 else if ( langue == 'FRANCAIS' )

	   var vLoc = '/cgi-bin/afficher.cgi?location=' +
           valeur + '&choix=aerodrome' + '&forme=Produit' + '&language=francais'; 
		   
	 else

	   var vLoc = '/cgi-bin/afficher.cgi?location=' +
           valeur + '&choix=weather' + '&forme=Produit' + '&language=langueInvalide'; 
	 	 
    }


   cgiWindow =
      open(vLoc,'window2','resizable=yes,scrollbars=yes,width=500,height=500');
   
   if(cgiWindow.opener == null) cgiWindow.opener = self;

 }


