<!--


//======================================================================
// November 2005 changes, ver 4.01
// added numonly and numlet functions to stop entry of invalid characters
// ======================================================================
var deg2rad = Math.PI / 180;
var rad2deg = 180.0 / Math.PI;
var pi = Math.PI;
var locres = 6; //locator characters
domain = 'cix.co.uk';
user = 'hzk';
var locc = 6;
onload = function()
{
document.myform.reset();
document.myform.loc1.focus();
document.myform2.reset();
// document.myform3.reset();
}
//-------------------------------------------------------------------
// this function allows only numbers in fields
function numonly(ev)
{
  var key;
  var keychar;
  if (window.event)
    key = window.event.keycode;
  else if (ev)
    key = ev.which;
  else return true;
  keychar = String.fromCharCode(key);
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
    return true;
  else if (((".0123456789").indexOf(keychar) > -1))
    return true;
  else
    return false;
}
//-------------------------------------------------------------------
// this function allows numbers, letters, spaces in fields
function numlet(ev)
{
  var key;
  var keychar;
  if (window.event)
    key = window.event.keycode;
  else if (ev)
    key = ev.which;
  else return true;
  keychar = String.fromCharCode(key);
  keychar = keychar.toLowerCase();
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
    return true;
  else if ((("abcdefghijklmnopqrstuvwxyz .0123456789").indexOf(keychar) > -1))
    return true;
  else
    return false;
}
//-------------------------------------------------------------------
function find_dist()
{
var loc1 = document.myform.loc1.value.toUpperCase();
var loc2 = document.myform.loc2.value.toUpperCase();
var loc1x = loc1;
var loc2x = loc2;
var valid = 1;  //1=valid 0=invalid

loc1 = expand_loc(loc1);
loc2 = expand_loc(loc2);
if (validate2(loc1, loc2) == 0)
  {
  alert("Locator format incorrect");
  return;
  }

document.myform.reset();
document.myform.loc1.value = loc1x;
document.myform.loc2.value = loc2x;

geo1 = conv_loc_to_deg(loc1);
geo2 = conv_loc_to_deg(loc2);

lon1 = geo1.longitude * deg2rad;
lat1 = geo1.latitude * deg2rad;

lon2 = geo2.longitude * deg2rad;
lat2 = geo2.latitude * deg2rad;

calc_gc(lat1, -lon1, lat2, -lon2);
calc_rl(lat1, -lon1, lat2, -lon2);
}

//-------------------------------------------------------------------
function calc_gc(lat1, lon1, lat2, lon2)
{
var d = acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon1 - lon2));
var gc_d = round((rad2deg * d) * 60 * 10) / 10;
var gc_dm = round(1.852 * gc_d * 10) / 10;
var gc_ds = round(1.150779 * gc_d * 10) / 10;

if (sin(lon2 - lon1) < 0)
  tc = acos((sin(lat2) - sin(lat1) * cos(d)) / (sin(d) * cos(lat1)));
else if (lon2 - lon1 == 0)
  if (lat2 < lat1)
    tc = deg2rad * 180;
  else
    tc = 0; 
else
  tc = 2 * pi - acos((sin(lat2) - sin(lat1) * cos(d)) / (sin(d) * cos(lat1)));

gc_tc = round(tc * rad2deg * 10) / 10;

