// FieldenMaps.info
// Co-ordinate Converter (Web) 'Page Interaction' JavaScript Functions [Great Britain]
// Version 1.2.003
// Partly ported from original Visual Basic code
// Revision Date: 4 May 2009
// Copyright © 2004-2009 Ed Fielden


// AddLoadEvent function by Simon Willison (http://simonwillison.net/)

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


// Display and convert initial position (GB = 55 deg N, 3 deg W)

addLoadEvent(function() {
  initctysel();
  getELID("txtLat").value = "55";
  getELID("txtLon").value = "3";
  getELID("cmbLonD").value = -1;
  cmdGeoD();
});


// Initialise County Selection Box

function initctysel() {
  var selbox=getELID("cmbCty");
    for(i=0; i<=CtyList.length-1; i++)
    {
      var newOpt = new Option(CtyList[i].Name, i);
      selbox.options[i] = newOpt;
    }
}


// Functions for disabling/enabling all input fields

function disableallinput() {
    var x=document.getElementsByTagName("input");
    for (var i=0;i<x.length;i++) { x[i].disabled = true; }
    var y=document.getElementsByTagName("select");
    for (var i=0;i<y.length;i++) { y[i].disabled = true; }
}

function enableallinput() {
    var x=document.getElementsByTagName("input");
    for (var i=0;i<x.length;i++) { x[i].disabled = false; }
    var y=document.getElementsByTagName("select");
    for (var i=0;i<y.length;i++) { y[i].disabled = false; }
}


// Initialise variable memory space

var georesult = new geodesic;
var tempgeoresult = new geodesic;
var latdms = new dmsdata;
var londms = new dmsdata;
var cooresult = new coord;
var tempcooresult = new coord;
var yardresult = new coord;
var refresult = new gridref;
var tempNGSq = new gridref;
var tempWOCGSq = new gridref;
var tempLat = new dmsdata;
var tempLon = new dmsdata;
var ctyresult = null;

var merid = false;
var nnnnn = 0;


// Initialise colours for text boxes

var txt_derived = '#0033FF';
var txt_altered = '#FF0033';
var txt_origin = '#000033';


// Input retrieval functions

function getELID(strelid) { return document.getElementById(strelid); }

function getDMS(strlat,strlon,strcmb) {
   var LatDref = getELID(strlat+"D"); var LatMref = getELID(strlat+"M"); var LatSref = getELID(strlat+"S");
   var LonDref = getELID(strlon+"D"); var LonMref = getELID(strlon+"M"); var LonSref = getELID(strlon+"S");
   var inlatD = RoundDec(stringmask(LatDref.value, numerics), 0);
   var inlatM = RoundDec(stringmask(LatMref.value, numerics), 0);
   var inlatS = RoundDec(stringmask(LatSref.value, numerics_d), 4);
   var inlonD = RoundDec(stringmask(LonDref.value, numerics), 0);
   var inlonM = RoundDec(stringmask(LonMref.value, numerics), 0);
   var inlonS = RoundDec(stringmask(LonSref.value, numerics_d), 4);
   var wore = getELID(strcmb).value;
   if (!isvalidgeodms(inlatD,inlatM,inlatS,inlonD,inlonM,inlonS,wore)) { alert('Invalid Geodetic co-ordinate entry'); return false; }
     else { LatDref.value = inlatD; LatMref.value = inlatM; LatSref.value = inlatS;
            LonDref.value = inlonD; LonMref.value = inlonM; LonSref.value = inlonS; }
   var getDMSresult = new geodesic(deg2rad(inlatD+(inlatM/60)+(inlatS/3600)),deg2rad((inlonD+(inlonM/60)+(inlonS/3600))*wore));
   return getDMSresult;
}


function getD(strlat,strlon,strcmb) {
   var inlata = RoundDec(stringmask(getELID(strlat).value, numerics_d), 8);
   var inlona = RoundDec(stringmask(getELID(strlon).value, numerics_d), 8);
   var newlat = getELID(strlat); var newlon = getELID(strlon);
   var wore = getELID(strcmb).value;
   if (!isvalidgeo(inlata,inlona,wore)) { alert('Invalid Geodetic co-ordinate entry'); return false; }
     else { newlat.value = inlata; newlon.value = smallnum2string(inlona); }
   var getDresult = new geodesic(deg2rad(inlata),deg2rad(inlona*wore));
   return getDresult;
}

function getcoord(strest,strnth,proj) {
   var east = RoundDec(stringmask(getELID(strest).value, numerics_d), 3);
   var north = RoundDec(stringmask(getELID(strnth).value, numerics_d), 3);
   var objrefe = getELID(strest); var objrefn = getELID(strnth);
   if (!isvalidcoord(east,north,proj)) { alert('Invalid Co-ordinate entry'); return false; }
     else { objrefe.value = east; objrefn.value = north;}
   var getcoordresult = new coord(east,north);
   return getcoordresult;
}

function getref(strsqr,strsqe,strsqn,proj,sqrary) {
   var objsq = getELID(strsqr); var objsqe = getELID(strsqe); var objsqn = getELID(strsqn);
   var insq = objsq.value;
   if (sqrary == OSNG_GS) { insq = stringmask(insq, alphabetics); insq = insq.toUpperCase(); }
   if (sqrary == OSNG_NS) { insq = stringmask(insq, alphanumerics); insq = insq.toUpperCase(); }
   if (sqrary == WOCG_GS) { insq = stringmask(insq, alphabetics); var insqL = insq.toLowerCase(); var insqU = insq.toUpperCase(); if (insq.length == 2) { insq = insqL.substring(0,1) + insqU.substring(1); } }
   var inge = fivefig(stringmask(objsqe.value, numerics)); var ingn = fivefig(stringmask(objsqn.value, numerics));
   if (!isvalidgridref(insq,inge,ingn,proj,sqrary)) { alert('Invalid Grid Reference entry'); return false; }
      else { objsq.value = insq; objsqe.value = inge; objsqn.value = ingn; }
   var getrefresult = new gridref(insq,inge,ingn);
   return getrefresult;
}

function getcoordcmb(strest,strnth,strcbe,strcbn,proj) {
   var obje = getELID(strest); var objn = getELID(strnth);
   var east = RoundDec(stringmask(obje.value, numerics_d), 2); var north = RoundDec(stringmask(objn.value, numerics_d), 2);
   var wore = getELID(strcbe).value; var sorn = getELID(strcbn).value;
   if (!isvalidcoord(east*wore,north*sorn,proj)) { alert('Invalid Co-ordinate entry'); return false; }
     else { obje.value = east; objn.value = north;}
   var getcoordcmbresult = new coord(east*wore,north*sorn);
   return getcoordcmbresult;
}


// Button functions

function cmdGeoDMS() {
  disableallinput;
   var getinput = getDMS("txtLat","txtLon","cmbLon");
   if (getinput == false) { enableallinput; return false; } else { var inlat = getinput.latitude; var inlon = getinput.longitude; }

   colourtext_Geo(txt_origin);

   disp_Geo_from_GeoDMS(inlat, inlon); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(inlat, inlon); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(inlat, inlon); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(inlat, inlon); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(inlat, inlon); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(inlat, inlon); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(inlat, inlon); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(inlat, inlon);

   enableallinput;
   return true;
}


function cmdGeoD() {
   disableallinput;
   
   var getinput = getD("txtLat","txtLon","cmbLonD");
   if (getinput == false) { enableallinput; return false; } else { var inlat = getinput.latitude; var inlon = getinput.longitude; }

   colourtext_GeoD(txt_origin);

   disp_GeoDMS_from_Geo(inlat, inlon); colourtext_Geo(txt_derived);
   
   disp_OSGB_from_Geo(inlat, inlon); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(inlat, inlon); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(inlat, inlon); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(inlat, inlon); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(inlat, inlon); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(inlat, inlon); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(inlat, inlon);

   enableallinput;
   return true;

}


