<?php
// growth_of_merchandise_trade_2006_2016.php

// RJM Programming
// January 2020

// Using //wdi.worldbank.org/table/6.1
// World Development Indicators:
// Growth of merchandise trade
// //localhost:8888/PHP/BubbleChart/bubble_chart.php?title=Net barter terms of Export trade index 2006-2016&label='Letter'&value=Export volume,Export value,'Country',Net barter terms of Export trade index&data=, [~A~,-1.8,6.8,'Afghanistan',2.2] , [~A~,11.5,17.3,'Albania',0.1]

function uvaltosfcp($string) { // thanks to https://stackoverflow.com/questions/1805802/php-convert-unicode-codepoint-to-utf-8
  return html_entity_decode(preg_replace("/U\+([0-9A-F]{5})/", "&#x\\1;", $string), ENT_NOQUOTES, 'UTF-8');
}

function flagflag($inctryname) {
  global $flagentity, $iso_country_codes, $lri, $dri;
  $uretv="";
  $flagentity="";
  if ($inctryname != "") {
  $matchfound=false;
  for ($im=1; $im<sizeof($iso_country_codes); $im+=2) {
    if (strpos(strtolower($iso_country_codes[$im]), strtolower($inctryname)) !== false) {
      for ($jm=0; $jm<sizeof($lri); $jm++) {
       if (strtoupper(substr($iso_country_codes[-1 + $im],0,1)) == $lri[$jm]) {
         $uretv.=$dri[$jm]; //uvaltosfcp($dri[$jm]);
         $matchfound=true;
       }
      }
      for ($jm=0; $jm<sizeof($lri); $jm++) {
       if (strtoupper(substr($iso_country_codes[-1 + $im],1,1)) == $lri[$jm]) {
         $uretv.=$dri[$jm]; //uvaltosfcp($dri[$jm]);
       }
      }
      $flagentity=uvaltosfcp($uretv); //$uretv;
      return $inctryname;
    }
  }
  if ($uretv == "" && !$matchfound) {
  for ($im=1; $im<sizeof($iso_country_codes); $im+=2) {
    if (strpos(explode(",",strtolower($iso_country_codes[$im]))[0], explode(",",strtolower($inctryname))[0]) !== false) {
      for ($jm=0; $jm<sizeof($lri); $jm++) {
       if (strtoupper(substr($iso_country_codes[-1 + $im],0,1)) == $lri[$jm]) {
         $uretv.=$dri[$jm]; //uvaltosfcp($dri[$jm]);
         $matchfound=true;
       }
      }
      for ($jm=0; $jm<sizeof($lri); $jm++) {
       if (strtoupper(substr($iso_country_codes[-1 + $im],1,1)) == $lri[$jm]) {
         $uretv.=$dri[$jm]; //uvaltosfcp($dri[$jm]);
       }
      }
      $flagentity=uvaltosfcp($uretv); //$uretv;
      return $inctryname;
    }
  }
  }

  if ($uretv == "" && !$matchfound) {
  for ($im=1; $im<sizeof($iso_country_codes); $im+=2) {
    if (strpos(explode(" (",strtolower($iso_country_codes[$im]))[0], explode(" (",strtolower($inctryname))[0]) !== false) {
      for ($jm=0; $jm<sizeof($lri); $jm++) {
       if (strtoupper(substr($iso_country_codes[-1 + $im],0,1)) == $lri[$jm]) {
         $uretv.=$dri[$jm]; //uvaltosfcp($dri[$jm]);
         $matchfound=true;
       }
      }
      for ($jm=0; $jm<sizeof($lri); $jm++) {
       if (strtoupper(substr($iso_country_codes[-1 + $im],1,1)) == $lri[$jm]) {
         $uretv.=$dri[$jm]; //uvaltosfcp($dri[$jm]);
       }
      }
      $flagentity=uvaltosfcp($uretv); //$uretv;
      return $inctryname;
    }
  }
  }


  }
  return $inctryname;
}