document.myform.gc_deg.value = gc_tc;
document.myform.gc_km.value = gc_dm; 
document.myform.gc_mi.value = gc_d;
//document.myform.gc_sm.value = gc_ds;
}
//-------------------------------------------------------------------
function calc_rl(lat1, lon1, lat2, lon2)
{
var dlon_w = mod(lon2 - lon1, pi * 2);
var dlon_e = mod(lon1 - lon2, pi * 2);

var dphi = ln(tan(lat2 / 2 + pi / 4) / tan(lat1 / 2 + pi / 4));

if (abs(lat2 - lat1) < sqrt(1e-15))
  var q = cos(lat1);
else
  var q = (lat2 - lat1) / dphi;

if (dlon_w < dlon_e)
  {
  var tc = mod(atan2(-dlon_w, dphi), 2.0 * pi);
  var d = sqrt(q * q * dlon_w * dlon_w + (lat2 - lat1) * (lat2 - lat1));
  }
else
  {
  var tc = mod(atan2(dlon_e, dphi), 2.0 * pi);
  var d = sqrt(q * q * dlon_e * dlon_e + (lat2 - lat1) * (lat2 - lat1));
  }

var rl_d = round(rad2deg * d * 60 * 10) / 10;
var rl_dm = round(1.852 * rl_d * 10) / 10;
var rl_ds = round(1.150779 * rl_d * 10) / 10;
var rl_tc = round(rad2deg * tc * 10) / 10;

//document.myform.rl_deg.value = rl_tc;
//document.myform.rl_km.value = rl_dm; 
//document.myform.rl_mi.value = rl_d;
//document.myform.rl_sm.value = rl_ds;
}
//-------------------------------------------------------------------
function expand_loc(original)
{
 var expanded = original;
 if (original.length == 6)
 	expanded = original + "55AA";
 if (original.length == 8)
	expanded = original + "LL";
 return(expanded);
}
//-------------------------------------------------------------------
function validate2(first, second)
{
 if ((first.length != 10) || (second.length != 10))
  return(0);
  
 // check locator format
 var myRegExp = /[A-R]{2}[0-9]{2}[A-X]{2}[0-9]{2}[A-X]{2}/;
 if ((! myRegExp.test(first)) || (! myRegExp.test(second)))
 return(0);
}
//-------------------------------------------------------------------
function conv_loc_to_deg(locator)
{
 var i = 0;
 var loca = new Array();

 while (i < 10)
  {
  loca[i] = locator.charCodeAt(i) - 65;
  i++;
  }
 loca[2] += 17;
 loca[3] += 17;
 loca[6] += 17;
 loca[7] += 17;
 var lon = (loca[0] * 20 + loca[2] * 2 + loca[4] / 12 + loca[6] / 120 + loca[8] / 2880 - 180);
 var lat = (loca[1] * 10 + loca[3] + loca[5] / 24 + loca[7] / 240 + loca[9] /5760 - 90);
 var geo = { latitude: lat, longitude: lon };
 return(geo);
}
// End of myform
//===================================================================
//functions for myform2
//===================================================================
function val_ll()
{
  var latd = document.myform2.latd.value;
  var latm = document.myform2.latm.value;
  var lats = document.myform2.lats.value;
  var latb = document.myform2.latb.selectedIndex;
  var lond = document.myform2.lond.value;
  var lonm = document.myform2.lonm.value;
  var lons = document.myform2.lons.value;
  var lonb = document.myform2.lonb.selectedIndex;
  locres = 6; //document.myform2.locc.selectedIndex; 

  // cope with blank fields
  if (latd == "")
  {
    alert("Latitude degrees must be entered");
    return;
  }

  if (latm == "")
    latm = "0";  document.myform2.latm.value = latm;
  if (lats == "")
    lats = "0";  document.myform2.lats.value = lats;
  if (latb == 0)
    latb = "1";  document.myform2.latb.selectedIndex = latb;  // 1 - это - default North
  if (lond == "")
    lond = "0";  document.myform2.lond.value = lond;
  if (lonm == "")
    lonm = "0";  document.myform2.lonm.value = lonm;
  if (lons == "")
    lons = "0";  document.myform2.lons.value = lons;
  if (lonb == 0)
    lonb = "2";  document.myform2.lonb.selectedIndex = lonb;  // 2 - это - default East

  //validate
  if (abs(Number(latd)) >= 90)
  {
    alert("Degrees wrong");
    return;
  }
  if (Number(latm) >= 60)
  {
    alert("Minutes wrong");
    return;
  }
  if (Number(lats) >= 60)
  {
    alert("Seconds wrong");
    return;
  }
  if (abs(Number(lond)) >= 180)
  {
    alert("Degrees wrong");
    return;
  }
  if (Number(lonm) >= 60)
  {
    alert("Minutes wrong");
    return;
  }
  if (Number(lons) >= 60)
  {
    alert("Seconds wrong");
    return;
  }

  var lat = Number(latd);
  lat = lat + Number(latm) / 60;
  lat = lat + Number(lats) / 3600;
  if (latb == 2)  // S
    lat = lat * -1;
  var lon = Number(lond);
  lon = lon + Number(lonm) / 60;
  lon = lon + Number(lons) / 3600;
  if (lonb == 1)  // W
    lon = lon * -1;

  //document.myform2.ngr.value = "";
  //document.myform2.grid.selectedIndex = 0;
  //document.myform2.northings.value = "";
  //document.myform2.eastings.value = "";
  document.myform2.locator.value = 6;
  document.myform2.latb.selectedIndex = latb;
  document.myform2.lonb.selectedIndex = lonb;
  var loc = calcloc(lon, lat);  //wgs84
  var locy = loc.substring(0, 6 + 2 * locres);
  document.myform2.locator.value = locy;	
  var grid = choose_where(lat, lon);
  var phip = lat * deg2rad;  // deg to rad
  var lambdap = lon * deg2rad;
  if (grid == "")
    return;
  var geo = convert_to_local(grid, phip, lambdap);
  phip = geo.latitude;
  lambdap = geo.longitude;
  ll2ne(phip, lambdap, grid);
}
//===================================================================
function transform(lat, lon, a, e, h, a2, e2, xp, yp, zp, xr, yr, zr, s)
{
  // convert to cartesian; lat, lon are radians
  sf = s * 0.000001;
  v = a / (sqrt(1 - (e *(sin(lat) * sin(lat)))));
  x = (v + h) * cos(lat) * cos(lon);
  y = (v + h) * cos(lat) * sin(lon);
  z = ((1 - e) * v + h) * sin(lat);

  xrot = (xr / 3600) * deg2rad;
  yrot = (yr / 3600) * deg2rad;
  zrot = (zr / 3600) * deg2rad;
  
  hx = x + (x * sf) - (y * zrot) + (z * yrot) + xp;
  hy = (x * zrot) + y + (y * sf) - (z * xrot) + yp;
  hz = (-1 * x * yrot) + (y * xrot) + z + (z * sf) + zp;

  // Convert back to lat, lon
  lon = atan(hy / hx);
  p = sqrt((hx * hx) + (hy * hy));
  lat = atan(hz / (p * (1 - e2)));
  v = a2 / (sqrt(1 - e2 * (sin(lat) * sin(lat))));
  errvalue = 1.0;
  lat0 = 0;
  while (errvalue > 0.001)
  {
    lat0 = atan((hz + e2 * v * sin(lat)) / p);  
    errvalue = abs(lat0 - lat);
    lat = lat0;
  }
  h = p / cos(lat) - v;
  var geo = { latitude: lat, longitude: lon };
  return(geo);
}
//===================================================================

