<?php
function clean_csv_commas($csv, $indelimis) {  // thanks to https://stackoverflow.com/questions/10739016/how-to-remove-commas-between-double-quotes-in-php
    $until=true;
    $qdelim = '"';
    $bqdelim = "'";
    $altbqdelim = "&amp;apos;";
    $comma = ',';
    $altcomma = '&amp;comma;';
    $altqdelim = '&amp;quot;';
    $prevcomma=FALSE;
    $aftcomma=FALSE;
    if ($indelimis == "'") {
      $qdelim=$indelimis;
      $bqdelim='"';
      $altqdelim = '&amp;apos;';
      $altbqdelim = '&amp;quot;';
    } 
    $len = strlen($csv);
    $inside_block = FALSE;
    $out='';
    $rep='';
    for ($i=0; $i<$len; $i++) {
        if (ord($csv[$i]) < 32) {
                $inside_block=FALSE;
                $until=false;
        } else if ($csv[$i] == $qdelim) {
            if ($i == 0) {
                $prevcomma=FALSE;
                $inside_block=TRUE;
            } else if (($i + 1) == $len) {
                $aftcomma=FALSE;
                $inside_block=FALSE;
            } else {
             if ($csv[-1 + $i] == ',' || ord($csv[-1 + $i]) < 32) { $prevcomma=TRUE; } else { $prevcomma=FALSE;   }
             if ($csv[1 + $i] == ',' || ord($csv[1 + $i]) < 32) { $aftcomma=TRUE; } else { $aftcomma=FALSE;   }
             if ($inside_block) {
                 if ($aftcomma) { $inside_block=FALSE; }
             } else {
                if ($prevcomma) { $inside_block=TRUE; }
             }
            }
        }

        //if ($until) {
        //    $rep.="\n" . $csv[$i] . " \$inside_block=" . $inside_block . " \$prevcomma=" . $prevcomma . " \$aftcomma=" . $aftcomma;
        //}
        if ($csv[$i] == $comma && $inside_block) {
        if ($until) {
            $rep.="\n" . $csv[$i] . " \$inside-block=" . $inside_block . " \$prevcomma=" . $prevcomma . " \$aftcomma=" . $aftcomma;
        }
            $out.=$altcomma;
        } else if ($csv[$i] == $qdelim && $inside_block && !$aftcomma && !$prevcomma) {
            $out.=$altqdelim;
        } else if ($csv[$i] == $bqdelim && $inside_block) {
            $out.=$altbqdelim;
        } else {
            $out.=$csv[$i];
        }

    }
    if ($indelimis == "") {
      //file_put_contents("zerocsv.xxx", $csv);
      //file_put_contents("one_csv.xxx", $rep);
      //file_put_contents("onecsv.xxx", $out);
      $out=str_replace("'","",str_replace('"','',clean_csv_commas($out, "'")));
      //file_put_contents("twocsv.xxx", $out);
    }
    return $out;
}

  if (isset($_GET['csvfile'])) {
    $fname=str_replace("+"," ",urldecode($_GET['csvfile']));
    $csvcont='';
    if (strpos(strtolower($fname),"http") !== false) {
     $csvcont=file_get_contents($fname);
    } else if (file_exists($fname)) {
     $csvcont=file_get_contents($fname);
    }
    echo '<html><body onload="parent.document.getElementById(' . "'" . ifcsv . "'" . ').srcdoc=' . "'<pre>" . str_replace("\r","<br>",str_replace("\n","<br>",str_replace("\r\n","<br>",str_replace("'","",clean_csv_commas($csvcont,""))))) . "</pre>'" . ';"></body></html>';
    exit;
  }
  $prehtml = "";
  $midhtml = "";
  $posthtml = "";
  function retval($inv) {
    if (strpos($inv, "E-") !== false) return "0";
    return $inv;
  }
?>

<html>
<title>Survey Traverse Calculation - RJM Programming - Copyright &copy; 2013 rjmprogramming.com.au all rights reserved.</title>
<head>
<meta charset="utf-8">
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<style>
table {
 border: 1px solid black;
}

.lightgray {
 background-color: lightgray;
}

.lightblue {
 background-color: lightblue;
}

.white {
 background-color: white;
}

