///////////////////////////////////////////////////////////////////////////////
/*
                       qdlogstats_geolocate_geoipdb.js

https://geoip-db.com/

This is a free service and does not require registration.

This module caches geo data and so only accesses the service once for each
unique host begin resolved.  This service only accepts IP adresses.


COPYRIGHT
---------
Copyright (C) 2018 Mark G.Daniel
This program, comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under the
conditions of the GNU GENERAL PUBLIC LICENSE, version 3, or any later version.
http://www.gnu.org/licenses/gpl.txt


VERSION
-------
06-NOV-2018  MGD  initial
*/
///////////////////////////////////////////////////////////////////////////////

var geoLocateHost = [];
var geoLocateData = [];
var geoLocateIPv4 = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/i
var geoLocateIPv6 = /^([0-9a-f]|:){1,4}(:([0-9a-f]{0,4})*){1,7}$/i

///////////////////////////////////////////////////////////////////////////////

function geoLocate (id, host)
{
   var idx, init;

   for (idx = 0; idx < geoLocateHost.length; idx++)
      if (geoLocateHost[idx] == host) break;

   if (!geoLocateData[idx])
   {
      geoLocateHost[idx] = host;
      geoLocateData[idx] = '';
      if (host.match(geoLocateIPv4) || host.match(geoLocateIPv6))
         geoLocateGeoIpDb(id,idx,host);
   }

   document.getElementById(id).innerHTML = geoLocateData[idx];
}

///////////////////////////////////////////////////////////////////////////////

function geoLocateGeoIpDb(id,idx,host)
{
   var url = 'https://geoip-db.com/jsonp/' + host;

   var xhr = new XMLHttpRequest();

   // make synchronous so as not to flood the service 
   xhr.open('GET', url, false);

   xhr.onreadystatechange = function()
   {
      if (this.readyState == 4)
      {
         var gld;
         if (this.status == 200)
         {
            var jsnp = this.responseText;
            var jsn = JSON.parse(jsnp.substr(9,jsnp.length-10));
            var country = jsn['country_name'];
            var state = jsn['state'];
            var city = jsn['city'];

            gld = ' {';

            if (country != null)
            {
               if (country != "")
               {
                  gld += country;

                  if (state && state != "" && state != country)
                     gld += ', ' + jsn['state'];

                  if (city && city != "" && city != state)
                     gld += ', ' + jsn['city'];
               }
               else
                  gld += '?';
            }
            else
               gld += 'bugcheck';

            gld += '}';
         }
         else
         if (this.status)
         {
            gld = ' {HTTP ' + this.status + ' error}';
         }
         else
            gld = '';

         geoLocateData[idx] = gld;
         document.getElementById(id).innerHTML = gld;
      }
   };

   xhr.send(); 
}

///////////////////////////////////////////////////////////////////////////////