//===================================================================
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))) * (sin(phi - phi0)) * (cos(phi + phi0)))
    + ((((15 / 8) * (n * n)) + ((15 / 8) * (n * n * n))) * (sin(2 * (phi - phi0))) * (cos(2 * (phi + phi0))))
    - (((35 / 24) * (n * n * n)) * (sin(3 * (phi - phi0))) * (cos(3 * (phi + phi0)))));
  return(Marc);
}
//=================================================================== 
function InitialLat(north, n0, af0, phi0, n, bf0)
{
  var phi1 = ((north - n0) / af0) + phi0;
  var M = Marc(bf0, n, phi0, phi1);
  var phi2 = ((north - n0 - M) / af0) + phi1;
  var ind = 0;
  while ((abs(north - n0 - M) > 0.00001) && (ind < 20))  // max 20 iterations in case of error
  {  
	ind = ind + 1;
	phi2 = ((north - n0 - M) / af0) + phi1;
    M = Marc(bf0, n, phi0, phi2);
    phi1 = phi2;
  }
  return(phi2);  
}
//===================================================================
function choose_where(lat, lon)
{
var where = "";
if ((lat >= 49) && (lat <= 50) && (lon >= -3) && (lon <= -2))
  where = "Channel Islands"
else if ((lon <= -6) && (lon > -11) && (lat > 51) && (lat <= 54))
  where = "Irish";
else if ((lon < -5.333) && (lon >- 11) && (lat >= 54) && (lat <= 55))
  where = "Irish";
else if ((lon < -5.9) && (lon >= -9) && (lat >= 55) && (lat < 55.5))
  where = "Irish";
else if ((lon < 1.8) && (lon > -9) && (lat > 49.8166) && (lat < 61))
  where = "British";
return where;
}
//===================================================================
function calcloc(e, n)
// calculate IARU locator from lat, lon
// includes correction to hopefully avoid js fp maths error.
// input is in degrees.
{
	e = e + 180;
	ee = e;
	n = n + 90;
	nn = n;
	var locator = "";
	e = e / 20 + 0.0000001;
	n = n / 10 + 0.0000001;
	locator = locator + chr(65 + e) + chr(65 + n);
	e = e - floor(e);
	n = n - floor(n);
	e = e * 10;
	n = n * 10;
	locator = locator + chr(48 + e) + chr(48 + n);
	e = e - floor(e);
	n = n - floor(n);
	e = e * 24;
	n = n * 24;
	locator = locator + chr(65 + e) + chr(65 + n);
	e = e - floor(e);
	n = n - floor(n);
	e = e * 10;
	n = n * 10;
	locator = locator + chr(48 + e) + chr(48 + n);
	e = e - floor(e);
	n = n - floor(n);
	e = e * 24;
	n = n * 24;
	locator = locator + chr(65 + e) + chr(65 + n);
	return locator;
}
//===================================================================
function conv_loc_to_ll()
{
  var i = 0;
  var locator = document.myform2.locator.value.toUpperCase();
  var locx = locator;
  var loca = new Array();
  var locc = 6; //This is my adding ***************************************************
  
  if (locator.length == 6)
	locator = locator + "55AA";
  if (locator.length == 8)
	locator = locator + "LL";
  if (locator.length != 10)
  {
    alert("Locator format incorrect");
    return;
  }

  // check locator format
  var myRegExp = /[A-R]{2}[0-9]{2}[A-X]{2}[0-9]{2}[A-X]{2}/;
  if (! myRegExp.test(locator))
  {
    alert("Locator format incorrect")
    return;
  }
  locres = 6; //document.myform2.locc.selectedIndex;
  document.myform2.reset();
  document.myform2.locator.value = locx;
  //document.myform2.locc.selectedIndex = 6 //locres;
  
  while (i < 10)
  {
    loca[i] = locator.charCodeAt(i) - 65;
    i++;
  }
  loca[2] += 17;
  loca[3] += 17;
  loca[6] += 17;
  loca[7] += 17;
  var lon = (loca[0] * 20 + loca[2] * 2 + loca[4] / 12 + loca[6] / 120 + loca[8] / 2880 - 180);
  var lat = (loca[1] * 10 + loca[3] + loca[5] / 24 + loca[7] / 240 + loca[9] /5760 - 90);
  todms(lat, lon);
  var grid = choose_where(lat, lon);
  var phip = lat * deg2rad;  // deg to rad
  var lambdap = lon * deg2rad;
  var geo = convert_to_local(grid, phip, lambdap);
  //phip = geo.latitude;
  //lambdap = geo.longitude;
  //ll2ne(phip, lambdap, grid);
//make_ngr(lat, lon);
}
//===================================================================
function todms(lat, lon)
// convert decimal degrees to dms
{
  var latbrg = 1;
  var lonbrg = 2;
  if (lat < 0)
    latbrg = 2
  if (lon < 0)
    lonbrg = 1;
  var tlat = abs(lat);
  var tlon = abs(lon);
  var deglat = floor(tlat);
  var t = (tlat - deglat) * 60;
  var minlat = floor(t);
  var seclat = (t - minlat) * 60;
  seclat = seclat + 0.005;  //round to .005 sec
  seclat = floor(seclat * 100) / 100;  //  works in js 1.4
  var deglon = floor(tlon);
  t = (tlon - deglon) * 60;
  var minlon = floor(t);
  var seclon = (t - minlon) * 60;
  seclon = seclon + 0.005;
  seclon = floor(seclon * 100) /100;  // js 1.4
  document.myform2.latd.value = deglat;
  document.myform2.latm.value = minlat;
  document.myform2.lats.value = seclat;
  document.myform2.latb.selectedIndex = latbrg;
  document.myform2.lond.value = deglon;
  document.myform2.lonm.value = minlon;
  document.myform2.lons.value = seclon;
  document.myform2.lonb.selectedIndex = lonbrg; 
}
//===================================================================
function convert_to_local(grid, phip, lambdap)
{
  var WGS84_AXIS = 6378137;
  var WGS84_ECCENTRIC = 0.00669438037928458;
  var OSGB_AXIS = 6377563.396;
  var OSGB_ECCENTRIC = 0.0066705397616;
  var IRISH_AXIS = 6377340.189;
  var IRISH_ECCENTRIC = 0.00667054015;
  var INT24_AXIS = 6378388.000;
  var INT24_ECCENTRIC = 0.0067226700223333;
  var height = 10;  // dummy height
  if (grid == "British")
  {
    var geo = transform(phip, lambdap, WGS84_AXIS, WGS84_ECCENTRIC, height, OSGB_AXIS, OSGB_ECCENTRIC, -446.448, 125.157, -542.06, -0.1502, -0.247, -0.8421, 20.4894);
  }
  if (grid == "Irish")
  {
    var geo = transform(phip, lambdap, WGS84_AXIS, WGS84_ECCENTRIC, height, IRISH_AXIS, IRISH_ECCENTRIC, -482.53, 130.596, -564.557, 1.042, 0.214, 0.631, -8.15);
  }
  if (grid == "Channel Islands")
  {  
    var geo = transform(phip, lambdap, WGS84_AXIS, WGS84_ECCENTRIC, height,  INT24_AXIS, INT24_ECCENTRIC, 83.901, 98.127, 118.635, 0, 0, 0, 0);
  } 
  return(geo);
}
//===================================================================
function convert_to_wgs(grid, phip, lambdap)
{
  var WGS84_AXIS = 6378137;
  var WGS84_ECCENTRIC = 0.00669438037928458;
  var OSGB_AXIS = 6377563.396;
  var OSGB_ECCENTRIC = 0.0066705397616;
  var IRISH_AXIS = 6377340.189;
  var IRISH_ECCENTRIC = 0.00667054015;
  var INT24_AXIS = 6378388.000;
  var INT24_ECCENTRIC = 0.0067226700223333;
  var height = 10;  // dummy height
  if (grid == 1)
  {
    var geo = transform(phip, lambdap, OSGB_AXIS, OSGB_ECCENTRIC, height, WGS84_AXIS, WGS84_ECCENTRIC, 446.448, -125.157, 542.06, 0.1502, 0.247, 0.8421, 20.4894);
  }
  if (grid == 2)
  {
    var geo = transform(phip, lambdap, IRISH_AXIS, IRISH_ECCENTRIC, height, WGS84_AXIS, WGS84_ECCENTRIC, 482.53, -130.596, 564.557, -1.042, -0.214, -0.631, -8.15);
  }
  if (grid == 3)
  {  
    var geo = transform(phip, lambdap, INT24_AXIS, INT24_ECCENTRIC, height,  WGS84_AXIS, WGS84_ECCENTRIC, -83.901, -98.127, -118.635, 0, 0, 0, 0);
  } 
  return(geo);
}
//===================================================================