$yrange="2006-2016";
$otherrange="2003-2013";
if (isset($_GET['yrange'])) { $yrange=str_replace("+"," ",urldecode($_GET['yrange'])); }
if ($yrange != "2006-2016") { $otherrange="2006-2016"; }
$lri=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
$dri=["U+1F1E6","U+1F1E7","U+1F1E8","U+1F1E9","U+1F1EA","U+1F1EB","U+1F1EC","U+1F1ED","U+1F1EE","U+1F1EF","U+1F1F0","U+1F1F1","U+1F1F2","U+1F1F3","U+1F1F4","U+1F1F5","U+1F1F6","U+1F1F7","U+1F1F8","U+1F1F9","U+1F1FA","U+1F1FB","U+1F1FC","U+1F1FD","U+1F1FE","U+1F1FF"];
$thexports="<th title='Click/touch red triangle to reveal Imports or black triangle to reveal Exports' colspan=2 align='left'>Exports <a title='none|9654' onclick=\"document.getElementById('trexports').style.display=this.title.split('|')[0];  this.innerHTML='&#' + this.title.split('|')[1] + ';';  var thistitle=this.title.split('|')[0].replace('none','TABLE-ROW').replace('table-row','none').toLowerCase();  var thisemoji=this.title.split('|')[1].replace('9654','128~315').replace('128315','96~54').replace('~','');  this.title=thistitle + '|' + thisemoji; \" style=\"text-decoration:none;cursor:pointer;\">&#128315;</a></th>";
$thimports="<th title='Click/touch red triangle to reveal Year Range dropdown or black triangle to reveal Imports' colspan=2 align='left'>Imports <a title='none|9654' onclick=\"document.getElementById('trimports').style.display=this.title.split('|')[0];  this.innerHTML='&#' + this.title.split('|')[1] + ';';  var thisititle=this.title.split('|')[0].replace('none','TABLE-ROW').replace('table-row','none').toLowerCase();  var thisiemoji=this.title.split('|')[1].replace('9654','128~315').replace('128315','96~54').replace('~','');  this.title=thisititle + '|' + thisiemoji; \" style=\"text-decoration:none;cursor:pointer;\">&#128315;</a></th>";
//$htmlis="<!doctype html><html><head><title>Growth of Merchandise Trade - RJM Programming - January,2020</title><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'><sc" . "ript type='text/javascript'> var cdone=','; function subclick(ifid) { if (cdone.indexOf(',' + ifid + ',') == -1) { cdone=cdone + ifid + ','; document.getElementById('sf' + ifid).click(); }  }   </s" . "cript></head><body style='background-color:yellow;'><h1 align='center'>Growth of Merchandise Trade " . $yrange . "</h1><h3 align='center'>Thanks to Worldbank data of http://wdi.worldbank.org/table/6.1</h3><details id=detexports open><summary id=sumexports>Exports</summary><table><tbody><tr><th colspan=26 align='left'></th></tr><tr></tr></tbody></table></details><details id=detimports open><summary id=sumimports>Imports</summary><table><tbody><tr><th colspan=26 align='left'></th></tr><tr></tr></tbody></table></details></body></html>";
$htmlis="<!doctype html><html><head><title>Growth of Merchandise Trade - RJM Programming - January,2020</title><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'><style> th { text-align: center; } </style><sc" . "ript type='text/javascript'> var cdone=','; function subclick(ifid) { if (cdone.indexOf(',' + ifid + ',') == -1) { cdone=cdone + ifid + ','; document.getElementById('sf' + ifid).click(); }  }   </s" . "cript></head><body style='background-color:yellow;'><h1 id=thexab align='center'>Growth of Merchandise Trade " . $yrange . "</h1><h3 align='center'>Thanks to Worldbank data of http://wdi.worldbank.org/table/6.1</h3><table><thead><tr>" . str_replace("<th ","<th id=the_x_ab ",$thexports) . str_replace("<th ","<th id=thexcd ",$thexports) . str_replace("<th ","<th id=thexef ",$thexports) . str_replace("<th ","<th id=thexgh ",$thexports) . str_replace("<th ","<th id=thexij ",$thexports) . str_replace("<th ","<th id=thexkl ",$thexports) . str_replace("<th ","<th id=thexmn ",$thexports) . str_replace("<th ","<th id=thexop ",$thexports) . str_replace("<th ","<th id=thexqr ",$thexports) . str_replace("<th ","<th id=thexst ",$thexports) . str_replace("<th ","<th id=thexuv ",$thexports) . str_replace("<th ","<th id=thexwx ",$thexports) . str_replace("<th ","<th id=thexyz ",$thexports) . "</tr></thead></details><tbody><tr id=trexports></tr><tr>" . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . $thimports . "</tr><tr id=trimports></tr></tbody></table></body></html>";
$things=["Country","Export volume","Import volume","Export value","Import value","Net barter terms of trade index"];
$values=["","","","","",""];
$etitle="Net barter terms of Export trade index " . $yrange;
$elabel="'Letter'";
$evalue="Export volume (average annual % growth),Export value (average annual % growth),'Country',Net barter terms of Export trade index (2000 = 100)";
$eurlprefix="bubble_chart.php?title=Net barter terms of Export trade index " . $yrange . "&onclick=y&label='Letter'&value=Export volume (average annual % growth),Export value (average annual % growth),'Country',Net barter terms of Export trade index&data=";
$exporturls=[$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix,$eurlprefix];
$ititle="Net barter terms of Import trade index " . $yrange;
$ilabel="'Letter'";
$ivalue="Import volume (average annual % growth),Import value (average annual % growth),'Country',Net barter terms of Import trade index";
$iurlprefix="bubble_chart.php?title=Net barter terms of Import trade index " . $yrange . "&onclick=y&label='Letter'&value=Import volume (average annual % growth),Import value (average annual % growth),'Country',Net barter terms of Export trade index&data=";
$importurls=[$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix,$iurlprefix];
$letters=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];	
$flagentity="";	
$iso_country_codes=['AF','Afghanistan',
'AX','Aland Islands',
'AL','Albania',
'DZ','Algeria',
'AS','American Samoa',
'AD','Andorra',
'AO','Angola',
'AI','Anguilla',
'AQ','Antarctica',
'AG','Antigua and Barbuda',
'AR','Argentina',
'AM','Armenia',
'AW','Aruba',
'AU','Australia',
'AT','Austria',
'AZ','Azerbaijan',
'BS','Bahamas the',
'BH','Bahrain',
'BD','Bangladesh',
'BB','Barbados',
'BY','Belarus',
'BE','Belgium',
'BZ','Belize',
'BJ','Benin',
'BM','Bermuda',
'BT','Bhutan',
'BO','Bolivia',
'BA','Bosnia and Herzegovina',
'BW','Botswana',
'BV','Bouvet Island (Bouvetoya)',
'BR','Brazil',
'IO','British Indian Ocean Territory (Chagos Archipelago)',
'VG','British Virgin Islands',
'BN','Brunei Darussalam',
'BG','Bulgaria',
'BF','Burkina Faso',
'BI','Burundi',
'KH','Cambodia',
'CM','Cameroon',
'CA','Canada',
'cv','Cabo Verde',
'CV','Cape Verde',
'KY','Cayman Islands',
'CF','Central African Republic',
'TD','Chad',
'CL','Chile',
'CN','China',
'CX','Christmas Island',
'CC','Cocos (Keeling) Islands',
'CO','Colombia',
'KM','Comoros the',
'CD','Congo, Dem. Rep.',
'CG','Congo, Rep.',
'CK','Cook Islands',
'CR','Costa Rica',
'CI','Cote d`Ivoire',
'HR','Croatia',
'CU','Cuba',
'CY','Cyprus',
'CZ','Czech Republic',
'DK','Denmark',
'DJ','Djibouti',
'DM','Dominica',
'DO','Dominican Republic',
'EC','Ecuador',
'EG','Egypt',
'SV','El Salvador',
'GQ','Equatorial Guinea',
'ER','Eritrea',
'EE','Estonia',
'ET','Ethiopia',
'FO','Faroe Islands',
'FK','Falkland Islands (Malvinas)',
'FJ','Fiji the Fiji Islands',
'FI','Finland',
'FR','France, French Republic',
'GF','French Guiana',
'PF','French Polynesia',
'TF','French Southern Territories',
'GA','Gabon',
'GM','Gambia the',
'GE','Georgia',
'DE','Germany',
'GH','Ghana',
'GI','Gibraltar',
'GR','Greece',
'GL','Greenland',
'GD','Grenada',
'GP','Guadeloupe',
'GU','Guam',
'GT','Guatemala',
'GG','Guernsey',
'GN','Guinea',
'GW','Guinea-Bissau',
'GY','Guyana',
'HT','Haiti',
'HM','Heard Island and McDonald Islands',
'VA','Holy See (Vatican City State)',
'HN','Honduras',
'hk','Hong Kong SAR',
'HK','Hong Kong',
'HU','Hungary',
'IS','Iceland',
'IN','India',
'ID','Indonesia',
'IR','Iran',
'IQ','Iraq',
'IE','Ireland',
'IM','Isle of Man',
'IL','Israel',
'IT','Italy',
'JM','Jamaica',
'JP','Japan',
'je','Channel Islands',
'JE','Jersey',
'JO','Jordan',
'KZ','Kazakhstan',
'KE','Kenya',
'KI','Kiribati',
'KP','Korea, Rep.',
'KR','Korea, Dem. People’s Rep.', 
'KW','Kuwait',
'KG','Kyrgyz Republic',
'lA','Lao PDR',
'la','Laos',
'LA','Lao',
'LV','Latvia',
'LB','Lebanon',
'LS','Lesotho',
'LR','Liberia',
'LY','Libyan Arab Jamahiriya',
'LI','Liechtenstein',
'LT','Lithuania',
'LU','Luxembourg',
'mo','Macao SAR',
'MO','Macao',
'MK','Macedonia',
'MG','Madagascar',
'MW','Malawi',
'MY','Malaysia',
'MV','Maldives',
'ML','Mali',
'MT','Malta',
'MH','Marshall Islands',
'MQ','Martinique',
'MR','Mauritania',
'MU','Mauritius',
'YT','Mayotte',
'MX','Mexico',
'FM','Micronesia',
'MD','Moldova',
'MC','Monaco',
'MN','Mongolia',
'ME','Montenegro',
'MS','Montserrat',
'MA','Morocco',
'MZ','Mozambique',
'MM','Myanmar',
'NA','Namibia',
'NR','Nauru',
'NP','Nepal',
'nl','Netherlands',
'AN','Netherlands Antilles',
'NL','Netherlands the',
'NC','New Caledonia',
'NZ','New Zealand',
'NI','Nicaragua',
'NE','Niger',
'NG','Nigeria',
'NU','Niue',
'NF','Norfolk Island',
'MK','North Macedonia',
'MP','Northern Mariana Islands',
'NO','Norway',
'OM','Oman',
'PK','Pakistan',
'PW','Palau',
'ps','West Bank and Gaza',
'PS','Palestinian Territory',
'PA','Panama',
'PG','Papua New Guinea',
'PY','Paraguay',
'PE','Peru',
'PH','Philippines',
'PN','Pitcairn Islands',
'PL','Poland',
'PT','Portugal, Portuguese Republic',
'PR','Puerto Rico',
'QA','Qatar',
'RE','Reunion',
'RO','Romania',
'RU','Russian Federation',
'RW','Rwanda',
'bl','St. Barthelemy',
'sh','St. Helena',
'kn','St. Kitts and Nevis',
'lc','St. Lucia',
'mF','Sint Maarten',
'mf','St. Martin',
'pm','St. Pierre and Miquelon',
'vc','St. Vincent and the Grenadines',
'BL','Saint Barthelemy',
'SH','Saint Helena',
'KN','Saint Kitts and Nevis',
'LC','Saint Lucia',
'MF','Saint Martin',
'PM','Saint Pierre and Miquelon',
'VC','Saint Vincent and the Grenadines',
'WS','Samoa',
'SM','San Marino',
'ST','Sao Tome and Principe',
'SA','Saudi Arabia',
'SN','Senegal',
'RS','Serbia',
'SC','Seychelles',
'SL','Sierra Leone',
'SG','Singapore',
'SK','Slovakia (Slovak Republic)',
'SI','Slovenia',
'SB','Solomon Islands',
'SO','Somalia, Somali Republic',
'ZA','South Africa',
'GS','South Georgia and the South Sandwich Islands',
'SS','South Sudan',
'ES','Spain',
'LK','Sri Lanka',
'SD','Sudan',
'SR','Suriname',
'SJ','Svalbard & Jan Mayen Islands',
'SZ','Swaziland',
'SE','Sweden',
'CH','Switzerland, Swiss Confederation',
'SY','Syrian Arab Republic',
'TW','Taiwan',
'TJ','Tajikistan',
'TZ','Tanzania',
'TH','Thailand',
'TL','Timor-Leste',
'TG','Togo',
'TK','Tokelau',
'TO','Tonga',
'TT','Trinidad and Tobago',
'TN','Tunisia',
'TR','Turkey',
'TM','Turkmenistan',
'TC','Turks and Caicos Islands',
'TV','Tuvalu',
'UG','Uganda',
'UA','Ukraine',
'AE','United Arab Emirates',
'GB','United Kingdom',
'US','United States of America',
'UM','United States Minor Outlying Islands',
'vi','Virgin Islands (U.S.)',
'VI','United States Virgin Islands',
'UY','Uruguay, Eastern Republic of',
'UZ','Uzbekistan',
'VU','Vanuatu',
'VE','Venezuela',
'VN','Vietnam',
'WF','Wallis and Futuna',
'EH','Western Sahara',
'YE','Yemen',
'ZM','Zambia',
'ZW','Zimbabwe'];
	
