<?php
// Moon Angle (now) at? calculation from RJM Programming  
// November 2013
// Thanks to:
//   http://translate.google.com.au/translate?hl=en&sl=de&u=http://de.wikipedia.org/wiki/Sublunarer_Punkt&prev=/search%3Fq%3D%2522Sublunarer%2522%2Bwikipedia%26client%3Dfirefox-a%26hs%3D3co%26rls%3Dorg.mozilla:en-US:official%26biw%3D1213%26bih%3D619
//   http://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CC8QFjAA&url=http%3A%2F%2Fgeoalliance.asu.edu%2Fazga_site%2Fsites%2Fdefault%2Ffiles%2FLessonFiles%2FDorn%2FPulse%2FDornPulse.ppt&ei=QTGRUuiXEcGtiAfwlIGICw&usg=AFQjCNEYL6f1yBLg5hdC5qgYnnfWS00nOg&sig2=2kQ25M8OoDltE6baNCRlnQ&bvm=bv.56988011,d.aGc
//   curl http://www.timeanddate.com/worldclock/sunearth.html
//   curl http://www.zodiacarts.com/Calendar.shtml

$extrainfo = '';
$zero = "0";
$disp = "none";
$ilatd = "<select onclick='ca();' id='latd'><option value='+27.9861'>+27.9861 Degrees</option>";
$ilatm = "<select onclick='ca();' id='latm'><option value='0.0'>Minutes</option>";
$ilats = "<select onclick='ca();' id='lats'><option value='0.0'>Seconds</option>";
$ilongd = "<select onclick='ca();' id='longd'><option value='+86.9230'>+86.9230 Degrees</option>";
$ilongm = "<select onclick='ca();' id='longm'><option value='0.0'>Minutes</option>";
$ilongs = "<select onclick='ca();' id='longs'><option value='0.0'>Seconds</option>";
$jlatd = "<select onclick='ca();' id='tald'><option value='+27.9861'>+27.9861 Degrees</option>";
$jlatm = "<select onclick='ca();' id='talm'><option value='0.0'>Minutes</option>";
$jlats = "<select onclick='ca();' id='tals'><option value='0.0'>Seconds</option>";
$jlongd = "<select onclick='ca();' id='gnold'><option value='+86.9230'>+86.9230 Degrees</option>";
$jlongm = "<select onclick='ca();' id='gnolm'><option value='0.0'>Minutes</option>";
$jlongs = "<select onclick='ca();' id='gnols'><option value='0.0'>Seconds</option>";
$exbit = "";
$ssplatd = "";
$ssplatm = "";
$ssplongd = "";
$ssplongm = "";

$latxprefix = "";
$longxprefix = "";
$zimg = "";

exec("curl http://www.zodiacarts.com/Calendar.shtml > " . dirname(__FILE__) . "/moon_angle_data.txt");
if (file_exists(dirname(__FILE__) . "/moon_angle_data.txt")) {
$zthing = ' width="72" height="72" align="middle"';
$zfhuh = fopen(dirname(__FILE__) . "/moon_angle_data.txt", "r");
$zcont = "";
while (($zbuffer = fgets($zfhuh, 4096)) !== false) {
         $zcont .= $zbuffer;
}
fclose($zfhuh);
unlink(dirname(__FILE__) . "/moon_angle_data.txt");
$zonp = strpos($zcont, $zthing);
$zok = '';
$zsuff = '';
 // $extrainfo = "not found";
if ($zonp !== false) {
 // $extrainfo = "found";
  $zimg = "http://www.zodiacarts.com";
  $zcont = substr($zcont, 0, $zonp) . '~';
  $zi = strlen($zcont) - 1;
  while (substr($zcont, $zi, 1) != '"') {
    $zi -= 1;
  }
  $zi -= 1;
  while (substr($zcont, $zi, 1) != '"') {
    if (substr($zcont, $zi, 1) == "." && substr($zcont, ($zi - 1), 1) == ".") {
      $zsuff = $zsuff;
    } else if (substr($zcont, $zi, 1) == "." && substr($zcont, ($zi + 1), 1) == ".") {
      $zsuff = $zsuff;
    } else {
      $zsuff = substr($zcont, $zi, 1) . $zsuff;
    }
    $zi -= 1;
  }
  $zimg = '<img id="sandg" style="display:none;" width="72" height="72" src="' . $zimg . $zsuff . '"></img>';
}
}

