<?php
  // geo_chart.php
  // Google Geo Chart supervisor
  // RJM Programming
  // November, 2015
  // 
  if (file_exists("csv.php")) {
    include "csv.php";
  } else if (file_exists("../csv.php")) { 
    include "../csv.php";
  }
  if (file_exists("bigemail.php")) {
    include "bigemail.php";
  } else if (file_exists("../bigemail.php")) { 
    include "../bigemail.php";
  }

$guess="";
$guessquestion="";
if (isset($_POST['guess'])) {
 $guess=urldecode($_POST['guess']);
if (isset($_POST['question'])) {
 $guessquestion=urldecode($_POST['question']);
} else if (isset($_GET['question'])) {
 $guessquestion=urldecode($_GET['question']);
}
} else if (isset($_GET['guess'])) {
 $guess=urldecode($_GET['guess']);
if (isset($_POST['question'])) {
 $guessquestion=urldecode($_POST['question']);
} else if (isset($_GET['question'])) {
 $guessquestion=urldecode($_GET['question']);
}
}

$top="";
if (isset($_POST['area']) || isset($_GET['area'])) $top=" ";
$ntop="1";
$roff="0";
$mtop="1";
$newextras="";
$lastgo = "";
$GETimage = "<img src='geo_chart.jpg'></img>";
$myextras = " + ' or maybe perhaps ' + '\\n\\n' + ',\"A tooltip (regarding {popularity}) of some sort like <a target=_blank href=' + \"'\" + 'https://en.wikipedia.org/wiki/{country}' + \"'\" + '>{country}</a>\"' + '\\n\\n'";

function server_remote_addr($inst) {
    global $lastgo;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    if (isset($_GET['popularity'])) {
      $file_handle = fopen("TestGeoChart_" . $rma . ".html", "w");
      fwrite($file_handle, $_SERVER['REQUEST_URI']);
      fclose($file_handle);
      $timesuff = date("Y-m-d-H-i-s");
      while (file_exists("GeoChart_" . $rma . "_" . $timesuff . ".html")) {
       $timesuff = date("Y-m-d-H-i-s");
      }
      $file_handle = fopen("GeoChart_" . $timesuff . ".html", "w");
      fwrite($file_handle, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>  <script type="text/javascript" src="http://www.rjmprogramming.com.au/gchartgen.js"></script> <script type="text/javascript" src="http://www.rjmprogramming.com.au/gchartgen.js"></script><meta http-equiv="Refresh" content="5;url=' . $_SERVER['REQUEST_URI'] . '"><meta name="viewport" content="width=device-width, initial-scale=0.75"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /><script src="http://code.jquery.com/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script></head><body> <div data-role="page"></div><!-- /page --></body></html>');
      fclose($file_handle);
      $lastgo = "&nbsp;&nbsp;&nbsp;<a target=_blank title='Last Geo Map' href='" . $_SERVER['REQUEST_URI'] . "'>Last</a>" . "&nbsp;&nbsp;<a href='mailto:rmetcalfe15@gmail.com?subject=Email Last Geo Map link to " . $_GET['title'] . " http://www.rjmprogramming.com.au/PHP/GeoChart/GeoChart_" . $timesuff . ".html" . "&body=Change Email Address then Email Last Geo Map link to " . $_GET['title'] . " http://www.rjmprogramming.com.au/PHP/GeoChart/GeoChart_" . $timesuff . ".html&cc=&bcc='>Email</a>&nbsp;&nbsp;<a onclick='var qqq=document.getElementById(" . '"' . "ourw" . '"' . "); qqq.style.display=" . '"' . "block" . '";' . "' title='Width?' href='#'>W?</a>&nbsp;&nbsp;<a onclick='var qqq=document.getElementById(" . '"' . "ourh" . '"' . "); qqq.style.display=" . '"' . "block" . '";' . "' title='Height?' href='#'>H?</a>&nbsp;&nbsp;<a title='Intensity Map?' href='../IntensityChart/intensity_chart.php?isMobile=y'>+</a>";
      if (isset($_GET['isMobile']) || isset($_POST['isMobile'])) {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
      $lastgo .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php?isMobile=y">Another?</a>' . $lastgo . "\n";
  } else {
      $lastgo .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php?isMobile=yjustmenu">Another?</a>' . $lastgo . "\n";
  }
      } else {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
      $lastgo .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php">Another?</a>';
  } else {
      $lastgo .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php?justmenu=justmenu">Another?</a>';
  }
      }
    } else if (strlen($inst) > 0 && file_exists("TestGeoChart_" . $rma . ".url")) {
      $file_handle = fopen("TestGeoChart_" . $rma . ".url", "r");
      $rma=fgets($file_handle);
      fclose($file_handle);
      $timesuff = date("Y-m-d-H-i-s");
      while (file_exists("GeoChart_" . $rma . "_" . $timesuff . ".html")) {
       $timesuff = date("Y-m-d-H-i-s");
      }
      $file_handle = fopen("GeoChart_" . $timesuff . ".html", "w");
      fwrite($file_handle, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>  <script type="text/javascript" src="http://www.rjmprogramming.com.au/gchartgen.js"></script><meta http-equiv="Refresh" content="5;url=' . $rma . '"><meta name="viewport" content="width=device-width, initial-scale=0.75"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /><script src="http://code.jquery.com/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script></head><body> <div data-role="page"></div><!-- /page --></body></html>');
      fclose($file_handle);
      $rma = "&nbsp;&nbsp;&nbsp;<a target=_blank title='Last Geo Map' href='" . $rma . "'>Last</a>" . "&nbsp;&nbsp;<a href='mailto:rmetcalfe15@gmail.com?subject=Email Last Geo Map link to http://www.rjmprogramming.com.au/PHP/GeoChart/GeoChart_" . $timesuff . ".html" . "&body=Change Email Address then Email Last Geo Map link to http://www.rjmprogramming.com.au/PHP/GeoChart/GeoChart_" . $timesuff . ".html&cc=&bcc='>Email</a>&nbsp;&nbsp;<a onclick='var qqq=document.getElementById(" . '"' . "ourw" . '"' . "); qqq.style.display=" . '"' . "block" . '";' . "' title='Width?' href='#'>W?</a>&nbsp;&nbsp;<a onclick='var qqq=document.getElementById(" . '"' . "ourh" . '"' . "); qqq.style.display=" . '"' . "block" . '";' . "' title='Height?' href='#'>H?</a>&nbsp;&nbsp;<a title='Intensity Map?' href='../IntensityChart/intensity_chart.php?isMobile=y'>+</a>";
      if (isset($_GET['isMobile']) || isset($_POST['isMobile'])) {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
      $rma .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php?isMobile=y">Another?</a>' . $lastgo . "\n";
  } else {
      $rma .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php?isMobile=yjustmenu">Another?</a>' . $lastgo . "\n";
  }
      } else {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
      $rma .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php">Another?</a>';
  } else {
      $rma .= '&nbsp;&nbsp;<a title="Another Geo Map" href="./geo_chart.php?justmenu=justmenu">Another?</a>';
  }
      }
      $lastgo = $rma;
    }
    return $rma;
}

  function retval($inv) {
    if (strpos($inv, "E-") !== false) return "0";
    return $inv;
  }



    echo '<html>' . "\n";
    echo '<head> <script type="text/javascript" src="http://www.rjmprogramming.com.au/gchartgen.js"></script> ' . "\n";
    echo '<title>Geo Map - RJM Programming - http://www.rjmprogramming.com.au (Copyright &copy; 2013 rjmprogramming.com.au all rights reserved.)</title>' . "\n";
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . "\n";
    echo '<meta name="viewport" content="width=device-width, initial-scale=0.75">' . "\n";
    echo '<script type="text/javascript" src="https://www.google.com/jsapi"></script>' . "\n";
    echo '<script type="text/javascript">' . "\n var exra=''; \n var comma=''; \n";

    echo "var ready=false, x=-1, y=-1, elemLeft=0, elemTop=0, ir=-1, ic=-1, talkingpoint=''; \n";   

 
    echo "var data, chart, pardata = (location.search.split('data=')[1] ? location.search.split('data=')[1].split('&')[0] : ''), paramdata=decodeURIComponent(pardata) , xcom=paramdata.replace(/, 66]/g,'').replace(/,66]/g,'').replace(/\[/g,'').replace(/~/g,'').replace(/'/g,''); \n";


 echo "   function ouralert(ourpis) {
   var postdparts0='';
