<?php
  // More Geolocation - Sharing Information - RJM Programming - August, 2014
  $ol = "getLocation(); loadgo = 0; ";
  $none = "none";
  $thename = "";
  $theplace = "";
  $xmyh2 = "Current Location is Latitude: and Longitude: ";
  $themap = "http://www.rjmprogramming.com.au";
  $theblurb = "";
  $seeother = "";
  $val = "";
  $tval = "";
  $longp = "+";
  if (isset($_POST['thelist'])) {
    if (isset($_POST['thelist']) != "") $seeother = urldecode($_POST['thelist']);
    $theblurb = file_get_contents(urlencode($seeother) . ".txt");
    $thename = file_get_contents(urlencode($seeother) . ".thename");
    $theplace = file_get_contents(urlencode($seeother) . ".theplace");
    $themap = file_get_contents(urlencode($seeother) . ".themap");
    $none = "block";
    $ol = ' window.open("' . $themap . '", "_blank"); loadgo = 0; ';
    if ($themap == "") {
      $ol = "getLocation(); loadgo = 0; ";
    } else {
      //http://maps.google.com/maps?z=20&t=e&q=loc:-33.9074847 151.17642300000003
      $latlong = explode("loc:", urldecode($themap));
      $llong = explode(":", "x");
      if (sizeof($latlong) > 1) {
        $llong = explode("+", substr($latlong, 1));
        if (sizeof($llong) == 1) {
          $llong = explode("-", substr($latlong, 1));
          if (sizeof($llong) == 1) {
            $llong = explode(" ", substr($latlong, 1));
          } else {
            $longp = "-";
          }
        }
      }
      if (sizeof($llong) > 1) {
        $xmyh2 = "Location is Latitude: " . str_replace("+", "", substr($latlong[0],0,1)) . substr($latlong[0],1) . " and Longitude: " . str_replace("+", "", $longp) . $llong[1];
      }
    }
  }
  if (isset($_GET['thelist'])) {
    if (isset($_GET['thelist']) != "") $seeother = urldecode($_GET['thelist']);
    $theblurb = file_get_contents(urlencode($seeother) . ".txt");
    $thename = file_get_contents(urlencode($seeother) . ".thename");
    $theplace = file_get_contents(urlencode($seeother) . ".theplace");
    $themap = file_get_contents(urlencode($seeother) . ".themap");
    $none = "block";
    $ol = ' window.open("' . $themap . '", "_blank"); loadgo = 0; var ww="' . urldecode($themap) . '"; ';
    if ($themap == "") {
      $ol = "getLocation(); loadgo = 0; ";
    } else {
      //http://maps.google.com/maps?z=20&t=e&q=loc:-33.9074847 151.17642300000003
      $latlong = explode("loc:", urldecode($themap));
      $llong = explode(":", "x");
      if (sizeof($latlong) > 1) {
        $llong = explode("+", substr($latlong[1], 1));
        if (sizeof($llong) == 1) {
          $llong = explode("-", substr($latlong[1], 1));
          if (sizeof($llong) == 1) {
            $llong = explode(" ", substr($latlong[1], 1));
          } else {
           $longp = "-";
          }
        }
      }
      if (sizeof($llong) > 1) {
        $xmyh2 = "Location is Latitude: " . str_replace("+", "", substr($latlong[1],0,1)) . substr($llong[0],1) . " and Longitude: " . str_replace("+", "", $longp) . $llong[1];
      }
    }
  }
  if (isset($_POST['thename']) && $seeother == "") {
    if (($_POST['thename'] . $_POST['theplace'] . $_POST['themap'] . $_POST['theblurb']) != "") {
      $tval = urldecode($_POST['thename']);
      $val = str_replace(" ", "_", urldecode($_POST['thename']));
      if ($tval == "") {
        if ($_POST['theplace'] != "") {
          $tval .= "I live in " . urldecode($_POST['theplace']);
          $val = str_replace(" ", "_", $tval);
        } else if ($_POST['themap'] != "") { 
          $tval .= "I live at Google Map URL " . urldecode($_POST['themap']);
          $val = str_replace(" ", "_", urlencode($tval));
        } else {
          $tval .= urldecode($_POST['theblurb']);
          $val = str_replace(" ", "_", $tval);
        }
      } else if ($_POST['theplace'] != "") {
        $tval .= " of " . urldecode($_POST['theplace']);
      } else if ($_POST['themap'] != "") { 
        $tval .= " at Google Map URL " . urlencode($_POST['themap']);
      }
    }
    $none = "none";
    //$theblurb = urldecode($_POST['theblurb']);
    file_put_contents($val . ".txt", urldecode($_POST['theblurb']));
    file_put_contents($val . ".thename", urldecode($_POST['thename']));
    file_put_contents($val . ".theplace", urldecode($_POST['theplace']));
    file_put_contents($val . ".themap", urldecode($_POST['themap']));
    file_put_contents("more_geolocation.txt", file_get_contents("more_geolocation.txt") . '<option value="' . $val . '">' . $tval . '</option>' . "\n");
  }