if (file_exists("wdi_worldbank_org-table-6.1" . str_replace("-2003-2013","",("-" . $yrange)) )) {
  $cont=explode("<tbody><tr><", file_get_contents("wdi_worldbank_org-table-6.1" . str_replace("-2003-2013","",("-" . $yrange))))[1];
} else {
  $cont=file_get_contents("HTTP://wdi.worldbank.org/table/6.1" . str_replace("-2003-2013","",("-" . $yrange)));
  file_put_contents("wdi_worldbank_org-table-6.1" . str_replace("-2003-2013","",("-" . $yrange)), $cont);
}

$bits=explode("<td>" . '<div class="spacer">', str_replace(" class=''","",   str_replace(" class=''","",str_replace(' class="country"',"",  str_replace("'Ivoire","`Ivoire",$cont)))         ));
//$bits=explode('<td class="">' . '<div class="spacer">', str_replace(" class=''","",$cont));
//echo " " . sizeof($bits) . "\n";
//exit;
//echo "1:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[1])) . "\n";
//echo "2:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[2])) . "\n";
//echo "3:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[3])) . "\n";
//echo "4:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[4])) . "\n";
//echo "5:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[5])) . "\n";
//echo "6:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[6])) . "\n";
//echo "7:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[7])) . "\n";
//echo "8:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[8])) . "\n";
//echo "9:" . str_replace("<","&lt;",str_replace(">","&gt;",$bits[9])) . "\n";
//exit;

