<?php
 // Idea: http://gomashup.com/cms/international_country_calling%20_codes
 // Validator: http://jsonlint.com/
 // http://gomashup.com/json.php?fds=geo/international/areacodes/country/Afghanistan&jsoncallback=?
 $selcountryval = "";
 $selcountrycodeval = "";
 $selareaval = "";
 $results = "";
 $gcountry = "";
 $gcountrycode = "";
 $ggcountry = "";
 $ggcountrycode = "";
 $garea = "";
 $sz = 2;
 
 if (isset($_GET['country']) || isset($_GET['countrycode']) || isset($_GET['area'])) {
 if (isset($_GET['area'])) {
 $ggcountry = ' ';
 $ggcountrycode = ' ';
 $pageContent = file_get_contents('http://gomashup.com/json.php?fds=geo/international/areacodes/area/' . $_GET['area'] . '&jsoncallback=?',0,null,null);
 } else if (isset($_GET['countrycode'])) {
 $gcountry = ' ';
 $pageContent = file_get_contents('http://gomashup.com/json.php?fds=geo/international/areacodes/countrycode/' . $_GET['countrycode'] . '&jsoncallback=?',0,null,null);
 } else if (isset($_GET['country'])) {
 $gcountrycode = ' ';
 $pageContent = file_get_contents('http://gomashup.com/json.php?fds=geo/international/areacodes/country/' . $_GET['country'] . '&jsoncallback=?',0,null,null);
 }
 $json = substr(str_replace(']}',']', str_replace('{x[','[', str_replace('"result":','', str_replace("))", "", str_replace("?(","",($pageContent . ")")))))),1);
 $json_output = json_decode($json); //,true); 
 if (sizeof($json_output) == 1) {
 if (!isset($_GET['area'])) $garea = ' ';
 if (!isset($_GET['country'])) $gcountry = ' ';
 if (!isset($_GET['countrycode'])) $gcountrycode = ' ';
 $ggcountry = '';
 $ggcountrycode = '';
 }
 
 $selcountryval = "<select style='display:none;' id='selcountryval'><option value=''>List of countrys below ...</option>";
 $selcountrycodeval = "<select style='display:none;' id='selcountrycodeval'><option value=''>List of country codes below ...</option>";
 $selareaval = "<select style='display:none;' id='selareaval'><option value=''>List of areas below ...</option>";
 $selareacode = "<select style='display:none;' id='selareacodeval'><option value=''>List of area codes below ...</option>";
 $sz = sizeof($json_output) + 2;
 for ($i=0; $i<sizeof($json_output); $i++) {
   if ($ggcountrycode == " ") {
     $ggcountrycode = $json_output[$i]->CountryCode;
   } else if (strlen($ggcountrycode) > 1 && $json_output[$i]->CountryCode != $ggcountrycode) {
     $ggcountrycode = '';
   }
   if ($ggcountry == " ") {
     $ggcountry = $json_output[$i]->Country;
   } else if (strlen($ggcountry) > 1 && $json_output[$i]->Country != $ggcountry) {
     $ggcountry = '';
   }
   if ($gcountrycode == " ") {
     $gcountrycode = $json_output[$i]->CountryCode;
   }
   if ($gcountry == " ") {
     $gcountry = $json_output[$i]->Country;
   }
   if ($garea == " ") {
     $garea = $json_output[$i]->Area;
   }
   $selcountrycodeval .= "\n<option value='" . $json_output[$i]->Country . "'>" . $json_output[$i]->Country . "</option>";
   $selcountryval .= "\n<option value='" . $json_output[$i]->CountryCode . "'>" . $json_output[$i]->CountryCode . "</option>";
   $selareaval .= "\n<option value='" . $json_output[$i]->Area . "'>" . $json_output[$i]->Area . "</option>";
   $selareacodeval .= "\n<option value='" . $json_output[$i]->AreaCode . "'>" . $json_output[$i]->AreaCode . "</option>";
   $results .= "\n<option value='" . $json_output[$i]->AreaCode . " ... " . $json_output[$i]->Area . " ... " . $json_output[$i]->Country . " ... " . $json_output[$i]->CountryCode . "'>" . $json_output[$i]->AreaCode . " ... " . $json_output[$i]->Area . " ... " . $json_output[$i]->Country . " ... " . $json_output[$i]->CountryCode . "</option>";
}
 $selcountrycodeval .= "\n</select>";
 $selcountryval .= "\n</select>";
 $selareaval .= "\n</select>";
 $selareacodeval .= "\n</select>";
 }
 echo "<html><head><title>International Calling Codes via http://gomashup.com/cms/international_country_calling_codes ... thanks.</title>";
?>
<script>
 function preanalyze(inputo, thisthing) {
   window.location = './international_calling_codes_plus.php?' + thisthing + '=' + inputo.value;
 } 
 
 function maybeopen() {
   var countryis=document.getElementById("country").value;
   var areais=document.getElementById("area").value;
   if (countryis.replace(" ","") != '') {
      window.open("https://maps.google.com.au/maps?q=" + countryis, "_blank", "left=430,top=100,width=900");
   }
   if (areais.replace(" ","") != '') {
      window.open("https://www.google.com.au/search?tbm=isch&q=" + areais, "_blank", "left=0,top=300,width=400,height=420");
   }
 }
 
</script>
<?php
 echo "\n</head>";
 echo "<body style='background-color:yellow;'><table border=5 style='background-color:lightgray;'><tr><td><label>&nbsp;Please enter a Country Name: </label></td><td><input  style='background-color:pink;' onchange=\"preanalyze(this,'country');\" id='country' type='text' value='" . $_GET['country'] . $gcountry . $ggcountry . "'></input></td></tr>";
 echo "<tr><td><label>&nbsp;... or Country Code: </label></td><td><input  style='background-color:pink;' onchange=\"preanalyze(this, 'countrycode');\" id='countrycode' type='text' value='" . $_GET['countrycode'] . $gcountrycode . $ggcountrycode . "'></input></td></tr>";
 echo "<tr><td><label>&nbsp;... or Area: </label></td><td><input  style='background-color:pink;' onchange=\"preanalyze(this, 'area');\" id='area' type='text' value='" . $_GET['area'] . $garea . "'></input></td></tr>";
 echo "<tr><td><label>&nbsp;International Call Area Codes: </label></td><td><select id='results' size=" . $sz . "><option value=''>Results will appear below</option>" . $results . "</select></td></tr>";
 //echo "\n" . $selcountryval . "&nbsp;&nbsp;" . $selcountrycodeval . "&nbsp;&nbsp;" . $selareaval . "&nbsp;&nbsp;" . $selareacodeval . "<script> selcountryobj=document.getElementById('selcountryval'); selcountrycodeobj=document.getElementById('selcountrycodeval'); countrycodeinput=document.getElementById('countrycode'); areainput=document.getElementById('area'); </script>";
 echo "<script> maybeopen(); </script></body></html>";
?>