//===================================================================

//===================================================================

//===================================================================

//end of myform2
//-------------------------------------------------------------------
//functions for myform3

//===================================================================
function geo_constants(ellipsoid)
{
// returns ellipsoid values
ellipsoid_axis = new Array();
ellipsoid_eccen = new Array();
ellipsoid_axis[0] = 6377563.396; ellipsoid_eccen[0] = 0.00667054;  //airy
ellipsoid_axis[1] = 6377340.189; ellipsoid_eccen[1] = 0.00667054;  // mod airy
ellipsoid_axis[2] = 6378160; ellipsoid_eccen[2] = 0.006694542;  //aust national
ellipsoid_axis[3] = 6377397.155; ellipsoid_eccen[3] = 0.006674372;  //bessel 1841
ellipsoid_axis[4] = 6378206.4; ellipsoid_eccen[4] = 0.006768658;  //clarke 1866
ellipsoid_axis[5] = 6378249.145; ellipsoid_eccen[5] = 0.006803511;  //clarke 1880
ellipsoid_axis[6] = 6377276.345; ellipsoid_eccen[6] = 0.00637847;  //everest
ellipsoid_axis[7] = 6377304.063; ellipsoid_eccen[7] = 0.006637847;  // mod everest
ellipsoid_axis[8] = 6378166; ellipsoid_eccen[8] = 0.006693422;  //fischer 1960
ellipsoid_axis[9] = 6378150; ellipsoid_eccen[9] = 0.006693422;  //fischer 1968
ellipsoid_axis[10] = 6378155; ellipsoid_eccen[10] = 0.006693422;  // mod fischer
ellipsoid_axis[11] = 6378160; ellipsoid_eccen[11] = 0.006694605;  //grs 1967
ellipsoid_axis[12] = 6378137; ellipsoid_eccen[12] = 0.00669438;  //  grs 1980
ellipsoid_axis[13] = 6378200; ellipsoid_eccen[13] = 0.006693422;  // helmert 1906
ellipsoid_axis[14] = 6378270; ellipsoid_eccen[14] = 0.006693422;  // hough
ellipsoid_axis[15] = 6378388; ellipsoid_eccen[15] = 0.00672267;  // int24
ellipsoid_axis[16] = 6378245; ellipsoid_eccen[16] = 0.006693422;  // krassovsky
ellipsoid_axis[17] = 6378160; ellipsoid_eccen[17] = 0.006694542;  // s america
ellipsoid_axis[18] = 6378165; ellipsoid_eccen[18] = 0.006693422;  // wgs-60
ellipsoid_axis[19] = 6378145; ellipsoid_eccen[19] = 0.006694542;  // wgs-66
ellipsoid_axis[20] = 6378135; ellipsoid_eccen[20] = 0.006694318;  // wgs-72
ellipsoid_axis[21] = 6378137; ellipsoid_eccen[21] = 0.00669438;  //wgs-84

// return values as an object
var ellipsoid = { axis: ellipsoid_axis[ellipsoid], 
                  eccentricity: ellipsoid_eccen[ellipsoid] };
return ellipsoid;
}