//new ...
//<td class="country"><div class="spacer"><a data-customlink="nl:body" data-text="Metadata:Afghanistan" href="javascript:void(0)" class="metaLink"  onclick="loadMetaData('AFG', 'C' ,'Country',  'Afghanistan')">Afghanistan</a></div></td>     <td class=''><div class="spacer">-0.7</div></td>     <td class=''><div class="spacer">11.0</div></td>     <td class=''><div class="spacer">3.2</div></td>     <td class=''><div class="spacer">12.9</div></td>     <td class=''><div class="spacer">163.6</div></td>    </tr>     <tr>     
//<td class="country"><div class="spacer"><a data-customlink="nl:body" data-text="Metadata:Albania" href="javascript:void(0)" class="metaLink"  onclick="loadMetaData('ALB', 'C' ,'Country',  'Albania')">Albania</a></div></td>                 <td class=''><div class="spacer">8.0</div></td>      <td class=''><div class="spacer">0.9</div></td>      <td class=''><div class="spacer">9.9</div></td>     <td class=''><div class="spacer">2.3</div></td>      <td class=''><div class="spacer">93.4</div></td>    </tr>    
//1:<a data-customlink="nl:body" data-text="Metadata:Afghanistan" href="javascript:void(0)" class="metaLink" onclick="loadMetaData('AFG', 'C' ,'Country', 'Afghanistan')">Afghanistan</a></div></td> 2:-0.7</div></td>
//old ...
//<td  class="country"><div class="spacer"><a href="javascript:void(0)" class="metaLink"  onclick="loadMetaData('AFG', 'C' ,'Country',  'Afghanistan')">Afghanistan</a></div></td>                                                               <td class=''><div class="spacer">-1.8</div></td>     <td class=''><div class="spacer">5.9</div></td>      <td class=''><div class="spacer">6.8</div></td>     <td class=''><div class="spacer">12.7</div></td>     <td class=''><div class="spacer">2.2</div></td>    </tr>     <tr>     
//<td  class="country"><div class="spacer"><a href="javascript:void(0)" class="metaLink"  onclick="loadMetaData('ALB', 'C' ,'Country',  'Albania')">Albania</a></div></td>                                                                       <td class=''><div class="spacer">11.5</div></td>     <td class=''><div class="spacer">4.9</div></td>      <td class=''><div class="spacer">17.3</div></td>    <td class=''><div class="spacer">10.3</div></td>     <td class=''><div class="spacer">0.1</div></td>    </tr> 
//1:<a data-customlink="nl:body" data-text="Metadata:Afghanistan" href="javascript:void(0)" class="metaLink" onclick="loadMetaData('AFG', 'C' ,'Country', 'Afghanistan')">Afghanistan</a></div></td> 2:-0.7</div></td>