function cmdGPSGeoD() {
   disableallinput;

   var getinput = getD("txtGPSLat","txtGPSLon","cmbGPSLonD");
   if (getinput == false) { enableallinput; return false; } else { var inlat = getinput.latitude; var inlon = getinput.longitude; }

   colourtext_GPSGeoD(txt_origin);

   disp_GPSGeoDMS_from_GPSGeo(inlat, inlon); colourtext_GPSGeo(txt_derived);
   
   disp_UTMWGS_from_GPSGeo(inlat, inlon); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(inlat, inlon); colourtext_EDGeo(txt_derived); colourtext_EDGeoD(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   var newgeoresult = disp_Geo_from_GPSGeo(inlat, inlon); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);
   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}

function cmdGPSGeoDMS() {
  disableallinput;

   var getinput = getDMS("txtGPSLat","txtGPSLon","cmbGPSLon");
   if (getinput == false) { enableallinput; return false; } else { var inlat = getinput.latitude; var inlon = getinput.longitude; }

   colourtext_GPSGeo(txt_origin);

   disp_GPSGeo_from_GPSGeoDMS(inlat, inlon); colourtext_GPSGeoD(txt_derived);

   disp_UTMWGS_from_GPSGeo(inlat, inlon); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(inlat, inlon); colourtext_EDGeo(txt_derived); colourtext_EDGeoD(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   var newgeoresult = disp_Geo_from_GPSGeo(inlat, inlon); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdEDGeoD() {
   disableallinput;

   var getinput = getD("txtEDLat","txtEDLon","cmbEDLonD");
   if (getinput == false) { enableallinput; return false; } else { var inlat = getinput.latitude; var inlon = getinput.longitude; }

   colourtext_EDGeoD(txt_origin);

   disp_EDGeoDMS_from_EDGeo(inlat, inlon); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(inlat, inlon); colourtext_UTMED(txt_derived);

   var gpsgeoresult = disp_GPSGeo_from_EDGeo(inlat, inlon); colourtext_GPSGeo(txt_derived); colourtext_GPSGeoD(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);

   var newgeoresult = disp_Geo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);
   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}

function cmdEDGeoDMS() {
  disableallinput;

   var getinput = getDMS("txtEDLat","txtEDLon","cmbEDLon");
   if (getinput == false) { enableallinput; return false; } else { var inlat = getinput.latitude; var inlon = getinput.longitude; }

   colourtext_EDGeo(txt_origin);

   disp_EDGeo_from_EDGeoDMS(inlat, inlon); colourtext_EDGeoD(txt_derived);
   disp_UTMED_from_EDGeo(inlat, inlon); colourtext_UTMED(txt_derived);

   var gpsgeoresult = disp_GPSGeo_from_EDGeo(inlat, inlon); colourtext_GPSGeo(txt_derived); colourtext_GPSGeoD(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);

   var newgeoresult = disp_Geo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);
   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdOSGB() {
   disableallinput;

   var getinput = getcoord("txtNGE","txtNGN",OSNG);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_OSGB(txt_origin);

   disp_OSGBr_from_OSGB(east, north); colourtext_OSGBr(txt_derived);
   disp_OSGBnr_from_OSGB(east, north); colourtext_OSGBnr(txt_derived);

   var newgeoresult = disp_Geo_from_OSGB(east, north); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdOSGBr() {
   disableallinput;

   var getinput = getref("txtNGSq","txtNGSqE","txtNGSqN",OSNG,OSNG_GS);
   if (getinput == false) { enableallinput; return false; } else { var insq = getinput.square; var inge = getinput.eastings; var ingn = getinput.northings; }

   colourtext_OSGBr(txt_origin);

   cooresult = disp_OSGB_from_OSGBr(insq, inge, ingn); colourtext_OSGB(txt_derived);
   disp_OSGBnr_from_OSGB(cooresult.eastings, cooresult.northings); colourtext_OSGBnr(txt_derived);

   var newgeoresult = disp_Geo_from_OSGB(RoundDec(cooresult.eastings, 3), RoundDec(cooresult.northings, 3)); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdOSGBnr() {
   disableallinput;

   var getinput = getref("txtNGnSq","txtNGnSqE","txtNGnSqN",OSNG,OSNG_NS);
   if (getinput == false) { enableallinput; return false; } else { var insq = getinput.square; var inge = getinput.eastings; var ingn = getinput.northings; }

   colourtext_OSGBnr(txt_origin);

   cooresult = disp_OSGB_from_OSGBnr(insq, inge, ingn); colourtext_OSGB(txt_derived);
   disp_OSGBr_from_OSGB(cooresult.eastings, cooresult.northings); colourtext_OSGBr(txt_derived);

   var newgeoresult = disp_Geo_from_OSGB(RoundDec(cooresult.eastings, 3), RoundDec(cooresult.northings, 3)); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdUTMWGS() {
   disableallinput;

   var utmzonesel = getELID("cmbUTMWGS").value;
   if (utmzonesel == -2) { projtemp = UTM29_WGS; }
   if (utmzonesel == -1) { projtemp = UTM30_WGS; }
   if (utmzonesel == 1) { projtemp = UTM31_WGS; }
   var getinput = getcoord("txtUTMWGSE","txtUTMWGSN",projtemp);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_UTMWGS(txt_origin);

   var gpsgeoresult = disp_GPSGeo_from_UTMWGS(east,north,projtemp); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   if (gpsgeoresult == false) { alert('UTM co-ordinate is out of range'); enableallinput; return false; }
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);
   var newgeoresult = disp_Geo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_GeoD(txt_derived); colourtext_Geo(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdUTMED() {
   disableallinput;

   var utmzonesel = getELID("cmbUTMED").value;
   if (utmzonesel == -2) { projtemp = UTM29_ED; }
   if (utmzonesel == -1) { projtemp = UTM30_ED; }
   if (utmzonesel == 1) { projtemp = UTM31_ED; }
   var getinput = getcoord("txtUTMEDE","txtUTMEDN",projtemp);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_UTMED(txt_origin);
   var edgeoresult = disp_EDGeo_from_UTMED(east,north,projtemp); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   if (edgeoresult == false) { alert('UTM co-ordinate is out of range'); enableallinput; return false; }
   var gpsgeoresult = disp_GPSGeo_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var newgeoresult = disp_Geo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_GeoD(txt_derived); colourtext_Geo(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdOSYG() {
   disableallinput;

   var getinput = getcoord("txtYGE","txtYGN",OSYG);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_OSYG(txt_origin);

   var newgeoresult = disp_Geo_from_OSYG(east, north); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdWOCG() {
   disableallinput;

   var getinput = getcoord("txtWOCGE","txtWOCGN",WOCG);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_WOCG(txt_origin);
   disp_WOCGr_from_WOCG(east, north); colourtext_WOCGr(txt_derived);

   var newgeoresult = disp_Geo_from_WOCG(east, north); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdWOCGr() {
   disableallinput;

   var getinput = getref("txtWOCGSq","txtWOCGSqE","txtWOCGSqN",WOCG,WOCG_GS);
   if (getinput == false) { enableallinput; return false; } else { var insq = getinput.square; var inge = getinput.eastings; var ingn = getinput.northings; }

   colourtext_WOCGr(txt_origin);
   cooresult = disp_WOCG_from_WOCGr(insq, inge, ingn); colourtext_WOCG(txt_derived);

   var newgeoresult = disp_Geo_from_WOCG(RoundDec(cooresult.eastings, 3), RoundDec(cooresult.northings, 3)); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdCDEL() {
   disableallinput;

   var getinput = getcoordcmb("txtCasE","txtCasN","cmbCasE","cmbCasN",CDEL);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_CDEL(txt_origin);

   var newgeoresult = disp_Geo_from_CDEL(east, north); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdBONS() {
   disableallinput;

   var getinput = getcoordcmb("txtBONE","txtBONN","cmbBONE","cmbBONN",BONS);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_BONS(txt_origin);
   var newgeoresult = disp_Geo_from_BONS(east, north); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude);

   enableallinput;
   return true;
}


function cmdCty() {
   disableallinput;

   var ctysel = getELID("cmbCty").selectedIndex;
   var temptemp = new CSgriddata(Airy,1,MeridList[CtyList[ctysel].Meridian].Lat0,MeridList[CtyList[ctysel].Meridian].Lon0,FO1,"ft",0,0,-550000,450000,-700000,1250000);

   var getinput = getcoordcmb("txtCtyE","txtCtyN","cmbCtyE","cmbCtyN",temptemp);
   if (getinput == false) { enableallinput; return false; } else { var east = getinput.eastings; var north = getinput.northings; }

   colourtext_Cty(txt_origin);
   var ctyshtsuccess = disp_CtySht_from_CTY(ctysel, east, north);
   var newgeoresult = disp_Geo_from_CTY(ctysel, east, north); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);

   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);

   if (ctyshtsuccess == true) { disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude, false, false); } else { getELID("ctyres").innerHTML = ""; disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude, false, true); }

   enableallinput;
   return true;
}


function cmdCtyS_25() {
   disableallinput;
   var ctysel = getELID("cmbCty").selectedIndex;
   var ctys_25_s = getELID("txtCtyS_25_S");
   var shtref = ctys_25_s.value; shtref = stringmask(shtref.toUpperCase(), numerics_a); 
   var shtidx = 0; var shtfound = false;
   for (ii=0;ii<CtyList[ctysel].Sheets.length;ii++)
   {
      if (shtref == CtyList[ctysel].Sheets[ii].ShtNum) { shtfound = true; shtidx = ii; }
   }
   if (shtfound == false) { alert('Sheet ' + shtref + ' does not exist in ' + CtyList[ctysel].Name); return false; }
   ctys_25_s.value = shtref;
   var ctys_25_sd = getELID("cmbCtyS_25_S");
   var ctys_25_e = getELID("txtCtyS_25_E");
   var ctys_25_n = getELID("txtCtyS_25_N");
   var newe = RoundDec(stringmask(ctys_25_e.value, numerics_d), 2);
   var newn = RoundDec(stringmask(ctys_25_n.value, numerics_d), 2);
   if (newe >= s25in.width || newn >= s25in.height) { alert('Invalid co-ordinate entry.'); return false; }
   ctys_25_e.value = newe; ctys_25_n.value = newn;

   var tempcoores = disp_CTY_from_CTYS25(ctysel,shtidx,ctys_25_sd.value,newe,newn);

   var newgeoresult = disp_Geo_from_CTY(ctysel,tempcoores.eastings,tempcoores.northings); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);
   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);

   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude, false, false);
   colourtext_CtyS_25(txt_origin); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_6F(txt_derived);
   colourtext_Cty(txt_derived);
 
   enableallinput;
   return true;
}


function cmdCtyS_6Q() {
   disableallinput;
   var ctysel = getELID("cmbCty").selectedIndex;
   var ctys_6q_s = getELID("txtCtyS_6Q_S");
   var shtref = ctys_6q_s.value; shtref = stringmask(shtref.toUpperCase(), numerics_a); 
   var shtidx = 0; var shtfound = false;
   for (ii=0;ii<CtyList[ctysel].Sheets.length;ii++)
   {
      if (shtref == CtyList[ctysel].Sheets[ii].ShtNum) { shtfound = true; shtidx = ii; }
   }
   if (shtfound == false) { alert('Sheet ' + shtref + ' does not exist in ' + CtyList[ctysel].Name); return false; }
   ctys_6q_s.value = shtref;
   var ctys_6q_sd = getELID("cmbCtyS_6Q_S");
   var ctys_6q_e = getELID("txtCtyS_6Q_E");
   var ctys_6q_n = getELID("txtCtyS_6Q_N");
   var newe = RoundDec(stringmask(ctys_6q_e.value, numerics_d), 2);
   var newn = RoundDec(stringmask(ctys_6q_n.value, numerics_d), 2);
   if (newe >= (s6in.width / 2) || newn >= (s6in.height / 2)) { alert('Invalid co-ordinate entry.'); return false; }
   ctys_6q_e.value = newe; ctys_6q_n.value = newn;

   var tempcoores = disp_CTY_from_CTYS6Q(ctysel,shtidx,ctys_6q_sd.value,newe,newn);

   var newgeoresult = disp_Geo_from_CTY(ctysel,tempcoores.eastings,tempcoores.northings); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);
   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);

   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude, false, false);
   colourtext_CtyS_6Q(txt_origin); colourtext_CtyS_6F(txt_derived); colourtext_CtyS_25(txt_derived);
   colourtext_Cty(txt_derived);
 
   enableallinput;
   return true;
}


