
  /**
    * Functionality for coordinate-transformations
    * The following transformations are supported:
    *
    * WGS84 to RT90
    * RT90 to WGS84
    * SWEREF99TM to RT90
    * RT90 to SWEREF99TM
    * WGS84 to SWEREF99TM
    * SWEREF99 TM to WGS84
    *
    * Note: According to Lantmäteriets guidelines SWEREF99 lat,long,h are used instead of WGS84 in all the calculations.
    *
    @author Erik Nilsson Metria
  */
  //Global var:s
  var rt90X,rt90Y,rt90Z;
  var wgs84lat,wgs84long;
  var wgs_sw_N,wgs_sw_E;
  var sw_wgs_lat, sw_wgs_long;
  

  /**
    * Returns the Sinus hyperbolicum value of x
    @author Erik Nilsson Metria
    @param x A double with the value that will be used to calculate sinh
    @return A double containing the Sinus hyperbolicum value of x
  */
  function sinh(x){
    return (Math.exp(x)-Math.exp(-x))/2;
  }
  /**
    * Returns the Cosinus hyperbolicum value of x
    @author Erik Nilsson Metria
    @param x A double with the value that will be used to calculate cosh
    @return A double containing the Cosinus hyperbolicum value of x
  */
  function cosh(x){
    return (Math.exp(x)+Math.exp(-x))/2;
  }
  /**
    * Returns the Arcus tangens hyperbolicum value of x
    @author Erik Nilsson Metria
    @param x A double with the value that will be used to calculate atanh
    @return A double containing the Arcus tangens hyperbolicum value of x
  */
  function atanh(x){
    return (Math.log((1+x)/(1-x)))/2;
  }

  /**
    * Transforms coordinates from WGS84 to RT90 and sets the calculated result in the variables rt90X and rt90Y
    @param LAT A double with the WGS84 latitude
    @param LNG A double with the WGS84 longitude
    @param HEIGHT A double with the WGS84 height
    
    Called from getRT_X, getRT_Y, getSW2RT_X and getSW2RT_Y
  */
  function wgs2rt(LAT, LNG, HEIGHT){
    var a_wgs = 6378137.0;
    var f_wgs = 1.0/298.257222101;
    var e_sqr_wgs = 2*f_wgs - f_wgs*f_wgs;
    var h_wgs = HEIGHT;

    var dX = -414.0978567149;
    var dY = -41.3381489658;
    var dZ = -603.0627177516;

    var wX = -0.8550434314;
    var wY = 2.1413465185;
    var wZ = -7.0227209516;

    var a_rt = 6377397.155;
    var f_rt = 1.0/299.1528128;
    var e_sqr_rt = 2*f_rt - f_rt*f_rt;
    var n_rt=f_rt/(2-f_rt);
    
    var a=e_sqr_rt;
    var b=(5*e_sqr_rt*e_sqr_rt-e_sqr_rt*e_sqr_rt*e_sqr_rt)/6;
    var c=(104*e_sqr_rt*e_sqr_rt*e_sqr_rt-45*e_sqr_rt*e_sqr_rt*e_sqr_rt*e_sqr_rt)/120;
    var d=1237*e_sqr_rt*e_sqr_rt*e_sqr_rt*e_sqr_rt/1260;
    
    
    var b1 = n_rt/2-2*n_rt*n_rt/3+5*n_rt*n_rt*n_rt/16+41*n_rt*n_rt*n_rt*n_rt/180; //0.000835225;
    var b2 = 13*n_rt*n_rt/48-3*n_rt*n_rt*n_rt/5+557*n_rt*n_rt*n_rt*n_rt/1440; //0.000000756302;
    var b3 = 61*n_rt*n_rt*n_rt/240-103*n_rt*n_rt*n_rt*n_rt/140; //0.00000000119269;
    var b4 = 49561*n_rt*n_rt*n_rt*n_rt/161280;
    
    var ad = (a_rt/(1+n_rt))*(1+(n_rt*n_rt/4)+(n_rt*n_rt*n_rt*n_rt/64));//6366742.52;
    var y0 = 1500000;

    var Pi = Math.PI;

    // from seconds to radians
    var wXr = (Pi / 180.0) * (wX / 3600.0);
    var wYr = (Pi / 180.0) * (wY / 3600.0);
    var wZr = (Pi / 180.0) * (wZ / 3600.0);

    var LAT_R = LAT * Pi / 180.0;
    var LON_R = LNG * Pi / 180.0;

    // Converting from LAT-LONG (WGS84) to LAT-LONG (RT90)

    var n = a_wgs / Math.sqrt(1 - e_sqr_wgs * Math.sin(LAT_R) * Math.sin(LAT_R) );
    var x = (n + h_wgs) * Math.cos(LAT_R) * Math.cos(LON_R);
    var y = (n + h_wgs) * Math.cos(LAT_R) * Math.sin(LON_R);
    var z = (n * (1 - e_sqr_wgs) + h_wgs) * Math.sin(LAT_R);

    var aa = Math.cos(wYr)*Math.cos(wZr);
    var bb = Math.cos(wXr)*Math.sin(wZr) + Math.sin(wXr)*Math.sin(wYr)*Math.cos(wZr);
    var cc =-Math.cos(wXr)*Math.sin(wYr)*Math.cos(wZr) + Math.sin(wXr)*Math.sin(wZr);
    var dd =-Math.cos(wYr)*Math.sin(wZr);
    var ee = Math.cos(wXr)*Math.cos(wZr) - Math.sin(wXr)*Math.sin(wYr)*Math.sin(wZr);
    var ff = Math.sin(wXr)*Math.cos(wZr) + Math.cos(wXr)*Math.sin(wYr)*Math.sin(wZr);
    var gg = Math.sin(wYr);
    var hh =-Math.sin(wXr)*Math.cos(wYr);
    var ii = Math.cos(wXr)*Math.cos(wYr);

    var tempx = x;
    var tempy = y;

    x = dX + aa*x     + bb*y     + cc*z;
    y = dY + dd*tempx + ee*y     + ff*z;
    z = dZ + gg*tempx + hh*tempy + ii*z;

    var p = Math.sqrt(x*x + y*y);
    var omega = Math.atan(z / (p * Math.sqrt(1 - e_sqr_rt)));

    LAT_R = Math.atan((z + ((a_rt * e_sqr_rt) / Math.sqrt(1 - e_sqr_rt)) * Math.sin(omega) * Math.sin(omega) *Math.sin(omega))/ (p - a_rt * e_sqr_rt * Math.cos(omega) * Math.cos(omega) *Math.cos(omega) ));
    LON_R = Math.atan(y / x);

    n = a_rt / Math.sqrt(1 - e_sqr_rt * Math.sin(LAT_R) * Math.sin(LAT_R) );
    rt90Z = x / (Math.cos(LAT_R)*Math.cos(LON_R)) - n;

    var lam = LON_R - ((15 + 48 / 60.0 + 29.8 / 3600) * Pi / 180);
    //var ls = (a * Math.sin(2 * LAT_R) + b * Math.sin(4 * LAT_R) + c * Math.sin(6 * LAT_R)) + LAT_R;
    var ls=LAT_R-((Math.sin(LAT_R)*Math.cos(LAT_R))*(a+b*Math.sin(LAT_R)*Math.sin(LAT_R)+c*Math.sin(LAT_R)*Math.sin(LAT_R)*Math.sin(LAT_R)*Math.sin(LAT_R)+d*Math.sin(LAT_R)*Math.sin(LAT_R)*Math.sin(LAT_R)*Math.sin(LAT_R)*Math.sin(LAT_R)*Math.sin(LAT_R)));
    var e = Math.atan(Math.tan(ls) / Math.cos(lam));
    var n1 = Math.cos(ls) * Math.sin(lam);
    n = atanh(n1);
    var n2 = 2 * n;
    var n4 = 4 * n;
    var n6 = 6 * n;
    rt90X = ad * (e + b1 * Math.sin(2 * e) * cosh(n2) + b2 * Math.sin(4 * e) * cosh(n4) + b3 * Math.sin(6 * e) * cosh(n6));
    rt90Y = y0 + ad*(n + b1 * Math.cos(2 * e) * sinh(n2) + b2 * Math.cos(4 * e) * sinh(n4) + b3 * Math.cos(6 * e) * sinh(n6));
  }
  /**
    * Transforms coordinates from WGS84 to RT90 and returns the x-value
    @author Erik Nilsson Metria
    @param LAT A double with the WGS84 latitude
    @param LNG A double with the WGS84 longitude
    @param HEIGHT A double with the WGS84 height
    @return A double with the x-value
  */
  function getRT_X(LAT, LNG, HEIGHT){//, double & localx, double & localy, double & localz)
    wgs2rt(LAT,LNG,HEIGHT);
    return rt90X;
  }
  /**
    * Transforms coordinates from WGS84 to RT90 and returns the y-value
    @author Erik Nilsson Metria
    @param LAT A double with the WGS84 latitude
    @param LNG A double with the WGS84 longitude
    @param HEIGHT A double with the WGS84 height
    @return A double with the y-value
  */
  function getRT_Y(LAT, LNG, HEIGHT){//, double & localx, double & localy, double & localz)
    wgs2rt(LAT,LNG,HEIGHT);
    return rt90Y;
  }


 /**
    * Transforms coordinates from RT90 to WGS84 and sets the calculated result in the variables wgs84lat and wgs84long;
    @param theX double with the RT90 x-value
    @param theY double with the RT90 y-value
    @param theH double with the RT90 h-value
    
    Called from getWGS_LAT, getWGS_LONG, getRT2SW_N and getRT2SW_E 
  */
  function rt2wgs(theX, theY, theH){

	//Börja med plana till geografiska koord
	var Pi = Math.PI;
	//var FN=-6226307.8640;
	//var FE=84182.8790;
	var FN=0;
	var FE=1500000;

	//var k0=1.000002540000;
	var k0=1;

	var f_rt=1/299.1528128;
	var e_sqr_rt=f_rt*(2-f_rt);
	var n_rt=f_rt/(2-f_rt);
	var a_rt = 6377397.155;
	var atak=(a_rt/(1+n_rt))*(1+(n_rt*n_rt/4)+(n_rt*n_rt*n_rt*n_rt/64));
	var epsilon=(theX-FN)/(k0*atak);
	var eta=(theY-FE)/(k0*atak);

	var d1=(n_rt/2)-(2*n_rt*n_rt/3)+(37*n_rt*n_rt*n_rt/96)-(n_rt*n_rt*n_rt*n_rt/360);
	var d2=(n_rt*n_rt/48)+(n_rt*n_rt*n_rt/15)-(437*n_rt*n_rt*n_rt*n_rt/1440);
	var d3=(17*n_rt*n_rt*n_rt/480)-(37*n_rt*n_rt*n_rt*n_rt/840);
	var d4=(4397*n_rt*n_rt*n_rt*n_rt/161280);

	var epsilonprim=epsilon-(d1*Math.sin(2*epsilon)*cosh(2*eta))-(d2*Math.sin(4*epsilon)*cosh(4*eta))-(d3*Math.sin(6*epsilon)*cosh(6*eta))-(d4*Math.sin(8*epsilon)*cosh(8*eta));
	var etaprim=eta-(d1*Math.cos(2*epsilon)*sinh(2*eta))-(d2*Math.cos(4*epsilon)*sinh(4*eta))-(d3*Math.cos(6*epsilon)*sinh(6*eta))-(d4*Math.cos(8*epsilon)*sinh(8*eta));

	var latKonf=Math.asin(Math.sin(epsilonprim)/cosh(etaprim));
	var longdif=Math.atan(sinh(etaprim)/Math.cos(epsilonprim));

	var medelLong=(15+(48/60)+(29.8/3600))*(Pi/180);
	//alert(medelLong);

	var Ast=e_sqr_rt+Math.pow(e_sqr_rt, 2)+Math.pow(e_sqr_rt, 3)+Math.pow(e_sqr_rt, 4);
	var Bst=-((7*Math.pow(e_sqr_rt, 2))+(17*Math.pow(e_sqr_rt, 3))+(30*Math.pow(e_sqr_rt, 4)))/6;
	var Cst=((224*Math.pow(e_sqr_rt, 3))+(889*Math.pow(e_sqr_rt, 4)))/120;
	var Dst=-((4279*Math.pow(e_sqr_rt, 4)))/1260;



	var lat_rt=latKonf+Math.sin(latKonf)*Math.cos(latKonf)*(Ast+(Bst*Math.pow(Math.sin(latKonf),2))+  (Cst*Math.pow(Math.sin(latKonf),4)) + (Dst*Math.pow(Math.sin(latKonf),6))   );
	var long_rt=medelLong+longdif;
	//alert("Latlong RT: "+ lat_rt*180/Pi+"  "+long_rt*180/Pi);

	//geografiska till geocentriska koord

	var Nprim=a_rt/(Math.sqrt(1-(e_sqr_rt*Math.pow(Math.sin(lat_rt),2))));

	var cX=(Nprim+theH)*Math.cos(lat_rt)*Math.cos(long_rt);
	var cY=(Nprim+theH)*Math.cos(lat_rt)*Math.sin(long_rt);
	var cZ=((Nprim*(1-e_sqr_rt))+theH)*(Math.sin(lat_rt));

	//prompt("","C: "+cX+ "  "+cY+ "  "+ cZ );
	

	//Från RT90 till WGS84
	 var dX = -414.0978567149;
	 var dY = -41.3381489658;
	 var dZ = -603.0627177516;

	 var wX = -0.8550434314;
	 var wY = 2.1413465185;
	 var wZ = -7.0227209516;


	// from seconds to radians
	var wXr = (Pi / 180.0) * (wX / 3600.0);
	var wYr = (Pi / 180.0) * (wY / 3600.0);
	var wZr = (Pi / 180.0) * (wZ / 3600.0);

	var aa = Math.cos(wYr)*Math.cos(wZr);
	var bb =-Math.cos(wYr)*Math.sin(wZr);
	var cc = Math.sin(wYr);
	var dd = Math.cos(wXr)*Math.sin(wZr) + Math.sin(wXr)*Math.sin(wYr)*Math.cos(wZr);
	var ee = Math.cos(wXr)*Math.cos(wZr) - Math.sin(wXr)*Math.sin(wYr)*Math.sin(wZr);
	var ff =-Math.sin(wXr)*Math.cos(wYr);
	var gg =-Math.cos(wXr)*Math.sin(wYr)*Math.cos(wZr) + Math.sin(wXr)*Math.sin(wZr);
	var hh = Math.sin(wXr)*Math.cos(wZr) + Math.cos(wXr)*Math.sin(wYr)*Math.sin(wZr);
	var ii = Math.cos(wXr)*Math.cos(wYr);

	var skalKorr=0;

	var rX = (aa*(cX-dX) + bb*(cY-dY) + cc*(cZ-dZ))/(1+skalKorr);
	var rY = (dd*(cX-dX) + ee*(cY-dY) + ff*(cZ-dZ))/(1+skalKorr);
	var rZ = (gg*(cX-dX) + hh*(cY-dY) + ii*(cZ-dZ))/(1+skalKorr);

	var a_wgs = 6378137.0;
	var f_wgs = 1.0/298.257222101;
	var e_sqr_wgs = 2*f_wgs - f_wgs*f_wgs;

	var p=Math.sqrt(Math.pow(rX,2)+Math.pow(rY,2));
	var teta=Math.atan((rZ/(p*(Math.sqrt(1-e_sqr_wgs)))));

	var Nprim_wgs=a_wgs/(Math.sqrt(1-(e_sqr_wgs*Math.pow(Math.sin(wLat),2))));


	var wLat=Math.atan((rZ+(( a_wgs*e_sqr_wgs/(Math.sqrt(1-e_sqr_wgs)) )*Math.pow(Math.sin(teta),3))) / (p-(a_wgs*e_sqr_wgs*Math.pow(Math.cos(teta),3))));
	var wLong=Math.atan(rY/rX);
	var wH=(p/Math.cos(wLong))-Nprim_wgs

	wgs84lat=wLat*180/Pi;
	wgs84long=wLong*180/Pi;

  }
 
 /**
      * Transforms coordinates from RT90 to WGS84 and returns the lat-value
      @author Erik Nilsson Metria
      @param theX double with the RT90 x-value
      @param theY double with the RT90 y-value
      @param theH double with the RT90 h-value
      @return A double with the lat-value
    */
    function getWGS_LAT(theX, theY, theH){
      rt2wgs(theX, theY, theH);
      return wgs84lat;
    }
   /**
         * Transforms coordinates from RT90 to WGS84 and returns the long-value
         @author Erik Nilsson Metria
         @param theX double with the RT90 x-value
         @param theY double with the RT90 y-value
         @param theH double with the RT90 h-value
         @return A double with the long-value
    */
    function getWGS_LONG(theX, theY, theH){
      rt2wgs(theX, theY, theH);
      return wgs84long;
    }
    
 
 /**
       * Transforms coordinates from Sweref99TM to WGS84 and returns the lat-value in decimal degrees
       @author Erik Nilsson Metria
       @param theN double with the Sweref99TM N-value
       @param theE double with the Sweref99TM E-value
       @return A double with the lat-value
     */
     function getSW2WGS_LAT(theN, theE){
       sw2wgs(theN, theE);
       return sw_wgs_lat;
     }
    /**
      * Transforms coordinates from Sweref99TM to WGS84 and returns the long-value in decimal degrees
      @author Erik Nilsson Metria
      @param theN double with the Sweref99TM N-value
      @param theE double with the Sweref99TM E-value
      @return A double with the long-value
     */
     function getSW2WGS_LONG(theN, theE){
       sw2wgs(theN, theE);
       return sw_wgs_long;
     }
 
 
 /**
       * Transforms coordinates from WGS84 to Sweref99TM and returns the N-value
       @author Erik Nilsson Metria
       @param theLat double with the WGS84 latitude-value
       @param theLong double with the WGS84 longitude-value
       @return A double with the lat-value
     */
     function getWGS2SW_N(theLat, theLong){
       wgs2sw(theLat, theLong);
       return wgs_sw_N;
     }
    /**
          * Transforms coordinates from WGS84 to Sweref99TM and returns the E-value
          @author Erik Nilsson Metria
          @param theLat double with the WGS84 latitude-value
          @param theLong double with the WGS84 longitude-value
          
          @return A double with the long-value
     */
     function getWGS2SW_E(theLat, theLong){
       wgs2sw(theLat, theLong);
       return wgs_sw_E;
     }
 
 
  /**
        * Transforms coordinates from RT90 to Sweref99TM and returns the N-value
        @author Erik Nilsson Metria
        @param theX double with the RT90 X-value
        @param theY double with the RT90 Y-value
        @return A double with the N-value
      */
      function getRT2SW_N(theX, theY){
        rt2wgs(theX, theY,0);
        wgs2sw(wgs84lat,wgs84long);
        return wgs_sw_N;
      }
     /**
           * Transforms coordinates from RT90 to Sweref99TM and returns the E-value
           @author Erik Nilsson Metria
           @param theX double with the RT90 X-value
           @param theY double with the RT90 Y-value
           @return A double with the E-value
      */
      function getRT2SW_E(theX, theY){
        rt2wgs(theX, theY,0);
        wgs2sw(wgs84lat,wgs84long);
        return wgs_sw_E;
      }
 /**
       * Transforms coordinates from Sweref99TM to RT90 and returns the X-value
       @author Erik Nilsson Metria
       @param theN double with the Sweref99 N-value
       @param theE double with the Sweref99 E-value
       @return A double with the X-value
     */
     function getSW2RT_X(theN, theE){
       sw2wgs(theN,theE);
       wgs2rt(sw_wgs_lat, sw_wgs_long,0);
       return rt90X;
     }
    /**
          * Transforms coordinates from Sweref99TM to RT90 and returns the Y-value
          @author Erik Nilsson Metria
          @param theN double with the Sweref99 N-value
	  @param theE double with the Sweref99 E-value
          @return A double with the Y-value
     */
     function getSW2RT_Y(theN, theE){
       sw2wgs(theN,theE);
       wgs2rt(sw_wgs_lat, sw_wgs_long,0);
       return rt90Y;
     }

 
  /**
     * Transforms coordinates from SWEREF99TM to WGS84 calculated result in the variables sw_wgs_lat and sw_wgs_long.
     @param theN A double with the SWEREF99TM N-coordinate
     @param theE A double with the SWEREF99TM E-coordinate
     
     Called from getSW2RT_X, and getSW2RT_Y
     
     // Formulas from http://www.lantmateriet.se/upload/filer/kartor/geodesi_gps_och_detaljmatning/geodesi/Formelsamling/Gauss_Conformal_Projection.pdf    
  */
    