$ioff=0;

for ($i=1; $i<sizeof($bits); $i++) {
  if ((($i + $ioff - 1) % (sizeof($values) - 1)) == 0) {
    if (flagflag($values[0]) != "") {
//echo "**" . $values[0] . "*";
//exit;
      $idx=ord(substr($values[0],0,1)) - ord('A');
      if (strpos($importurls[$idx], ",'" . $values[0]) == false) {
      if ($flagentity != "") {
      $importurls[$idx].=", [~" . $flagentity . "~," . $values[2] . "," . $values[4] . ",'" . $values[0] . " " . $flagentity . "'," . $values[5] . "]";
      $exporturls[$idx].=", [~" . $flagentity . "~," . $values[1] . "," . $values[3] . ",'" . $values[0] . " " . $flagentity . "'," . $values[5] . "]";
      } else {
      $importurls[$idx].=", [~" . substr($values[0],0,1) . "~," . $values[2] . "," . $values[4] . ",'" . $values[0] . "'," . $values[5] . "]";
      $exporturls[$idx].=", [~" . substr($values[0],0,1) . "~," . $values[1] . "," . $values[3] . ",'" . $values[0] . "'," . $values[5] . "]";
      }
      }
      $values[0] = "";
    }
    // >Afghanistan</a<</div<</td<<td class=''<<div class="spacer"<
    $presubbits=explode(">", str_replace("<", ">", $bits[-1 + $i]));
    $values[0]=str_replace("'", "`", trim($presubbits[-7 + sizeof($presubbits)]));
    $values[0]=explode(">", explode("</a>", explode("</div>", $values[0])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $values[0])[0])[0]))];