?>
<html>
<head>
<title>Geolocation - Get Your Latitude and Longitude - thanks to http://www.tutorialspoint.com/html5/html5_geolocation.htm - RJM Programming - July, 2014</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script style="text/javascript">
var ua = navigator.userAgent; 
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /Andrxoid/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
var firstgo = 1;
var loadgo = 1;
var dyw = null;
var cdate = new Date();  // thanks to http://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript
var latitude; // = position.coords.latitude;
var longitude; // = position.coords.longitude;
var prelat = "", prelong = "";

function andthen() {
        window.open("http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+"), "_blank");

}

function sqchange(isdyw) {
    dyw = isdyw;
    if (dyw.substring(0,1) == "Y" || dyw.substring(0,1) == "y") {
      if (latitude >= 0.0) prelat = "+";
      if (longitude >= 0.0) prelong = "+";
      if (dyw.substring(0,3) == "Yes" || dyw.substring(0,3) == "YES" || dyw.substring(0,3) == "yes") {
        document.getElementById('myform').style.display = 'block';
        document.getElementById('thename').style.display = 'block';
        document.getElementById('theplace').style.display = 'block';
        document.getElementById('themap').style.display = 'block';
        document.getElementById('thelist').style.display = 'block';
        document.getElementById('themap').value = "http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+");
        document.getElementById('theblurb').style.display = 'block';
        //document.getElementById('mapplace').src = "http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+");
        if (isiPad) {
        document.getElementById('q').style.display = 'none';
        document.getElementById('sq').style.display = 'none';
        setTimeout(andthen, 2000);
        } else {
        window.open("http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+"), "_blank");
        }
      } else {
        if (isiPad) {
        document.getElementById('q').style.display = 'none';
        document.getElementById('sq').style.display = 'none';
        setTimeout(andthen, 2000);
        } else {
        window.open("http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+"), "_blank");
        }
      }
    }
}

function showLocation( position ) {
  cdate = new Date();  // thanks to http://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
  prelat = ""; 
  prelong = "";
  document.getElementById('myh2').innerHTML = "Current Location is Latitude: " + latitude + " and Longitude: " + longitude + " at " + ("0" + cdate.getDate()).slice(-2) + "/" + ("0" + eval(1 + cdate.getMonth())).slice(-2)  + "/" + cdate.getFullYear() + ":" + ("0" + cdate.getHours()).slice(-2) + ":" + ("0" + cdate.getMinutes()).slice(-2) + ":" + ("0" + cdate.getSeconds()).slice(-2);
  if (firstgo == 1) {
    firstgo = 0;
    if (isiPad) {
        document.getElementById('q').style.display = 'block';
        document.getElementById('sq').style.display = 'block';
    } else {

    //while (dyw == null) {
    dyw = prompt("You are at (Latitude, Longitude) (" + latitude + ", " + longitude + ").  Do you want to zoom in to a Google map of your location?  Y/[N] ... if you answer Yes, you can share with others a 'blurb' about yourself and where you live, that others can read", "N");
    //}
    if (dyw.substring(0,1) == "Y" || dyw.substring(0,1) == "y") {
      if (latitude >= 0.0) prelat = "+";
      if (longitude >= 0.0) prelong = "+";
      if (dyw.substring(0,3) == "Yes" || dyw.substring(0,3) == "YES" || dyw.substring(0,3) == "yes") {
        document.getElementById('myform').style.display = 'block';
        document.getElementById('thename').style.display = 'block';
        document.getElementById('theplace').style.display = 'block';
        document.getElementById('themap').style.display = 'block';
        document.getElementById('thelist').style.display = 'block';
        document.getElementById('themap').value = "http://maps.google.com/maps?z=20&t=e&q=loc:" + prelat + latitude + prelong + longitude;
        document.getElementById('theblurb').style.display = 'block';
        //document.getElementById('mapplace').src = "http://maps.google.com/maps?z=20&t=e&q=loc:" + prelat + latitude + prelong + longitude;
        window.open("http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+"), "_blank");
      } else {
        window.open("http://maps.google.com/maps?z=20&t=e&q=loc:" + (prelat + latitude + prelong + longitude).replace(" ", "+"), "_blank");
      }
      }
    }
    setInterval(getLocation, 5000);
  }
}