exec("curl http://www.timeanddate.com/worldclock/sunearth.html > " . dirname(__FILE__) . "/moon_angle_data.txt");
if (!file_exists(dirname(__FILE__) . "/moon_angle_data.txt")) {
header("Location: http://www.rjmprogramming.com.au/NoWayJose/");
} else {
$fhuh = fopen(dirname(__FILE__) . "/moon_angle_data.txt", "r");
$cont = "";
$thing = "<h2>Position of the Moon</h2>On ";
while (($buffer = fgets($fhuh, 4096)) !== false) {
         $cont .= $buffer;
}
fclose($fhuh);
unlink(dirname(__FILE__) . "/moon_angle_data.txt");
$onp = strpos($cont, $thing);

if ($onp === false) {
header("Location: http://www.rjmprogramming.com.au/nowayjose/");
} else {
$rest = substr($cont, ($onp + strlen($thing)));
$restx = substr($cont, ($onp + strlen($thing)), 300);
if (strpos($restx, "South") !== false) $latxprefix = "-";
if (strpos($restx, "West") !== false) $longxprefix = "-";

$rest = str_replace("<", "&lt;", $rest);
$rest = str_replace(">", "&gt;", $rest);
$onp2 = strpos($rest, "UTC");
$onp3 = strpos($rest, "td class=r");
if ($onp2 === false || $onp3 == false) {
header("Location: http://www.rjmprogramming.com.au/no_way_jose/");
} else {
$todayis = substr($rest, 0, ($onp2 + 3));
//$todayis = $rest;
$rest = substr($rest, $onp3);
$onp3 = strpos($rest, "td class=r");
$iijj = strlen("td class=r") + 4;
$ssplatd = $latxprefix;
while (substr($rest, ($onp3 + $iijj), 1) == "-" || (substr($rest, ($onp3 + $iijj), 1) >= "0" && substr($rest, ($onp3 + $iijj), 1) <= "9")) {
  $ssplatd .= substr($rest, ($onp3 + $iijj), 1);
  $iijj += 1;
}
while (substr($rest, ($onp3 + $iijj), 1) < "0" || substr($rest, ($onp3 + $iijj), 1) > "9") {
  $iijj += 1;
}
while (substr($rest, ($onp3 + $iijj), 1) == "-" || (substr($rest, ($onp3 + $iijj), 1) >= "0" && substr($rest, ($onp3 + $iijj), 1) <= "9")) {
  $ssplatm .= substr($rest, ($onp3 + $iijj), 1);
  $iijj += 1;
}
$rest = substr($rest, ($onp3 + $iijj));
$onp3 = strpos($rest, "td class=r");
$iijj = strlen("td class=r") + 4;
$ssplongd = $longxprefix;
while (substr($rest, ($onp3 + $iijj), 1) == "-" || (substr($rest, ($onp3 + $iijj), 1) >= "0" && substr($rest, ($onp3 + $iijj), 1) <= "9")) {
  $ssplongd .= substr($rest, ($onp3 + $iijj), 1);
  $iijj += 1;
}
while (substr($rest, ($onp3 + $iijj), 1) < "0" || substr($rest, ($onp3 + $iijj), 1) > "9") {
  $iijj += 1;
}
while (substr($rest, ($onp3 + $iijj), 1) == "-" || (substr($rest, ($onp3 + $iijj), 1) >= "0" && substr($rest, ($onp3 + $iijj), 1) <= "9")) {
  $ssplongm .= substr($rest, ($onp3 + $iijj), 1);
  $iijj += 1;
}
}

}
}