function sw2wgs(theN,theE){
	
	var Pi = Math.PI;
	var a=6378137;
	var f=1/298.257222101;
	var e2=f*(2-f);
	var k0=0.9996;
	var n=f/(2-f);
	var atak=(a/(1+n))*(1+(n*n/4)+(n*n*n*n/64));
	var FN=0;
	var FE=500000;
	var eta=(theN-FN)/(k0*atak);
	var teta=(theE-FE)/(k0*atak);
	var delta1=(n/2)-(2*n*n/3)+(37*n*n*n/96)-(n*n*n*n/360);
	var delta2=(n*n/48)+(n*n*n/15)-(437*n*n*n*n/1440);
	var delta3=(17*n*n*n/480)-(37*n*n*n*n/840);
	var delta4=4397*n*n*n*n/161280;
	var eta_prick=eta-(delta1*Math.sin(2*eta)*cosh(2*teta))-(  delta2*Math.sin(4*eta)*cosh(4*teta)) -(delta3*Math.sin(6*eta)*cosh(6*teta))-(delta4*Math.sin(8*eta)*cosh(8*teta));
	var teta_prick=teta-(delta1*Math.cos(2*eta)*sinh(2*teta))-(delta2*Math.cos(4*eta)*sinh(4*teta))-(delta3*Math.cos(6*eta)*sinh(6*teta))-(delta4*Math.cos(8*eta)*sinh(8*teta));
	var fi_star=Math.asin(Math.sin(eta_prick)/cosh(teta_prick));
	var delta_lambda=Math.atan(sinh(teta_prick)/Math.cos(eta_prick));
	var A_star=e2+e2*e2+e2*e2*e2+e2*e2*e2*e2;
	var B_star=-((7*e2*e2)+(17*e2*e2*e2)+(30*e2*e2*e2*e2))/6;
	var C_star=((224*e2*e2*e2)+(889*e2*e2*e2*e2))/120;
	var D_star=-((4279*e2*e2*e2*e2)/1260);
	var lambda0=2*Pi*15/360;
	var lat=fi_star+(Math.sin(fi_star)*Math.cos(fi_star)*( A_star+(B_star*(Math.sin(fi_star)*Math.sin(fi_star)))  +  (C_star*(Math.sin(fi_star)*Math.sin(fi_star)*Math.sin(fi_star)*Math.sin(fi_star))) + (D_star*(Math.sin(fi_star)*Math.sin(fi_star)*Math.sin(fi_star)*Math.sin(fi_star)*Math.sin(fi_star)*Math.sin(fi_star)))    ));
	var lng=lambda0+delta_lambda;
	
	sw_wgs_lat=lat*180/Pi;
	sw_wgs_long=lng*180/Pi;


}

  /**
     * Transforms coordinates from WGS84 to SWEREF99TM. Calculated result in the variables wgs_sw_N and wgs_sw_E.
     @param theLat A double with the WGS84 Lat-coordinate
     @param theLong A double with the WGS84 Long-coordinate
     
     Called from getWGS2SW_N,getWGS2SW_E, getRT2SW_N, getRT2SW_E
     
     // Formulas from http://www.lantmateriet.se/upload/filer/kartor/geodesi_gps_och_detaljmatning/geodesi/Formelsamling/Gauss_Conformal_Projection.pdf    
  */