function errorHandler( err ) {
  if (err.code == 1) {
    // access is denied
    alert("Access is denied");
  }
  
}

function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler,
                                 {maximumAge: 75000});
}
</script>
</head>
<body onload='<?php echo $ol; ?>' style='background-color: yellow;'>
<h2 id="myh2"><?php echo $xmyh2; ?></h2>
<p id='q' name='q' style='display:none;'>You are at (Latitude, Longitude) (" + latitude + ", " + longitude + ").  Do you want to zoom in to a Google map of your location?  Y/[N] ... if you answer Yes, you can share with others a 'blurb' about yourself and where you live, that others can read.</p>&nbsp;&nbsp;<select id='sq' name='sq' onchange=' sqchange(this.value); ' style='display:none;'><option value='N'>Please select option below ...</option><option value='N'>N</option><option value='Y'>Y</option><option value='Yes'>Yes</option></select>
<form id='myform' name='myform' style='display:<?php echo $none; ?>;' action='./more_geolocation.php' method='POST' style='background-color: pink;'>
Name (okay to be anonymous): <input name="thename" id="thename" type="text" style="background-color: lightblue;display:<?php echo $none; ?>;" value="<?php echo $thename; ?>"></input><br>
Where I Live: <input name="theplace" id="theplace" type="text" style="background-color: lightblue;display:<?php echo $none; ?>;" value="<?php echo $theplace; ?>"></input><br>
My Google Map URL: <input name="themap" id="themap" type="text" size=80 style="background-color: lightblue;display:<?php echo $none; ?>;" value="<?php echo $themap; ?>"></input><br>
My Story (optional): <textarea name="theblurb" id="theblurb" rows=10 cols=80 style="background-color: lightblue;display:<?php echo $none; ?>;" value="<?php echo str_replace('"', ' ', str_replace("\\'", "'", $theblurb)); ?>"><?php echo str_replace('"', ' ', str_replace("\\'", "'", $theblurb)); ?></textarea><br>
See Other Stories of People Who Have Shared: <select onchange=" if (loadgo == 0) { location.href='./more_geolocation.php?thelist=' + encodeURIComponent(this.value); } loadgo = 0; " name="thelist" id="thelist" style="background-color: lightblue;display:<?php echo $none; ?>;" value="<?php echo $seeother; ?>"><option value="">Optionally choose a sharer below ...</option><?php echo file_get_contents("more_geolocation.txt"); ?></select><br>
<input style="background-color: lightgreen;" type="submit" name="submit" id="submit" value="Share My Details Above"></input>&nbsp;&nbsp;<input style="background-color: lightgray;" type="reset" name="reset" id="reset" value="Reset"></input>
<iframe id="mapplace" style="display:none;" src="<?php echo $themap; ?>" title="RJM Programming landing page ... Geolocation ideas thanks to http://www.tutorialspoint.com/html5/html5_geolocation.htm" width="100%" height="900"></iframe>
</body>
</html>