function cmdCtyS_6F() {
   disableallinput;
   var ctysel = getELID("cmbCty").selectedIndex;
   var ctys_6f_s = getELID("txtCtyS_6F_S");
   var shtref = ctys_6f_s.value; shtref = stringmask(shtref.toUpperCase(), alphanumerics); 
   var shtidx = 0; var shtfound = false;
   for (ii=0;ii<CtyList[ctysel].Sheets.length;ii++)
   {
      if (shtref == CtyList[ctysel].Sheets[ii].ShtNum) { shtfound = true; shtidx = ii; }
   }
   if (shtfound == false) { alert('Sheet ' + shtref + ' does not exist in ' + CtyList[ctysel].Name); return false; }
   ctys_6f_s.value = shtref;
   var ctys_6f_e = getELID("txtCtyS_6F_E");
   var ctys_6f_n = getELID("txtCtyS_6F_N");
   var newe = RoundDec(stringmask(ctys_6f_e.value, numerics_d), 2);
   var newn = RoundDec(stringmask(ctys_6f_n.value, numerics_d), 2);
   if (newe >= s6in.width || newn >= s6in.height) { alert('Invalid co-ordinate entry.'); return false; }
   ctys_6f_e.value = newe; ctys_6f_n.value = newn;

   var tempcoores = disp_CTY_from_CTYS6F(ctysel,shtidx,newe,newn);

   var newgeoresult = disp_Geo_from_CTY(ctysel,tempcoores.eastings,tempcoores.northings); colourtext_Geo(txt_derived); colourtext_GeoD(txt_derived);
   disp_OSGB_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSGB(txt_derived); colourtext_OSGBr(txt_derived); colourtext_OSGBnr(txt_derived);
   var gpsgeoresult = disp_GPSGeo_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_GPSGeoD(txt_derived); colourtext_GPSGeo(txt_derived);
   disp_UTMWGS_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_UTMWGS(txt_derived);
   var edgeoresult = disp_EDGeo_from_GPSGeo(gpsgeoresult.latitude, gpsgeoresult.longitude); colourtext_EDGeoD(txt_derived); colourtext_EDGeo(txt_derived);
   disp_UTMED_from_EDGeo(edgeoresult.latitude, edgeoresult.longitude); colourtext_UTMED(txt_derived);

   disp_OSYG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_OSYG(txt_derived);
   disp_WOCG_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_WOCG(txt_derived); colourtext_WOCGr(txt_derived);
   disp_CDEL_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_CDEL(txt_derived);
   disp_BONS_from_Geo(newgeoresult.latitude, newgeoresult.longitude); colourtext_BONS(txt_derived);

   disp_CTY_from_Geo(newgeoresult.latitude, newgeoresult.longitude, false, false);
   colourtext_CtyS_6F(txt_origin); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_25(txt_derived);
   colourtext_Cty(txt_derived);
 
   enableallinput;
   return true;
}