.lightgreen {
 background-color: lightgreen;
}

.yellow {
 background-color: yellow;
}

.cyellow {
 color: yellow;
}

.pink {
 background-color: pink;
}
</style>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    var ibit = "";
    var icb = "ichart_div";
var inum = 0;
var xnum = 0;
var ynum = 0;
var numlegs=0;
var firstBearing=0;
var currb = 0.0;
var currx = 0.0;
var curry = 0.0;
var lastBearing="-0";
var adjust=0;
var lbbit = "";
var lbval = "";
var ballval = "";
var prmstr = window.location.search.substr(1);
var prmarr = prmstr.split ("&");
var params = {};
var parambits = {};
var enarr = {};
var enarrstr = "";
var eoff=0.0;
var noff=0.0;

if (prmstr.indexOf("=") != (0 - 1)) {
 for ( var i = 0; i < prmarr.length; i++) {
    var tmparr = prmarr[i].split("=");
    params[tmparr[0]] = tmparr[1];
    //alert(tmparr[1]);
    numlegs = -1;
    parambits = tmparr[1].split(",");
 }
}


<?php 
if (!isset($_GET['coords'])) $htm = "<html><head><script> " . "\n";
//if (isset($_GET['coords'])) {
$htm .= ' google.load("visualization", "1", {packages:["corechart"]}); ' . "\n";
$htm .= " google.setOnLoadCallback(drawChart); \n";
//$htm .= " var button = document.getElementById('b1'); \n";
$htm .= " var button = null; \n";
$htm .= " var dataTable = null; \n";
$htm .= " function drawChart() { \n";
        
$htm .= " dataTable = new google.visualization.DataTable(); \n";
$htm .= " dataTable.addColumn('number', 'East'); \n";
$htm .= " dataTable.addColumn('number', 'North'); \n";
$htm .= " dataTable.addRows([ \n";
$prehtm = $htm;

 if (isset($_GET['coords'])) {
    $htm .= $_GET['coords'];
    $midhtm .= $_GET['coords'];
 } else if (isset($_GET['traverse'])) {
  $xxx = "0" . $_GET['traverse'] . ",";
  $legs = str_replace(",","",substr($xxx, 0, (strpos($xxx, ","))));
  for ($il=0; $il<$legs; $il++) {
    $htm .= "[".($il * 1.0).",".($il * 1.0)."],\n";
    $midhtm .= "[".($il * 1.0).",".($il * 1.0)."],\n";
  }
  $htm .= "[".($legs * 1.0).",".($legs * 1.0)."]\n";
  $midhtm .= "[".($legs * 1.0).",".($legs * 1.0)."]\n";
 }

$htm .= "        ]);\n";
$htm .= "        var dataView = new google.visualization.DataView(dataTable);\n";

$htm .= "        var options = {\n";
$htm .= "          backgroundColor: 'lightgray',\n";
$htm .= "          title: 'Survey Traverse'\n";
$htm .= "        };\n";

$htm .= "        var chart = new google.visualization.LineChart(document.getElementById('chart_div')); \n";
//$htm .= "        button.disabled = true; \n";
//$htm .= "        google.visualization.events.addListener(chart, 'ready', \n";
//$htm .= "          function() { \n";
//$htm .= "              button.disabled = false; \n";
//$htm .= "              }); \n";
//$htm .= "              button.onclick = function() { \n";
//$htm .= "              dataTable.setValue(inum, xnum, ynum); \n";
//$htm .= "              chart.draw(dataTable, options); \n";
//$htm .= "              } \n";

$htm .= "        chart.draw(dataTable, options); \n";
$htm .= "        } \n";
$posthtm .= "        ]);\n";
$posthtm .= "        var dataView = new google.visualization.DataView(dataTable);\n";

$posthtm .= "        var options = {\n";
$posthtm .= "          backgroundColor: 'lightgray',\n";
$posthtm .= "          title: 'Survey Traverse'\n";
$posthtm .= "        };\n";