echo '<html>';
echo '<head>';
echo '<title>Noon Moon Angle viewed from Earth Today at Latitude and Longitude via RJM Programming' . $extrainfo . '</title>';
echo '<meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1" />';
echo "\n <style>";
echo "\n .skyandground { ";
echo "\n height: 70px; ";
echo "\n width: 70px; ";
echo "\n -moz-border-radius: 35px; ";
echo "\n border-radius: 35px; ";
echo "\n } ";
//$todayis .= " lat " . $ssplatd . " " . $ssplatm . " long " . $ssplongd . " " . $ssplongm;

for ($ilat=-90; $ilat <= -1; $ilat++) {
   $exbit = "";
   $ilatd .= "<option value='" . $ilat . ".0'>" . $ilat . "</option>";
   if ("$ssplatd" == "$ilat") $exbit = ' selected="selected"';
   $jlatd .= "<option value='" . $ilat . ".0'" . $exbit . ">" . $ilat . "</option>";
}
for ($ilat=0; $ilat <= 90; $ilat++) {
echo "\n #mydodah" . $ilat;
echo "\n { ";
 echo "transform: rotate(" . $ilat . "deg); \n";
 echo "-ms-transform: rotate(" . $ilat . "deg); /* IE 9 */ \n";
 echo "-webkit-transform: rotate(" . $ilat . "deg); /* Safari and Chrome */ \n";
echo "\n } ";

   $exbit = "";
   $exmbit = "";
   $ilatd .= "<option value='+" . $ilat . ".0'>+" . $ilat . "</option>";
   if ("$ssplatd" == "$ilat") $exbit = ' selected="selected"';
   $jlatd .= "<option value='+" . $ilat . ".0'" . $exbit . ">+" . $ilat . "</option>";
   if ($ilat <= 59) {
    $ilatm .= "<option value='" . $ilat . ".0'>" . $ilat . "</option>";
    $ilats .= "<option value='" . $ilat . ".0'>" . $ilat . "</option>";
    if ("$ssplatm" == "$ilat") $exmbit = ' selected="selected"';
    $jlatm .= "<option value='" . $ilat . ".0'" . $exmbit . ">" . $ilat . "</option>";
    $jlats .= "<option value='" . $ilat . ".0'>" . $ilat . "</option>";
   }
}
for ($ilong=-180; $ilong <= -1; $ilong++) {
   $exbit = "";
   $ilongd .= "<option value='" . $ilong . ".0'>" . $ilong . "</option>";
   if ("$ssplongd" == "$ilong") $exbit = ' selected="selected"';
   $jlongd .= "<option value='" . $ilong . ".0'" . $exbit . ">" . $ilong . "</option>";
}
for ($ilong=0; $ilong <= 180; $ilong++) {
   $exbit = "";
   $exmbit = "";
   $ilongd .= "<option value='+" . $ilong . ".0'>+" . $ilong . "</option>";
   if ("$ssplongd" == "$ilong") $exbit = ' selected="selected"';
   $jlongd .= "<option value='+" . $ilong . ".0'" . $exbit . ">+" . $ilong . "</option>";
   if ($ilong <= 59) {
    $ilongm .= "<option value='" . $ilong . ".0'>" . $ilong . "</option>";
    $ilongs .= "<option value='" . $ilong . ".0'>" . $ilong . "</option>";
    if ("$ssplongm" == "$ilong") $exmbit = ' selected="selected"';
    $jlongm .= "<option value='" . $ilong . ".0'" . $exmbit . ">" . $ilong . "</option>";
    $jlongs .= "<option value='" . $ilong . ".0'>" . $ilong . "</option>";
   }
}
$ilatd .= "</select>";
$ilongd .= "</select>";
$ilatm .= "</select>";
$ilongm .= "</select>";
$ilats .= "</select>";
$ilongs .= "</select>";
$jlatd .= "</select>";
$jlongd .= "</select>";
$jlatm .= "</select>";
$jlongm .= "</select>";
$jlats .= "</select>";
$jlongs .= "</select>";
echo "\n </style>";

