<?php
// ireland_lighthouses.php
// RJM Programming
// July, 2015
// Thanks to Wikipedia at https://en.wikipedia.org/wiki/List_of_lighthouses_in_Ireland

 function selcreate($def) {
   $ret=$def;
   $selstr='<select onchange=" window.location=this.value; "><option value="' . str_replace(" ", "_", strtolower($def)) . '_lighthouses.php">' . $def . '</option>';
   $cnt=0;
   foreach (glob("*_lighthouses.php") as $filename) {
    if (strpos(($filename . "*"), (str_replace(" ", "_", strtolower($def)) . '_lighthouses.php*')) === false) {
     $cnt++;
     $newidea=str_replace("_", " ", str_replace("_lighthouses.php", "", strtolower($filename)));
     $newideas=explode(" ", $newidea);
     $ideas=strtoupper(substr($newideas[0],0,1)) . strtolower(substr($newideas[0],1));
     for ($ii=1; $ii<sizeof($newideas); $ii++) {
       $ideas.=(" " . strtoupper(substr($newideas[$ii],0,1)) . strtolower(substr($newideas[$ii],1)));
     }
     $selstr.='<option value="' . $filename . '">' . $ideas . '</option>';
    }
   } 
   if ($cnt > 0) return $selstr . "</select>";  
   return $ret;
 }
 

 $lhcont=str_replace("</table>", "<table", file_get_contents("https://en.wikipedia.org/wiki/List_of_lighthouses_in_Ireland"));
 $url="http://www.rjmprogramming.com.au/PHP/Map/map.php";
 $method="POST";
 $iname="my_iframe";
 $ourtitle=urlencode("Ireland Lighthouses");
 $ourlabel="[%27Lat%27,";
 $ourvalue="%27Lon%27,%20%27Name%27]";
 $ourdata="";
 $prefix=$url . "?title=" . $ourtitle . "&label=" . $ourlabel . "&value=" . $ourvalue . "&data=";
 $isok=true;
 $titleis="";
 $altis="";
 $imgbit="";
 $simgbit="";
 $seimgbit="";
 // Prepare HTML form and iframe for later ...
 $ourform="<" . "form target='" . $iname . "' method='" . $method . "' action='" . $url . "'><input name='title' type='hidden' id='title' value='" . $ourtitle . "'></input><input name='label' type='hidden' id='label' value='" . $ourlabel . "'></input><input type='hidden' name='wouldlikeyoutoseekpermission' id='wouldlikeyoutoseekpermission' value='y'></input><input name='value' type='hidden' id='value' value='" . $ourvalue . "'></input><textarea style='display:none;' rows=40 cols=80 name='data' id='data' value=''></textarea><input style='display:none;' type='submit' id='submit'></input></form>\n";
 $ourscript="<" . "script type='text/javascript'>\nfunction populate() { document.getElementById('title').value=decodeURIComponent('" . $ourtitle . "'); document.getElementById('label').value=decodeURIComponent('" . $ourlabel . "'); document.getElementById('value').value=decodeURIComponent('" . $ourvalue . "'); document.getElementById('data').value=''; \n";
 // Write out start of HTML ready for the one table row that will be created ...
 echo '<html><head><script type="text/javascript" src="ireland_lighthouses.js" async="async"></script><link href="//www.rjmprogramming.com.au/PHP/emboss_h1.css" rel="stylesheet" type="text/css"><style> body {  background-color: lightblue;  } td { border: 2px solid blue; padding-left:10px; padding-right: 10px; background-color: lightgreen; } </style><title>Ireland Lighthouses - RJM Programming - July, 2015</title><' . 'script type="text/javascript"> function after() { document.getElementById("submit").click(); document.getElementById("' .$iname . '").style.display="inline"; } </' . 'script></head><body onload="populate();"><div align="center"><h1>' . selcreate('Ireland') . ' Lighthouses ... Thanks to <a target=_blank title="Ireland Lighthouses information from Wikipedia ... thanks" href="https://en.wikipedia.org/wiki/List_of_lighthouses_in_Ireland">Wikipedia</a></h1></div>' . "\n";
 $bextras = "<table cellspacing=5 style='margin-top:-16px;'><tr><b>";

 $docb=false;
  try {
  $doc = new DOMDocument();
  $doc->preserveWhiteSpace = FALSE;
  $before = libxml_use_internal_errors(true);
  $doc->loadHTML(file_get_contents(dirname(__FILE__) . "/ireland_lighthouses.txt"));
  libxml_use_internal_errors($before);  
  $docb = true;
  } catch (Exception $e) {
  throw new Exception( 'Something really wrong', 0, $e);
  }

      if ($docb && 1 == 1) {
        foreach ($doc->getElementsByTagName("li") as $link) {
          $linkc14n=$link->C14N();
          $hrefs=explode("href=", $linkc14n);
          $posthrefs=explode(" ", $hrefs[sizeof($hrefs) - 1]);
          $ihs=explode("</a>", $linkc14n);
          $ih=explode(">", $ihs[0]);  
          $bits=explode(" - ", $linkc14n);  
          $altis=$ih[sizeof($ih) - 1];
          $titleis=$ih[sizeof($ih) - 1];
          $altis=str_replace("'", "`", $altis);
          $titleis=str_replace("'", "`", $titleis);
          $xpostabits=explode("°", $linkc14n);  
          $dpostabits=explode(".", $linkc14n);
          $latsofar="0";
          $longsofar="0";
          $sl="";
          if (sizeof($dpostabits) >= 2) {
          if (strpos($dpostabits[sizeof($dpostabits) - 2], "S") !== false) {
            $bs=explode("S", $dpostabits[sizeof($dpostabits) - 2]);
            //$longsofar=trim($bs[sizeof($bs) - 1]);
            $longsofar="";
            $ip=(strlen($dpostabits[sizeof($dpostabits) - 2]) - 1);
            while ($ip >= 0 && (substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) < '0' || substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) > '9')) {
				$ip--;
            }
            while ($ip >= 0 && substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) <= '9') {
				$longsofar=substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) . $longsofar;
				$ip--;
            }
            $latsofar="-";
            $ip=(strlen($dpostabits[sizeof($dpostabits) - 3]) - 1);
            while ($ip >= 0 && (substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) < '0' || substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) > '9')) {
				$ip--;
            }
            while ($ip >= 0 && substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) <= '9') {
				$latsofar=str_replace("-", "-" . substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1), $latsofar);
				$ip--;
            }
            $latsofar.=".";
            $ip=0;
            while (substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) <= '9') {
				$latsofar.=substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1);
				$ip++;
            }
          } else {
            $bs=explode("N", $dpostabits[sizeof($dpostabits) - 2]);
            //$longsofar=trim($bs[sizeof($bs) - 1]);
            $longsofar="";
            $ip=(strlen($dpostabits[sizeof($dpostabits) - 2]) - 1);
            while ($ip >= 0 && (substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) < '0' || substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) > '9')) {
				$ip--;
            }
            while ($ip >= 0 && substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) <= '9') {
				$longsofar=substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) . $longsofar;
				$ip--;
            }
            $latsofar="+";
            $ip=(strlen($dpostabits[sizeof($dpostabits) - 3]) - 1);
            while ($ip >= 0 && (substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) < '0' || substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) > '9')) {
				$ip--;
            }
            while ($ip >= 0 && substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1) <= '9') {
				$latsofar=str_replace("+", "+" . substr($dpostabits[sizeof($dpostabits) - 3], $ip, 1), $latsofar);
				$ip--;
            }
            if (1 == 4) $latsofar=str_replace("+", "", $latsofar);
            $latsofar.=".";
            $ip=0;
            while (substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1) <= '9') {
				$latsofar.=substr($dpostabits[sizeof($dpostabits) - 2], $ip, 1);
				$ip++;
            }
          } 
          } 
          
          if (strpos($xpostabits[sizeof($xpostabits) - 1], "W") !== false) {
            $longsofar="-" . $longsofar . ".";
            if ($sl == "") $sl=$longsofar;
            $ip=0;
            while (substr($dpostabits[sizeof($dpostabits) - 1], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 1], $ip, 1) <= '9') {
				$longsofar.=substr($dpostabits[sizeof($dpostabits) - 1], $ip, 1);
				$ip++;
            }
          } else {
            $longsofar="+" . $longsofar . ".";
            if ($sl == "") $sl=$longsofar;
            $ip=0;
            while (substr($dpostabits[sizeof($dpostabits) - 1], $ip, 1) >= '0' && substr($dpostabits[sizeof($dpostabits) - 1], $ip, 1) <= '9') {
				$longsofar.=substr($dpostabits[sizeof($dpostabits) - 1], $ip, 1);
				$ip++;
            }
          } 
          $latis=$latsofar;
          $longis=$longsofar;
          if (sizeof($xpostabits) > 1) {
           $bextras = str_replace("<b>", "<td>" . $posthrefs[0] . "<a title='" . str_replace((", " . $altis), "", ($altis . ", " . $titleis)) . "' target=_blank href='https://maps.google.com.au/maps?" . "z=15&t=m&q=loc:" . str_replace("+","",$latis) . "+" . $longis . "'>" . str_replace((", " . $altis), "", ($altis . ", " . $titleis)) . $imgbit . "</td><b>", $bextras);
           $seimgbit=urlencode(">" . str_replace((", " . $altis), "", ($altis . ", " . $titleis)) ) . "%3C/a%3E";
           $thisdata = ",[" . str_replace("+","",$latis) . "," . str_replace("+","",$longis) . "," . "%22A%20Wikipedia%20clicking%20link%20for%20Google%20Map%20based%20on%20latitude%20and%20longitude%20of%20%3Ca%20target=_blank%20href=\\\\\"https://maps.google.com.au/maps?" . urlencode("z=15&t=m&q=loc:") . urlencode(str_replace("+","",$latis) . "+" . $longis) . "\\\\\"" . $seimgbit . "%22,~" . urlencode(str_replace((", " . $altis), "", ($altis . ", " . $titleis)) ) . "~]";
           // Write out Javascript to populate textarea and rest of form data that will be posted to iframe ...
           $ourscript .= "\n document.getElementById('data').value+=decodeURIComponent('" . $thisdata . "'); ";
           $ourscript .= "\n document.getElementById('data').innerHTML+=decodeURIComponent('" . $thisdata . "'); ";
           $ourdata .= $thisdata;
          }
		}
        echo $bextras;
      } 


  
 // Write out end of HTML including end of row and end of table and form (method=POST with a submit button programmatically clicked via setInterval() at body onload event) and Javascript code and Google Chart Map Chart map.php iframe.
 echo "\n</tr></table>" . $ourform . $ourscript . "\n if (1 == 1) { setTimeout(after, 2000); } \n } \n</" . "script><div align='center' style='margin-top:0px;'><iframe name='my_iframe' id='my_iframe' src='javascript:none' width=500 height=400 style='display:none;'></iframe></div></body></html>";
?>