//===================================================================
function val_ll3()
{
// get ellipsoid
var d = document.myform3.ellipsoid.selectedIndex;
var ellipsoid = geo_constants(d);

if (ellipsoid.axis == 0)
  {
  alert("Ellipsoid must be entered");
  return;
  }
var axis = ellipsoid.axis;
var eccent = ellipsoid.eccentricity;

var latd = document.myform3.latd.value;
var latm = document.myform3.latm.value;
var lats = document.myform3.lats.value;
var latb = document.myform3.latb.selectedIndex;
var lond = document.myform3.lond.value;
var lonm = document.myform3.lonm.value;
var lons = document.myform3.lons.value;
var lonb = document.myform3.lonb.selectedIndex;
locres = document.myform3.locc.selectedIndex;

// cope with blank fields
if (latd == "")
  {
  alert("Latitude degrees must be entered");
  return;
  }

if (latm == "")
  latm = "0";  
if (lats == "")
  lats = "0";
if (latb == 0)
  latb = "1";
if (lond == "")
  lond = "0";
if (lonm == "")
  lonm = "0";
if (lons == "")
  lons = "0";
if (lonb == 0)
  lonb = 1;

//validate
//validate
var valid = validate_dms(latd, latm, lats, latb, lonm, lond, lons, lonb);
if (valid == 0)
{
 alert("Invalid degrees, minutes or seconds");
 return;
}

// clear form and show entered values
document.myform3.reset();
document.myform3.ellipsoid.selectedIndex = d;
document.myform3.latd.value = latd;
document.myform3.latm.value = latm;
document.myform3.lats.value = lats;
document.myform3.lond.value = lond;
document.myform3.lonm.value = lonm;
document.myform3.lons.value = lons;
document.myform3.latb.selectedIndex = latb;  // default N
document.myform3.lonb.selectedIndex = lonb;  // default W
document.myform3.locc.selectedIndex = locres;

var lat = Number(latd);
lat = lat + Number(latm) / 60;
lat = lat + Number(lats) / 3600;
if (latb == 2)  // S
  lat = lat * -1;
var lon = Number(lond);
lon = lon + Number(lonm) / 60;
lon = lon + Number(lons) / 3600;
if (lonb == 1)  // W
  lon = lon * -1;

if (lat >= 84 || lat <= -80)
  {
  alert("UTM latitudes should be between 84N and 80S\nCalculation will proceed anyway as locator will be valid.");
  }

calc_utm(lat, lon, axis, eccent);
loc = calcloc(lon, lat)
var locy = loc.substring(0, 6 + 2 * locres);
document.myform3.locator.value = locy;
}

