//--------------------------------------------------------------------------------
// Fonctions JavaScript de gestion des fenêtres de visualisation
//--------------------------------------------------------------------------------
var fenetre_lexique; // Fenêtre créée pour contenir le lexique
var fenetre_icpe; // Fenêtre créée pour contenir les informations sur les ICPE

//--------------------------------------------------------------------------------
// Fermeture d'une fenetre
//--------------------------------------------------------------------------------
function fermer_fenetre(fenetre)
 {
 if ((typeof(fenetre)!="undefined")&&(fenetre.closed==false)){fenetre.close();}
 }

//--------------------------------------------------------------------------------
// Ouverture de la fenêtre lexique (on commence par fermer la fenêtre)
//--------------------------------------------------------------------------------
function ouvrir_lexique(loc)
 {
 fermer_fenetre(fenetre_lexique);
 fenetre_lexique=window.open("lexique.htm","Lexique","width=800,height=600,scrollbars=yes,scrolling=yes");
 loc="lexique.htm#"+loc; // on va directement à loc
 fenetre_lexique.location.href=loc;
 }

function ouvrir_icpe(base, numero)
 {
 fermer_fenetre(fenetre_icpe);
 taille=window.height;
 alert(taille);
 loc="afficher_icpe.php?base="+base+"&numero="+numero;
 fenetre_icpe=window.open(loc,"ICPE","directories=no,status=no,toolbar=yes,resizable=yes,x=0,y=0,width=1000,height=600,scrollbars=yes,scrolling=yes");
 fenetre_icpe.focus();
 }


function test_date(form)
 {
 var check=0;
 if(form.elements[0].value+form.elements[1].value=='11')// teste la date du rapport
  {alert('Vous devez entrer une date de rédaction pour ce rapport au CDH');check=1;}
 if(form.elements[3].value+form.elements[4].value=='11')// teste la date du CDH
  {alert('Vous devez entrer la date du CDH');check=1;}
 if(check==0){form.submit();}
 }

function test_date2(form)// sans CDH
 {
 if(form.elements[0].value+form.elements[1].value=='11')// teste la date du rapport
  {alert("Vous devez entrer la date de rédaction du rapport de l'Inspection");return;}
 form.elements[3].value=0;
 form.elements[4].value=0;
 form.elements[5].value=0;
 form.submit();
 }

function test_date_AP(form)
 {
 var check=0;
 if(form.elements[0].value+form.elements[1].value=='11')// teste la date de l'AP
   {alert("Vous devez entrer une date pour le rapport de l'Inspection");check=1;}
 if(check==0){form.submit();}
 }

function test_date_visite(form)
 {
 var check=0;
 if(form.elements[0].value+form.elements[1].value=='11')// teste la date de la visite d'inspection
   {alert("Vous devez entrer une date pour la visite d'inspection");check=1;}
 if(check==0){form.submit();}
 }

function test_ap(form)
 {
 if(form.fich_ap.value=='')
  {alert('Vous devez choisir un nom de fichier');return;}
 if(form.fich_ap.value.indexOf('.pdf',0)==-1)
  {alert('Le fichier doit être au format PDF (extension *.pdf)');return;}

 form.submit();
 }

 