//echo "!" . $i . "!" . $values[0] . "!" . str_replace("<","&lt;",str_replace(">","&gt;",$presubbits));
//exit;
    //if (substr($values[0],0,1) == "B" || substr($values[0],0,1) == "C" || substr($values[0],0,1) == "M" || substr($values[0],0,1) == "P" || substr($values[0],0,1) == "R" || substr($values[0],0,1) == "S") {
    //  $cvals=explode(" ", $values[0]);
    //  $values[0] = $cvals[0];
    //}
  }
  $subbits=explode("</div>", $bits[$i]);
//echo "#" . $subbits[0] . "#";
//exit;
  if (strpos($bits[$i], ' data-text="Metadata:') != false) {
  // <a data-customlink="nl:body" data-text="Metadata:Andorra" href="javascript:void(0)" class="metaLink" onclick="loadMetaData('AND', 'C' ,'Country', 'Andorra')">Andorra</a></div></td>
  $ioff=(sizeof($values) - ($i % sizeof($values))); //       ... if i=5  bad is ((5-1) % 5) needs to be 0
  $values[0]=explode(">", explode("</a>", explode("</div>", $bits[$i])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $bits[$i])[0])[0]))];
if ($ioff != 0) {
   // Andorra at 24 computes to 3 <a data-customlink="nl:body" data-text="Metadata:Andorra" href="javascript:void(0)" class="metaLink" onclick="loadMetaData('AND', 'C' ,'Country', 'Andorra')">Andorra</a></div></td>
   //echo $values[0] . " ... " . $i . " " . $ioff; // at " . $i . " computes to " . str_replace("<", "&lt;", str_replace(">", "&gt;", $values[((($i - 1) % (sizeof($values) - 1)) + 1)])) . " (" . ((($i - 1) % (sizeof($values) - 1)) + 1) . ") " . str_replace("<", "&lt;", str_replace(">", "&gt;", $bits[$i]));
   //exit;
   //if (strpos($bits[$i + 1]. ' ' . $bits[$i + 2]. ' ' . $bits[$i + 3]. ' ' . $bits[$i + 4]. ' ' . $bits[$i + 5], "<") !== false) { echo $values[0] . ' ' . $bits[$i + 1]. ' ' . $bits[$i + 2]. ' ' . $bits[$i + 3]. ' ' . $bits[$i + 4]. ' ' . $bits[$i + 5]. ' ';
   //exit;
   //}
  $values[1]=str_replace("..","0.0",explode("<",$bits[$i + 1])[0]); //explode(">", explode("</a>", explode("</div>", $bits[$i + 1])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $bits[$i + 1])[0])[0]))];
  $values[2]=str_replace("..","0.0",explode("<",$bits[$i + 2])[0]); //explode(">", explode("</a>", explode("</div>", $bits[$i + 2])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $bits[$i + 2])[0])[0]))];
  $values[3]=str_replace("..","0.0",explode("<",$bits[$i + 3])[0]); //explode(">", explode("</a>", explode("</div>", $bits[$i + 3])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $bits[$i + 3])[0])[0]))];
  $values[4]=str_replace("..","0.0",explode("<",$bits[$i + 4])[0]); //explode(">", explode("</a>", explode("</div>", $bits[$i + 4])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $bits[$i + 4])[0])[0]))];
  $values[5]=str_replace("..","0.0",explode("<",$bits[$i + 5])[0]); //explode(">", explode("</a>", explode("</div>", $bits[$i + 5])[0])[0])[-1 + sizeof(explode(">", explode("</a>", explode("</div>", $bits[$i + 5])[0])[0]))];
}
  } else if ($ioff == 0) { 
  $values[(($i + $ioff - 1) % (sizeof($values) - 1)) + 1]=str_replace("..","0.0",$subbits[0]);
  }
