//==============================================================
// Fonction doAsepTime est basee sur doTime mais modifiee pour ne recuperer 
//          que les champs utiles soit : AAAA MM JJ HH UTC.
// Fonction doAsepTime retourne dans :
//          input name="HreAsep value="YYYY MM DD HH" lorsque vide (anglais)
//          input name="HreAsep value="AAAA MM JJ HH" lorsque vide (francais)
//==============================================================

function validateASEP_PIN(langue)
   {
   var pin = document.PINAccess.ASEPPIN.value;
   var len = pin.length;

   if (len != 11) 
      {
//      alert( "len != 11" );
      return false;
      }

   x = pin.substring( 0, 4 );
// fonction VerifierCaracteresNumeriques est dans ValiderId.js
   if (!verifierCaracteresNumeriques(x)) 
      {
      return false;
      }
   x = pin.substring( 4, 5 );
   if (x != '-') 
      {
      return false;
      }
   x = pin.substring( 5, 11 );
   if (!verifierCaracteresNumeriques(x)) 
      {
      return false;
      }
   return true;
   }

function recallASEP_PIN( langue )
   {
   var valid = '';

   valid = validateASEP_PIN( langue );

   if ( valid )
      document.PINAccess.submit();
   else
      {
      var pin = document.PINAccess.ASEPPIN.value;
      if (langue == 'francais')
         alert( "Le format du NIP est incorrect. \n Le NIP entré est: \"" + pin + "\". \n\nLe NIP doit être de 11 caractères et avoir ce format : XXXX-XXXXXX soit 4 chiffres suivis d'un trait d'union et de 6 autres chiffres." );
      else
         alert( "The format of the PIN is invalid. \n The PIN entered is: \"" + pin + "\". \n\nPIN should be made of 11 characters with this format: XXXX-XXXXXX with 4 digits followed by an hyphen and 6 other digits." );
      return true;
      }
   }


function setDeltaTemps() 
   {
   document.Produit.DeltaTemps.value = document.Produit.hreAsep.selectedIndex;
   }

function doAsepTimeList() 
   {
//..............................................
// strUTCTime sera retourne a l 'interface (html)
	strUTCTime = "UTC Time: ";
//...............................................

 	local = new Date();
 	tzOffset = local.getTimezoneOffset()/60;
   var objSelect = document.Produit.hreAsep;

   if ((ASEP_FIRST != "_ASEPFIRST_")&&(ASEP_FIRST != ""))
      {
      tm = new Date ( ASEP_FIRST );
      }
   else
      {
      tm = new Date ( local.getTime() + (tzOffset/1) * 3600 * 1000 );
      }

   var action = ASEP_ACTION.substr( 0, 7 );

   if ((action != "Edit-RB")&&(ASEP_NBHOUR != "_ASEPNBHOUR_"))
      nbhre = ASEP_NBHOUR;
   else
      nbhre = 48

   objSelect.length = nbhre;
//   alert( "test1:ASEP_NBHOUR=" + ASEP_NBHOUR +"/"+ nbhre );   
   if ( document.CreePage.Langue.value == "anglais" )
      objSelect.options[0] = new Option( "--Select--" );
   else
      objSelect.options[0] = new Option( "--Sélectionnez--" );

   objSelect.selectedIndex = 0;
   for ( i = 1; i <= nbhre ; i++ ) 
      {
      year = tm.getYear()
	   if (year < 1900) year = 1900 + year;
	   strUTCTime = format(year) + "-" + format(tm.getMonth() + 1) + "-" + format(tm.getDate()) + " " + format(tm.getHours());
      objSelect.options[i] = new Option( strUTCTime );
      if (strUTCTime == ASEP_DT) objSelect.selectedIndex = i;
      tm = new Date ( tm.getTime() + 3600 * 1000 );
      }
//   alert( "test _TMDELTA_ :" + ASEP_TMDELTA +"/" + objSelect.selectedIndex );
   if (ASEP_TMDELTA != "_TMDELTA_" && objSelect.selectedIndex == 0)
      {
      objSelect.selectedIndex = ASEP_TMDELTA;
      }

   if (document.Produit.hreAsep_1.value) {
      objSelect.selectedIndex = document.Produit.hreAsep_1.value;
      ASEP_TMDELTA = document.Produit.hreAsep_1.value;
      setDeltaTemps();
   }

   setTimeout("doAsepTimeList()", 3600000);
   }