function Lamb_ED50(T_X_Lamb,T_Y_Lamb)
<!-- De Lambert II étendu (NTF) métrique - Clarke 1880 (1) -- VERS -- ED50 -->
  {
  <!-- Début des calculs sur une base de Lambert II ---- Voir si portable en Etendu -->
  <!-- Mise en Float les variables lues pour éviter les erreurs NaN (Not a Number) -->
  var Lamb_EE = parseFloat(T_X_Lamb);
  var Lamb_NN = parseFloat(T_Y_Lamb);
  <!-- Recherche de la zone Lambert du point - Inutilisé pour l'instant - Seul Lambert II-->
  var Lamb_Zone = Math.floor(Lamb_NN / 1000000);
  
  <!-- Constante pour la Zone II Lambert -->
  <!-- Calcul des constantes sur le goide Clarke 1880 pour Lambert II -->
  var Lamb_a = 6378249.2;
  var Lamb_f = 293.466021300;
  var Lamb_b = Lamb_a * (1 - 1 / Lamb_f);
  var Lamb_e = Math.sqrt((Math.pow(Lamb_a,2) - Math.pow(Lamb_b,2)) / Math.pow(Lamb_a,2));
  var Lamb_Phi1 = 50.99879884 / 200 * 180;
  var Lamb_Phi2 = 52.99557167 / 200 * 180;
  var Lamb_vo1 = Lamb_a / Math.sqrt(1 - Math.pow(Lamb_e,2) * (Math.pow(Math.sin(Lamb_Phi1 * Math.PI / 180),2)));
  var Lamb_vo2 = Lamb_a / Math.sqrt(1 - Math.pow(Lamb_e,2) * (Math.pow(Math.sin(Lamb_Phi2 * Math.PI / 180),2)));
  <!-- Phi0 est la Latitude du parallèle d origine -->
  var Lamb_Phi0 = 52 * 0.9;
  var Lamb_po1 = Lamb_a * (1 - Math.pow(Lamb_e,2)) / Math.pow((Math.sqrt(1 - Math.pow(Lamb_e,2) * Math.pow((Math.sin(Lamb_Phi1 * Math.PI / 180)),2))),3);
  var Lamb_po2 = Lamb_a * (1 - Math.pow(Lamb_e,2)) / Math.pow((Math.sqrt(1 - Math.pow(Lamb_e,2) * Math.pow((Math.sin(Lamb_Phi2 * Math.PI / 180)),2))),3);
  var Lamb_m1 = 1 + Lamb_po1 / 2 / Lamb_vo1 * Math.pow(((Lamb_Phi1 - Lamb_Phi0) * Math.PI / 180),2); 
  var Lamb_m2 = 1 + Lamb_po2 / 2 / Lamb_vo2 * Math.pow(((Lamb_Phi2 - Lamb_Phi0) * Math.PI / 180),2);
  var Lamb_m = (Lamb_m1 + Lamb_m2) / 2;
  var Lamb_CE = 600;
  var Lamb_CN = 2200;
  var Lamb_mL = 2 - Lamb_m;
  var Lamb_v0 = Lamb_a / Math.sqrt(1 - Math.pow(Lamb_e,2) * (Math.pow(Math.sin(Lamb_Phi0 * Math.PI / 180),2)));
  var Lamb_R0 = Lamb_v0 / Math.tan(Lamb_Phi0 * Math.PI / 180);
  <!-- mLR0 est le Rayon du parallèle d origine après réduction d echelle -->
  var Lamb_mLR0 = Lamb_mL * Lamb_R0;
  var Lamb_Ls = Math.log(Math.tan(Math.PI / 4 + Lamb_Phi0 / 2 * Math.PI / 180)) - Lamb_e / 2 * Math.log((1 + Lamb_e * Math.sin(Lamb_Phi0 * Math.PI / 180 )) / (1 - Lamb_e * Math.sin(Lamb_Phi0 * Math.PI / 180)));
  
  <!-- Abscisse en m dans le repère associé aux méridien et parallèle d'origine -->
  var Lamb_E1 = Lamb_EE - Lamb_CE * 1000;
  <!-- Ordonnée en m dans le repère associé aux méridien et parallèle d'origine -->
  var Lamb_N1 = Lamb_NN - Lamb_CN * 1000;
  <!-- Convergence des méridiens en degrés -->
  Lamb_gamma = Math.atan(Lamb_E1 / (Lamb_mLR0 - Lamb_N1)) * 180 / Math.PI;
  <!-- Lambda0 est la Longitude du méridien de Paris en degré -->
  var Lamb_Lambda0 = 2.596921296 / 200 * 180;
  <!-- Longitude du point recherché en degré par rapport à Greenwich -->
  var NTF_Lambda = (Lamb_gamma / Math.sin(Lamb_Phi0 * Math.PI / 180) + Lamb_Lambda0);
  
  <!-- Rayon du parallèle passant par le point recherché - en mètres -->
  var Lamb_R = (Lamb_mLR0 - Lamb_N1) / Math.cos(Lamb_gamma * Math.PI / 180);
  <!-- Valeur de L0 pour Phi0 -->
  var Lamb_L0 = Math.log(Math.tan(Math.PI / 4 + Lamb_Phi0 * Math.PI / 360)) - (Lamb_e / 2) * Math.log((1 + Lamb_e * Math.sin(Lamb_Phi0 * Math.PI / 180)) / (1 - Lamb_e * Math.sin(Lamb_Phi0 * Math.PI / 180)));
  <!-- Latitude isométrique L en fonction  de Phi -->
  var Lamb_L = Lamb_L0 + Math.log(Lamb_mLR0 / Lamb_R) / Math.sin(Lamb_Phi0 * Math.PI / 180);
  <!-- Latitude du point recherché -->
  <!-- Latitude Phi en degrés décimaux -->
  var ecart = 1;
  var Phi = 2 * Math.atan(Math.exp(Lamb_L)) - Math.PI / 2;
  while (ecart > 0.000000000001)
    {
    Phi_1 = 2 * (Math.atan(Math.exp(Lamb_L + Lamb_e / 2 * Math.log((1 + Lamb_e * Math.sin(Phi)) / (1 - Lamb_e * Math.sin(Phi)))))) - Math.PI / 2;
    ecart = Math.abs(Phi_1 - Phi);
    Phi = Phi_1;
    }
  var NTF_Phi = Phi * 180 / Math.PI;
  
  <!-- Module de correction à la projection du point donné -->
  <!-- Rayon de courbure de l ellipse normale principale -->
  var Lamb_v = Lamb_a / Math.sqrt(1 - Math.pow(Lamb_e,2) * (Math.pow(Math.sin(NTF_Phi * Math.PI / 180),2)));
  <!-- Module de réduction à la projection -->
  var Lamb_mr = Lamb_R * Math.sin(Lamb_Phi0 * Math.PI / 180) / Lamb_v / Math.cos(NTF_Phi * Math.PI / 180);
  <!-- Coefficient d altération linéaire en centimètres par kilomètres -->
  var Lamb_kr = (Lamb_mr - 1) * 100000;
  
  <!-- Mise en forme des latitude Longitude en DMS -->
  <!-- Tranformation Degrés décimaux Longitude arrivée en degré minute seconde avec reconnaissance E W -->
  if (NTF_Lambda > 0)
    {
    var E_W_Long_NTF = "E";
    }
  else
    {
    var E_W_Long_NTF = "W";
    }
  var NTF_Lambda = Math.abs(NTF_Lambda);
  var Deg_Long_NTF = Math.abs(Math.floor(NTF_Lambda));
  var Min_Long_NTF = Math.floor((NTF_Lambda - Deg_Long_NTF) * 60);
  var Sec_Long_NTF = Math.round(((NTF_Lambda - Deg_Long_NTF - Min_Long_NTF / 60) * 3600)*1000)/1000;
  
     <!-- Tranformation Degrés décimaux Latitude arrivée en degré minute seconde avec reconnaissance N S -->
  if (NTF_Phi > 0)
    {
    var N_S_Lat_NTF = "N";
    }
  else
    {
    var N_S_Lat_NTF = "S";
    }
  var NTF_Phi = Math.abs(NTF_Phi);
  var Deg_Lat_NTF = Math.floor(NTF_Phi);
  var Min_Lat_NTF = Math.floor((NTF_Phi - Deg_Lat_NTF) * 60);
  var Sec_Lat_NTF = Math.round(((NTF_Phi - Deg_Lat_NTF - Min_Lat_NTF / 60) * 3600)*1000)/1000;

  


  <!-- Constantes Ellipsoïde Hayford PRIAM ED50 -->
  var Hayford_a = 6378388.00;
  var Hayford_f = 297;
  var Hayford_b = Hayford_a - (Hayford_a / Hayford_f);
  var Hayford_e = (Math.pow(Hayford_a,2) - Math.pow(Hayford_b,2)) / Math.pow(Hayford_a,2);

  <!-- Constantes Ellipsoïde Clarke GeoConcept NTF -->
  var Clarke_a = 6378249.2;
  var Clarke_b = 6356515;
  var Clarke_f = 1 / ((Clarke_a - Clarke_b) / Clarke_a);
  var Clarke_e = (Math.pow(Clarke_a,2) - Math.pow(Clarke_b,2)) / Math.pow(Clarke_a,2);

  <!-- Constantes Tx, Ty, Tz en mètres de Transformation de 1 vers 3 -->
  var Tx = -84;
  var Ty = 37;
  var Tz = 437;

  <!-- Coordonnées LONGITUDE NTF Saisies (Si W -> -1 ou Si E -> 1) Pri_Deg, Pri_Min, Pri_Sec (virgules autorisées) -->
  <!-- Test du menu déroulant E ou W générant 1 ou -1 LONG -->
  if(E_W_Long_NTF == "E")
    {
    var NTF_Long_Signe = 1;
    }
  if(E_W_Long_NTF == "W")
    {
    var NTF_Long_Signe = -1;
    }
  <!-- Mise en Float des variables lues pour éviter les erreurs NaN (Not a Number) -->
  var NTF_Long_Sgn = parseFloat(NTF_Long_Signe);
  var NTF_Long_Deg = parseFloat(Deg_Long_NTF);
  var NTF_Long_Min = parseFloat(Min_Long_NTF);
  var NTF_Long_Sec = parseFloat(Sec_Long_NTF);

  <!-- Coordonnées LONGITUDE NTF (Lambda) Décimale calculée -->
  var NTF_Long_Dec = NTF_Long_Sgn * ((NTF_Long_Deg) + (NTF_Long_Min / 60) + (NTF_Long_Sec / 3600));

  <!-- Coordonnées LATITUDE NTF Saisies (Si N -> 1 ou Si S -> -1) Pri_Deg, Pri_Min, Pri_Sec (virgules autorisées) -->
  if(N_S_Lat_NTF == "N")
    {
    var NTF_Lat_Signe = 1;
    }
  if(N_S_Lat_NTF == "S")
    {
    var NTF_Lat_Signe = -1;
    }
  <!-- Mise en Float les variables lues pour éviter les erreurs NaN (Not a Number) -->
  var NTF_Lat_Sgn = parseFloat(NTF_Lat_Signe);
  var NTF_Lat_Deg = parseFloat(Deg_Lat_NTF);
  var NTF_Lat_Min = parseFloat(Min_Lat_NTF);
  var NTF_Lat_Sec = parseFloat(Sec_Lat_NTF);

  <!-- Coordonnées LATITUDE NTF (Phi) Décimale calculée -->
  var NTF_Lat_Dec = NTF_Lat_Sgn * ((NTF_Lat_Deg) + (NTF_Lat_Min / 60) + (NTF_Lat_Sec / 3600));

  <!-- Pas de considération de hauteur (en mètres) mis à 0 -- Peut être perfectible en ajoutant les tables de hauteur -->
  var Hauteur = 0;

  <!-- Référence Méridien de Grennwich pas décalage d'un système à l'autre -->

     <!-- Partie - coordonnées géocentriques sur l'ellipsoïde 'NTF' Origine (Clarke 1880 - NTF)
  <!-- Variable v (en mètres) calculée par la fct Hayford_a / Racine(1-(Hayford_e*sin(Pri_Lat_Dec*(Pi/180)))^2) -->
  var NTF_v = Clarke_a / (Math.sqrt(1 - (Clarke_e * Math.pow(Math.sin(NTF_Lat_Dec * (Math.PI / 180)),2))))

  <!-- Variable X calculée en mètres X = (v+h).Cos(Phi).Cos(Lambda) -->
  var NTF_X = (NTF_v + Hauteur) * Math.cos(NTF_Lat_Dec * (Math.PI / 180)) * Math.cos(NTF_Long_Dec * (Math.PI / 180));

  <!-- Variable Y calculée en mètres Y = (v+h).Cos(Phi).Sin(Lambda) -->
  var NTF_Y = (NTF_v + Hauteur) * Math.cos(NTF_Lat_Dec * (Math.PI / 180)) * Math.sin(NTF_Long_Dec * (Math.PI / 180));

  <!-- Variable Z calculée en mètres Z = (v*(1-e)+h).Sin(Phi) -->
  var NTF_Z = (NTF_v * (1 - Clarke_e) + Hauteur) * Math.sin(NTF_Lat_Dec * (Math.PI / 180));

     <!-- Partie - coordonnées géocentriques sur l'ellipsoïde 'ED50' Destination ( Hayford 1909 - ED50 Priam) -->
  <!-- Décalage longitude Paris Greenwich (2° 20' 14.025"") - Valeur en Degrés décimaux -->
  var Paris_Green = 2 + (20 / 60) + (14.025 / 3600);
  <!-- Dans ce cas aucune différence - même référence = Greenwich nommé Lambda_0 -->
  var Lambda_0 = 0;

  <!-- Variable X calculée en mètres X'= Tx + X * Cos(Lambda_0) + Y * Sin(Lambda_0) -->
  var ED50_X = Tx + NTF_X * Math.cos(Lambda_0 * (Math.PI / 180)) + NTF_Y * Math.sin(Lambda_0 * (Math.PI / 180));

  <!-- Variable Y calculée en mètres Y'= Ty - X * Sin(Lambda_0) + Y * Cos(Lambda_0) -->
  var ED50_Y = Ty - NTF_X * Math.sin(Lambda_0 * (Math.PI / 180)) + NTF_Y * Math.cos(Lambda_0 * (Math.PI / 180));

  <!-- Variable Z calculée en mètres Z'= Z + Tz -->
  var ED50_Z = NTF_Z + Tz;

  <!-- Variable Re_1 calculée en mètres Re_1 = RACINE (X'^2 + Y'^2) Re_1 Car autre formule de calcul donne meme résultat -->
  var Re_1 = Math.sqrt(Math.pow(ED50_X,2) + (Math.pow(ED50_Y,2)));

     <!-- Partie Coordonnées géographiques sur l'ellipsoïde de destination - Hayford 1909 - Origine Greenwich -->
  <!-- Longitude Lambda en degrés décimaux sur Hayford -->
  var Long_ED50_Dec = Math.atan(ED50_Y / ED50_X) / (Math.PI / 180);

  <!-- Latitude Phi en degrés décimaux sur Hayford - Lat_ED50_Dec = difficile à énoncer calcul récurcif -->
  var ecart = 1;
  var Phi = NTF_Lat_Dec / (180 * Math.PI);
  while (ecart > 0.0000000001)
    {
    Phi_1 = Math.atan((ED50_Z + Hayford_e * Math.sin(Phi) * Hayford_a / Math.sqrt(1 - Hayford_e * Math.pow(Math.sin(Phi),2))) / Re_1);
    ecart = Math.abs(Phi - Phi_1);
    Phi = Phi_1;
    }
  var Lat_ED50_Dec = Phi * (180 / Math.PI);

  <!-- Variable v en mètre sur Geoïde de destination - ED50_v -->
  var ED50_v = Hayford_a / (Math.sqrt(1 - (Hayford_e * Math.pow(Math.sin(Lat_ED50_Dec * (Math.PI / 180)),2))));

  <!-- Hauteur ellipsoïdale h_ED50 en mètres - Résultat à corriger pour passer à des altitudes NGF au dessus de l'éllipsoïde -->
  var h_ED50 = Re_1 / Math.cos(Lat_ED50_Dec*(Math.PI / 180)) - ED50_v;

     <!-- Tranformation Degrés décimaux Longitude arrivée en degré minute seconde avec reconnaissance E W -->
  if (Long_ED50_Dec > 0)
    {
    var E_W_Long_ED50 = "E";
    }
  else
    {
    var E_W_Long_ED50 = "W";
    }
  var Long_ED50_Dec = Math.abs(Long_ED50_Dec);
  var Deg_Long_ED50 = Math.abs(Math.floor(Long_ED50_Dec));
  var Min_Long_ED50 = Math.floor((Long_ED50_Dec - Deg_Long_ED50) * 60);
  var Sec_Long_ED50 = Math.round(((Long_ED50_Dec - Deg_Long_ED50 - Min_Long_ED50 / 60) * 3600)*1000)/1000;
  
     <!-- Tranformation Degrés décimaux Latitude arrivée en degré minute seconde avec reconnaissance N S -->
  if (Lat_ED50_Dec > 0)
    {
    var N_S_Lat_ED50 = "N";
    }
  else
    {
    var N_S_Lat_ED50 = "S";
    }
  var Lat_ED50_Dec = Math.abs(Lat_ED50_Dec);
  var Deg_Lat_ED50 = Math.floor(Lat_ED50_Dec);
  var Min_Lat_ED50 = Math.floor((Lat_ED50_Dec - Deg_Lat_ED50) * 60);
  var Sec_Lat_ED50 = Math.round(((Lat_ED50_Dec - Deg_Lat_ED50 - Min_Lat_ED50 / 60) * 3600)*1000)/1000;

  var center=Long_ED50_Dec+":"+Lat_ED50_Dec;
  var text="http://map.eea.eu.int/getmap.asp?MapType=Image2000_color2&CoordSys=ll&symbolgroup=eper&scale=25&scalebar=1&P="+center+":10:::1000&center="+center+"&size=w800";
  window.open(text,"Localisation","width=820,height=750,directories=no,menubar=no,resizable=yes,channelmode=no");
  return text;
     
<!-- Fin de la fonction NTF_ED50 - GeoConcept (NTF) Sexa - Clarke 1880 (1) -- VERS -- PRIAM (ED50) - Hayford 1909 (3) -->

 
 }