//echo "!!" . $i . "!" . $values[0] . "!" . str_replace("<","&lt;",str_replace(">","&gt;",$presubbits));
//exit;
}
if (flagflag($values[0]) != "" || 1 == 1) {
//echo "*" . $values[0] . "*";
//exit;
    if (flagflag($values[0]) != "") {
    $idx=ord(substr($values[0],0,1)) - ord('A');
    if (strpos($importurls[$idx], ",'" . $values[0]) == false) {
    if ($flagentity != "") {    
    $importurls[$idx].=", [~" . $flagentity . "~," . $values[2] . "," . $values[4] . ",'" . $values[0] . " " . $flagentity . "'," . $values[5] . "]";
    $exporturls[$idx].=", [~" . $flagentity . "~," . $values[1] . "," . $values[3] . ",'" . $values[0] . " " . $flagentity . "'," . $values[5] . "]";
    } else {
    $importurls[$idx].=", [~" . substr($values[0],0,1) . "~," . $values[2] . "," . $values[4] . ",'" . $values[0] . "'," . $values[5] . "]";
    $exporturls[$idx].=", [~" . substr($values[0],0,1) . "~," . $values[1] . "," . $values[3] . ",'" . $values[0] . "'," . $values[5] . "]";
    }
    }
    }
    //echo "<p>" . $values[0] . " " . $values[1] . " " . $values[2] . " " . $values[3] . " " . $values[4] . " " . $values[5] . " " . $values[6] . "</p><br>";
    //echo "<p>\$exporturls[" . $idx . "]=" . $exporturls[$idx] . "</p><br>";
    for ($i=0; $i<sizeof($importurls); $i++) {
      $onli="";
      $onle="";
      $ximporturls=$importurls[$i];
      $xexporturls=$exporturls[$i];
      if (1 == 1 || $i == 1 || $i == 2 || $i == 12 || $i == 15 || $i == 17 || $i == 18) $onli=" onload=\" subclick('i" . $i . "'); \" "; 
      if (1 == 1 || $i == 1 || $i == 2 || $i == 12 || $i == 15 || $i == 17 || $i == 18) $onle=" onload=\" subclick('e" . $i . "'); \" "; 
      if (1 == 1 || $i == 1 || $i == 2 || $i == 12 || $i == 15 || $i == 17 || $i == 18) {
        $huhs=explode("&data=", $importurls[$i]);
        $ximporturls=str_replace($yrange, $yrange . " " . $letters[$i] . "",$huhs[0]);
      }
      if (1 == 1 || $i == 1 || $i == 2 || $i == 12 || $i == 15 || $i == 17 || $i == 18) {
        $huhs=explode("&data=", $exporturls[$i]);
        $xexporturls=str_replace($yrange, $yrange . " " . $letters[$i] . "",$huhs[0]); 
      }
      $htmlis=str_replace("</tr></tbody>", "<td><form target='i" . $i . "' style='display:none;' name='fi" . $i . "' id='fi" . $i . "' action=\"" . explode("?",str_replace("&data=","",$iurlprefix))[0] . "\" method='POST'><input type='hidden' name='onclick' value='y'></input><input type='hidden' name='title' value=\"" . str_replace($yrange, $yrange . " " . $letters[$i] . "",$ititle) . "\"></input><input type='hidden' name='label' value=\"" . $ilabel . "\"></input><input type='hidden' name='value' value=\"" . $ivalue . "\"></input>\n<input type='hidden' name='data' value=\"" . str_replace($iurlprefix,"",$importurls[$i]) . "\"></input>\n<input id='sfi" . $i . "' type='submit' value='Submit'></input></form><iframe " . $onli . " name='i" . $i . "' id='i" . $i . "' height=600 width=930 src=\"" . str_replace($yrange, $yrange . " " . $letters[$i] . "",$ximporturls) . "\"></iframe></td></tr></tbody>", $htmlis);
      $htmlis=str_replace("</tr><tr><th", "<td><form target='e" . $i . "' style='display:none;' name='fe" . $i . "' id='fe" . $i . "' action=\"" . explode("?",str_replace("&data=","",$eurlprefix))[0] . "\" method='POST'><input type='hidden' name='onclick' value='y'></input><input type='hidden' name='title' value=\"" . str_replace($yrange, $yrange . " " . $letters[$i] . "",$etitle) . "\"></input><input type='hidden' name='label' value=\"" . $elabel . "\"></input><input type='hidden' name='value' value=\"" . $evalue . "\"></input>\n<input type='hidden' name='data' value=\"" . str_replace($eurlprefix,"",$exporturls[$i]) . "\"></input>\n<input id='sfe" . $i . "' type='submit' value='Submit'></input></form><iframe " . $onle . " name='e" . $i . "' id='e" . $i . "' height=600 width=930 src=\"" . str_replace($yrange, $yrange . " " . $letters[$i] . "",$xexporturls) . "\"></iframe></td></tr><tr><th", $htmlis);
    }

    //for ($i=0; $i<sizeof($exporturls); $i++) {
    //  $htmlis=str_replace("</tr><tr><th", "<td><iframe height=600 width=850 src=\"" . str_replace("index 2006-2016", "index 2006-2016 " . $letters[$i] . "",$exporturls[$i]) . "\"></iframe></td></tr><tr><th", $htmlis);
    //}

    echo str_replace('</body>', '<br><br><select onchange="if (this.value.length == 2) { location.href=' . "'" . '#thex' . "'" . ' + this.value.toLowerCase(); } else if (this.value.length == 0) {  location.href=document.URL.split(' . "'" . '?' . "'" . ')[0].split(' . "'" . '#' . "'" . ')[0].replace(/\_2006\_2016/g,' . "'" . '' . "'" . ');  } else { location.href=document.URL.split(' . "'" . '?' . "'" . ')[0].split(' . "'" . '#' . "'" . ')[0] + ' . "'" . '?yrange=' . "'" . ' + this.value; }"><option value=' . $yrange . '>' . $yrange . '</option><option value=' . $otherrange . '>' . $otherrange . '</option><option value="">No Flags 2003-2013</option><option value="AB">A,B</option><option value="CD">C,D</option><option value="EF">E,F</option><option value="GH">G,H</option><option value="IJ">I,J</option><option value="KL">K,L</option><option value="MN">M,N</option><option value="OP">O,P</option><option value="QR">Q,R</option><option value="ST">S,T</option><option value="UV">U,V</option><option value="WX">W,X</option><option value="YZ">Y,Z</option></select></body>', str_replace(' <a ', ' <select onchange="if (this.value.length == 2) { location.href=' . "'" . '#thex' . "'" . ' + this.value.toLowerCase(); } else if (this.value.length == 0) {  location.href=document.URL.split(' . "'" . '?' . "'" . ')[0].split(' . "'" . '#' . "'" . ')[0].replace(/\_2006\_2016/g,' . "'" . '' . "'" . ');  } else { location.href=document.URL.split(' . "'" . '?' . "'" . ')[0].split(' . "'" . '#' . "'" . ')[0] + ' . "'" . '?yrange=' . "'" . ' + this.value; }"><option value=' . $yrange . '>' . $yrange . '</option><option value=' . $otherrange . '>' . $otherrange . '</option><option value="">No Flags 2003-2013</option><option value="AB">A,B</option><option value="CD">C,D</option><option value="EF">E,F</option><option value="GH">G,H</option><option value="IJ">I,J</option><option value="KL">K,L</option><option value="MN">M,N</option><option value="OP">O,P</option><option value="QR">Q,R</option><option value="ST">S,T</option><option value="UV">U,V</option><option value="WX">W,X</option><option value="YZ">Y,Z</option></select> <a ', $htmlis));
}

?>

