function longToMercator(_long) {
                 horzPos=_long*100000;
				 return horzPos
}

function latToMercator(_lat) {
                 d = Math.min(0.99999999, Math.max(-0.99999999, Math.sin((_lat)*Math.PI/180)));
                 vertPos = (0.5*Math.log((1+d)/(1-d))*180/Math.PI) * -100000;
				 return vertPos;
}

function latlongToMercator() {
                 horzPos=lon*100000;
                 d = Math.min(0.99999999, Math.max(-0.99999999, Math.sin((lat)*Math.PI/180)));
                 vertPos = (0.5*Math.log((1+d)/(1-d))*180/Math.PI) * -100000;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function mercatorToLatlong() {
                 var lat,lon;
                 lon=horzPos/100000;
                 vertPos = vertPos / -100000;
                 lat = (2*Math.atan(Math.exp(vertPos*Math.PI/180))-Math.PI/2)*180/Math.PI;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GIStools() {
     var t = this;

                ///////////////////////////////////////
                var Sqr = function(x)
                                {return Math.pow(x,0.5);
                };
                ///////////////////////////////////////
                var Marc=function(bf0,n,PHI0,PHI) {
                                n2=n*n;
                                return bf0 * (((1 + n + ((5 / 4) * (n2)) + ((5 / 4) * (n2*n))) * (PHI - PHI0)) - (((3 * n) + (3 * (n2)) + ((21 / 8) * (n2*n))) * (Math.sin(PHI - PHI0)) * (Math.cos(PHI + PHI0))) + ((((15 / 8) * (n2)) + ((15 / 8) * (n2*n))) * (Math.sin(2 * (PHI - PHI0))) * (Math.cos(2 * (PHI + PHI0)))) - (((35 / 24) * (n2*n)) * (Math.sin(3 * (PHI - PHI0))) * (Math.cos(3 * (PHI + PHI0)))))
                };
                ///////////////////////////////////////
                var InitialLat=function(North,n0,afo,PHI0,n,bfo) {
                                 PHI1 = ((North - n0) / afo) + PHI0;
                                 M = Marc(bfo, n, PHI0, PHI1);
                                 PHI2 = ((North - n0 - M) / afo) + PHI1;   
                                 while (Math.abs(North - n0 - M) > 0.000000001) {
                                                  PHI2 = ((North - n0 - M) / afo) + PHI1;
                                                  M = Marc(bfo, n, PHI0, PHI2);
                                                  PHI1 = PHI2;
                                 }return PHI2;
                };
                ///////////////////////////////////////
                var East_North_to_Lat_Long=function(East,North,a,b,e0,n0,f0,PHI0,LAM0) {
                                Pi=4*Math.atan(1);
                                RadPHI0 = PHI0 * (Pi / 180);
                                RadLAM0 = LAM0 * (Pi / 180);af0 = a * f0;
                                bf0 = b * f0;
                                af02=af0*af0;
                                bf02=bf0*bf0;e2 = ((af02) - (bf02)) / (af02);n = (af0 - bf0) / (af0 + bf0);
                                Et = East - e0;
                                Et2=Et*Et;
                                PHId = InitialLat(North,  n0, af0, RadPHI0, n, bf0);
                                SinPHId2=Math.sin(PHId)*Math.sin(PHId);
                                nu = af0 / (Sqr(1 - (e2 * (SinPHId2))));
                                nu2=nu*nu;rho = (nu * (1 - e2)) / (1 - (e2 * SinPHId2));
                                eta2 = (nu / rho) - 1;
                                TanPHId=Math.tan(PHId);
                                TanPHId2=TanPHId*TanPHId;
                                VII = (Math.tan(PHId)) / (2 * rho * nu);
                                VIII = ((Math.tan(PHId)) / (24 * rho * (nu2*nu))) * (5 + (3 * (TanPHId2)) + eta2 - (9 * eta2 * (TanPHId2)));
                                IX = ((Math.tan(PHId)) / (720 * rho * (nu2*nu2*nu))) * (61 + (90 * (TanPHId2)) + (45 * (TanPHId2*TanPHId2)));
                                X = (1/Math.cos(PHId)) / nu;
                                XI = ((1/Math.cos(PHId)) / (6 * (nu2*nu))) * ((nu / rho) + (2 * (TanPHId2)));
                                XII = ((1/Math.cos(PHId)) / (120 * (nu2*nu*nu2))) * (5 + (28 * (TanPHId2)) + (24 * (TanPHId2*TanPHId2)));
                                XIIA = ((1/Math.cos(PHId)) / (5040 * (nu2*nu*nu2*nu2))) * (61 + (662 * (TanPHId2)) + (1320 * (TanPHId2*TanPHId2)) + (720 * (TanPHId2*TanPHId2*TanPHId2)));
                                
                                var point = {vertPos: 0, horzPos: 0} 
                                point.horzPos = (180 / Pi) * (RadLAM0 + (Et * X) - ((Et2*Et) * XI) + ((Et2*Et2*Et) * XII) - ((Et2*Et2*Et2*Et) * XIIA));
                                point.vertPos = (180 / Pi) * (PHId - ((Et2) * VII) + ((Et2*Et2) * VIII) - ((Et2*Et2*Et2) * IX));
                                return point;
                };
                ///////////////////////////////////
                var TranMerc_Origin_Lat,TranMerc_Origin_Long,TranMerc_False_Northing,TranMerc_False_Easting,TranMerc_Scale_Factor,RAD_PER_DEG,Pi
                PI=Math.PI
                RAD_PER_DEG = PI/180.0

                var MAX_LAT = ((PI * 90.0)/180.0)
                var MAX_DELTA_LONG = ((PI * 90.0)/180.0)
                var MIN_SCALE_FACTOR = 0.3
                var MAX_SCALE_FACTOR = 3.0

                var TranMerc_a= 6378137.0
                var TranMerc_f= 1 / 298.257223563
                var TranMerc_es= 0.0066943799901413800
                var TranMerc_ebs= 0.0067394967565869

                var TranMerc_ap= 6367449.1458008
                var TranMerc_bp= 16038.508696861
                var TranMerc_cp= 16.832613334334
                var TranMerc_dp= 0.021984404273757
                var TranMerc_ep= 3.1148371319283e-005

                var TranMerc_Delta_Easting= 40000000.0
                var TranMerc_Delta_Northing= 40000000.0
                ///////////////////////////////////
                var Sqr2=function(x){
                                 return Math.pow(x,0.5);
                };
                ///////////////////////////////////
                var SPHTMD=function(Latitude){
                                 return (TranMerc_ap * Latitude - TranMerc_bp * Math.sin(2.e0 * Latitude) + TranMerc_cp * Math.sin(4.e0 * Latitude) - TranMerc_dp * Math.sin(6.e0 * Latitude) + TranMerc_ep * Math.sin(8.e0 * Latitude) )
                };
                ///////////////////////////////////
                var SPHSN=function(Latitude){
                                 return (TranMerc_a / Sqr2( 1.e0 - TranMerc_es * Math.pow(Math.sin(Latitude), 2)))
                };
                ///////////////////////////////////
                var SPHSR=function(Latitude){
                                 return (TranMerc_a * (1.e0 - TranMerc_es) / Math.pow(DENOM(Latitude), 3))
                };
                ///////////////////////////////////
                var DENOM=function(Latitude){
                                 return (Sqr2(1.e0 - TranMerc_es * Math.pow(Math.sin(Latitude),2)))
                };
                ///////////////////////////////////
                var SetTransMercParams=function(OriginLatitude, CentralMeridian, FalseEasting, FalseNorthing, ScaleFactor) {
                                OriginLatitude= OriginLatitude*RAD_PER_DEG
                                CentralMeridian= CentralMeridian*RAD_PER_DEG

                                TranMerc_Origin_Lat= OriginLatitude
                                if (CentralMeridian > Pi) CentralMeridian= CentralMeridian-Pi*2
                                TranMerc_Origin_Long= CentralMeridian
                                TranMerc_False_Northing= FalseNorthing
                                TranMerc_False_Easting= FalseEasting
                                TranMerc_Scale_Factor= ScaleFactor
                  }
                ///////////////////////////////////
                var Lat_Long_to_East_North = function(Latitude, Longitude) {
                  var c,c2,c3,c5,c7,dlam,eta,eta2,eta3,eta4,s,sn,t,tan2,tan3,tan4,tan5,tan6,t1,t2,t3,t4,t5,t6,t7,t8,t9,tmd,tmdo,temp_Origin,temp_Long

                  Latitude = Latitude*RAD_PER_DEG
                  Longitude = Longitude*RAD_PER_DEG

                 if (Longitude > Pi) Longitude= Longitude-Pi*2

                  if ((Longitude < (TranMerc_Origin_Long - MAX_DELTA_LONG)) || (Longitude > (TranMerc_Origin_Long + MAX_DELTA_LONG))) {
                                if (Longitude < 0) temp_Long= Longitude + Pi*2
                                  else temp_Long= Longitude

                                if (TranMerc_Origin_Long < 0) temp_Origin= TranMerc_Origin_Long + Pi*2
                                  else temp_Origin= TranMerc_Origin_Long

                  }


                  dlam = Longitude - TranMerc_Origin_Long


                  if (dlam > Pi) dlam= dlam-Pi*2
                  if (dlam < -Pi) dlam= dlam+Pi*2
                  if (Math.abs(dlam) < 2.e-10) dlam= 0.0

                  s= Math.sin(Latitude)
                  c= Math.cos(Latitude)
                  c2= c * c
                  c3= c2 * c
                  c5= c3 * c2
                  c7= c5 * c2
                  t= Math.tan (Latitude)
                  tan2= t * t
                  tan3= tan2 * t
                  tan4= tan3 * t
                  tan5= tan4 * t
                  tan6= tan5 * t
                  eta= TranMerc_ebs * c2
                  eta2= eta * eta
                  eta3= eta2 * eta
                  eta4= eta3 * eta

                  sn= SPHSN(Latitude)
                  tmd= SPHTMD(Latitude)
                  tmdo= SPHTMD(TranMerc_Origin_Lat)

                  t1= (tmd - tmdo) * TranMerc_Scale_Factor
                  t2= sn * s * c * TranMerc_Scale_Factor/ 2.e0
                  t3= sn * s * c3 * TranMerc_Scale_Factor * (5.e0 - tan2 + 9.e0 * eta + 4.e0 * eta2) /24.e0

                  t4= sn * s * c5 * TranMerc_Scale_Factor * (61.e0 - 58.e0 * tan2 + tan4 + 270.e0 * eta - 330.e0 * tan2 * eta + 445.e0 * eta2+ 324.e0 * eta3 -680.e0 * tan2 * eta2 + 88.e0 * eta4 -600.e0 * tan2 * eta3 - 192.e0 * tan2 * eta4) / 720.e0

                  t5= sn * s * c7 * TranMerc_Scale_Factor * (1385.e0 - 3111.e0 * tan2 + 543.e0 * tan4 - tan6) / 40320.e0

                  Northing= TranMerc_False_Northing + t1 + Math.pow(dlam,2.e0) * t2 + Math.pow(dlam,4.e0) * t3 + Math.pow(dlam,6.e0) * t4 + Math.pow(dlam,8.e0) * t5
                  
                  Northing = Northing*-1

                  t6= sn * c * TranMerc_Scale_Factor
                  t7= sn * c3 * TranMerc_Scale_Factor * (1.e0 - tan2 + eta ) /6.e0
                  t8= sn * c5 * TranMerc_Scale_Factor * (5.e0 - 18.e0 * tan2 + tan4+ 14.e0 * eta - 58.e0 * tan2 * eta + 13.e0 * eta2 + 4.e0 * eta3- 64.e0 * tan2 * eta2 - 24.e0 * tan2 * eta3 )/ 120.e0
                  t9= sn * c7 * TranMerc_Scale_Factor * ( 61.e0 - 479.e0 * tan2+ 179.e0 * tan4 - tan6 ) /5040.e0


                  Easting = TranMerc_False_Easting + dlam * t6 + Math.pow(dlam,3.e0) * t7+ Math.pow(dlam,5.e0) * t8 + Math.pow(dlam,7.e0) * t9
                  var pos = {horzPos: 0, vertPos: 0} 
                  pos.horzPos = Math.round(Easting)
                  pos.vertPos = Math.round(Northing)
                  return pos;
                }




                ///////////////////////////////////
                t.LTLGtoITM = function(Lat,Lng){
                   SetTransMercParams(31.734393, 35.204516, 219529.584, 626907.390, 1.0000067)
                   return Lat_Long_to_East_North(Lat,Lng)
                }
                ///////////////////////////////////
                t.ITMtoLTLG = function(East,North) {
                                return East_North_to_Lat_Long(Math.round(East),Math.round(-1*North),6378137.000,6356752.313,219529.584,626907.3899999999,1.0000067,31.73439361,35.20451694)
                };
                ///////////////////////////////////
                t.MercatorToITM = function(East,North) {
                    East  = East / 100000
                                North = North / -100000
                   // Convert from Mercator to LatLon
                                North = (2*Math.atan(Math.exp(North*Math.PI/180))-Math.PI/2)*180/Math.PI;
                //            North = Math.round(North * -100000)
                   ////////////////////////////////////
                   SetTransMercParams(31.734393, 35.204516, 219529.584, 626907.390, 1.0000067)
                   return Lat_Long_to_East_North(North,East)
    };
                ///////////////////////////////////////
                t.ITMToMercator = function(East,North) {
                   pos = East_North_to_Lat_Long(Math.round(East),Math.round(-1*North),6378137.000,6356752.313,219529.584,626907.3899999999,1.0000067,31.73439361,35.20451694)
                   d = Math.min(0.99999999, Math.max(-0.99999999, Math.sin((pos.vertPos)*Math.PI/180)))
                   pos.vertPos = (0.5*Math.log((1+d)/(1-d))*180/Math.PI)
                   pos.vertPos = pos.vertPos * 100000
                   pos.horzPos = pos.horzPos * 100000
                   return pos
    };
                ///////////////////////////////////////
}

gIStools = new GIStools();
//point = gIStools.MercatorToITM(3555807,-3353848)
//point = gIStools.ITMToMercator(253021,-631189)
