<?php
$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";

      $xgd = ""; 
      $gd = ""; 
      $ntt=false;
      $nondefs=" ";
      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'];
       } 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']);
       }
       $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']))) {
       $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']);
      }
      $nondefs="width: " . $GETwidth . ", height: " . $GETheight;
      $newextras.=$nondefs;
      
      echo "   var refresh=0; prerefresh=0;   \n";
      echo ' google.load("visualization", "1", {packages:["geochart"]}); ' . "\n";
      echo ' google.setOnLoadCallback(drawChart); ' . "\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 "     var 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 ' var 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 " var chart = new google.visualization.GeoChart(document.getElementById('chart_div')); \n";

 if ($ntt) {
   
      //echo "   var selection; \n";
      echo "   var sliceid = 0; /" . "/ thanks to https://gist.github.com/alexrainman/bb8d49357250df0859c0  \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";
      }
}
      echo " chart.draw(data, {" . $newextras . "}); \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 = ""; ' . "\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)", "' . $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>
