var watchId;
var map;
var geolocation;
var lastupdate = 0;

function timestamp() {
 return Math.floor(new Date().getTime()/1000);
}

function ajax_put(URL, divname) {
 if(navigator.appName == "Microsoft Internet Explorer") {
   var http = new ActiveXObject("Microsoft.XMLHTTP");
 } else {
   var http = new XMLHttpRequest();
 }
  http.open("GET", URL, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      if ( divname.indexOf(':') != -1 ) {
       var myDivnames = divname.split(':'); 
       var myResponse = http.responseText.split(':');
       for ( var i in myResponse ) {
        if (typeof document.getElementById(myDivnames[i]).value != 'undefined')
         document.getElementById(myDivnames[i]).value = myResponse[i]; else
         document.getElementById(myDivnames[i]).innerHTML = myResponse[i];
       }
      } else
      if (typeof document.getElementById(divname).value != 'undefined')
        document.getElementById(divname).value = http.responseText; else
        document.getElementById(divname).innerHTML = http.responseText;
    } 
  }
  http.send(null);
}


function update_div(data, divname) {
      if (typeof document.getElementById(divname).value != 'undefined')
        document.getElementById(divname).value = data; else
        document.getElementById(divname).innerHTML = data;
}


function stopLocation() {
 geolocation.clearWatch(watchId);
 document.getElementById('geo_stop').disabled  = true;
 document.getElementById('geo_start').disabled = false;
}

function getLocation() {
if (navigator.geolocation) 
 geolocation = navigator.geolocation; else {
 try {window.gears = !!(typeof GearsFactory != 'undefined' || navigator.mimeTypes['application/x-googlegears'] || new ActiveXObject('Gears.Factory'));}catch(e){}
 if (window.gears) geolocation = google.gears.factory.create('beta.geolocation');
}

if (typeof geolocation != 'undefined') {
 try {
  watchId = geolocation.watchPosition(showLocation, showError, {timeout:30000,enableHighAccuracy:true,maximumAge:20000});
  document.getElementById('geo_stop').disabled  = false;
  document.getElementById('geo_start').disabled = true;
 }
 catch (e) {
  geolocation.getCurrentPosition(showLocation, showError, {timeout:30000,enableHighAccuracy:true,maximumAge:20000}); 
 }
} else
 update_div('GeoLocation not available. Try installing <a href="http://gears.google.com/?action=install&message=Install Google Gears for automatic positioning!&return=http://www.sk6aw.net/i/locator.php">Google Gears</a>, or enter a position manually.','geo');
 lastupdate = 0;
}

function showError(error) {
 update_div(error.message,"acc-txt");
 stopLocation();
}
 
function showLocation(position) {
 update_div(Math.round(position.coords.accuracy), "acc");
 if (document.toGrid.long.value != position.coords.longitude || document.toGrid.lat.value  != position.coords.latitude ) {
 document.toGrid.long.value = position.coords.longitude.toFixed(6);
 document.toGrid.lat.value  = position.coords.latitude.toFixed(6);

 if ( lastupdate+15 > timestamp() ) return; // Not more than once every fifteen seconds...
 lastupdate = timestamp();

 ajax_put("/i/loc.php?lon="+position.coords.longitude+"&lat="+position.coords.latitude+"&acc="+position.coords.accuracy, 'grid:latd:latm:lats:longd:longm:longs:acc-txt');
}
}