// Result display functions

function putGeo(latlon) {
   var newlat = getELID("txtLat"); var newlon = getELID("txtLon"); var newlns = getELID("cmbLonD");
   latt = RoundDec(rad2deg(latlon.latitude), 8);
   lonn = RoundDec(rad2deg(latlon.longitude), 8);
   newlat.value = Math.abs(latt);
   if (Math.abs(lonn) < 1)
        { newlon.value = smallnum2string(Math.abs(lonn)) }
   else { newlon.value = Math.abs(lonn).toString(); }
   if (Math.max(lonn, 0) == 0) { newlns.value = -1; } else { newlns.value = 1; }
   disp_GeoDMS_from_Geo(latlon.latitude, latlon.longitude);
      var georesult2 = new geodesic(deg2rad(latt),deg2rad(lonn));
   return georesult2;
}

function putGPSGeo(latlon) {
   var newlat = getELID("txtGPSLat"); var newlon = getELID("txtGPSLon"); var newlns = getELID("cmbGPSLonD");
   latt = RoundDec(rad2deg(latlon.latitude), 8);
   lonn = RoundDec(rad2deg(latlon.longitude), 8);
   newlat.value = Math.abs(latt);
   if (Math.abs(lonn) < 1)
        { newlon.value = smallnum2string(Math.abs(lonn)) }
   else { newlon.value = Math.abs(lonn).toString(); }
   if (Math.max(lonn, 0) == 0) { newlns.value = -1; } else { newlns.value = 1; }
   disp_GPSGeoDMS_from_GPSGeo(latlon.latitude, latlon.longitude);
      var georesult2 = new geodesic(deg2rad(latt),deg2rad(lonn));
   return georesult2;
}

function putEDGeo(latlon) {
   var newlat = getELID("txtEDLat"); var newlon = getELID("txtEDLon"); var newlns = getELID("cmbEDLonD");
   latt = RoundDec(rad2deg(latlon.latitude), 8);
   lonn = RoundDec(rad2deg(latlon.longitude), 8);
   newlat.value = Math.abs(latt);
   if (Math.abs(lonn) < 1)
        { newlon.value = smallnum2string(Math.abs(lonn)) }
   else { newlon.value = Math.abs(lonn).toString(); }
   if (Math.max(lonn, 0) == 0) { newlns.value = -1; } else { newlns.value = 1; }
   disp_EDGeoDMS_from_EDGeo(latlon.latitude, latlon.longitude);
      var georesult2 = new geodesic(deg2rad(latt),deg2rad(lonn));
   return georesult2;
}

function putDMS(latt,lonn,strpfx,strcbp) {
   var lattt = RoundDec(rad2deg(latt), 8); var lonnn = RoundDec(rad2deg(lonn), 8);
   var newlatD = getELID(strpfx+"LatD"); var newlatM = getELID(strpfx+"LatM"); var newlatS = getELID(strpfx+"LatS");
   var newlonD = getELID(strpfx+"LonD"); var newlonM = getELID(strpfx+"LonM"); var newlonS = getELID(strpfx+"LonS");
   var newlonB = getELID(strcbp+"Lon");
   var dmsres = sec2dms(Math.abs(lattt) * 3600);
   newlatD.value = dmsres.degrees; newlatM.value = dmsres.minutes; newlatS.value = dmsres.seconds;
   dmsres = sec2dms(Math.abs(lonnn) * 3600);
   newlonD.value = dmsres.degrees; newlonM.value = dmsres.minutes; newlonS.value = dmsres.seconds;
   if (Math.max(lonnn, 0) == 0) { newlonB.value = -1; } else { newlonB.value = 1; }
}

function putref(reftemp,strpfx) {
   getELID(strpfx+"Sq").value = reftemp.square;
   getELID(strpfx+"SqE").value = reftemp.eastings;
   getELID(strpfx+"SqN").value = reftemp.northings;
}

function putcoord(cootemp,strpfx) {
   getELID(strpfx+"E").value = RoundDec(cootemp.eastings, 3);
   getELID(strpfx+"N").value = RoundDec(cootemp.northings, 3);
}

function blankref(strpfx) {
   getELID(strpfx+"Sq").value='-'; getELID(strpfx+"SqE").value='-'; getELID(strpfx+"SqN").value='-';
}

function blankcrd(strpfx) {
   getELID(strpfx+"N").value='-'; getELID(strpfx+"E").value='-';
}

function putcoord2(cootemp,strpfx,proj) {
   var obje = getELID(strpfx+"E"); var objn = getELID(strpfx+"N");
   var roundeast = RoundDec(cootemp.eastings, 3);
   var roundnorth = RoundDec(cootemp.northings, 3);
   if (roundeast < proj.e_min || roundeast >= proj.e_max || roundnorth < proj.n_min || roundnorth >= proj.n_max) {
       obje.value = "-"; objn.value = "-"; return false;
     } else { obje.value = roundeast; objn.value = roundnorth; return cootemp; }
}

function putcoordcmb(cootemp,strpfx,strcbp,proj) {
   var obje = getELID(strpfx+"E"); var objn = getELID(strpfx+"N");
   var wore = getELID(strcbp+"E"); var sorn = getELID(strcbp+"N");
   var roundeast = RoundDec(cootemp.eastings, 2);
   var roundnorth = RoundDec(cootemp.northings, 2);
   if (roundeast < proj.e_min || roundeast >= proj.e_max || roundnorth < proj.n_min || roundnorth >= proj.n_max) {
       obje.value = "-"; objn.value = "-"; wore.value = 1 ; sorn.value = 1;
      } else {
       obje.value = Math.abs(roundeast); objn.value = Math.abs(roundnorth);
       if (roundeast < 0) { wore.value = -1; } else { wore.value = 1; }
       if (roundnorth < 0) { sorn.value = -1; } else { sorn.value = 1; }
      }
}

function disp_OSGBr_from_OSGB(east, north) {
   refresult = conv_EN2GR(east, north, OSNG_GS);
   putref(refresult,"txtNG");
}

function disp_OSGBnr_from_OSGB(east, north) {
   refresult = conv_EN2GR(east, north, OSNG_NS);
   putref(refresult,"txtNGn");
}

function disp_OSGB_from_OSGBr(insq, inge, ingn) {
   var newcooresult = conv_GR2EN(insq, inge, ingn, OSNG_GS);
   putcoord(newcooresult,"txtNG");
   return newcooresult;
}

function disp_OSGB_from_OSGBnr(insq, inge, ingn) {
   var newcooresult = conv_GR2EN(insq, inge, ingn, OSNG_NS);
   putcoord(newcooresult,"txtNG");
   return newcooresult;
}

function disp_OSGB_from_Geo(inlat, inlon) {
   var cooresult = Geo2TM(inlat, inlon, OSNG);
   var temp2 = putcoord2(cooresult,"txtNG",OSNG);
   if (temp2 == false) { blankref("txtNG"); blankref("txtNGn"); }
      else { disp_OSGBr_from_OSGB(cooresult.eastings, cooresult.northings); disp_OSGBnr_from_OSGB(cooresult.eastings, cooresult.northings); }
   return cooresult;
}