function doAsepOptionsValues() 
   {
//   alert( "aov: temp estime:" + ASEP_NBHOUR + "/" + document.Produit.tpsEstime_1.value
//    + "  Altitude: " + ASEP_FL + "/" + document.Produit.niveauVol_1.value
//    + "  Heure de départ:" + ASEP_DT + "/" + document.Produit.hreAsep_1.value );
   if ( document.CreePage.Langue.value == "anglais" )
      var texteInitial = "--Select--";
   else
      var texteInitial = "--Sélectionnez--";

// initialize and set current value for Estimate Time EnRoute
   nbhre = 12;
   var objSelect = document.Produit.tpsEstime;
   objSelect.length = nbhre;
   objSelect.options[0] = new Option( texteInitial );
   for ( i = 1; i <= nbhre ; i++ ) 
      {
      objSelect.options[i] = new Option( i );
      }
   if ( document.Produit.tpsEstime_1.value ) objSelect.selectedIndex = document.Produit.tpsEstime_1.value;
   else objSelect.selectedIndex = ASEP_ETER;


// initialize and set current value for Flight Level
   nb = 40;
   objSelect = document.Produit.niveauVol
   objSelect.length = nb;
   var langue = document.CreePage.Langue.value;
   objSelect.options[0] = new Option( texteInitial );
   if ( langue == "anglais" )
      {
      for ( i = 1; i <= nb ; i++ )
         {
         if (i < 18) fl = i*1000 + " feet";
         else fl = "FL" + i + "0";
         objSelect.options[i] = new Option( fl );
         if (fl == ASEP_FL) objSelect.selectedIndex = i;
         }
      }
   else
      {
      for ( i = 1; i <= nb ; i++ )
         {
         if (i < 18) fl = i*1000 + " pieds";
         else fl = "FL" + i + "0";
         objSelect.options[i] = new Option( fl );
         if (fl == ASEP_FL) objSelect.selectedIndex = i;
         }
      }
      
   if ( document.Produit.niveauVol_1.value ) objSelect.selectedIndex = document.Produit.niveauVol_1.value;

   if (ASEP_VERSION != "_VERSION_")
      document.Produit.Version.value = ASEP_VERSION;
   if (ASEP_TMDELTA != "_TMDELTA_")
      document.Produit.DeltaTemps.value = ASEP_TMDELTA;

   if (document.Produit.DeltaTemps.value == "_TMDELTA_") 
      document.Produit.DeltaTemps.value = 1;
   if (document.Produit.Version.value == "_VERSION_") 
      document.Produit.DeltaTemps.value = "T";
   // Mettre les selection ASEP vides si provient de swrb*.cgi avec valeurs par defaut
   setDefaultEmptyASEP();
   } 

function SetMapDefault() 
   {
   if (have_java && use_map)
      {
      if (document.MapProduit.FilterStations[0].checked)
         document.ASEPMAP.showAllStations();
      else if (document.MapProduit.FilterStations[1].checked)
         document.ASEPMAP.showStnsWithTAF();
      else
         document.ASEPMAP.showStnsWithMETAR();
      }
   }

function setDefaultValuesASEP()
   {
   // Mettre des valeurs par defaut pour eviter que  des cgi ne plantent
   if (document.Produit.niveauVol.selectedIndex == 0)
      {
      document.Produit.niveauVol.selectedIndex=1;
      document.Produit.niveauVol_1.value=1;
      }
   if (document.Produit.hreAsep.selectedIndex == 0)
      {
      document.Produit.hreAsep.selectedIndex=1;
      document.Produit.hreAsep_1.value=1;
      setDeltaTemps();
      }
   if (document.Produit.tpsEstime.selectedIndex == 0)
      {
      document.Produit.tpsEstime.selectedIndex=1;
      document.Produit.tpsEstime_1.value=1;
      }
   }