$posthtm .= "        var chart = new google.visualization.LineChart(document.getElementById('chart_div')); \n";
//$posthtm .= "        button.disabled = true; \n";
$posthtm .= "        google.visualization.events.addListener(chart, 'ready', \n";
$posthtm .= "          function() { \n";
$posthtm .= "              button.disabled = false; \n";
$posthtm .= "              }); \n";
//$posthtm .= "              button.onclick = function() { \n";
//$posthtm .= "              dataTable.setValue(inum, xnum, ynum); \n";
//$posthtm .= "              chart.draw(dataTable, options); \n";
//$posthtm .= "              } \n";

$posthtm .= "        chart.draw(dataTable, options); \n";
$posthtm .= "        } \n";
 if (isset($_GET['coords'])) {
   echo $htm;
 }
//}
?>
      

function ifzeromakezero(innum) {
 var cinnum = innum.toString();
 if (cinnum.indexOf("e-") != (0 - 1)) {
   return 0.000;
 }
 return innum;
}

function checkAtStart() {    // check query string
 if (numlegs != 0) {
  //alert(params['traverse']);
  processTraverse(params['traverse']);
 } else {
  numlegs = prompt("Please tell me how many Traverse legs you have", "0");
  if (numlegs != "0" && numlegs != null) {
   window.location = window.location + "?traverse=" + numlegs + ",0";
  }
 }
}

function fixthis(afix) {
 var areal = 0.0;
 var breal = 0.0;
 var creal = 0.0;
 var ien = 0;
 areal = eval(afix);
 //alert(enarrstr);
 enarr = enarrstr.split(",");
      //var zicd = document.getElementById(icb);
      //icb = "ichart_div2";
 //ibit = "";
 comma = "";
 for ( var j = 0; j <= numlegs; j++) {
     document.getElementById('E' + j).value = ifzeromakezero(enarr[ien]);
     ien += 1;
     document.getElementById('N' + j).value = ifzeromakezero(enarr[ien]);
     //ibit += comma + "[" + xnum + "," + ynum + "]";
     //comma = ",";
     //if (j == numlegs) {
      //zicd.src = "./SurveyTraverse.php?coords=" + ibit;
      //zicd.style.display = "block";
     //}
     ien += 1;
     breal = eval(document.getElementById('A' + j).value);
     creal = eval(areal+breal);
     while (creal >= 360.0) {
       creal = eval(creal-360.0);
     }
     while (creal < 0.0) {
       creal = eval(creal+360.0);
     }
     document.getElementById('A' + j).value = ifzeromakezero(creal);
     var deg = eval(creal);
       document.getElementById('A' + j).title = document.getElementById('A' + j).title + " ... DMS " + Math.floor(deg) + "°";
       deg = deg - Math.floor(deg);
     if (Math.floor((deg * 60.0)) < 10.0) document.getElementById('A' + j).title = document.getElementById('A' + j).title + "0";
       document.getElementById('A' + j).title = document.getElementById('A' + j).title + Math.floor((deg * 60.0)) + "'";
        deg = (deg * 60.0 - Math.floor((deg * 60.0))) * 60.0;
     if (deg < 10) document.getElementById('A' + j).title = document.getElementById('A' + j).title + "0";
       document.getElementById('A' + j).title = document.getElementById('A' + j).title + deg + '"';
 }
 //alert(afix + " " + areal);
 adjust = -1;
 document.getElementById('Ball').innerHTML = ""; //lbbit;
 document.getElementById('lastBearing').value = ifzeromakezero(lbval);
 analyze(document.getElementById('adjust'));
 document.getElementById('adjust').value = 'Bowditch Adjust (double click for Line Graph)';
}