function disp_UTMWGS_from_GPSGeo(latt, lonn) {
   if (lonn < deg2rad(-6)) { projtemp = UTM29_WGS; getELID("cmbUTMWGS").value = -2; }
   if (lonn < deg2rad(0) && lonn >= deg2rad(-6)) { projtemp = UTM30_WGS; getELID("cmbUTMWGS").value = -1; }
   if (lonn >= deg2rad(0)) { projtemp = UTM31_WGS; getELID("cmbUTMWGS").value = 1; }
   var newcooresult = Geo2TM(latt, lonn, projtemp);
   putcoord(newcooresult,"txtUTMWGS");
   return newcooresult;
}

function disp_UTMED_from_EDGeo(latt, lonn) {
   if (lonn < deg2rad(-6)) { projtemp = UTM29_ED; getELID("cmbUTMED").value = -2; }
   if (lonn < deg2rad(0) && lonn >= deg2rad(-6)) { projtemp = UTM30_ED; getELID("cmbUTMED").value = -1; }
   if (lonn >= deg2rad(0)) { projtemp = UTM31_ED; getELID("cmbUTMED").value = 1; }
   var newcooresult = Geo2TM(latt, lonn, projtemp);
   putcoord(newcooresult,"txtUTMED");
   return newcooresult;
}

function disp_WOCG_from_WOCGr(insq, inge, ingn) {
   var newcooresult = conv_GR2EN(insq, inge, ingn, WOCG_GS);
   putcoord(newcooresult,"txtWOCG");
   return newcooresult;
}

function disp_WOCGr_from_WOCG(east, north) {
   refresult = conv_EN2GR(east, north, WOCG_GS);
   putref(refresult,"txtWOCG");
}

function disp_OSYG_from_Geo(inlat, inlon) {
   var cooresult = Geo2TM(inlat, inlon, OSYG);
   var temp2 = putcoord2(cooresult,"txtYG",OSYG);
   return true;
}

function disp_WOCG_from_Geo(inlat, inlon) {
   var cooresult = Geo2CS(inlat, inlon, WOCG);
   var temp2 = putcoord2(cooresult,"txtWOCG",WOCG);
   if (temp2 == false) { blankref("txtWOCG"); }
       else { disp_WOCGr_from_WOCG(cooresult.eastings, cooresult.northings); }
   return true;
}

function disp_CDEL_from_Geo(inlat, inlon) {
   var cooresult = Geo2CS(inlat, inlon, CDEL);
   putcoordcmb(cooresult,"txtCas","cmbCas",CDEL);
   return true;
}

function disp_BONS_from_Geo(inlat, inlon) {
   var cooresult = Geo2BN(inlat, inlon, BONS);
   putcoordcmb(cooresult,"txtBON","cmbBON",CDEL);
   return true;
}

function disp_EDGeo_from_GPSGeo(latt, lonn) {
   georesult = Geo2Geo(latt, lonn, WGS84, ED50);
   return putEDGeo(georesult);
}

function disp_GPSGeo_from_Geo(latt, lonn) {
   georesult = OSGB362WGS84(latt, lonn);
   //alert(georesult.extra);
   return putGPSGeo(georesult);
}

function disp_GPSGeo_from_EDGeo(latt, lonn) {
   georesult = Geo2Geo(latt, lonn, ED50, WGS84);
   return putGPSGeo(georesult);
}

function disp_GPSGeo_from_UTMWGS(east, north, proj) {
   georesult = TM2Geo(east, north, proj);
   if (georesult.longitude < 0) { lonsgn = -1; } else {lonsgn = 1; }
   if (!isvalidgeo(rad2deg(georesult.latitude),rad2deg(Math.abs(georesult.longitude)),lonsgn)) { return false; }
   if (georesult.longitude < (proj.Lon0-deg2rad(3)))
   {
      cooresult = Geo2TM(georesult.latitude,proj.Lon0-deg2rad(3),proj);
      if (east < (cooresult.eastings-40000)) { return false; }
   }
   if (georesult.longitude < (proj.Lon0+deg2rad(3)))
   {
      cooresult = Geo2TM(georesult.latitude,proj.Lon0+deg2rad(3),proj);
      if (east > (cooresult.eastings+40000)) { return false; }
   }
   return putGPSGeo(georesult);
}

function disp_EDGeo_from_UTMED(east, north, proj) {
   georesult = TM2Geo(east, north, proj);
   if (georesult.longitude < 0) { lonsgn = -1; } else {lonsgn = 1; }
   if (!isvalidgeo(rad2deg(georesult.latitude),rad2deg(Math.abs(georesult.longitude)),lonsgn)) { return false; }
   if (georesult.longitude < (proj.Lon0-deg2rad(3)))
   {
      cooresult = Geo2TM(georesult.latitude,proj.Lon0-deg2rad(3),proj);
      if (east < (cooresult.eastings-40000)) { return false; }
   }
   if (georesult.longitude < (proj.Lon0+deg2rad(3)))
   {
      cooresult = Geo2TM(georesult.latitude,proj.Lon0+deg2rad(3),proj);
      if (east > (cooresult.eastings+40000)) { return false; }
   }
   return putEDGeo(georesult);
}

function disp_Geo_from_GPSGeo(latt, lonn) {
   georesult = WGS842OSGB36(latt,lonn);
   //alert(georesult.extra);
   return putGeo(georesult);
}

function disp_EDGeoDMS_from_EDGeo(latt, lonn) {
   putDMS(latt,lonn,"txtED","cmbED");
   return true;
}

function disp_Geo_from_OSGB(eeee, nnnn) {
   georesult = TM2Geo(eeee, nnnn, OSNG);
   return putGeo(georesult);
}

function disp_Geo_from_OSYG(eeee, nnnn) {
   georesult = TM2Geo(eeee, nnnn, OSYG);
   return putGeo(georesult);
}

function disp_GeoDMS_from_Geo(latt, lonn) {
   putDMS(latt,lonn,"txt","cmb");
   return true;
}

function disp_GPSGeoDMS_from_GPSGeo(latt, lonn) {
   putDMS(latt,lonn,"txtGPS","cmbGPS");
   return true;
}

function disp_Geo_from_GeoDMS(latt, lonn) {
   var lattt = rad2deg(latt); var lonnn = rad2deg(lonn);
   var newlat = getELID("txtLat"); var newlon = getELID("txtLon"); var newlonB = getELID("cmbLonD");
   newlat.value = RoundDec(Math.abs(lattt), 8);
   lonnn = RoundDec(lonnn, 8);
   if (Math.abs(lonnn) < 1)
        { newlon.value = smallnum2string(Math.abs(lonnn)) }
   else { newlon.value = Math.abs(lonnn).toString(); }
   if (Math.max(lonnn, 0) == 0) { newlonB.value = -1; } else { newlonB.value = 1; }
   return true;
}

function disp_GPSGeo_from_GPSGeoDMS(latt, lonn) {
   var lattt = rad2deg(latt); var lonnn = rad2deg(lonn);
   var newlat = getELID("txtGPSLat"); var newlon = getELID("txtGPSLon"); var newlonB = getELID("cmbGPSLonD");
   newlat.value = RoundDec(Math.abs(lattt), 8);
   lonnn = RoundDec(lonnn, 8);
   if (Math.abs(lonnn) < 1)
        { newlon.value = smallnum2string(Math.abs(lonnn)) }
   else { newlon.value = Math.abs(lonnn).toString(); }
   if (Math.max(lonnn, 0) == 0) { newlonB.value = -1; } else { newlonB.value = 1; }
   return true;
}