?>
<script>
function ca() {
  var mya=document.getElementById("mya");
  mya.innerHTML = "Calculate Noon Moon Angle Today For";
  mya.style.borderColor = "red";
  mya.style.backgroundColor = "yellow";
}

function anothergo() {
  var latd=document.getElementById("latd");
  var latm=document.getElementById("latm");
  var lats=document.getElementById("lats");
  var longd=document.getElementById("longd");
  var longm=document.getElementById("longm");
  var longs=document.getElementById("longs");

  var tald=document.getElementById("tald");
  var talm=document.getElementById("talm");
  var tals=document.getElementById("tals");
  var gnold=document.getElementById("gnold");
  var gnolm=document.getElementById("gnolm");
  var gnols=document.getElementById("gnols");


  var latsign = "+";
  if (latd.value.replace("-", "") != latd.value) latsign = "-";
  var longsign = "+";
  if (longd.value.replace("-", "") != longd.value) longsign = "-";
  var alat = eval(latd.value);
  alat = Math.abs(alat);
  var alatsuffix = 0;
  var along = eval(longd.value);
  along = Math.abs(along);
  var alongsuffix = 0;
  if (latm.value != "0" || lats.value != "0") {
    alatsuffix = eval((latm.value) / 60.0);
    alatsuffix = eval(alatsuffix + (lats.value) / 3600.0);
  } else if (latd.value.replace(".", "") != latd.value) {
    alatsuffix = 0;
  }
  if (longm.value != "0" || longs.value != "0") {
    alongsuffix = eval((longm.value) / 60.0);
    alongsuffix = eval(alongsuffix + (longs.value) / 3600.0);
  } else if (longd.value.replace(".", "") != longd.value) {
    alongsuffix = 0;
  }
  
  var lata = eval(alatsuffix + alat);
  var latis=latsign + lata;
<?php
  if (!isset($_GET['done'])) echo 'latis=prompt("Please enter decimal degrees of Latitude 1", latsign + lata); ' . "\n";
?>

  var xlatis = latis.replace("-", "").replace("+", "");
  if (xlatis == latis) latis = "+" + latis;
  var longa = eval(alongsuffix + along);
  var longis=longsign + longa;
<?php
  if (!isset($_GET['done'])) echo 'longis=prompt("Please enter decimal degrees of Longitude 1", longsign + longa); ' . "\n";
?>
  var xlongis = longis.replace("-", "").replace("+", "");
  if (xlongis == longis) longis = "+" + longis;
  

  latsign = "+";
  if (tald.value.replace("-", "") != tald.value) latsign = "-";
  longsign = "+";
  if (gnold.value.replace("-", "") != gnold.value) longsign = "-";
  alat = eval(tald.value);
  alat = Math.abs(alat);
  alatsuffix = 0;
  along = eval(gnold.value);
  along = Math.abs(along);
  alongsuffix = 0;
  if (talm.value != "0" || tals.value != "0") {
    alatsuffix = eval((talm.value) / 60.0);
    alatsuffix = eval(alatsuffix + (tals.value) / 3600.0);
  } else if (tald.value.replace(".", "") != tald.value) {
    alatsuffix = 0;
  }
  if (gnolm.value != "0" || gnols.value != "0") {
    alongsuffix = eval((gnolm.value) / 60.0);
    alongsuffix = eval(alongsuffix + (gnols.value) / 3600.0);
  } else if (gnold.value.replace(".", "") != gnold.value) {
    alongsuffix = 0;
  }
  
  var tala = eval(alatsuffix + alat);
  var talis=latsign + tala; // prompt("Please enter decimal degrees of Latitude 2", latsign + tala);
  var xtalis = talis.replace("-", "").replace("+", "");
  if (xtalis == talis) talis = "+" + talis;
  var gnola = eval(alongsuffix + along);
  var gnolis=longsign + gnola;  //prompt("Please enter decimal degrees of Longitude 2", longsign + gnola);
  var xgnolis = gnolis.replace("-", "").replace("+", "");
  if (xgnolis == gnolis) gnolis = "+" + gnolis;
  
  var zenith_angle = 0.0;
  if ((talis >= 0.0 && latis >= 0.0) || (talis <= 0.0 && latis <= 0.0) || 1 == 1) {
    zenith_angle = eval((latis)-(talis));
  } else {
    zenith_angle = eval((latis)+(talis));
  }
  var ourang = 90.0 - Math.abs(zenith_angle);
  if (ourang < 0) ourang = 0;
  
  var osg = document.getElementById("sandg");
  osg.style.display = 'block';
  var omyh2 = document.getElementById("mydodah");
  omyh2.style.display = 'block';
  var xourang = ourang.toString();
  var ipf=xourang.indexOf(".");
  var pourang = xourang;
  if (ipf != -1) {
   pourang = xourang.substring(0, ipf);
   pourang = pourang.replace(".", "");
   omyh2.innerHTML = '<div id="mydodah' + pourang + '" style="height:100px; width: 100px;"><h2 class="skyandground" style="color: red;" id="myh2">&lt;----o<br>&lt;----o<br>&lt;----o</h2></div>';
   xourang = xourang.substring(0, (ipf + 2));
  } else {
   omyh2.innerHTML = '<div id="mydodah' + ourang + '" style="height:100px; width: 100px;"><h2 class="skyandground" style="color: red;" id="myh2">&lt;----o<br>&lt;----o<br>&lt;----o</h2></div>';
  }
  
  var mya=document.getElementById("mya");
  mya.innerHTML = "Noon Moon Angle from Earth Today for Latitude,Longitude below is " + xourang + " degrees";

  var oif=document.getElementById("oif");
  var latbitmore = "";
  var longbitmore = "";
  if (xlatis.replace(".", "") == xlatis) latbitmore = ".0";
  if (xlongis.replace(".", "") == xlongis) longbitmore = ".0";
  //alert("https://maps.google.com/maps?saddr=" + latis + "," + longis + "&daddr=" + talis + "," + gnolis);
  //oif.innerHTML = '<iframe width="940px;" height="550px;" id="if" src="' + "https://maps.google.com/maps?saddr=" + latis + "," + longis + "&daddr=" + talis + "," + gnolis + '" title="Thanks Google Maps"></iframe>';
  window.open("https://maps.google.com/maps?saddr=" + latis + "," + longis + "&daddr=" + talis + "," + gnolis, "_blank", "left=120,top=330,width=830,height=430");
}