function analyze(but) {
 if (adjust <= 0) {
   if (adjust == 0) {
     var kk;
     enarrstr = "";
     lbbit = document.getElementById('Ball').innerHTML;
     lbval = document.getElementById('lastBearing').value;
   for ( var jj = 0; jj < numlegs; jj++) {
     kk = jj+1;
     if (adjust == 0) {
      enarrstr = enarrstr + document.getElementById('E' + jj).value + ",";
      enarrstr = enarrstr + document.getElementById('N' + jj).value + ",";
      if (kk == numlegs) {
     if (adjust == 0) {
      enarrstr = enarrstr + document.getElementById('E' + numlegs).value + ",";
      enarrstr = enarrstr + document.getElementById('N' + numlegs).value + ",";
     }
     }
     }
    }
   }
   document.getElementById('adjust').title = 'Bowditch Adjust';
   document.getElementById('adjust').value = 'Adjust (double click for Line Graph)';
   var x360 = 0.0;
   var areal = 0.0;
   var breal = 0.0;
   var deltae = 0.0;
   var deltan = 0.0;
   var badj = 0.0;
   var thise="", thisn="", laste="", lastn="", lastb="", lpre=".0", vareal="", k;
   laste = document.getElementById('E' + numlegs).value;
   lastn = document.getElementById('N' + numlegs).value;
   currb = eval(document.getElementById('firstBearing').value);
   currx = eval(document.getElementById('E0').value);
   curry = eval(document.getElementById('N0').value);
   for ( var j = 0; j < numlegs; j++) {
     k = j+1;
     thise = document.getElementById('E' + k).value;
     thisn = document.getElementById('N' + k).value;
     areal = eval(currb);
     breal = eval(document.getElementById('A' + j).value);
     areal += breal;
     currb = eval(areal);
     x360 = eval(currb);
     while (x360 >= 360.0) {
       x360 = eval(x360-360.0);
     }
     document.getElementById('B' + j).innerHTML = ifzeromakezero(x360);
     deltae = eval(Math.sin(currb * Math.PI / 180.0) * document.getElementById('L' + j).value);
     deltan = eval(Math.cos(currb * Math.PI / 180.0) * document.getElementById('L' + j).value);
     document.getElementById('dE' + j).innerHTML = ifzeromakezero(deltae);
     document.getElementById('dN' + j).innerHTML = ifzeromakezero(deltan);
     currx += deltae;
     curry += deltan;
     //alert(j);
     document.getElementById('E' + k).value = ifzeromakezero(currx);
     document.getElementById('N' + k).value = ifzeromakezero(curry);
     

  inum = j;
  xnum = document.getElementById('E' + j).value;
  ynum = document.getElementById('N' + j).value;
  //button.click();
  
      ibit += "[" + xnum + "," + ynum + "],";


     //alert("k");
           if (k == numlegs) {
      eoff = ifzeromakezero(eval(-currx/numlegs));
      noff = ifzeromakezero(eval(-curry/numlegs));
      ibit += "[" + document.getElementById('E' + k).value + "," + document.getElementById('N' + k).value + "]";
      //alert("Eoff=" + eoff);
   //alert(ibit);
      var icd = document.getElementById(icb);
      icb = "ichart_div2";
      icd.src = "./SurveyTraverse.php?coords=" + ibit;
      icd.style.display = "block";
           }
     if (thise.replace("-0.000","").length != 0 && thisn.replace("-0.000","").length != 0) {
      if (k == numlegs) {
     if (adjust == 0) {
      enarrstr = enarrstr + document.getElementById('E' + numlegs).value + ",";
      enarrstr = enarrstr + document.getElementById('N' + numlegs).value + ",";
     }
       document.getElementById('Eall').innerHTML = ifzeromakezero(eval(thise-currx));
       document.getElementById('Nall').innerHTML = ifzeromakezero(eval(thisn-curry));
      } else {      
       document.getElementById('eE' + k).innerHTML = ifzeromakezero(eval(thise-currx));
       document.getElementById('eN' + k).innerHTML = ifzeromakezero(eval(thisn-curry));
      }
     }
     //alert(j);
     areal = eval(currb);
     areal += 180.0;
     currb = eval(areal);
     //alert("j");
   }
   //alert(currx + " " + curry);
   //thise = laste;
   //thisn = lastn;
   //alert(laste + " " + lastn);
   if (laste.replace("-0.000","").length != 0 && lastn.replace("-0.000","").length != 0) {
      document.getElementById('Eall').innerHTML = ifzeromakezero(eval(laste-currx));
      document.getElementById('Nall').innerHTML = ifzeromakezero(eval(lastn-curry));
   }
   
   //if (adjust == 0) {
   if (document.getElementById('A' + numlegs).value.replace("-0").length > 0) {
    if (document.getElementById('lastBearing').value.replace("-0").length > 0) {
     lastb = eval(document.getElementById('lastBearing').value);
     areal = eval(currb);
     breal = eval(document.getElementById('A' + numlegs).value);
     areal -= breal;
     //areal -= 180.0;
     currb = eval(areal);
     x360 = eval(currb);
     while (x360 >= 360.0) {
       x360 = eval(x360-360.0);
     }
     areal = eval(lastb-x360);
     while (areal > 180.0) {
       areal = eval(areal-360.0);
     }
     while (areal <= -180.0) {
       areal = eval(areal+360.0);
     }
     
     badj = eval(numlegs);
     
     var xoff = eval(document.getElementById('Eall').innerHTML);
     var yoff = eval(document.getElementById('Nall').innerHTML);

     if (xoff.length > 0 && yoff.length > 0) {
     eoff = eval(xoff/badj);
     noff = eval(yoff/badj);
     }
     
     badj = eval(badj+1);
     vareal = areal.toString();
     //alert("Vareal=" + vareal + " badj=" + badj);
     if (vareal.indexOf(".") == (0 - 1)) vareal = vareal + ".0";
     badj = eval(areal/badj);
     vareal = badj.toString();
     if (vareal.indexOf(".") == (0 - 1)) vareal = vareal + ".0";
     //alert("vareal=" + vareal + " badj=" + badj);
     if (adjust == 0) {
       document.getElementById('Ball').innerHTML = ifzeromakezero(areal);
       document.getElementById('Ball').innerHTML += "&nbsp;<input type='button' onclick='fixthis(" + '"' + vareal + '"' + ");' value='Adjust Angles'></input>";
       document.getElementById('lastBearing').value = ifzeromakezero(areal);
     }
    }
   //}
   }
   adjust = 1;
 } else {
// alert("eoff=" + eoff + " noFF=" + noff);
   but.style.display = 'none';
   var xxoff = eval(eoff);
   var yyoff = eval(noff);
   var eis=0.0, nis=0.0;
      ibit = "[" + document.getElementById('E' + 0).value + "," + document.getElementById('N' + 0).value + "]";
   for ( var jj = 1; jj <= numlegs; jj++) {
      eis = eval(document.getElementById('E' + jj).value);
      nis = eval(document.getElementById('N' + jj).value);      
      document.getElementById('E' + jj).value = ifzeromakezero(eval(xxoff + eis));    
      if (document.getElementById('E' + jj).value.indexOf("e-") != (0 - 1)) document.getElementById('E' + jj).value = "0.000";
      document.getElementById('N' + jj).value = ifzeromakezero(eval(yyoff + nis)); 
      if (document.getElementById('N' + jj).value.indexOf("e-") != (0 - 1)) document.getElementById('N' + jj).value = "0.000";

  inum = jj;
  xnum = document.getElementById('E' + jj).value;
  ynum = document.getElementById('N' + jj).value;
  
      ibit += ",[" + xnum + "," + ynum + "]";

   //alert("xxoff=" + xxoff + " yyoff=" + yyoff);
      xxoff += eval(eoff);    
      yyoff += eval(noff);    
   }
   //alert(ibit);
      var xicd = document.getElementById(icb);
      icb = "ichart_div2";
      xicd.src = "./SurveyTraverse.php?coords=" + ibit;
      xicd.style.display = "block";


 }
}