//===================================================================
function calc_utm(lat, lon, axis, eccent)
{
var k0 = 0.9996;
var latrad = lat * deg2rad;
var longrad = lon * deg2rad;
var zonenum = floor((lon + 180) / 6) + 1;
if (lat >= 56.0 && lat < 64.0 && lon >= 3.0 && lon < 12.0 )
  zonenum = 32;
// Special zones for Svalbard
if( lat >= 72.0 && lat < 84.0 ) 
  {
  if (lon >= 0.0  && lon <  9.0 ) zonenum = 31;
  else if ( lon >= 9.0  && lon < 21.0 ) zonenum = 33;
  else if ( lon >= 21.0 && lon < 33.0 ) zonenum = 35;
  else if ( lon >= 33.0 && lon < 42.0 ) zonenum = 37;
 }

var lonorig = (zonenum - 1) * 6 - 180 + 3;  //+3 puts origin in middle of zone
var lonorigrad = lonorig * deg2rad;

//get letter
var letter = get_zoneletter(lat);

var eccPrimeSquared = (eccent) / (1 - eccent);

//calculate
var N = axis / sqrt(1 - eccent * sin(latrad) * sin(latrad));
var T = tan(latrad) * tan(latrad);
var C = eccPrimeSquared * cos(latrad) * cos(latrad);
var A = cos(latrad) * (longrad - lonorigrad);
var M = axis * ((1 - eccent / 4 - 3 * eccent * eccent / 64 - 5 * eccent * eccent * eccent / 256) * latrad - (3 * eccent / 8 + 3 * eccent * eccent / 32 + 45 * eccent * eccent *eccent / 1024) * sin(2 * latrad) + (15 * eccent * eccent / 256 + 45 * eccent * eccent * eccent / 1024) * sin(4 * latrad) - (35 * eccent * eccent * eccent / 3072) * sin(6 * latrad));

var easting = (k0 * N * (A + (1 - T + C) * A * A * A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120) + 500000.0);
var northing = (k0 * (M + N * tan(latrad) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24 + (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720)));
if (lat < 0)
  northing += 10000000.0; //10000000 meter offset for southern hemisphere

// round up
easting = floor(easting);
northing = floor(northing);

// correlate index with letter
var cx ="C01D02E03F04G05H06J07K08L09M10N11P12Q13R14S15T16U17V18W19X20";
var cxi = cx.indexOf(letter,0);
var zl = Number(cx.substr(cxi + 1, 2));

document.myform3.north.value = northing;
document.myform3.east.value = easting;
document.myform3.zone.value = zonenum;
document.myform3.zletter.selectedIndex = zl;
}