function disp_EDGeo_from_EDGeoDMS(latt, lonn) {
   var lattt = rad2deg(latt); var lonnn = rad2deg(lonn);
   var newlat = getELID("txtEDLat"); var newlon = getELID("txtEDLon"); var newlonB = getELID("cmbEDLonD");
   newlat.value = RoundDec(Math.abs(lattt), 8);
   lonnn = RoundDec(lonnn, 8);
   if (Math.abs(lonnn) < 1)
        { newlon.value = smallnum2string(Math.abs(lonnn)) }
   else { newlon.value = Math.abs(lonnn).toString(); }
   if (Math.max(lonnn, 0) == 0) { newlonB.value = -1; } else { newlonB.value = 1; }
   return true;
}

function disp_Geo_from_WOCG(eeee, nnnn) {
   georesult = CS2Geo(eeee, nnnn, WOCG);
   return putGeo(georesult);
}

function disp_Geo_from_CDEL(eeee, nnnn) {
   georesult = CS2Geo(eeee, nnnn, CDEL);
   return putGeo(georesult);
}

function disp_Geo_from_BONS(eeee, nnnn) {
   georesult = BN2Geo(eeee, nnnn, BONS);
   return putGeo(georesult);
}


function disp_Geo_from_CTY(ctysel, raw_e, raw_n) {
   tempproj = new CSgriddata(Airy, 1, MeridList[CtyList[ctysel].Meridian].Lat0, MeridList[CtyList[ctysel].Meridian].Lon0, FO1, 'ft', 0, 0, 0, 0, 0, 0);
   georesult = CS2Geo(raw_e, raw_n, tempproj);
   return putGeo(georesult);
}

function disp_CTY_from_CTYS6F(selcty,shref,she,shn) {
   var tmp_e = RoundDec(she + CtyList[selcty].Sheets[shref].ShtW, 2);
   var tmp_n = RoundDec(shn + CtyList[selcty].Sheets[shref].ShtS, 2);
   var returnvalue = new coord(tmp_e, tmp_n);
   var temptemp = new CSgriddata(Airy,1,MeridList[CtyList[selcty].Meridian].Lat0,MeridList[CtyList[selcty].Meridian].Lon0,FO1,"ft",0,0,-550000,450000,-700000,1250000);
   putcoordcmb(returnvalue,"txtCty","cmbCty",temptemp);
   return returnvalue;
}

function disp_CTY_from_CTYS6Q(selcty,shref,shsub,she,shn) {
   var tmp_e = RoundDec(she + CtyList[selcty].Sheets[shref].ShtW, 2);
   var tmp_n = RoundDec(shn + CtyList[selcty].Sheets[shref].ShtS, 2);
   var ncoeff = Math.floor(shsub / 2); tmp_n = RoundDec(tmp_n + (ncoeff * (CtyList[selcty].sdims.height / 2)), 2);
   var ecoeff = shsub - (ncoeff * 2); tmp_e = RoundDec(tmp_e + (ecoeff * (CtyList[selcty].sdims.width / 2)), 2);
   var returnvalue = new coord(tmp_e, tmp_n);
   var temptemp = new CSgriddata(Airy,1,MeridList[CtyList[selcty].Meridian].Lat0,MeridList[CtyList[selcty].Meridian].Lon0,FO1,"ft",0,0,-550000,450000,-700000,1250000);
   putcoordcmb(returnvalue,"txtCty","cmbCty",temptemp);
   return returnvalue;
}

function disp_CTY_from_CTYS25(selcty,shref,shsub,she,shn) {
   var tmp_e = RoundDec(she + CtyList[selcty].Sheets[shref].ShtW, 2);
   var tmp_n = RoundDec(shn + CtyList[selcty].Sheets[shref].ShtS, 2);
   if (CtyList[selcty].sdims == s6in)
   {
      var ncoeff = Math.floor(shsub / 4); tmp_n = RoundDec(tmp_n + (ncoeff * (CtyList[selcty].sdims.height / 4)), 2);
      var ecoeff = shsub - (ncoeff * 4); tmp_e = RoundDec(tmp_e + (ecoeff * (CtyList[selcty].sdims.width / 4)), 2);
   }
   var returnvalue = new coord(tmp_e, tmp_n);
   var temptemp = new CSgriddata(Airy,1,MeridList[CtyList[selcty].Meridian].Lat0,MeridList[CtyList[selcty].Meridian].Lon0,FO1,"ft",0,0,-550000,450000,-700000,1250000);
   putcoordcmb(returnvalue,"txtCty","cmbCty",temptemp);
   return returnvalue;
}

function disp_CtySht_from_CTY(ctysel, raw_e, raw_n) {
   var shtfound = false;
   var shtsel = 0;
   for (ic=0; ic<CtyList[ctysel].Sheets.length; ic++)
   {
      if (raw_e >= CtyList[ctysel].Sheets[ic].ShtW && raw_e < CtyList[ctysel].Sheets[ic].ShtW + CtyList[ctysel].sdims.width && raw_n >= CtyList[ctysel].Sheets[ic].ShtS && raw_n < CtyList[ctysel].Sheets[ic].ShtS + CtyList[ctysel].sdims.height)
      {
         shtfound = true;
         shtsel = ic;
      }
   }
   if (shtfound == true)
   {
      var cres = new ctyshtres(ctysel, shtsel, raw_e, raw_n);
      dispctysht(cres);
      colourtext_CtyS_6F(txt_derived); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_25(txt_derived);
      return true;
   }
   else
   {
     getELID("txtCtyS_6F_S").value = "-"; getELID("txtCtyS_6F_E").value = "-"; getELID("txtCtyS_6F_N").value = "-";
     getELID("txtCtyS_6Q_S").value = "-"; getELID("cmbCtyS_6Q_S").value = 2; getELID("txtCtyS_6Q_E").value = "-"; getELID("txtCtyS_6Q_N").value = "-";
     getELID("txtCtyS_25_S").value = "-"; getELID("cmbCtyS_25_S").value = 12; getELID("txtCtyS_25_E").value = "-"; getELID("txtCtyS_25_N").value = "-";
     colourtext_CtyS_6F(txt_altered); colourtext_CtyS_6Q(txt_altered); colourtext_CtyS_25(txt_altered);
     return false;
   }
}