function wgs2sw(theLat,theLong){
	var Pi = Math.PI;
	var a=6378137;
	var f=1/298.257222101;
	var e2	=f*(2-f);
	var lat=2*Pi*theLat/360;
	var lng=2*Pi*theLong/360;
	var lambda0=2*Pi*15/360;
	var k0=0.9996;
	var delta_lambda=lng-lambda0;
	var FN=0;
	var FE=500000;
	var n=f/(2-f);
	var atak=(a/(1+n))*(1+(n*n/4)+(n*n*n*n/64));
	var A=e2;
	var B=((5*e2*e2)-(e2*e2*e2))/6;
	var C=((104*e2*e2*e2)-(45*e2*e2*e2*e2))/120;
	var D=1237*e2*e2*e2*e2/1260;
	var sin_fi=Math.sin(lat);
	var cos_fi=Math.cos(lat);
	var fi_star=lat-((sin_fi*cos_fi)*(A+(B*sin_fi*sin_fi)+(C*sin_fi*sin_fi*sin_fi*sin_fi)+(D*sin_fi*sin_fi*sin_fi*sin_fi*sin_fi*sin_fi)));
	var eta_prick=Math.atan((Math.tan(fi_star)/Math.cos(delta_lambda)));
	var teta_prick=atanh(Math.cos(fi_star)*Math.sin(delta_lambda));
	var beta1=(n/2)-(2*n*n/3)+(5*n*n*n/16)+(41*n*n*n*n/180);
	var beta2=(13*n*n/48)-(3*n*n*n/5)+(557*n*n*n*n/1440);
	var beta3=(61*n*n*n/240)-(103*n*n*n*n/140);
	var beta4=49561*(n*n*n*n)/161280;
	
	wgs_sw_N=(k0*atak*(eta_prick+(beta1*Math.sin(2*eta_prick)*cosh(2*teta_prick)) + (beta2*Math.sin(4*eta_prick)*cosh(4*teta_prick)) + (beta3*Math.sin(6*eta_prick)*cosh(6*teta_prick)) + (beta4*Math.sin(8*eta_prick)*cosh(8*teta_prick)    )    ))+FN;
	wgs_sw_E=(k0*atak*(teta_prick+(beta1*Math.cos(2*eta_prick)*sinh(2*teta_prick)) + (beta2*Math.cos(4*eta_prick)*sinh(4*teta_prick)) + (beta3*Math.cos(6*eta_prick)*sinh(6*teta_prick)) + (beta4*Math.cos(8*eta_prick)*sinh(8*teta_prick)    )    ))+FE;
}