//===================================================================

//===================================================================

//===================================================================


//===================================================================
function conv_loc_to_ll3()
{
var locator = document.myform3.locator.value.toUpperCase();
var locx = locator;

locator = expand_loc(locator);

if (validate(locator) == 0)
  {
  alert("Locator format incorrect");
  return;
  }
  
document.myform3.reset();

var ellipsoid = geo_constants(21);  // get data for WGS-84
var axis = ellipsoid.axis;
var eccent = ellipsoid.eccentricity;

document.myform3.locator.value = locx;
document.myform3.ellipsoid.selectedIndex = 21;

geo = conv_loc_to_deg(locator);
var lon = geo.longitude;
var lat = geo.latitude;

todms3(lat, lon);
calc_utm(lat, lon, axis, eccent);
}
// end of myform3 functions

//-------------------------------------------------------------------
function validate_dms(latd, latm, lats, latb, lonm, lond, lons, lonb)
{
 var valid = 1;
 if (abs(Number(latd)) >= 90)
  valid = 0;
 if (Number(latm) >= 60)
  valid = 0;
 if (Number(lats) >= 60)
  valid = 0;
 if (abs(Number(lond)) >= 180)
  valid = 0;
 if (Number(lonm) >= 60)
  valid = 0;
 if (Number(lons) >= 60)
  valid = 0;
 
 return(valid);
}
//-------------------------------------------------------------------