function disp_CTY_from_Geo(ltt, lnn, colrepl, rawlist) {
   var tempresult = Geo2Cty(ltt, lnn);
   if (typeof colrepl == "undefined") {var colrep = true;} else {var colrep = colrepl;}
   if (typeof rawlist == "undefined") {rawlist = false;} 
   var resdiv = getELID("ctyres");

   if (tempresult.length > 0)
   {
     var dispctynum = 0;
     var selctyid = getELID("cmbCty").value;
     for (zw=0;zw<tempresult.length;zw++) { if (tempresult[zw].cty == selctyid) {dispctynum = zw;} }

     if (rawlist == false)
     {
       dispctycfg(tempresult[dispctynum].cty);

       getELID("cmbCty").options[tempresult[dispctynum].cty].selected = true;
       getELID("ctyorig").innerHTML = "Projection origin: " + MeridList[CtyList[tempresult[dispctynum].cty].Meridian].Name;

       dispctysht(tempresult[dispctynum]);
     }
     else {
        resdiv.innerHTML = "";
        for (zw=0; zw<tempresult.length; zw++)
        {
            if (resdiv.innerHTML != "") {resdiv.innerHTML += "<br>";}
            resdiv.innerHTML += "<a href='#' onClick='chgctyres(ctyresult,-1," + zw + "); return false;'>" + CtyList[tempresult[zw].cty].Name + "</a>";
        }
        ctyresult = tempresult;
        if (colrep == true) {colourtext_Cty(txt_derived); colourtext_CtyS_6F(txt_derived); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_25(txt_derived); }
        return true;
     }

     if (tempresult.length > 1 && rawlist == false) {
        resdiv.innerHTML = "";
        for (zw=0; zw<tempresult.length; zw++)
        {
          if (zw != dispctynum)
          {
            if (resdiv.innerHTML != "") {resdiv.innerHTML += "<br>";}
            resdiv.innerHTML += "<a href='#' onClick='chgctyres(ctyresult," + dispctynum + "," + zw + "); return false;'>" + CtyList[tempresult[zw].cty].Name + "</a>";
          }
        }
        ctyresult = tempresult;
        if (colrep == true) {colourtext_Cty(txt_derived); colourtext_CtyS_6F(txt_derived); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_25(txt_derived); }
        return true;
     }
     else
     {
        if (rawlist == false) {resdiv.innerHTML = "No alternative counties found."; }
        if (colrep == true) {colourtext_Cty(txt_derived); colourtext_CtyS_6F(txt_derived); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_25(txt_derived); }
        return true;
     }
   }
   else
   {
        var coordresult = Geo2CtyRaw(ltt, lnn, getELID("cmbCty").value);
        var temp_e = RoundDec(coordresult.eastings, 2);
        var temp_n = RoundDec(coordresult.northings, 2);

        if (temp_e > 450000 || temp_e < -550000 || temp_n < -700000 || temp_n > 1250000)
        {
           getELID("txtCtyE").value = "-";
           getELID("txtCtyN").value = "-";
           getELID("cmbCtyE").value = 1; getELID("cmbCtyN").value = 1;
           if (colrep == true) {colourtext_Cty(txt_altered); }
        }
        else
        {
           getELID("txtCtyE").value = Math.abs(temp_e);
           getELID("txtCtyN").value = Math.abs(temp_n);
           if (temp_e < 0) {getELID("cmbCtyE").value = -1; } else {getELID("cmbCtyE").value = 1;}
           if (temp_n < 0) {getELID("cmbCtyN").value = -1; } else {getELID("cmbCtyN").value = 1;}
           if (colrep == true) {colourtext_Cty(txt_derived); }
        }

        getELID("txtCtyS_6F_S").value = "-"; getELID("txtCtyS_6F_E").value = "-"; getELID("txtCtyS_6F_N").value = "-";
        getELID("txtCtyS_6Q_S").value = "-"; getELID("cmbCtyS_6Q_S").value = 2; getELID("txtCtyS_6Q_E").value = "-"; getELID("txtCtyS_6Q_N").value = "-";
        getELID("txtCtyS_25_S").value = "-"; getELID("cmbCtyS_25_S").value = 12; getELID("txtCtyS_25_E").value = "-"; getELID("txtCtyS_25_N").value = "-";
        resdiv.innerHTML = "No matching county sheets found.";
        if (colrep == true) { colourtext_CtyS_6F(txt_altered); colourtext_CtyS_6Q(txt_altered); colourtext_CtyS_25(txt_altered); }
        return false;
   }
}


function dispctycfg(ctynum) {
   getELID("ctyorig").innerHTML = "Projection origin: " + MeridList[CtyList[ctynum].Meridian].Name;

   var ref_frm6F = getELID("frmCTYS_6F");
   var ref_frm6Q = getELID("frmCTYS_6Q");
   var ref_frm25 = getELID("frmCTYS_25");

   var ctycfg = CtyList[ctynum].cfg;
   switch (ctycfg)
   {
    case 1: { ref_frm25.style.display = 'block'; ref_frm6F.style.display = 'none'; ref_frm6Q.style.display = 'none'; break }
    case 2: { ref_frm6Q.style.display = 'block'; ref_frm25.style.display = 'none'; ref_frm6F.style.display = 'none'; break }
    case 3: { ref_frm6Q.style.display = 'block'; ref_frm25.style.display = 'block'; ref_frm6F.style.display = 'none'; break }
    case 4: { ref_frm6F.style.display = 'block'; ref_frm6Q.style.display = 'none'; ref_frm25.style.display = 'none'; break }
    case 5: { ref_frm6F.style.display = 'block'; ref_frm25.style.display = 'block'; ref_frm6Q.style.display = 'none'; break }
    case 6: { ref_frm6F.style.display = 'block'; ref_frm6Q.style.display = 'block'; ref_frm25.style.display = 'none'; break }
    case 7: { ref_frm6F.style.display = 'block'; ref_frm6Q.style.display = 'block'; ref_frm25.style.display = 'block'; break }
    default: { ref_frm6F.style.display = 'block'; ref_frm6Q.style.display = 'block'; ref_frm25.style.display = 'block'; break }
   }

     if (CtyList[ctynum].sdims == s6in) {
           getELID("CtyS_25_S_deriv").style.display = 'inline';
     }
     else { if (CtyList[ctynum].sdims == s25in) {
           getELID("CtyS_25_S_deriv").style.display = 'none';
        }
     }
}


function ctyaltered(ctynum) {
   dispctycfg(ctynum);
   if (getELID("ctyres").innerHTML.substring(0,2) == "No") {
      colourtext_CtyA(txt_altered); getELID("ctyres").innerHTML = "No result to display.<br>Please click a 'Convert' button to generate results.";
   } else {
      var ctyfound=false;
      for (i=0;i<ctyresult.length;i++) {
         if (ctyresult[i].cty == ctynum) {zz=i; ctyfound=true;}
      }
      if (ctyfound==true) {chgctyres(ctyresult,0,zz); }
         else {colourtext_CtyA(txt_altered); 
           getELID("ctyres").innerHTML = "No result to display.<br>Please click a 'Convert' button to generate results.";
         }
   }
}


function dispctysht(result) {
     var temp_e = RoundDec(result.rwe, 2); var temp_n = RoundDec(result.rwn, 2);
     getELID("txtCtyE").value = Math.abs(temp_e); getELID("txtCtyN").value = Math.abs(temp_n);
     if (temp_e < 0) {getELID("cmbCtyE").value = -1; } else {getELID("cmbCtyE").value = 1;}
     if (temp_n < 0) {getELID("cmbCtyN").value = -1; } else {getELID("cmbCtyN").value = 1;}

     if (CtyList[result.cty].sdims == s6in) {
        var ctymain_s = getELID("txtCtyS_6F_S"); var ctymain_e = getELID("txtCtyS_6F_E"); var ctymain_n = getELID("txtCtyS_6F_N");
        var cty6inq_s = getELID("txtCtyS_6Q_S"); var cty6q_sub = getELID("cmbCtyS_6Q_S"); var cty6inq_e = getELID("txtCtyS_6Q_E"); var cty6inq_n = getELID("txtCtyS_6Q_N");
        var cty25in_s = getELID("txtCtyS_25_S"); var cty25in_e = getELID("txtCtyS_25_E"); var cty25in_n = getELID("txtCtyS_25_N"); var cty25_sub = getELID("cmbCtyS_25_S");

        var temp_6F_e = RoundDec(temp_e - CtyList[result.cty].Sheets[result.sht].ShtW, 2);
        var temp_6F_n = RoundDec(temp_n - CtyList[result.cty].Sheets[result.sht].ShtS, 2);

        var cty6q_e = Math.floor(temp_6F_e / (CtyList[result.cty].sdims.width / 2));
        var cty6q_n = Math.floor(temp_6F_n / (CtyList[result.cty].sdims.height / 2));
        var cty6q = cty6q_e + (2 * cty6q_n);
        var temp_6Q_e = RoundDec(temp_6F_e - (cty6q_e * (CtyList[result.cty].sdims.width / 2)), 2);
        var temp_6Q_n = RoundDec(temp_6F_n - (cty6q_n * (CtyList[result.cty].sdims.height / 2)), 2);

        var cty25_e = Math.floor(temp_6F_e / (CtyList[result.cty].sdims.width / 4));
        var cty25_n = Math.floor(temp_6F_n / (CtyList[result.cty].sdims.height / 4));
        var cty25 = cty25_e + (4 * cty25_n);
        var temp_25_e = RoundDec(temp_6F_e - (cty25_e * (CtyList[result.cty].sdims.width / 4)), 2);
        var temp_25_n = RoundDec(temp_6F_n - (cty25_n * (CtyList[result.cty].sdims.height / 4)), 2);

        ctymain_s.value = CtyList[result.cty].Sheets[result.sht].ShtNum;
        ctymain_e.value = temp_6F_e; ctymain_n.value = temp_6F_n;

        cty6inq_s.value = CtyList[result.cty].Sheets[result.sht].ShtNum; cty6q_sub.value = cty6q;
        cty6inq_e.value = temp_6Q_e; cty6inq_n.value = temp_6Q_n;

        cty25in_s.value = CtyList[result.cty].Sheets[result.sht].ShtNum; cty25_sub.value = cty25;
        cty25in_e.value = temp_25_e; cty25in_n.value = temp_25_n;
     }
     else { if (CtyList[result.cty].sdims == s25in) {
           var ctymain_s = getELID("txtCtyS_25_S"); var ctymain_e = getELID("txtCtyS_25_E"); var ctymain_n = getELID("txtCtyS_25_N");
           var temp_6F_e = RoundDec(temp_e - CtyList[result.cty].Sheets[result.sht].ShtW, 2);
           var temp_6F_n = RoundDec(temp_n - CtyList[result.cty].Sheets[result.sht].ShtS, 2);
           ctymain_s.value = CtyList[result.cty].Sheets[result.sht].ShtNum;
           ctymain_e.value = temp_6F_e; ctymain_n.value = temp_6F_n;
        }
     }
}