function anglechange(ame) {
  var deg=0.0;
  var mn=0;
  var secs=0;
  if (ame.value.indexOf(".") == (0 - 1)) {
     mn = prompt("Enter the minutes value of your " + ame.value + " degree angle/bearing", "0");
     if (mn == null) mn = "0";
     if (mn.length == 1) mn = "0" + mn;
     secs = prompt("Enter the seconds value of your " + ame.value + " degree, " + mn + " minutes angle/bearing", "0");
     if (secs == null) secs = "0";
     if (secs.indexOf(".") == 0) secs = "00" + secs;
     if (secs.length == 1 || secs.indexOf(".") == 1) secs = "0" + secs;
     if (secs.indexOf(".") == (0 - 1)) secs = secs + ".0";
     if (ame.value.indexOf("-") == (0 - 1)) {
       deg = eval(ame.value);
       if (deg >= 360) {
        while (deg >= 360) {
         deg = deg - 360;
        }
       }
       deg = eval(deg + (mn / 60.0));
       deg = eval(deg + (secs / 3600.0));
       ame.value = deg;
       ame.title = ame.title + " ... DMS " + Math.floor(deg) + "°";
        deg = deg - Math.floor(deg);
     if (Math.floor((deg * 60.0)) < 10.0) ame.title = ame.title + "0";
        ame.title = ame.title + Math.floor((deg * 60.0)) + "'";
        deg = (deg * 60.0 - Math.floor((deg * 60.0))) * 60.0;
     if (deg < 10) ame.title = ame.title + "0";
        ame.title = ame.title + deg + '"';

     } else {
       deg = eval(ame.value);
       //alert(ame.value + " " + deg);
       deg = -deg;
       //alert(ame.value + "+" + deg);
       if (deg >= 360) {
        while (deg >= 360) {
         deg = deg - 360;
       //alert(ame.value + ":" + deg);
        }
       }
       deg = eval(deg + (mn / 60.0));
       deg = eval(deg + (secs / 3600.0));
       //deg = -deg;
       deg = eval(360.0 - deg);
       ame.value = deg;
       ame.title = ame.title + " ... DMS " + Math.floor(deg) + "°";
       deg = deg - Math.floor(deg);
     if (Math.floor((deg * 60.0)) < 10.0) ame.title = ame.title + "0";
       ame.title = ame.title + Math.floor((deg * 60.0)) + "'";
        deg = (deg * 60.0 - Math.floor((deg * 60.0))) * 60.0;
     if (deg < 10) ame.title = ame.title + "0";
       ame.title = ame.title + deg + '"';

     }
     
  } else {
     if (ame.value.indexOf("-") == (0 - 1)) {
       deg = eval(ame.value);
       if (deg >= 360) {
        while (deg >= 360) {
         deg = deg - 360;
        }
        ame.value = deg;
        ame.title = ame.title + " ... DMS " + Math.floor(deg) + "°";
        deg = deg - Math.floor(deg);
     if (Math.floor((deg * 60.0)) < 10.0) ame.title = ame.title + "0";
        ame.title = ame.title + Math.floor((deg * 60.0)) + "'";
        deg = deg * 60.0 - Math.floor((deg * 60.0));
     if (deg < 10) ame.title = ame.title + "0";
        ame.title = ame.title + deg + '"';
       } else {
        deg = ame.value;
        ame.title = ame.title + " " + Math.floor(deg) + "°";
        deg = deg - Math.floor(deg);
     if (Math.floor((deg * 60.0)) < 10.0) ame.title = ame.title + "0";
        ame.title = ame.title + Math.floor((deg * 60.0)) + "'";
        deg = (deg * 60.0 - Math.floor((deg * 60.0))) * 60.0;
     if (deg < 10) ame.title = ame.title + "0";
        ame.title = ame.title + deg + '"';
       }
     } else {
       deg = eval(ame.value);
       deg = -deg;
       if (deg >= 360) {
        while (deg >= 360) {
         deg = deg - 360;
        }
       }
       ame.value = eval(360.0 - deg);
       deg = ame.value;
       ame.title = ame.title + " ... DMS " + Math.floor(deg) + "°";
       deg = deg - Math.floor(deg);
     if (Math.floor((deg * 60.0)) < 10.0) ame.title = ame.title + "0";
       ame.title = ame.title + Math.floor((deg * 60.0)) + "'";
        deg = (deg * 60.0 - Math.floor((deg * 60.0))) * 60.0;
     if (deg < 10) ame.title = ame.title + "0";
       ame.title = ame.title + deg + '"';
     }
  }
}