function validate(locator)
{
 if (locator.length != 10)
  return(0);
  
 // check locator format
 var myRegExp = /[A-R]{2}[0-9]{2}[A-X]{2}[0-9]{2}[A-X]{2}/;
 if (! myRegExp.test(locator))
 return(0);
}
//-------------------------------------------------------------------


//===================================================================
function usage2()
{
window.open('usage.htm', 'usage', "resizable=yes, menubar=no, scrollbars=yes, width=600, height=600");
}

//===================================================================
function usage3()
{
window.open('utmusage.htm', 'utmusage', "resizable=yes, menubar=no, scrollbars=yes, width=600, height=600");
}

//===================================================================

//end of myform3
//-------------------------------------------------------------------
// Maths functions

function mod(y, x)
{
if (y >= 0)
  return y - x * floor(y / x);
else
  return y + x * (floor(-y / x) + 1.0);
}

function atan2(y, x)
{
	return Math.atan2(y, x);
}

function sqrt(x)
{
	return Math.sqrt(x);
}

function tan(x)
{
	return Math.tan(x);
}

function sin(x)
{
	return Math.sin(x);
}

function cos(x)
{
	return Math.cos(x);
}

function acos(x)
{
	return Math.acos(x);
}

function floor(x)
{
	return Math.floor(x);
}

function round(x)
{
	return Math.round(x);
}

function ceil(x)
{
	return Math.ceil(x)
}

function ln(x)
{
	return Math.log(x);
}

function abs(x)
{
	return Math.abs(x);
}

function pow(x, y)
{
	return Math.pow(x, y);
}

function atan(x)
{
	return Math.atan(x);
}

function chr(x)
{
return String.fromCharCode(x);
}

function round(x)
{
	return Math.round(x);
}



// -->