function chgctyres(ctyres, oldnum, newnum) {
   getELID("cmbCty").options[ctyres[newnum].cty].selected = true;
   dispctycfg(ctyres[newnum].cty);
   dispctysht(ctyres[newnum]);
   colourtext_Cty(txt_derived); colourtext_CtyS_6F(txt_derived); colourtext_CtyS_6Q(txt_derived); colourtext_CtyS_25(txt_derived);
   if (ctyres.length == 1) { getELID("ctyres").innerHTML = "No alternative counties found."; } else {
      var altctystring = "";
      for (zw=0; zw<ctyresult.length; zw++)
      {
          if (zw != newnum) {
             if (altctystring != "") {altctystring += "<br>";}
             altctystring += "<a href='#' onClick='chgctyres(ctyresult," + newnum + "," + zw + "); return false;'>" + CtyList[ctyres[zw].cty].Name + "</a>";
          }
      }
      getELID("ctyres").innerHTML = altctystring;
   }
}



// Text colouring functions

function colourtext(elid, coll) {
   var elhandle = getELID(elid); elhandle.style.color = coll;
   return true;
}

function colourtext_GPSGeo(colsel) {
   colourtext("txtGPSLatD", colsel); colourtext("txtGPSLatM", colsel); colourtext("txtGPSLatS", colsel);
   colourtext("txtGPSLonD", colsel); colourtext("txtGPSLonM", colsel); colourtext("txtGPSLonS", colsel);
   colourtext("cmbGPSLon", colsel);
   return true;
}

function colourtext_EDGeo(colsel) {
   colourtext("txtEDLatD", colsel); colourtext("txtEDLatM", colsel); colourtext("txtEDLatS", colsel);
   colourtext("txtEDLonD", colsel); colourtext("txtEDLonM", colsel); colourtext("txtEDLonS", colsel);
   colourtext("cmbEDLon", colsel);
   return true;
}

function colourtext_EDGeoD(colsel) {
   colourtext("txtEDLat", colsel); colourtext("txtEDLon", colsel); colourtext("cmbEDLonD", colsel);
   return true;
}

function colourtext_GPSGeoD(colsel) {
   colourtext("txtGPSLat", colsel); colourtext("txtGPSLon", colsel); colourtext("cmbGPSLonD", colsel);
   return true;
}

function colourtext_Geo(colsel) {
   colourtext("txtLatD", colsel); colourtext("txtLatM", colsel); colourtext("txtLatS", colsel);
   colourtext("txtLonD", colsel); colourtext("txtLonM", colsel); colourtext("txtLonS", colsel);
   colourtext("cmbLon", colsel);
   return true;
}

function colourtext_GeoD(colsel) {
   colourtext("txtLat", colsel); colourtext("txtLon", colsel); colourtext("cmbLonD", colsel);
   return true;
}

function colourtext_OSGB(colsel) {
   colourtext("txtNGE", colsel); colourtext("txtNGN", colsel);
   return true;
}

function colourtext_OSGBr(colsel) {
   colourtext("txtNGSq", colsel); colourtext("txtNGSqE", colsel); colourtext("txtNGSqN", colsel);
   return true;
}

function colourtext_OSGBnr(colsel) {
   colourtext("txtNGnSq", colsel); colourtext("txtNGnSqE", colsel); colourtext("txtNGnSqN", colsel);
   return true;
}

function colourtext_OSYG(colsel) {
   colourtext("txtYGE", colsel); colourtext("txtYGN", colsel);
   return true;
}

function colourtext_WOCG(colsel) {
   colourtext("txtWOCGE", colsel); colourtext("txtWOCGN", colsel);
   return true;
}

function colourtext_WOCGr(colsel) {
   colourtext("txtWOCGSq", colsel); colourtext("txtWOCGSqE", colsel); colourtext("txtWOCGSqN", colsel);
   return true;
}

function colourtext_CDEL(colsel) {
   colourtext("txtCasE", colsel); colourtext("txtCasN", colsel); colourtext("cmbCasE", colsel); colourtext("cmbCasN", colsel);
   return true;
}

function colourtext_BONS(colsel) {
   colourtext("txtBONE", colsel); colourtext("txtBONN", colsel); colourtext("cmbBONE", colsel); colourtext("cmbBONN", colsel);
   return true;
}

function colourtext_UTMWGS(colsel) {
   colourtext("txtUTMWGSE", colsel); colourtext("txtUTMWGSN", colsel); colourtext("cmbUTMWGS", colsel);
   return true;
}

function colourtext_UTMED(colsel) {
   colourtext("txtUTMEDE", colsel); colourtext("txtUTMEDN", colsel); colourtext("cmbUTMED", colsel);
   return true;
}

function colourtext_CtyA(colsel) {
   colourtext_Cty(colsel); colourtext_CtyS_6F(colsel); colourtext_CtyS_6Q(colsel); colourtext_CtyS_25(colsel);
   return true;
}

function colourtext_Cty(colsel) {
   colourtext("cmbCty", colsel); colourtext("txtCtyE", colsel); colourtext("txtCtyN", colsel); colourtext("cmbCtyE", colsel); colourtext("cmbCtyN", colsel);
   return true;
}

function colourtext_CtyS_6F(colsel) {
   colourtext("txtCtyS_6F_S", colsel); colourtext("txtCtyS_6F_E", colsel); colourtext("txtCtyS_6F_N", colsel);
   return true;
}

function colourtext_CtyS_6Q(colsel) {
   colourtext("cmbCtyS_6Q_S", colsel); colourtext("txtCtyS_6Q_S", colsel); colourtext("txtCtyS_6Q_E", colsel); colourtext("txtCtyS_6Q_N", colsel);
   return true;
}

function colourtext_CtyS_25(colsel) {
   colourtext("cmbCtyS_25_S", colsel); colourtext("txtCtyS_25_S", colsel); colourtext("txtCtyS_25_E", colsel); colourtext("txtCtyS_25_N", colsel);
   return true;
}
