function gPoint(){
this.falseEasting = 0;
this.falseNorthing = 0;
this.longOfOrigin = 0;
this.latOfOrigin = 0;
this.firstStdParallel = 0;
this.secondStdParallel = 0;
this.lat 	= 0;
this.longi = 0;
this.lccEasting=0;
this.lccNorthingting=0;
this.a = 0;
this.e2 = 0;

this.configlamb=configLambertProjection;
this.lamb2gps=convertLCCtoLL;
this.gps2lamb=convertLLtoLCC;

}

//Configuration de la projection
function configLambertProjection (jfalseEasting, jfalseNorthing,jlongOfOrigin, jlatOfOrigin,jfirstStdParallel, jsecondStdParallel)
{
  this.falseEasting = jfalseEasting;
  this.falseNorthing = jfalseNorthing;
  this.longOfOrigin = jlongOfOrigin;
  this.latOfOrigin = jlatOfOrigin;
  this.firstStdParallel = jfirstStdParallel;
  this.secondStdParallel = jsecondStdParallel;
}
//Conversion gps vers lambert
function convertLLtoLCC()
{
	je = Math.sqrt(this.e2);
	jphi 	= deg2rad(this.lat);						// Latitude to convert
	jphi1	= deg2rad(this.firstStdParallel);			// Latitude of 1st std parallel
	jphi2	= deg2rad(this.secondStdParallel);		// Latitude of 2nd std parallel
	jlamda	= deg2rad(this.longi);						// Lonitude to convert
	jphio	= deg2rad(this.latOfOrigin);				// Latitude of  Origin
	jlamdao	= deg2rad(this.longOfOrigin);				// Longitude of  Origin
	jm1 = Math.cos(jphi1) / Math.sqrt(( 1 - this.e2*Math.sin(jphi1)*Math.sin(jphi1)));
	jm2 = Math.cos(jphi2) / Math.sqrt(( 1 - this.e2*Math.sin(jphi2)*Math.sin(jphi2)));
	jt1 = Math.tan((Math.PI/4)-(jphi1/2)) / Math.pow(( ( 1 - je*Math.sin(jphi1) ) / ( 1 + je*Math.sin(jphi1) )),je/2);
	jt2 = Math.tan((Math.PI/4)-(jphi2/2)) / Math.pow(( ( 1 - je*Math.sin(jphi2) ) / ( 1 + je*Math.sin(jphi2) )),je/2);
	jto = Math.tan((Math.PI/4)-(jphio/2)) / Math.pow(( ( 1 - je*Math.sin(jphio) ) / ( 1 + je*Math.sin(jphio) )),je/2);
	jt  = Math.tan((Math.PI/4)-(jphi /2)) / Math.pow(( ( 1 - je*Math.sin(jphi ) ) / ( 1 + je*Math.sin(jphi ) )),je/2);
	jn	= (Math.log(jm1)-Math.log(jm2)) / (Math.log(jt1)-Math.log(jt2));
	jF	= jm1/(jn*Math.pow(jt1,jn));
	jrf	= this.a*jF*Math.pow(jto,jn);
	jr	= this.a*jF*Math.pow(jt,jn);
	jtheta = jn*(jlamda - jlamdao);
	this.lccEasting = this.falseEasting + jr*Math.sin(jtheta);
	this.lccNorthing = this.falseNorthing + jrf - jr*Math.cos(jtheta);
}

//Conversion lambert vers gps
function convertLCCtoLL()
{
  je = Math.sqrt(this.e2);
  jphi1	= deg2rad(this.firstStdParallel);			// Latitude of 1st std parallel
  jphi2	= deg2rad(this.secondStdParallel);		// Latitude of 2nd std parallel
  jphio	= deg2rad(this.latOfOrigin);				// Latitude of  Origin
  jlamdao	= deg2rad(this.longOfOrigin);				// Longitude of  Origin
  jE		= this.lccEasting;
  jN		= this.lccNorthing;
  jEf		= this.falseEasting;
  jNf		= this.falseNorthing;
  jm1 = Math.cos(jphi1) / Math.sqrt(( 1 - this.e2*Math.sin(jphi1)*Math.sin(jphi1)));
  jm2 = Math.cos(jphi2) / Math.sqrt(( 1 - this.e2*Math.sin(jphi2)*Math.sin(jphi2)));
  jt1 = Math.tan((Math.PI/4)-(jphi1/2)) / Math.pow(( ( 1 - je*Math.sin(jphi1) ) / ( 1 + je*Math.sin(jphi1) )),je/2);
  jt2 = Math.tan((Math.PI/4)-(jphi2/2)) / Math.pow(( ( 1 - je*Math.sin(jphi2) ) / ( 1 + je*Math.sin(jphi2) )),je/2);
  jto = Math.tan((Math.PI/4)-(jphio/2)) / Math.pow(( ( 1 - je*Math.sin(jphio) ) / ( 1 + je*Math.sin(jphio) )),je/2);
  jn	= (Math.log(jm1)-Math.log(jm2)) / (Math.log(jt1)-Math.log(jt2));
  jF	= jm1/(jn*Math.pow(jt1,jn));
  jrf	= this.a*jF*Math.pow(jto,jn);
  jr_	= Math.sqrt( Math.pow((jE-jEf),2) + Math.pow((jrf-(jN-jNf)),2) );
  jt_	= Math.pow(jr_/(this.a*jF),(1/jn));
  jtheta_ = Math.atan((jE-jEf)/(jrf-(jN-jNf)));
  jlamda	= jtheta_/jn + jlamdao;
  jphi0	= (Math.PI/2) - 2*Math.atan(jt_);
  jphi1	= (Math.PI/2) - 2*Math.atan(jt_*Math.pow(((1-je*Math.sin(jphi0))/(1+je*Math.sin(jphi0))),je/2));
  jphi2	= (Math.PI/2) - 2*Math.atan(jt_*Math.pow(((1-je*Math.sin(jphi1))/(1+je*Math.sin(jphi1))),je/2));
  jphi	= (Math.PI/2) - 2*Math.atan(jt_*Math.pow(((1-je*Math.sin(jphi2))/(1+je*Math.sin(jphi2))),je/2));
  this.lat 	= rad2deg(jphi);
  this.longi = rad2deg(jlamda);
}

