// ******************************************************
// ******************************************************
//
// OBJET        : VALIDATIONS (EN JAVASCRIPT) DES CHAMPS
//                POUR LES PAGES NOTAM-E.HTML ET NOTAM-F.HTML
//   
//                1) max de 6 ID (min de 1)
//                2) ID de taille 4 alphanumerique (non signe)
//
// ******************************************************
// ******************************************************


// ===================================================================
// ===================================================================
// VERIFICATION INITIAL des champs WITH_NOTAM / WITHOUT_NOTAM
// ===================================================================
// ===================================================================

function initialNotam( )
   {
   if ( document.Produit.Action && document.Produit.Action.value.substr(0,4)=="Edit")
      {
      // Activer with_notam si au moins un type de NOTAM est selectionne:
      if ( (document.Produit.ni_File_cor && document.Produit.ni_File_cor.checked)
        || (document.Produit.ni_File_wpt && document.Produit.ni_File_wpt.checked)
        || (document.Produit.ni_Aerodrome && document.Produit.ni_Aerodrome.checked)
        || (document.Produit.ni_File && document.Produit.ni_File.checked)
        || (document.Produit.ni_HQ && document.Produit.ni_HQ.checked)
        || (document.Produit.ni_FIR && document.Produit.ni_FIR.checked)
        || (document.Produit.ni_CZNB && document.Produit.ni_CZNB.checked) )
         {
            //document.Produit.with_notam.checked = true;
            //document.Produit.without_notam.checked = false;
            with(document.Produit.with_notam) { if (! checked) click(); }
         }
      else
         {
            //document.Produit.with_notam.checked = false;
            //document.Produit.without_notam.checked = true;
            with(document.Produit.without_notam) if (! checked) click();
         }
      }
   }

// ===================================================================
// ===================================================================
// VALIDER LA PAGE "NOTAM-E.HTML ou encore NOTAM-F.HTML"
// ===================================================================
// ===================================================================

function testerNotam( langue, NoSession)
   {
   var aero = '';

   // Au moins un type de NOTAM doit etre selectionne:
   if (    !document.Produit.ni_Aerodrome.checked
        && !document.Produit.ni_File.checked
        && !document.Produit.ni_HQ.checked
        && !document.Produit.ni_FIR.checked
        && !document.Produit.ni_CZNB.checked )
      {
      if ( langue == "francais" )
         {
         alert("Vous devez sélectionner au moins un type de NOTAM.");
         }
      else
         {
         alert("You must select at least one type of NOTAM.");
         }
      }
   else
      {
      aero = testerAeroID( langue );

      // pour aller mettre la valeur de document.CreePage.Langue.value et 
      // document.CreePage.NoSession.value et les mettre dans 
      // document.Produit.Langue.value et document.Produit.NoSession.value qui sont
      // dans la page notam-e/f html dans la forme Produit.
      if ( aero == true )
         {
         document.Produit.submit();
         }
      }
   }


// -------------------------------------------------------------------
// VERIFIER QUE LE TEXT FIELD "Stations" REMPLIT LES 
// CONDITIONS SUIVANTES:
//   --> MINIMUM: 1 ID
//   --> MAXIMUM: 6 ID
//   --> 1 ESPACE ENTRE CHAQUE ID
//   --> CARACTERES ALPHA-NUMERIQUES
//   --> TAILLE DES ID: 4
// -------------------------------------------------------------------

function testerAeroID(langue)
{
  var nouveau = '';
  var tab_contenu = new Array();


// LIRE LE(S) ID(S) DANS LA PAGE HTML
  var contenuField = document.Produit.Stations.value.toUpperCase();


// ENLEVER LES ESPACES AU DEBUT, A LA FIN ET A L'INTERIEUR
  contenuField = stripSpacesBeforeAfter( contenuField );

  var contenu_tmp  = enleverEspacesEnTrop( contenuField );

  for( var i = 0; i < contenu_tmp.length - 1; i++ )
    nouveau = nouveau + contenu_tmp.charAt(i);  

// ID OBLIGATOIRE
  if( contenu_tmp == "" )
  {
    if( langue == 'anglais' )
    {
      alert('Please enter an aerodrome ID.');
      return false;
    }
    else
    {
      alert('Veuillez entrer un identificateur d\'aérodrome.');      
      return false;
    }
  }
  

  var contenu_colle = "";


// ECRIRE LE(S) NOUVEAU(X) ID(S) DANS LE TEXT FIELD DE LA PAGE HTML
  document.Produit.Stations.value = nouveau;

  contenu_colle = stripSpacesInside( contenu_tmp );


// VERIFIER QUE LE ID EST COMPOSE DE CARACTERES ALPHA-NUMERIQUES SEULEMENT
  if( !verifierCaracteres( contenu_colle ) )
  {
    if( langue == 'anglais' )
    {
      alert('Only alphanumeric characters allowed.');
      return false;
    }
    else
    {
      alert('Seuls les caractères alphanumériques sont permis.');
      return false;
    }
  }

  
// VERIFIER QUE LE NOMBRE MAXIMAL DE ID EST 6.
  if( !verifier_nb_aerodromes( contenu_tmp, 6 ) )
  {
    if( langue == 'anglais' )
    {
      alert('The maximum number of aerodrome ID is 6.');
      return false;
    }
    else
    {
      alert('Le nombre maximum d\'identificateur d\'aérodromes est de 6.');  
      return false;
    }
  }


// VERIFIER LA TAILLE DU ID
  if( !verifierTailleID( contenu_tmp, false ) && contenu_tmp != '' )
  {
    if( langue == 'anglais' )
    {
      alert('Please use 4 alphanumeric characters to define the aerodrome ID .');
      return false;
    }
    else
    {
      alert('Veuillez utiliser 4 caractères alphanumériques pour définir l\'identificateur de l\'aérodrome.');
      return false;
    }
  }

  return true;

}