function achange() {
}

function processTraverse(ininfo) {   // data items
 for ( var i = 0; i < parambits.length; i++) {
    if (numlegs == (0 - 1)) {
      numlegs = parambits[i];
      if (parambits.length > 1) firstBearing = parambits[i+1];
    }
    //alert(parambits[i]);
 }
 var zero="0.000";
 var lightblue = "";
 if (numlegs > 0) {
    document.write("<h1 align='center'>Survey Traverse Calculation</h1><div align='center'><input id='adjust' type='button' title='Analyze' onclick='analyze(this);' value='Analyze when Fully Filled Out Below'></input>&nbsp;<a title='Restart' target='_blank' href='SurveyTraverse.php'>Restart</a></div><br><br><div align='center'><table><tbody>");
    document.write("<tr class='lightgray'><td title='Please enter Angles in Degrees'>Angles</td><td title='Please enter Bearings in Degrees'>Bearings</td><td>Lengths</td><td>Delta E</td><td>Delta N</td><td>E</td><td>N</td><td>Point</td></tr>");
    document.write("<tr class='yellow'><td></td><td><input  title='Entries with . are assumed as decimal degrees, else enter integer angle Degree value and will prompt for rest' onchange='anglechange(this);'  type='text' id='firstBearing' value='" + firstBearing + "'></input></td><td></td><td></td><td></td><td></td><td></td><td><input class='lightgray' onclick='achange();' type='text' id='firstName'></input></td></tr>");
    for ( var j = 0; j < numlegs; j++) {
      document.write("<tr class='pink'><td><input title='Entries with . are assumed as decimal degrees, else enter integer angle Degree value and will prompt for rest' onchange='anglechange(this);' type='text' id='A" + j + "' value='0'></input></td><td id='dB" + j + "'></td><td></td><td><p class='cyellow' id='eE" + j + "'></p></td><td><p class='cyellow' id='eN" + j + "'></p></td><td><input " + lightblue + " onclick='achange();' type='text' id='E" + j + "' value='" + zero + "'></input></td><td><input " + lightblue + " onclick='achange();' type='text' id='N" + j + "' value='" + zero + "'></input></td><td><input  class='lightblue' onclick='achange();' type='text' id='P" + j + "'></input></td></tr>");
      document.write("<tr class='yellow'><td></td><td id='B" + j + "'>0</td><td><input onclick='achange();' type='text' id='L" + j + "' value='0.000'></input></td><td id='dE" + j + "'>0.000</td><td id='dN" + j + "'>0.000</td><td></td><td></td><td></td></tr>");      
      zero = "";
      lightblue = " class='lightblue'";
    }
    document.write("<tr class='pink'><td><input class='lightgray' title='Entries with . are assumed as decimal degrees, else enter integer angle Degree value and will prompt for rest' onchange='anglechange(this);' type='text' id='A" + numlegs + "' value='-0'></input></td><td id='Ball' class='white'></td><td></td><td id='Eall' class='white'></td><td id='Nall' class='white'></td><td><input class='lightgreen' onclick='achange();' type='text' id='E" + numlegs + "' value='-0.000'></input></td><td><input class='lightgreen' onclick='achange();' type='text' id='N" + numlegs + "' value='-0.000'></input></td><td><input class='lightblue' onclick='achange();' type='text' id='P" + numlegs + "'></input></td></tr>");
    document.write("<tr class='yellow'><td></td><td><input class='lightgray' title='Entries with . are assumed as decimal degrees, else enter integer angle Degree value and will prompt for rest' onchange='anglechange(this);' type='text' id='lastBearing' value='" + lastBearing + "'></input></td><td></td><td></td><td></td><td></td><td></td><td><input class='lightgray' onclick='achange();' type='text' id='firstName'></input></td></tr>");
    document.write("</tbody></table></div>");
 }
}

<?php 
 if (!isset($_GET['coords'])) {
  echo "checkAtStart(); " . "\n";
 }
?>
</script>
<body>
<br><br><?php 
//if (isset($_GET['coords'])) {
  $htm .= '  <div id="chart_div" style="width: 900px; height: 900px; background-color: yellow;"></div> ' . "\n";
  $htm .= '</body></html>';
  $posthtm .= '  <div id="chart_div" style="width: 900px; height: 900px; background-color: yellow;"></div> ' . "\n";
  $posthtm .= '</body></html>';
  if (isset($_GET['coords'])) {
    echo '  <div id="chart_div" style="width: 900px; height: 900px; background-color: yellow;"></div> ' . "\n";
  } else {
    echo '  <iframe id="ichart_div2" width=950 height=950 style="display: none;" src=""></iframe> ' . "\n";
    echo '  <iframe id="ichart_div" width=950 height=950 style="display: none;" src=""></iframe> ' . "\n";
  }
//}
?>
</body>
</html>