</script>

</head>
<body>
<table style="background-color: lightblue;">
<tr><td style="border: solid 3px green; padding: 3px 3px 3px 3px;">
<p id="todayis"><?php echo "As of ... " . $todayis; ?></p><a id='mya' style="border: solid 2px white; padding: 2px 2px 2px 2px;" onclick='anothergo();' title='Another noon moon angle calculation?' href='#'>Another noon moon angle calculation?  </a><br>
<hr></hr> Latitude 1: <?php echo $ilatd; ?><?php echo $ilatm; ?><?php echo $ilats; ?> Longitude 1: <?php echo $ilongd; ?></select><?php echo $ilongm; ?><?php echo $ilongs; ?> 
<div id='subsolar_point' style='display:none;'><br> Latitude 2: <?php echo $jlatd; ?><?php echo $jlatm; ?><?php echo $jlats; ?> Longitude 2: <?php echo $jlongd; ?></select><?php echo $jlongm; ?><?php echo $jlongs; ?></div>
</td></tr><tr><td id="oif">
</td></tr></table>
<?php
if ($zimg != "") {
 echo $zimg . "\n";
} else {
 echo '<div class="skyandground" id="sandg" style="background-color: lightgray; display: none;"></div>' . "\n";
}
?>
<div id="mydodah" style="background-color: ochre; display: none; height:100px; width: 100px; border-bottom: 5px solid green; ">
</div>
</body>
</html>