//conversion degre vers radian
function deg2rad(deg){
  return parseFloat(deg)*Math.PI/180;
}

//conversion radian vers degre
function rad2deg(rad){
  return parseFloat(rad)*180/Math.PI;
}

//conversion degre decimaux vers degre DMS
function dec2dms(dec){
  deg=parseInt(dec);
  tmp=(parseFloat(dec)-parseFloat(deg))*60;
  min=parseInt(tmp);
  sec=(tmp-min)*60
  sec=sec.toFixed(4)
  return deg+"°"+min+"'"+sec+'"';
}

//conversion degre DMS vers degre decimaux
function dms2dec(dms){
  deg=parseFloat(dms.substring(0,dms.indexOf("°")));
  min=parseFloat(dms.substring(dms.indexOf("°")+1,dms.indexOf("'")))/60;
  sec=parseFloat(dms.substring(dms.indexOf("'")+1,dms.indexOf('"')))/3600;
  return parseFloat(deg+min+sec).toFixed(6);
}

//Calcul et mise a jour à la volée des coordonnées gps ou lambert
function suit_souris_gps(evt){
  position(evt);
  thas=new gPoint();
  if (document.getElementById("type_proj").value=="lam"){
    thas.configlamb(600000, 2200000, 2.33723, 46.8, 45.89892, 47.69601);
  }else{
    thas.configlamb(600000, 200000, 2.33723, 46.8, 45.89892, 47.69601);
  }
  thas.lccEasting=(xmin + xscmap);
  thas.lccNorthing=(ymax - yscmap);
  thas.a = 6378249;
  thas.e2 = 0.006803511;
  x = Math.round(xmin + xscmap);
  y = Math.round(ymax - yscmap);
  thas.lamb2gps();
  if (document.getElementById("type_proj").value=="gps"){
    if (unite_cour=="DEC"){
      window.document.getElementById("gpsy").value=thas.lat.toFixed(6)+' N';
        if (thas.longi<0){
           window.document.getElementById("gpsx").value=Math.abs(thas.longi.toFixed(6))+' W';
        }else{
          window.document.getElementById("gpsx").value=thas.longi.toFixed(6)+' E';
        }
    }else{
      window.document.getElementById("gpsy").value=dec2dms(thas.lat)+' N';
      if (thas.longi<0){
        window.document.getElementById("gpsx").value=dec2dms(Math.abs(thas.longi))+' W';
      }else{
        window.document.getElementById("gpsx").value=dec2dms(thas.longi)+' E';
      }
    }
  }else{
    window.document.getElementById("gpsy").value=y;
    window.document.getElementById("gpsx").value=x;
  }
}

//Centre sur les coordonnées GPS fournies
function centrer_gps(){
  x=window.document.getElementById("gpsx").value;
  y=window.document.getElementById("gpsy").value;
  thas=new gPoint();
    
  if (window.document.getElementById("type_proj").value=="gps") {
    if (unite_cour=="DMS"){
      x=dms2dec(x);
      y=dms2dec(y);
    }else{
      x=parseFloat(x);
      y=parseFloat(y);
    }
    thas.configlamb(600000, 2200000, 2.33723, 46.8, 45.89892, 47.69601);
    thas.lat=parseFloat(y);
    thas.longi=parseFloat(x);
    thas.a = 6378249;
    thas.e2 = 0.006803511;
    thas.gps2lamb();
  }else{
    thas.lccEasting=parseInt(x);
    thas.lccNorthing=parseInt(y);
  }
  rectvc=controlmap.getElementById("rectvc");
  rectvc.setAttribute("x",parseInt(thas.lccEasting-xmin)-parseInt(rectvc.getAttribute("width")/2));
  rectvc.setAttribute("y",parseInt(ymax)-parseInt(thas.lccNorthing)-parseInt(rectvc.getAttribute("height")/2));
  newscmapvb=rectvc.getAttribute("x")+' '+rectvc.getAttribute("y")+' '+parseInt(rectvc.getAttribute("width"))+' '+parseInt(rectvc.getAttribute("height"));
  tabscmapguivb=controlmap.getElementById("scmapgui").getAttribute("viewBox").split(" ");
  newscmapguivb=(rectvc.getAttribute("x")-((tabscmapguivb[2]-rectvc.getAttribute("width"))/2))+' '+(rectvc.getAttribute("y")-((tabscmapguivb[3]-rectvc.getAttribute("height"))/2))+' '+parseInt(tabscmapguivb[2])+' '+parseInt(tabscmapguivb[3]);
  controlmap.getElementById('scmapgui').setAttribute("viewBox",newscmapguivb);
  chargecouchegui(tabcouchegui);
  mainmap.getElementById('scmap').setAttribute("viewBox",newscmapvb);
  chargecouche1(tabcouche);
}