if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i) || ('" . $guess . "' != '' && '" . $guess . "'.replace('Andorra','').replace('Monaco','').replace('Vatican City','').replace('Monaco','').replace('Liechtenstein','').replace('Luxembourg','').replace('San Marino','').replace('Nauru','').replace('Maldives','').replace('Grenada','').replace('Malta','').replace('Saint Kitts and Nevis','') == '')) {
if (xcom.length < 300) {
postdparts0=prompt(ourpis.replace('Click ','We think that clicks below may not work so you could type country from ' + xcom + ' or you can try click '),'');
if (postdparts0 == null) postdparts0='';
} else {
postdparts0=prompt(ourpis.replace('Click ','We think that clicks below may not work so you could type country below or you can try click '),'');
if (postdparts0 == null) postdparts0='';
}
  var agoes=[0,0]; pscore='';
  if (parent.document.getElementById('score')) {           pscore=parent.document.getElementById('score').innerHTML;  agoes=pscore.replace('Score:','').split('/');
 }
 if (eval(('' + postdparts0.toLowerCase().replace(']','')).indexOf('" . $guess . "'.toLowerCase()) / 2) >= 0) {
 if (pscore == '') {
alert('Correct.'); 
} else if (agoes.length == 2) {
parent.document.getElementById('score').innerHTML='Score: ' + eval(1 + eval(agoes[0])) + '/' + eval(1 + eval(agoes[1].split(' ')[0])); 
} else if (agoes.length == 1) { 
parent.document.getElementById('score').innerHTML='' + eval(1 + eval(agoes[0])); 
 }
} else if (postdparts0 != '') {
if (agoes.length == 2) {
parent.document.getElementById('score').innerHTML='Score: ' + eval(0 + eval(agoes[0])) + '/' + eval(1 + eval(agoes[1].split(' ')[0])) + ' The correct answer is " . $guess . "'; 
} else if (agoes.length == 1) { 
parent.document.getElementById('score').innerHTML=' ' + eval(0 + eval(agoes[0])); 
 }
}
 } else {
  alert(ourpis);
 }
} \n\n  ";
 
      $GETtp="";
      if (isset($_GET['tp'])) $GETtp = urldecode($_GET['tp']);
      if (isset($_POST['tp'])) $GETtp = urldecode($_POST['tp']);
      if ($GETtp != "") echo "\n talkingpoint=String.fromCharCode(10) + String.fromCharCode(10) + " . '"' . str_replace("`", '" + String.fromCharCode(10) + "', str_replace("\n", '" + String.fromCharCode(10) + "', $GETtp)) . '"' . " + String.fromCharCode(10) + String.fromCharCode(10); \n";

      $GETr = "";
      $GETc = "";
      if (isset($_GET['r'])) $GETr = urldecode($_GET['r']);
      if (isset($_POST['r'])) $GETr = urldecode($_POST['r']);
      if (isset($_GET['c'])) $GETc = urldecode($_GET['c']);
      if (isset($_POST['c'])) $GETc = urldecode($_POST['c']);
      if ($GETr != "") {
       if (substr($GETr,0,1) == "0" && strlen($GETr) > 1) {
        $top="top.";
        $ntop="2";
        $mtop="1";
        $roff="1";
       }
       echo "\n ir=eval('" . $GETr . "'); \n";
      }
      if ($GETc != "") {
       if (substr($GETc,0,1) == "0" && strlen($GETc) > 1) {
        $top="top.";
        $ntop="2";
        $mtop="1";
        $roff="1";
       }
       echo "\n ic=eval('" . $GETc . "'); \n";
      }

      $GETx = "";
      $GETy = "";
      if (isset($_GET['x'])) $GETx = urldecode($_GET['x']);
      if (isset($_POST['x'])) $GETx = urldecode($_POST['x']);
      if (isset($_GET['y'])) $GETy = urldecode($_GET['y']);
      if (isset($_POST['y'])) $GETy = urldecode($_POST['y']);
      if ($GETx != "" && $GETy != "") {
        echo "\n x=eval('" . $GETx . "'); \n";
        echo "\n y=eval('" . $GETy . "'); \n";
      }

      

      $xgd = ""; 
      $gd = ""; 
      $ntt=false;
      $GETarea="";
      $nondefs=" ";
      if (isset($_GET['area'])) {
         if ($top == "") $top=" ";
         $GETarea = str_replace("~","",str_replace(",~~", "", ",~" . $_GET['area'] . "~"));
      }
      if (isset($_GET['popularity']) || isset($_POST['popularity'])) server_remote_addr("");
      $GETtitle = "My World Survey";
      $GETcountry = "Country";
      $GETpopularity = "Popularity";
      $GETdata = ",['Germany',200],['United States',300],['Brazil',400],['Canada',500],['France',600],['RU',700] ";      
      if (isset($_GET['isMobile']) || isset($_POST['isMobile']) || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
      $GETwidth = "305";
      $GETheight = "190";
      $myextras = "";
      } else {
      $GETwidth = "556";
      $GETheight = "347";
      }
      if (isset($_GET['isMobile']) || isset($_POST['isMobile'])) $GETimage="";
      if (isset($_GET['popularity'])) {
       if ($GETimage == "" && strpos($_GET['data'], "%2C") === false) {
       $GETtitle = $_GET['title'];
       $GETcountry = $_GET['country'];
       $GETpopularity = $_GET['popularity'];
       $GETdata = $_GET['data'];
       $GETwidth = $_GET['width'];
       $GETheight = $_GET['height'];
       if (isset($_GET['area'])) {
         if ($top == "") $top=" ";
         $GETarea = str_replace("~","",str_replace(",~~", "", ",~" . $_GET['area'] . "~"));
       }
       } else {
       $GETtitle = urldecode($_GET['title']);
       $GETcountry = urldecode($_GET['country']);
       $GETpopularity = urldecode($_GET['popularity']);
       $GETdata = urldecode($_GET['data']);
       $GETwidth = urldecode($_GET['width']);
       $GETheight = urldecode($_GET['height']);
       if (isset($_POST['area'])) {
         if ($top == "") $top=" ";
         $GETarea = str_replace("~","",str_replace(",~~", "", ",~" . urldecode($_POST['area']) . "~"));
       }
       }
       $ourtitles=explode(";", $_GET['title']);
       if (sizeof($ourtitles) > 1) {
        $newextras="region: '" . $ourtitles[0] . "', displayMode: 'markers', colorAxis: {colors: ['green', 'blue']}, ";
       }
       $GETtitle = $ourtitles[sizeof($ourtitles) - 1];
       $GETimage="";
      } else if (isset($_POST['popularity']) && (isset($_POST['isMobile']) || isset($_POST['wouldlikeyoutoseekpermission']))) {
       $GETimage="";
       $GETtitle = urldecode($_POST['title']);
       $ourtitles=explode(";", $GETtitle);
       if (sizeof($ourtitles) > 1) {
        $newextras="region: '" . $ourtitles[0] . "', displayMode: 'markers', colorAxis: {colors: ['green', 'blue']}, ";
       }
       $GETtitle = $ourtitles[sizeof($ourtitles) - 1];
       $GETcountry = urldecode($_POST['country']);
       $GETpopularity = urldecode($_POST['popularity']);
       $GETdata = urldecode($_POST['data']);
       $GETwidth = urldecode($_POST['width']);
       $GETheight = urldecode($_POST['height']);
       if (isset($_POST['area'])) {
         if ($top == "") $top=" ";
         $GETarea = str_replace("~","",str_replace(",~~", "", ",~" . urldecode($_POST['area']) . "~"));
       }
      }

      echo " var colnames= '" . $GETcountry . "," . str_replace("'", "", $GETpopularity) . $GETarea . "'; \n";

      $nondefs="width: " . $GETwidth . ", height: " . $GETheight;
      $newextras.=$nondefs;
      
      echo "   var sliceid = 0; /" . "/ thanks to https://gist.github.com/alexrainman/bb8d49357250df0859c0  \n";
      echo "   var refresh=0; prerefresh=0;   \n";
      echo ' google.load("visualization", "1", {packages:["geochart"]}); ' . "\n";
      echo ' google.setOnLoadCallback(drawChart); ' . "\n";
      
      if ($GETarea == '') {
       echo "\nfunction unzero(cin) { return cin; } \n";
      } else {
       echo "\nfunction unzero(cin) { return cin.replace('.0000000000', ' and " . str_replace(","," ",$GETarea) . " of '); } \n";
      }
      
      
      echo ' function drawChart() { ' . "\n";
 if ((isset($_GET['data']) || isset($_POST['data'])) && (isset($_GET['tooltip']) || isset($_POST['tooltip']) || strpos($GETdata, "'") !== false || strpos($GETdata, '"') !== false)) {
      $ntt=true;
  //$newextras=$nondefs . ", focusTarget: 'category', tooltip: {isHtml: true} ";
  $newextras=$nondefs . ", tooltip: {isHtml: true}, showTip: true"; //, selectionMode: 'multiple', tooltip: {trigger: 'selection'}, aggregationTarget: 'none' ";
  echo "       var tooltip = [ \n";
  $bits=explode("~,", substr($GETdata,1));
  $delts="";
  for ($iy=1; $iy<sizeof($bits); $iy++) {
   $postbits=explode(",", $bits[$iy]);
   $xgd.=str_replace("%3C", "<", str_replace("%3E", ">", $postbits[0]));
   echo $delts . str_replace("%3C", "<", str_replace("%3E", ">", $postbits[0]));
   $delts=",";
  }
  echo "]; \n";
  $gd=$GETdata;
  for ($iy=1; $iy<sizeof($bits); $iy++) {
   $postbits=explode(",", $bits[$iy]);
   $gd=str_replace("," . $postbits[0] . "", "", $gd);
  }
  $GETdata=$gd;
  echo "     data = new google.visualization.DataTable();  \n";
  echo "   data.addColumn('string', '" . $GETcountry . "');  \n";
  //echo "   data.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});  \n";
  $things=explode(",",$GETpopularity);
  for ($it=0; $it<sizeof($things); $it++) { 
    echo "   data.addColumn('number', '" . $things[$it] . "');  \n";
  }
  echo " data.addRows([ \n";
  //echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", substr($GETdata,1))))));
  echo str_replace("~", "'", substr($GETdata,1));
  echo "   ]); \n";
 } else {
      echo ' data = google.visualization.arrayToDataTable([ ' . "\n";
      echo " ['" . $GETcountry . "','" . str_replace(",", "','", $GETpopularity) . "'], \n";
      echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", $GETdata)))));
      echo "        ]);\n";
 }
      echo " chart = new google.visualization.GeoChart(document.getElementById('chart_div')); \n";

 if ($ntt) {
   
      //echo "   var selection; \n";

      echo "   function checkhere() { prerefresh++; if (prerefresh == 20) { if (refresh == 0) { refresh=1; document.getElementById('ihide').value=1;  } } \n";
      echo "   refresh=document.getElementById('ihide').value; if (refresh != 0) { prerefresh=0; refresh++; document.getElementById('ihide').value=refresh; if (refresh == 10) { var huhss=document.URL.split('&xx='); if (huhss.length == 1) {  top.location.href=document.URL + '&xx=1'; } else { var two=eval(huhss[1]); two++; top.location.href=huhss[0] + '&xx=' + two;  }  }    } setTimeout(checkhere,1000); \n } \n";

      echo "   checkhere(); \n";
  
      echo "   function eventHandler(e){ \n";
      //echo "     chart.setSelection([e]); \n";
      echo "     try { \n";
      echo "       selection = chart.getSelection(); \n";
      echo "       sliceid = selection[0].row; \n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(0)").css("font-weight", "bold"); ' . "\n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(1)").html(tooltip[sliceid]).css("font-family", "Arial");  ' . "\n";
      echo "     } \n";
      echo "     catch(err) { \n";
      echo "       ; \n";
      echo "     } \n";
      echo "   } \n";
  
      echo "   function ceventHandler(e){   \n";
      //echo "   google.visualization.events.addListener(chart, 'select', eventHandler); \n";
      //echo "     chart.setSelection([e]); \n";
      echo "     try { \n";
      echo "       selection = chart.getSelection(); \n";
      echo "       sliceid = selection[0].row; \n if (1 == 2) { alert(tooltip[sliceid]); } document.getElementById('chart_div').innerHTML+=' '; \n document.getElementById('ihide').value='1'; \n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(0)").css("font-weight", "bold"); ' . "\n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(1)").html(tooltip[sliceid]).css("font-family", "Verdana");  ' . "\n";
      echo "     } \n";
      echo "     catch(err) {   \n";
      echo "       ; \n";
      echo "     } \n";
      echo "   } \n";
  
  
      if (strpos($xgd, "<") !== false) {
        echo "   google.visualization.events.addListener(chart, 'select', ceventHandler); \n";
        echo "   google.visualization.events.addListener(chart, 'onmouseover', eventHandler);  \n";
      } else {
        echo "   google.visualization.events.addListener(chart, 'onmouseover', eventHandler); \n";
      }
}
      $GETonclick = "";
      if (isset($_GET['onclick'])) $GETonclick = urldecode($_GET['onclick']);
      if (isset($_POST['onclick'])) $GETonclick = urldecode($_POST['onclick']);
      if ($GETonclick != "") {
      echo "google.visualization.events.addListener(chart, 'ready', function() { ready=true; }); \n";
      echo "google.visualization.events.addListener(chart, 'select', myselectfunction); \n function myselectfunction() {
 if (ready) {
  var relist=['remail',''],ire,ansbits,avblurb=talkingpoint,ioffset=0,ans=null,doit=true,kl,lk, thisnum, num=0,message = '', dparts, colparts=colnames.split(','), colav=new Array(), predparts, postdparts, diffcolnames=colnames,diffvalues, str;
  selection = chart.getSelection();
  sliceid = selection[0].row;

  for (var i = 0; i < selection.length; i++) {
    var item = selection[i];
    if ((eval(ir) >= 0 && eval(ic) < 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(999);
        item.row=eval(ir + " . $roff . ");
        //item.column=ic;
  //alert(9999);
        ir=-1;
        ic=-1;
      } else {
        ir=item.row;
        //ic=item.column;
        //document.title=ir + ',' + ic;
        if ('" . $top . "' == ' ') {
         top.document.location=top.document.URL.replace('&r=','&xr=').replace('?','?r=' + ir + '&');
         return;
        }
      }
      //document.title=str;
      message=" . $top . "document.head.innerHTML.replace(/, \[/g,',' + '[').replace('[' + String.fromCharCode(39),'[ ' + String.fromCharCode(39)).replace('[' + String.fromCharCode(39),', [' + String.fromCharCode(39)).substring(ioffset);
      while (message.indexOf(',' + ' [') != -1) {
       message=message.replace(',' + ' [',',' + '[');
      }
      //alert(message);
      dparts=message.split(',[');
        //document.title='';
        colav.push(0.0);
      for (kl=" . $ntop . "; kl<colparts.length; kl++) {
        //alert('dp[1]=' + dparts[1]);
        //document.title=1;
        colav.push(0.0);
        doit=true;
        num=1;
        for (lk=" . $mtop . "; lk<dparts.length; lk++) {
         if (doit) {
        //document.title=3;
        //alert('dparts[' + lk + ']=' + dparts[lk].replace(']',','));
         thisnum=dparts[lk].replace(']);',',').replace(']',',').split(',');
         //if (kl == 1) document.title+=4 + ' .. '  + eval(thisnum[kl]) + ' ... ' + lk + ' ... ' + colav[kl] + ' ... ' + thisnum.length + ' ... ' + dparts[lk];
         //alert(dparts[lk].replace(']',',') + ' 77 ' + thisnum.length + ' vs ' + kl);
        colav[kl] = eval(eval(colav[kl] * eval(-1 + num)) + eval(thisnum[kl])) / eval(0.0 + num);
        //alert(777);
        //document.title=5;
         if (dparts[lk].indexOf(';') != -1) doit=false;
         if (doit) num++;
         }
        }
        //alert(7);
        //document.title=7;
        if (avblurb == '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
        //document.title=8;
        //avblurb+='Average ' + colparts[kl] + ' over ' + num + ' ' + colparts[0] + ' values is ' + colav[kl] + String.fromCharCode(10);
        //document.title=7;
      }
      if (avblurb != '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
        //alert(dparts.length + ' bigger than ' + eval(1 + item.row));
      if (eval(dparts.length) >= eval(1 + item.row)) {
      //alert(97);
        postdparts=dparts[eval(1 + item.row)].replace(']',',').split(',');
       //alert(977);
       if (eval(postdparts.length) >= eval(1 + item.column)) {
      //alert(197);
         if (eval(colparts.length) >= 2) { 
      //alert(397);
          if (1 == 1) {
          avblurb+=String.fromCharCode(10) + String.fromCharCode(10) + 'Show this as Pie Chart=P' + String.fromCharCode(10) + String.fromCharCode(10);
          avblurb+='Comma Separated Values Spreadsheet Export=filename.csv' + String.fromCharCode(10) + '' + String.fromCharCode(10) + 'Email with or without Talking Point=fillin.email@address This is when the improvement started.  Robert' + String.fromCharCode(10) + '' + String.fromCharCode(10);
          avblurb+='Change data as required below (keeping it the same or being blank or choosing Cancel leaves the status quo)';
          ans=null; \n";

  if ($guess != "") {
          echo "\n var agoes=[0,0]; pscore='';\n if (parent.document.getElementById('score')) {\n pscore=parent.document.getElementById('score').innerHTML;\n agoes=pscore.replace('Score:','').split('/');\n }\n if (eval(('' + postdparts[0].replace(']','')).indexOf('" . $guess . "') / 2) >= 0) {\n if (pscore == '') {\n alert('Correct.'); }\n else if (agoes.length == 2) {\n parent.document.getElementById('score').innerHTML='Score: ' + eval(1 + eval(agoes[0])) + '/' + eval(1 + eval(agoes[1].split(' ')[0]));\n    } else if (agoes.length == 1) { parent.document.getElementById('score').innerHTML='' + eval(1 + eval(agoes[0]));   } } else { var huhhuh='Your guess of ' + postdparts[0].replace(']','') + ' is not correct.  The correct answer is ' + '" . $guess . ".';  if (agoes.length == 2) {\n parent.document.getElementById('score').innerHTML='Score: ' + eval(0 + eval(agoes[0])) + '/' + eval(1 + eval(agoes[1].split(' ')[0]));\n    } else if (agoes.length == 1) { parent.document.getElementById('score').innerHTML=' ' + eval(0 + eval(agoes[0]));   }  alert(huhhuh);  } \n"; 
  } else {
          echo "\n ans=prompt(unzero('For ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[1] + ' of ' + postdparts[1].replace(']','')) + avblurb, paramdata); \n";
  }

      echo "
          if (ans != null) {
            if (ans != '' && ans != paramdata) {
             if ((ans.toLowerCase() + '~').indexOf('.csv~') != -1 && ans.toLowerCase().indexOf('&') == -1) ans=paramdata + '&csv=' + ans;
             ansbits=ans.split(' ');
             if (ansbits[0].indexOf('@') != -1) {
              if (document.getElementById('altemail')) {
              document.getElementById('altemail').value=ansbits[0];
              document.getElementById('remail').href=document.getElementById('remail').href.replace('mt=','mt=' + ansbits[0] + '&junk=youshallneverfindthis').replace('get=','mt=' + ansbits[0] + '&junk=youshallneverfindthis');
              }
              if (document.getElementById('email')) relist[1]='email';
              for (ire=0; ire<2; ire++) {
              if (relist[ire] != '') {
              document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace('fillin.email@address', ansbits[0]);
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&r=') + item.row) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&r=') + item.row;
              //if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + item.column) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&c=') + item.column;
              if (ansbits.length > 1) {
                ans=ans.replace(ansbits[0] + ' ','');
                while (ans.indexOf(' ') != -1) {
                  ans=ans.replace(' ','%20');
                }
                while (ans.indexOf('?') != -1) {
                  ans=ans.replace('?','%3f');
                }
                while (ans.indexOf('&') != -1) {
                  ans=ans.replace('&','%26');
                }
                if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) == -1 && document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) == -1) {
                  document.getElementById(relist[ire]).href+=encodeURIComponent('&tp=') + encodeURIComponent(ans);
                } else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('&tp='), encodeURIComponent('&tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
				} else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('?tp='), encodeURIComponent('?tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
                }
                while (document.getElementById(relist[ire]).href.indexOf(' ') != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(' ','%20');
                }
                //alert(document.getElementById(relist[ire]).href);
              }
              document.getElementById(relist[ire]).click();
              }
              }
             } else if (ans.substring(0,1).toLowerCase() == 'p') {
              location.href=document.URL.replace('/GeoChart', '/PieChart').replace('geo_chart.php', 'pie_chart.php');
             } else {
              location.href=document.URL.replace(pardata, ourencodeURIComponent(ans));
             }
            }
          }
          } else {
          alert('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + ' while ' + colparts[eval(3 - item.column)] + ' were ' + postdparts[eval(3 - item.column)].replace(']','') + ' the difference being ' + eval(eval(postdparts[1].replace(']','')) - eval(postdparts[2].replace(']',''))) + avblurb);
          }
         }
        }
      }
    } else if ((item.row != null && item.column != null) || (eval(ir) >= 0 && eval(ic) >= 0)) {
      if (eval(ir) >= 0 && eval(ic) >= 0) {
  //alert(999);
        item.row=eval(ir + " . $roff . ");
        item.column=ic;
  //alert(9999);
        ir=-1;
        ic=-1;
      } else {
        ir=item.row;
        ic=item.column;
        //document.title=ir + ',' + ic;
      }
      str = data.getFormattedValue(item.row, item.column);
      message += '{row:' + item.row + ',column:' + item.column + '} = ' + str;
    } else if ((item.row != null) || (eval(ir) >= 0 && eval(ic) < 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(999);
        item.row=ir;
        //item.column=ic;
  //alert(9999);
        ir=-1;
        //ic=-1;
      } else {
        ir=item.row;
        //ic=item.column;
        //document.title=ir + ',' + ic;
        if ('" . $top . "' == ' ') {
         top.document.location=top.document.URL.replace('&r=','&xr=').replace('?','?r=' + ir + '&');
         return;
        }
      }
      str = data.getFormattedValue(item.row, 0);
      //document.title=str;
      //document.title=str;
      message=" . $top . "document.head.innerHTML.replace(/, \[/g,',' + '[').replace('[' + String.fromCharCode(39),'[ ' + String.fromCharCode(39)).replace('[' + String.fromCharCode(39),', [' + String.fromCharCode(39)).substring(ioffset);
      while (message.indexOf(',' + ' [') != -1) {
       message=message.replace(',' + ' [',',' + '[');
      }
      dparts=message.split(',[');
        //document.title='';
        colav.push(0.0);
      for (kl=" . $ntop . "; kl<colparts.length; kl++) {
        //document.title=1;
        colav.push(0.0);
        doit=true;
        num=1;
        for (lk=" . $mtop . "; lk<dparts.length; lk++) {
         if (doit) {
        //document.title=3;
         //alert(dparts[lk].replace(']',','));
         thisnum=dparts[lk].replace(']',',').split(',');
         //if (kl == 1) document.title+=4 + ' .. '  + eval(thisnum[kl]) + ' ... ' + lk + ' ... ' + colav[kl] + ' ... ' + thisnum.length + ' ... ' + dparts[lk];
         colav[kl] = eval(eval(colav[kl] * eval(-1 + num)) + eval(thisnum[kl])) / eval(0.0 + num);
        //document.title=5;
         if (dparts[lk].indexOf(';') != -1) doit=false;
         if (doit) num++;
         }
        }
        //alert('07');
        //document.title=7;
        if (avblurb == '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
        //document.title=8;
        //avblurb+='Average ' + colparts[kl] + ' over ' + num + ' ' + colparts[0] + ' values is ' + colav[kl] + String.fromCharCode(10);
        //document.title=7;
      }
      if (avblurb != '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
      if (eval(dparts.length) >= eval(1 + item.row)) {
        postdparts=dparts[eval(1 + item.row)].replace(']',',').split(',');
        if (eval(postdparts.length) >= eval(1 + item.column)) {
         if (eval(colparts.length) >= 2) { 
          if (1 == 1) {
          avblurb+=String.fromCharCode(10) + String.fromCharCode(10) + 'Show this as Pie Chart=P' + String.fromCharCode(10) + String.fromCharCode(10);
          avblurb+='Comma Separated Values Spreadsheet Export=filename.csv' + String.fromCharCode(10) + '' + String.fromCharCode(10) + 'Email with or without Talking Point=fillin.email@address This is when the improvement started.  Robert' + String.fromCharCode(10) + '' + String.fromCharCode(10);
          avblurb+='Change data as required below (keeping it the same or being blank or choosing Cancel leaves the status quo)';
          ans=null; \n";

  if ($guess != "") {
          echo "\n var agoes=[0,0]; pscore='';\n if (parent.document.getElementById('score')) {\n pscore=parent.document.getElementById('score').innerHTML;\n agoes=pscore.replace('Score:','').split('/');\n }\n if (eval(('' + postdparts[0].replace(']','')).indexOf('" . $guess . "') / 2) >= 0) {\n if (pscore == '') {\n alert('Correct.'); }\n else if (agoes.length == 2) {\n parent.document.getElementById('score').innerHTML='Score: ' + eval(1 + eval(agoes[0])) + '/' + eval(1 + eval(agoes[1].split(' ')[0]));\n    } else if (agoes.length == 1) { parent.document.getElementById('score').innerHTML='' + eval(1 + eval(agoes[0]));   } } else { var huhhuh='Your guess of ' + postdparts[0].replace(']','') + ' is not correct.  The correct answer is ' + '" . $guess . ".';  if (agoes.length == 2) {\n parent.document.getElementById('score').innerHTML='Score: ' + eval(0 + eval(agoes[0])) + '/' + eval(1 + eval(agoes[1].split(' ')[0]));\n    } else if (agoes.length == 1) { parent.document.getElementById('score').innerHTML=' ' + eval(0 + eval(agoes[0]));   } alert(huhhuh);  } \n"; 
 } else {
          echo "\n ans=prompt(unzero('For ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[1] + ' of ' + postdparts[1].replace(']','')) + avblurb, paramdata); \n";
  }
      echo "
          if (ans != null) {
            if (ans != '' && ans != paramdata) {
             if ((ans.toLowerCase() + '~').indexOf('.csv~') != -1 && ans.toLowerCase().indexOf('&') == -1) ans=paramdata + '&csv=' + ans;
             ansbits=ans.split(' ');
             if (ansbits[0].indexOf('@') != -1) {
              if (document.getElementById('altemail')) {
              document.getElementById('altemail').value=ansbits[0];
              document.getElementById('remail').href=document.getElementById('remail').href.replace('mt=','mt=' + ansbits[0] + '&junk=youshallneverfindthis').replace('get=','mt=' + ansbits[0] + '&junk=youshallneverfindthis');
              }
              if (document.getElementById('email')) relist[1]='email';
              for (ire=0; ire<2; ire++) {
              if (relist[ire] != '') {
              document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace('fillin.email@address', ansbits[0]);
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&r=') + item.row) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&r=') + item.row;
              //if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + item.column) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&c=') + item.column;
              if (ansbits.length > 1) {
                ans=ans.replace(ansbits[0] + ' ','');
                while (ans.indexOf(' ') != -1) {
                  ans=ans.replace(' ','%20');
                }
                while (ans.indexOf('?') != -1) {
                  ans=ans.replace('?','%3f');
                }
                while (ans.indexOf('&') != -1) {
                  ans=ans.replace('&','%26');
                }
                if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) == -1 && document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) == -1) {
                  document.getElementById(relist[ire]).href+=encodeURIComponent('&tp=') + encodeURIComponent(ans);
                } else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('&tp='), encodeURIComponent('&tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
				} else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('?tp='), encodeURIComponent('?tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
                }
                while (document.getElementById(relist[ire]).href.indexOf(' ') != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(' ','%20');
                }
                //alert(document.getElementById(relist[ire]).href);
              }
              document.getElementById(relist[ire]).click();
              }
              }
             } else if (ans.substring(0,1).toLowerCase() == 'p') {
              location.href=document.URL.replace('/GeoChart', '/PieChart').replace('geo_chart.php', 'pie_chart.php');
             } else {
              location.href=document.URL.replace(pardata, ourencodeURIComponent(ans));
             }
            }
          }
          } else {
          alert('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + ' while ' + colparts[eval(3 - item.column)] + ' were ' + postdparts[eval(3 - item.column)].replace(']','') + ' the difference being ' + eval(eval(postdparts[1].replace(']','')) - eval(postdparts[2].replace(']',''))) + avblurb);
          }
         }
        }
      }

    } else if ((item.column != null) || (eval(ir) < 0 && eval(ic) >= 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(999);
        //item.row=ir;
        item.column=ic;
  //alert(9999);
        //ir=-1;
        ic=-1;
      } else {
        //ir=item.row;
        ic=item.column;
        //document.title=ir + ',' + ic;
      }
      str = data.getFormattedValue(0, item.column);
      message += '{row:none, column:' + item.column + '}; value (row 0) = ' + str;
    }
  }
  if (message == '') {
    message = 'nothing';
  }
  if (1 == 2) console.log('You selected ' + message);
 }
 ir=-1;
 ic=-1;    
 } 
      //);  
      ";
      }      

      echo " chart.draw(data, {" . $newextras . "}); \n";
      if ($GETr != "" && $GETc == "") echo " chart.setSelection([{'row': " . $GETr . ", 'column': null}]); setTimeout(myselectfunction,4100); \n";
      if ($GETr == "" && $GETc != "") echo " chart.setSelection([{'row': null, 'column': " . $GETc . "}]);  setTimeout(myselectfunction,4100); \n";
      if ($GETr != "" && $GETc != "") echo " chart.setSelection([{'row': " . $GETr . ", 'column': " . $GETc . "}]);  setTimeout(myselectfunction,4100); \n";
      echo " } \n";
      
      if ($guess != "") {
       if ($guessquestion != "") {
        echo "\n function guesswork() { ouralert('" . $guessquestion . "  Click country answer below." . "'); } \n";
       } else {
        echo "\n function guesswork() { ouralert('Click country answer below." . "'); } \n";
       }
       echo "\n\n setTimeout(guesswork,2300); \n\n";
      }      
      echo " </script> \n";
      echo ' <!--meta name="viewport" content="width=device-width, initial-scale=0.75"--> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /><script src="http://code.jquery.com/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script></head>' . " \n";
      //echo " <body onload='setTimeout(prelater, 2 * 1000);'> \n";
      echo " <body style='background-color:lightgray;'><div data-role='page'><div data-role='header'> \n";
      if (isset($_GET['isMobile']) || isset($_POST['isMobile'])) {
      echo " <h2>" . $GETtitle . " Geo Map</h2></div><!-- /header --><div data-role='content'>" . $GETimage . " \n";
      } else {
      echo " <h1>" . $GETtitle . " Geo Map</h1></div><!-- /header --><div data-role='content'>" . $GETimage . " \n";
      }
      if ((!isset($_GET['isMobile']) && !isset($_POST['isMobile'])) || isset($_GET['popularity'])) {
      echo '<div id="chart_div"></div>' . "\n";
      if (isset($_GET['isMobile']) || isset($_POST['isMobile'])) {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
       echo '<a title="Another Geo Map" href="./geo_chart.php?isMobile=y">Another geo map?</a>' . $lastgo . "\n";
  } else {
       echo '<a title="Another Geo Map" href="./geo_chart.php?isMobile=yjustmenu">Another geo map?</a>' . $lastgo . "\n";
  }
      } else {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
       echo '<a title="Another Geo Map" href="./geo_chart.php">Another geo map?</a>' . $lastgo . "\n";
  } else {
       echo '<a title="Another Geo Map" href="./geo_chart.php?justmenu=justmenu">Another geo map?</a>' . $lastgo . "\n";
  }
      }
      }
      if (!isset($_GET['popularity']) && !isset($_POST['popularity']) && strpos($_SERVER['QUERY_STRING'], "x=x") === false) {
      
        if ((isset($_GET['isMobile']) || isset($_POST['isMobile'])) && strpos($_SERVER['QUERY_STRING'], "x=x") === false) {
      echo '<div id="chart_div"></div>' . "\n";
         echo '<form target="_top" style="margin-top:-10px;" action="./geo_chart.php" method="GET">' . "\n";
         echo '<input name="isMobile" type="hidden" value="y"></input><input name="x" type="hidden" value="x"></input><br>' . "\n";
         echo '<table cellpadding=0 style="background-color:pink;"><tr><td>Title</td><td><input onclick=' . "'" . 'this.style.backgroundColor = "white"; ' . "'" . ' style="background-color:yellow;" id="title" name="title" type="text" value="' . $GETtitle . '"></input></td></tr>' . "\n";
         echo '<tr id="ourw" style="display:none;"><td>Width</td><td><input onclick=' . "'" . 'this.style.backgroundColor = "white"; ' . "'" . ' style="background-color:white;" id="width" name="width" type="text" value="' . $GETwidth . '"></input></td></tr>' . "\n";
         echo '<tr id="ourh" style="display:none;"><td>Height</td><td><input style="background-color:white;" id="height" name="height" type="text" value="' . $GETheight . '"></input></td></tr>' . "\n";
         echo '<tr><td>Via</td><td><input onclick=' . "'" . 'this.style.backgroundColor = "white"; ' . "'" . '  style="background-color:yellow;" id="country" name="country" type="text" value="' . $GETcountry . '"></input></td></tr>' . "\n";
         echo '<tr><td>Basis</td><td><input onclick=' . "'" . 'this.style.backgroundColor = "white"; ' . "'" . '  style="background-color:yellow;" id="popularity" name="popularity" type="text" value="' . $GETpopularity . '"></input></td></tr>' . "\n";
         echo '<tr><td><input style="background-color:yellow;" type="button" onclick=' . "'" . 'later(document.getElementById("title").value,document.getElementById("width").value,document.getElementById("height").value,document.getElementById("country").value,document.getElementById("popularity").value,"&amp;isMobile=y");' . "'" . ' value="Data"></input></td><td><input onclick=' . "'" . 'this.style.backgroundColor = "white"; ' . "'" . ' style="width:240px;font-size:6px;background-color:yellow;" id="data" name="data" type="text" value="' . $GETdata . '"></input></td></tr>' . "\n";     
         //echo '<tr><td><input style="background-color:green;" id="submit" type="submit" value="Show"></input></td><td><input style="background-color:yellow;" id="prompt" type="button" value="Prompt Me" onclick="location.href=' . "'" . './geo_chart.php' . "'" . ';"></input><input style="background-color:yellow;" id="clear" type="button" value="Clear" onclick="clearit();"></input>' . server_remote_addr('1') . '</td></tr></table>' . "\n";
         echo '<tr><td><input style="background-color:green;" id="submit" type="submit" value="Show"></input></td><td><input style="background-color:yellow;" id="prompt" type="button" value="Prompt Me" onclick="top.location.href=' . "'" . './geo_chart.php' . "'" . ';"></input><input style="background-color:yellow;" id="clear" type="button" value="Clear" onclick="clearit();"></input></td></tr></table>' . "\n";
         echo '</form></div><!-- /content -->' . "\n";
      //echo '<div id="chart_div"></div>' . "\n";
      if (strpos($lastgo, "Another") === false) {
      if (isset($_GET['isMobile']) || isset($_POST['isMobile'])) {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
       echo '<a title="Another Geo Map" href="./geo_chart.php?isMobile=y">Another geo map?</a>' . $lastgo . "\n";
  } else {
       echo '<a title="Another Geo Map" href="./geo_chart.php?isMobile=yjustmenu">Another geo map?</a>' . $lastgo . "\n";
  }
      } else {
  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
       echo '<a title="Another Geo Map" href="./geo_chart.php">Another geo map?</a>' . $lastgo . "\n";
  } else {
       echo '<a title="Another Geo Map" href="./geo_chart.php?justmenu=justmenu">Another geo map?</a>' . $lastgo . "\n";
  }
      }
      }
        }
      
        echo '<' . 'script' . ' type="text/javascript">' . "\n ";
          if (!isset($_GET['isMobile']) && !isset($_POST['isMobile'])) {
   //echo '<' . 'script type="text/javascript">' . "\n";
   echo ' setTimeout(prelater, 2 * 1000);' . "\n";
   //echo '<' . '/' . 'script>' . "\n";
  }

        echo ' function clearit() {  ' . "\n" . ' if (document.getElementById("data").style.backgroundColor.indexOf("yellow") != (0 - 1)) document.getElementById("data").value=""; ' . "\n";
        echo ' if (document.getElementById("title").style.backgroundColor.indexOf("yellow") != (0 - 1)) document.getElementById("title").value=""; ' . "\n";
        echo ' if (document.getElementById("width").style.backgroundColor.indexOf("yellow") != (0 - 1)) document.getElementById("width").value=""; ' . "\n";
        echo ' if (document.getElementById("height").style.backgroundColor.indexOf("yellow") != (0 - 1)) document.getElementById("height").value=""; ' . "\n";
        echo ' if (document.getElementById("country").style.backgroundColor.indexOf("yellow") != (0 - 1)) document.getElementById("country").value=""; ' . "\n";
        echo ' if (document.getElementById("popularity").style.backgroundColor.indexOf("yellow") != (0 - 1)) document.getElementById("popularity").value=""; ' . "\n" . ' }  ' . "\n";

        echo "function prelater() { \n later('','','','','',''); \n } \n function later(t,w,h,c,p,m) { \n" . ' var datalineprefix = " "; ' . "\n";
        echo ' var datalinesuffix = ""; ' . "\n";
        echo ' var extra = "", four; ' . "\n";
        echo ' var thisline = 1; ' . "\n";
        echo ' var totalleft = 100.0; ' . "\n";
        echo ' var title = ""; ' . "\n";
        echo ' var width = ""; ' . "\n";
        echo ' var height = ""; ' . "\n";
        echo ' var country = ""; ' . "\n";
        echo ' var popularity = ""; ' . "\n";
        echo ' if ((t.length + w.length + h.length + c.length + p.length) == 0) { ' . "\n";
        echo '   title = prompt("Enter Geo Map Title (prefix by a Region Code (eg. IT) (followed by ;) before title as an available option, suffix by &onclick=y for added onclick functionality (and to make a quiz question for someone after all your entries also append (things like) &guess=Brazil&question=Which country had Tijuana brass?))", "' . $GETtitle . '"); ' . "\n";
        echo ' }  ' . "\n";
        echo ' if ((t.length + w.length + h.length + c.length + p.length) != 0) { ' . "\n";
        echo ' title = t; ' . "\n";
        echo ' width = w; ' . "\n";
        echo ' height = h; ' . "\n";
        echo ' country = c; ' . "\n";
        echo ' popularity = p; ' . "\n";
        echo ' } else if (title != null) { ' . "\n";
        echo ' width = prompt("Enter Geo Map Width", "' . $GETwidth . '"); ' . "\n";
        echo ' height = prompt("Enter Geo Map Height", "' . $GETheight . '"); ' . "\n";
        echo ' country = prompt("Enter Country Label", "' . $GETcountry . '"); ' . "\n";
        echo ' popularity = prompt("Enter " + country + " Numerical Measure(s) (ie. can be comma separated)", "' . $GETpopularity . '"); ' . "\n";
        echo ' }  ' . "\n";
        echo ' var datac = ""; ' . "\n";
        echo ' var sq = "' . "'" . '"; ' . "\n";
        //echo ' datalineprefix = prompt("Enter " + country + " Name " + thisline, ""); ' . "\n";
        echo " datalineprefix = prompt(\"Enter \" + country + \" Name \" + thisline +  ' (optionally append with ' + '\\n\\n' + ',\"A tooltip (regarding {popularity}) of some sort like <a target=_blank href=' + \"'\" + 'https://www.google.com/#q={country}' + \"'\" + '>{country}</a>\" ' + '\\n\\n'" . $myextras . " + ' as an example of what is possible with HTML included)', ''); " . "\n";
        echo ' if (datalineprefix != null) { iof=0; dlsa=datalineprefix.split(","); if (dlsa.length > 1 && (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(",")))) { while (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(","))) { datalineprefix=datalineprefix.replace(",", "%2C"); iof+=0;   }  }    datalineprefix=datalineprefix.replace(/#/g, "%23"); if (datalineprefix.indexOf("{country}") != -1 && datalineprefix.indexOf(",") != -1) { dlsa=datalineprefix.split(","); if (dlsa.length > 1) {  datalineprefix=datalineprefix.replace("{country}",encodeURIComponent(dlsa[0])).replace("{country}",encodeURIComponent(dlsa[0]));   } }   } ' . "\n";
        echo ' while (datalineprefix.length != 0 && (popularity.indexOf("ercent") == (0 - 1) || popularity.indexOf("  ercent ") != (0 - 1) || (popularity.indexOf("ercent") != (0 - 1) && totalleft > 0))) { ' . "\n";
        echo ' if (datalinesuffix.length != 0) { ' . "\n";
        echo "    datalineprefix = prompt(\"Enter \" + country + \" Name \" + thisline +  ' (for no more please hit Cancel button and optionally append with ' + '\\n\\n' + ',\"A tooltip (regarding {popularity}) of some sort like <a target=_blank href=' + \"'\" + 'https://www.google.com/#q={country}' + \"'\" + '>{country}</a>\" ' + '\\n\\n'" . $myextras . " + ' as an example of what is possible with HTML included)', ''); " . "\n";
        echo '    if (datalineprefix != null) {  iof=0; dlsa=datalineprefix.split(","); if (dlsa.length > 1 && (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(",")))) { while (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(","))) { datalineprefix=datalineprefix.replace(",", "%2C"); iof+=0;   }  }   datalineprefix=datalineprefix.replace(/#/g, "%23"); if (datalineprefix.indexOf("{country}") != -1 && datalineprefix.indexOf(",") != -1) { dlsa=datalineprefix.split(","); if (dlsa.length > 1) {  datalineprefix=datalineprefix.replace("{country}",encodeURIComponent(dlsa[0])).replace("{country}",encodeURIComponent(dlsa[0]));   } }   } ' . "\n";
        echo ' } ' . "\n";
        //echo ' } ' . "\n";
        echo ' if (datalineprefix == null) { ' . "\n";
        echo '     datalineprefix = ""; ' . "\n";
        echo '     totalleft = 0; ' . "\n";
        echo ' } else if (datalineprefix.length != 0) { ' . "\n";
        echo '     extra = "";  ' . "\n";
        echo '     if (popularity.indexOf("ercent") != (0 - 1) && popularity.indexOf("  ercent ") == (0 - 1)) extra = " (for " + totalleft + " enter nothing or hit Cancel button)";  ' . "\n";
        echo '     datalinesuffix = prompt("Enter " + popularity.replace("  ercent ", "") + " " + thisline + extra, "0"); ' . "\n";
        echo '     if (datalinesuffix == null && popularity.indexOf("ercent") != (0 - 1) && popularity.indexOf("  ercent ") == (0 - 1)) datalinesuffix = totalleft; ' . "\n";
        echo '     if (datalinesuffix == null) datalinesuffix = "0"; ' . "\n";
        echo '     if (datalinesuffix != null) datalineprefix = datalineprefix.replace("{popularity}", datalinesuffix); ' . "\n";
        echo '     if (datalinesuffix.length == 0) { ' . "\n";
        echo '       datalinesuffix = eval(totalleft); ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(totalleft) == eval(datalinesuffix) && popularity.indexOf("ercent") != (0 - 1) && popularity.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(datalinesuffix) > eval(totalleft) && popularity.indexOf("ercent") != (0 - 1) && popularity.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       datalinesuffix = eval(totalleft); ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(datalinesuffix) > eval(totalleft) && popularity.indexOf("ercent") == (0 - 1) && popularity.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       popularity = popularity + "  ercent "; ' . "\n";
        echo '     } else { ' . "\n";
        echo '       totalleft = eval(totalleft) - eval(datalinesuffix); ' . "\n";
        echo '     } ' . "\n";
        //echo '     datac = datac + ",[~" + datalineprefix + "~," + datalinesuffix + "]"; ' . "\n";
        echo '     if (datalineprefix.indexOf("' . "'" . '") == -1) {  four=datalinesuffix.split(","); if (exra != "") { exra=String.fromCharCode(39) + "Regarding " + popularity + four[0] + String.fromCharCode(39) + ","; }  datac = datac + comma + " [~" + datalineprefix + "~," + exra + datalinesuffix + "] "; } ' . "\n";
        echo '     if (datalineprefix.indexOf("' . "'" . '") != -1) {  four=datalinesuffix.split(","); exra=String.fromCharCode(39) + "Regarding " + popularity + four[0] + String.fromCharCode(39) + ","; datac = datac + comma + " [~" + datalineprefix.replace(",", "~,") +  "," + datalinesuffix + "] "; } ' . "\n"; 
        echo '     thisline++; ' . "\n comma=','; \n";
        echo '   } ' . "\n";
        echo ' } ' . "\n";
          if (!isset($_GET['isMobile']) && !isset($_POST['isMobile'])) {
        echo ' window.location = "./geo_chart.php?title=" + title + m + "&width=" + width + "&height=" + height + "&country=" + country + "&popularity=" + popularity.replace("  ercent ", "") + "&data=" + datac; ' . "\n";
		  } else {
        echo ' window.location = "./geo_chart.php?isMobile=y&title=" + title + m + "&width=" + width + "&height=" + height + "&country=" + country + "&popularity=" + popularity.replace("  ercent ", "") + "&data=" + datac; ' . "\n";
          }
        echo "} \n"; // . "} \n";
        echo '</' . 'script' . '>' . "\n";
      } 
      
    echo "\n" . '<input type="hidden" id="ihide" value="0"></input>' . "\n";

  if (strpos($_SERVER['QUERY_STRING'], "justmenu") === false && strpos($_SERVER['HTTP_REFERER'], "justmenu") === false) {
  echo '</div><!-- /page --></body>' . "\n";
  } else {   
  echo '</div><!-- /page -->&nbsp;&nbsp;&nbsp;<a target="top" title="menu" href="http://www.rjmprogramming.com.au/PHP/butsel.php?justmenu=justmenu">Menu</a></body>' . "\n";
  }
    ?>
</html>
