
function Eccentricity(major,minor)
{return(major*major-minor*minor)/(major*major);}
function LLtoNE(lat,lon)
{var deg2rad=Math.PI/180;var rad2deg=180.0/Math.PI;var phi=lat*deg2rad;var lam=lon*deg2rad;a=6377563.396;b=6356256.91;e0=400000;n0=-100000;f0=0.9996012717;e2=0.0066705397616;lam0=-0.034906585039886591;phi0=0.85521133347722145;var af0=a*f0;var bf0=b*f0;var slat2=Math.sin(phi)*Math.sin(phi);var nu=af0/(Math.sqrt(1-(e2*(slat2))));var rho=(nu*(1-e2))/(1-(e2*slat2));var eta2=(nu/rho)-1;var p=lam-lam0;var IV=nu*Math.cos(phi);var clat3=Math.pow(Math.cos(phi),3);var tlat2=Math.tan(phi)*Math.tan(phi);var V=(nu/6)*clat3*((nu/rho)-tlat2);var clat5=Math.pow(Math.cos(phi),5);var tlat4=Math.pow(Math.tan(phi),4);var VI=(nu/120)*clat5*((5-(18*tlat2))+tlat4+(14*eta2)-(58*tlat2*eta2));east=e0+(p*IV)+(Math.pow(p,3)*V)+(Math.pow(p,5)*VI);var n=(af0-bf0)/(af0+bf0);var M=Marc(bf0,n,phi0,phi);var I=M+(n0);var II=(nu/2)*Math.sin(phi)*Math.cos(phi);var III=((nu/24)*Math.sin(phi)*Math.pow(Math.cos(phi),3))*(5-Math.pow(Math.tan(phi),2)+(9*eta2));var IIIA=((nu/720)*Math.sin(phi)*clat5)*(61-(58*tlat2)+tlat4);north=I+((p*p)*II)+(Math.pow(p,4)*III)+(Math.pow(p,6)*IIIA);east=Math.round(east);north=Math.round(north);nstr=String(north);estr=String(east);return new GridRef(estr,nstr);}
function LLtoIrelandNE(lat,lon)
{var deg2rad=Math.PI/180;var rad2deg=180.0/Math.PI;var phi=lat*deg2rad;var lam=lon*deg2rad;a=6377340.189;b=6356034.447;e0=200000;n0=250000;f0=1.000035;e2=0.00667054015;lam0=-0.13962634015954636615389526147909;phi0=0.93375114981696632365417456114141;var af0=a*f0;var bf0=b*f0;var slat2=Math.sin(phi)*Math.sin(phi);var nu=af0/(Math.sqrt(1-(e2*(slat2))));var rho=(nu*(1-e2))/(1-(e2*slat2));var eta2=(nu/rho)-1;var p=lam-lam0;var IV=nu*Math.cos(phi);var clat3=Math.pow(Math.cos(phi),3);var tlat2=Math.tan(phi)*Math.tan(phi);var V=(nu/6)*clat3*((nu/rho)-tlat2);var clat5=Math.pow(Math.cos(phi),5);var tlat4=Math.pow(Math.tan(phi),4);var VI=(nu/120)*clat5*((5-(18*tlat2))+tlat4+(14*eta2)-(58*tlat2*eta2));east=e0+(p*IV)+(Math.pow(p,3)*V)+(Math.pow(p,5)*VI);var n=(af0-bf0)/(af0+bf0);var M=Marc(bf0,n,phi0,phi);var I=M+(n0);var II=(nu/2)*Math.sin(phi)*Math.cos(phi);var III=((nu/24)*Math.sin(phi)*Math.pow(Math.cos(phi),3))*(5-Math.pow(Math.tan(phi),2)+(9*eta2));var IIIA=((nu/720)*Math.sin(phi)*clat5)*(61-(58*tlat2)+tlat4);north=I+((p*p)*II)+(Math.pow(p,4)*III)+(Math.pow(p,6)*IIIA);east=Math.round(east);north=Math.round(north);nstr=String(north);estr=String(east);return new GridRef(estr,nstr);}
function Marc(bf0,n,phi0,phi)
{var Marc=bf0*(((1+n+((5/4)*(n*n))+((5/4)*(n*n*n)))*(phi-phi0))
-(((3*n)+(3*(n*n))+((21/8)*(n*n*n)))*(Math.sin(phi-phi0))*(Math.cos(phi+phi0)))
+((((15/8)*(n*n))+((15/8)*(n*n*n)))*(Math.sin(2*(phi-phi0)))*(Math.cos(2*(phi+phi0))))
-(((35/24)*(n*n*n))*(Math.sin(3*(phi-phi0)))*(Math.cos(3*(phi+phi0)))));return(Marc);}
function GridRef(easting,northing){this.easting=easting;this.northing=northing;}
function LatLng(lat,lng){this.lat=lat;this.lng=lng;}
function NE2NGR(east,north)
{var eX=east/500000;var nX=north/500000;var tmp=Math.floor(eX)-5.0*Math.floor(nX)+17.0;nX=5*(nX-Math.floor(nX));eX=20-5.0*Math.floor(nX)+Math.floor(5.0*(eX-Math.floor(eX)));if(eX>7.5)
eX=eX+1;if(tmp>7.5)
tmp=tmp+1;var eing=String(east);var ning=String(north);var lnth=eing.length;eing=eing.substring(lnth-5,lnth);lnth=ning.length;ning=ning.substring(lnth-5,lnth);ngr=String.fromCharCode(tmp+65)+String.fromCharCode(eX+65)+" "+eing+" "+ning;return ngr;}
function OSRefToLatLng(easting,northing){var airy1830=new Eccentricity(6377563.396,6356256.909);var OSGB_F0=0.9996012717;var N0=-100000.0;var E0=400000.0;var phi0=deg2rad(49.0);var lambda0=deg2rad(-2.0);var a=airy1830.maj;var b=airy1830.min;var eSquared=airy1830.ecc;var phi=0.0;var lambda=0.0;var E=Number(easting);var N=Number(northing);var n=(a-b)/(a+b);var M=0.0;var phiPrime=((N-N0)/(a*OSGB_F0))+phi0;do{M=(b*OSGB_F0)*(((1+n+((5.0/4.0)*n*n)+((5.0/4.0)*n*n*n))*(phiPrime-phi0))
-(((3*n)+(3*n*n)+((21.0/8.0)*n*n*n))*Math.sin(phiPrime-phi0)*Math.cos(phiPrime+phi0))
+((((15.0/8.0)*n*n)+((15.0/8.0)*n*n*n))*Math.sin(2.0*(phiPrime-phi0))*Math.cos(2.0*(phiPrime+phi0)))
-(((35.0/24.0)*n*n*n)*Math.sin(3.0*(phiPrime-phi0))*Math.cos(3.0*(phiPrime+phi0))));phiPrime+=(N-N0-M)/(a*OSGB_F0);}while((N-N0-M)>=0.001);var v=a*OSGB_F0*Math.pow(1.0-eSquared*sinSquared(phiPrime),-0.5);var rho=a*OSGB_F0*(1.0-eSquared)*Math.pow(1.0-eSquared*sinSquared(phiPrime),-1.5);var etaSquared=(v/rho)-1.0;var VII=Math.tan(phiPrime)/(2*rho*v);var VIII=(Math.tan(phiPrime)/(24.0*rho*Math.pow(v,3.0)))*(5.0
+(3.0*tanSquared(phiPrime))
+etaSquared
-(9.0*tanSquared(phiPrime)*etaSquared));var IX=(Math.tan(phiPrime)/(720.0*rho*Math.pow(v,5.0)))*(61.0
+(90.0*tanSquared(phiPrime))
+(45.0*tanSquared(phiPrime)*tanSquared(phiPrime)));var X=sec(phiPrime)/v;var XI=(sec(phiPrime)/(6.0*v*v*v))*((v/rho)+(2*tanSquared(phiPrime)));var XII=(sec(phiPrime)/(120.0*Math.pow(v,5.0)))*(5.0
+(28.0*tanSquared(phiPrime))
+(24.0*tanSquared(phiPrime)*tanSquared(phiPrime)));var XIIA=(sec(phiPrime)/(5040.0*Math.pow(v,7.0)))*(61.0
+(662.0*tanSquared(phiPrime))
+(1320.0*tanSquared(phiPrime)*tanSquared(phiPrime))
+(720.0*tanSquared(phiPrime)*tanSquared(phiPrime)*tanSquared(phiPrime)));phi=phiPrime
-(VII*Math.pow(E-E0,2.0))
+(VIII*Math.pow(E-E0,4.0))
-(IX*Math.pow(E-E0,6.0));lambda=lambda0
+(X*(E-E0))
-(XI*Math.pow(E-E0,3.0))
+(XII*Math.pow(E-E0,5.0))
-(XIIA*Math.pow(E-E0,7.0));alert(phi);return new LatLng(rad2deg(phi),rad2deg(lambda));}
function deg2rad(x){return x*(Math.PI/180);}
function sinSquared(x){return Math.sin(x)*Math.sin(x);}
function cosSquared(x){return Math.cos(x)*Math.cos(x);}
function tanSquared(x){return Math.tan(x)*Math.tan(x);}
function sec(x){return 1.0/Math.cos(x);}
function deg2rad(x){return x*(Math.PI/180);}
function rad2deg(x){return x*(180/Math.PI);}
function chr(x){var h=x.toString(16);if(h.length==1)
h="0"+h;h="%"+h;return unescape(h);}
function ord(x){var c=x.charAt(0);var i;for(i=0;i<256;++i){var h=i.toString(16);if(h.length==1)
h="0"+h;h="%"+h;h=unescape(h);if(h==c)
break;}
return i;}