function setDefaultEmptyASEP()
   {
   // Mettre les selection ASEP vides si valeurs initiales incompletes
   with ( document.Produit )
      {
      if ( hreAsep.selectedIndex == 1
        && tpsEstime.selectedIndex == 1
        && ! ( rwt_asepCat.checked || rwt_asepFQ.checked || rwt_asepHWind.checked
            || rwt_asepHtWind.checked || rwt_asepCld.checked || rwt_asepMslp.checked
            || rwt_asepIcg.checked || rwt_asepTT.checked )
         )
         {
         niveauVol.selectedIndex=0;
         niveauVol_1.value=0;
         hreAsep.selectedIndex=0;
         hreAsep_1.value=0;
         tpsEstime.selectedIndex=0;
         tpsEstime_1.value=0;
         }
      } 
   } 


function switchRouteBriefing()
   {
   setDefaultValuesASEP();
   if (document.Produit.Version.value == "T")
      {
      document.Produit.Version.value = "M";
      document.Produit.action = "/cgi-bin/swrbmap.cgi"
      }
   else
      {
      document.Produit.Version.value = "T";
      document.Produit.action = "/cgi-bin/swrbtext.cgi"
      }

   document.Produit.submit();
   }

function UpdateTextFromMap() 
   {
   if (have_java && use_map)
      {
      document.Produit.Depart.value = document.ASEPMAP.getDepart()
      document.Produit.Destination.value = document.ASEPMAP.getDestination()
      document.Produit.Alternates.value = document.ASEPMAP.getAlternates()
      document.Produit.EnRoute.value = document.ASEPMAP.getEnRoute()
      }
   }

function UpdateMapFromText() 
   {
   if (have_java && use_map)
      {
      route = document.Produit.Depart.value + " " + document.Produit.EnRoute.value + 
               " " + document.Produit.Destination.value ;
      document.ASEPMAP.setRoute( route )
      document.ASEPMAP.setAlternates( document.Produit.Alternates.value )
      }
   }

function SelectAllASEPProducts() 
   {
   document.Produit.rwt_asepCat.checked = true;
   document.Produit.rwt_asepFQ.checked = true;
   document.Produit.rwt_asepHWind.checked  = true;
   document.Produit.rwt_asepHtWind.checked  = true;
   document.Produit.rwt_asepCld.checked  = true;
   document.Produit.rwt_asepMslp.checked  = true;
   document.Produit.rwt_asepIcg.checked  = true;
   document.Produit.rwt_asepTT.checked  = true;
   document.Produit.rwt_asepTT.checked  = true;
   }

function ClearAllASEPProducts() 
   {
   /* Si vous voulez aussi vider les choix de sélection, dé-commentez ce qui suit:
      document.Produit.niveauVol.selectedIndex=0;
      document.Produit.niveauVol_1.value=0;
      document.Produit.hreAsep.selectedIndex=0;
      document.Produit.hreAsep_1.value=0;
      document.Produit.tpsEstime.selectedIndex=0;
      document.Produit.tpsEstime_1.value=0;
   */
   document.Produit.rwt_asepCat.checked = false;
   document.Produit.rwt_asepFQ.checked = false;
   document.Produit.rwt_asepHWind.checked  = false;
   document.Produit.rwt_asepHtWind.checked  = false;
   document.Produit.rwt_asepCld.checked  = false;
   document.Produit.rwt_asepMslp.checked  = false;
   document.Produit.rwt_asepIcg.checked  = false;
   document.Produit.rwt_asepTT.checked  = false;
   document.Produit.rwt_asepTT.checked  = false;
   }

if (ASEP_DEPART == "_DEPART_") ASEP_DEPART = "";
if (ASEP_ENROUTE == "_ENROUTE_") ASEP_ENROUTE = "";
if (ASEP_DESTINATION == "_DESTINATION_") ASEP_DESTINATION = "";
if (ASEP_ALTERNATES == "_ALTERNATES_") ASEP_ALTERNATES= "";
if (ASEP_VERSION == "_VERSION_") ASEP_VERSION = "T";
if (ASEP_DT == "_TMDEPART_") ASEP_DT = "";
if (ASEP_ETER == "_ESTTMENR_") ASEP_ETER = "0";
if (ASEP_FL == "_FLEVEL_") ASEP_FL = "FL050";

if (navigator.appName == 'Microsoft Internet Explorer' &&
      navigator.userAgent.indexOf('MSIE') &&
      (parseInt(navigator.appVersion)==3))
   have_java = false;
else
   have_java = navigator.javaEnabled();

if (ASEP_VERSION == "M")
   use_map = true;
else
   use_map = false;