// -------------------------------------------------------------------
// ENLEVER LES ESPACES (AVANT ET APRES).
// -------------------------------------------------------------------

function stripSpacesBeforeAfter(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);

  return x;
}




// -------------------------------------------------------------------
// LAISSER UN SEUL ESPACE ENTRE LES MOTS.
// -------------------------------------------------------------------

function enleverEspacesEnTrop( contenuTextBox )
{
  var nouvelleChaine = ""; 
  tab = contenuTextBox.split(" ");

  for( var i = 0; i < tab.length; i++ )
  {
    if( tab[i].length > 0 )
      nouvelleChaine = nouvelleChaine + tab[i] + " ";
  } 
 
  return nouvelleChaine;
}

// -------------------------------------------------------------------
// ENLEVER LES ESPACES AU DEBUT ET A LA FIN
// LAISSER UN SEUL ESPACE ENTRE LES MOTS.
// -------------------------------------------------------------------

function stripSpacesInside(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;
}



// -------------------------------------------------------------------
// VERIFIER S'IL Y A SEULEMENT DES CARACTERES ALPHA-NUMERIQUES 
// -------------------------------------------------------------------

function verifierCaracteres(x)
{
  var valid="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

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

  return true;
}



// -------------------------------------------------------------------
// VERIFIER S'IL Y A, AU MAXIMUM, "NOMBRE" AERODROME_ID.
// NOTE: IL PEUT Y EN AVOIR AUCUN.
// -------------------------------------------------------------------

function verifier_nb_aerodromes(x, nombre)
{
  var quantite = 0;
  
  for( var i = 0; i < x.length; i++ )
  {
    if( x.charAt(i) == ' ' )
      quantite = quantite + 1;
  }
  
  if( quantite > nombre )
    return false;
  else
    return true;

}



// ------------------------------------------
// VERIFIER SI
//   --> CHAQUE ID EST DE LONGUEUR  4 (S'IL N'Y PAS DE COORDONNEES).
// ------------------------------------------

function verifierTailleID( lesID, possede_coordonnees )
{
  if( possede_coordonnees == true )
  {
    if( !contientEspaces( lesID ) )
    {
      var lng = lesID.length - 1;

//      if( lng != 3 && lng !=  4 && lng != 10 && lng != 11 )
//      if( lng != 3 && lng !=  4 )
      if( lng !=  4 )
       return false;
      else
       return true;
    }

    else
    {
     var tabID = new Array();
  
     tabID = lesID.split(' ');
     
     for( var i = 0; i < tabID.length -1; i++ )
     {
//       if( tabID[i].length != 3  && tabID[i].length != 4 &&
//           tabID[i].length != 10 && tabID[i].length != 11 )
//       if( tabID[i].length != 3  && tabID[i].length != 4 )
       if( tabID[i].length != 4 )

         return false;
     }
     return true;
    }
  }
  
  else
  {
    if( !contientEspaces( lesID ) )
    {
      var lng = lesID.length - 1;

//      if( lng != 3 && lng !=  4 )
      if( lng !=  4 )
       return false;
      else
       return true;
    }

    else
    {
     var tabID = new Array();
  
     tabID = lesID.split(' ');
     
     for( var i = 0; i < tabID.length -1; i++ )
     {
//       if( tabID[i].length < 3 || tabID[i].length > 4 )
       if( tabID[i].length !=  4 )
       return false;
     }
     return true;
    }
  }
}



// -------------------------------------------------------------------
// VERIFIER SI LE CONTENU RECU CONTIENT DES ESPACES
// -------------------------------------------------------------------

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


// Patch temporaire (urgence, 6 juillet 2006)
// On deselectionne les options corridor/waypoint si
// aucun type de NOTAM est choisi.
function notam_uncheck_option()
   {
   if (    !document.Produit.ni_Aerodrome.checked 
        && !document.Produit.ni_FIR.checked       
        && !document.Produit.ni_HQ.checked )
      {
      document.Produit.ni_TypeVol[0].checked = false;
      document.Produit.ni_TypeVol[1].checked = false;
      }
   }


// Patch temporaire (urgence, 6 juillet 2006)
// On ne permet pas a l'usager de selectionner corridor/waypoint
// AVANT qu'il ait choisi au moins un type de NOTAM.
// Raison: l'usager n'est plus capable de de-selectionner un radio
// une fois clique (a cause du cas de "edit" dans "My Wx Data"). 
function notam_check_type( langue )
   {
   var notam_msg_a = 'To obtain NOTAM, you must first select at least one NOTAM type.';
   var notam_msg_f = 'Pour obtenir des NOTAM, vous devez d\'abord sélectionner au moins un type de NOTAM.';
   
   if (    document.Produit.ni_TypeVol[0].checked 
        || document.Produit.ni_TypeVol[1].checked )
      {
      if (    !document.Produit.ni_Aerodrome.checked 
           && !document.Produit.ni_FIR.checked       
           && !document.Produit.ni_HQ.checked )
            {
            if ( langue == "francais" )
               {
               alert(notam_msg_f);
               document.Produit.ni_TypeVol[0].checked = false;
               document.Produit.ni_TypeVol[1].checked = false;
               }
            else
               {
               alert(notam_msg_a);
               document.Produit.ni_TypeVol[0].checked = false;
               document.Produit.ni_TypeVol[1].checked = false;
               }
            }
      }
   }
