<?php
 // Idea: http://gomashup.com/json.php?fds=finance/aus/industry-group/[Industry-group]&jsoncallback=?
 // Validator: http://jsonlint.com/
 $an = "";
 $selcompanyval = "<select style='display:none;' id='selcompanyval'><option value=''>List of companies below ...</option>";
 $selcodeval = "<select style='display:none;' id='selcodeval'><option value=''>List of codes below ...</option>";
 $selindustrygroupval = "<select style='display:none;' id='selindustrygroupval'><option value=''>List of industry groups below ...</option>";
 $his = "";
 $asxarr = array("Automobile &amp; Components","Banks","Capital Goods","Classification Pending","Commercial &amp; Professional Services","Consumer Durables &amp; Apparel","Consumer Services","Diversified Financials","Energy","Food &amp; Staples Retailing","Food Beverage &amp; Tobacco","GICS Sector Code Not Applicable","Health Care Equipment &amp; Services","Household &amp; Personal Products","Insurance","Materials","Media","Pharmaceuticals","Real Estate","Retailing","Semiconductors &amp; Semiconductor Equipment","Software &amp; Services","Technology Hardware &amp; Equipment","Telecommunication Services","Transportation","Utilities","AKQ","FOX","GID","HCS","RVA");
 date_default_timezone_set('UTC');
 $timeis = date('h:i:s');
 $dateis = date('Y-m-d');
 $vstimeis = $timeis;
 $vsdateis = $dateis;
 if (file_exists("Banks_" . $dateis . ".dat") && !isset($_GET['refresh'])) {
   $fl=fopen("lastGo.is", "r");
   $vstimeis = fread($fl, 100);
   fclose($fl);
 } else {
   $fl=fopen("lastGo.is", "w");
   fwrite($fl, $timeis);
   fclose($fl);
 }
 foreach ($asxarr as &$value) {   
 if (file_exists(str_replace("_", "%20", $value) . "_" . $dateis . ".dat") && !isset($_GET['refresh'])) {
   if ($his == "") {
     $his = "<html><head><title>ASX Listing Information ... collected on (" . $vsdateis . "-" . str_replace("-", ":", $vstimeis) . " GMT) ... via http://gomashup.com/cms/Australian Securities Exchange ASX Code JSON Data Feeds ... thanks.";
     echo $his;
   }
   $pageContent = "";
   $fl=fopen(str_replace("_", "%20", $value) . "_" . $dateis . ".dat", "r");
   while (($buffer = fgets($fl, 500000)) !== false) {
     $pageContent .= $buffer;
   }
   fclose($fl);
 } else {
   if ($his == "") {
     $his = "<html><head><title>ASX Listing Information ... reading now (" . $dateis . "-" . $timeis . " GMT) ... via http://gomashup.com/cms/Australian Securities Exchange ASX Code JSON Data Feeds ... thanks.";
     echo $his;
   }
   $pageContent = file_get_contents('http://gomashup.com/json.php?fds=finance/aus/industry-group/' . str_replace(" ", "%20", $value) . '&jsoncallback=?',0,null,null);
   $fl=fopen(str_replace("_", "%20", $value) . "_" . $dateis . ".dat", "w");
   fwrite($fl, $pageContent);
   fclose($fl);
 }
 $json = substr(str_replace('industry-group','industrygroup', str_replace('company-name','companyname', str_replace(']}',']', str_replace('{x[','[', str_replace('"result":','', str_replace(")", "", str_replace("?(","",$pageContent))))))),1);
 $json_output = json_decode($json); //,true); 
 $an .= $value;
 for ($i=0; $i<sizeof($json_output); $i++) {
   //echo $i;
   $selcodeval .= "\n<option value='";
   $selcodeval .= $json_output[$i]->code;
   $selcodeval .= "'>";
   $selcodeval .= $json_output[$i]->code;
   $selcodeval .= "</option>";
   $selindustrygroupval .= "\n<option value='";
   $selindustrygroupval .= $json_output[$i]->industrygroup;
   $selindustrygroupval .= "'>";
   $selindustrygroupval .= $json_output[$i]->industrygroup;
   $selindustrygroupval .= "</option>";
   $selcompanyval .= "\n<option value='";
   $selcompanyval .= $json_output[$i]->companyname;
   $selcompanyval .= "'>";
   $selcompanyval .= $json_output[$i]->companyname;
   $selcompanyval .= "</option>";
 }
}
 $selcodeval .= "\n</select>";
 $selcompanyval .= "\n</select>";
 $selindustrygroupval .= "\n</select>";
 echo "</title>\n<script> ";
 echo "\n var selcompanyobj;";
 echo "\n var selcodeobj;";
 echo "\n var selindustrygroupobj;";
 echo "\n var codeinput;";
 echo "\n var industrygroupinput;";
 echo "\nfunction analyze(mt) {";
 ?>
    var codeis = '';
    var industrygroupis = '';
    var seli = -1;
    var mtv = "";
    var better = 0;
    for (var i=0; i < selcompanyobj.length; i++) {
        if (selcompanyobj[i].childNodes[0].nodeValue.toUpperCase().indexOf(mt.value.toUpperCase()) != -1) {
            if (better == 0 || selcompanyobj[i].childNodes[0].nodeValue.toUpperCase().length == mt.value.length) {
              seli = (i + 1);
              industrygroupis = selindustrygroupobj[i].childNodes[0].nodeValue;
              mtv = selcompanyobj[i].childNodes[0].nodeValue;
              better = 1;
            }
        }
    }
    if (seli < 0) {
    better = 1;
    for (var i=0; i < selcodeobj.length; i++) {
        if (selcodeobj[i].childNodes[0].nodeValue.toUpperCase().indexOf(mt.value.toUpperCase()) != -1) {
            if (better == 0 || selcodeobj[i].childNodes[0].nodeValue.toUpperCase().length == mt.value.length) {
              seli = (i + 1);
              industrygroupis = selindustrygroupobj[i].childNodes[0].nodeValue;
              mtv = selcompanyobj[i].childNodes[0].nodeValue;
              better = 1;
            }
        }
    }
    }
    if (seli >= 0) {
      mt.value = mtv;
      codeis = selcodeobj[seli - 1].childNodes[0].nodeValue;
      selcodeobj.value = selcodeobj[seli - 1].childNodes[0].nodeValue;
      codeinput.value = codeis;
      industrygroupinput.value = industrygroupis;     
       
      window.open("https://www.google.com.au/search?tbm=isch&q=" + mt.value, "_blank", "left=0,top=370,width=440,height=420");
      window.open("http://search.asx.com.au/s/search.html?query=" + codeis + "&collection=asx-meta&profile=web", "_blank", "left=470,top=100,width=800,height=800");
    } else {
      selcompanyobj.value = '';
      selcodeobj.value = '';
      codeinput.value = '';
      industrygroupinput.value = '';
      alert(mt.value + ' was not found ... sorry.');
      var maybe=prompt(mt.value + ' was not found on ASX database, but can try to look for information on ASX Search and Google Image Search if you like?  Change default value to nothing if you do not want to do this.', mt.value);
      if (maybe != '' && maybe != null) {
       window.open("https://www.google.com.au/search?tbm=isch&q=" + maybe, "_blank", "left=0,top=370,width=440,height=420");
       window.open("http://search.asx.com.au/s/search.html?query=" + maybe + "&collection=asx-meta&profile=web", "_blank", "left=470,top=100,width=800,height=800");
      }
    }
<?php
 echo "\n}";
 echo "\n</script>";
 echo "\n<style>";
 
  echo "\nbody {";
  echo "\n   background-color: yellow;";
  echo "\n }";
 
  echo "\n table {";
  echo "\n   border: 5px solid red;";
  echo "\n   background-color: lightgray;";
  echo "\n }";
   
  echo "\ninput.pinky {";
  echo "\n   background-color: pink;";
  echo "\n }";
 
 echo "\n</style>";
 echo "\n</head>";
 echo "<body style='background-color:yellow;'><table border=5 style='background-color:lightgray;'>\n<tr><td><label>Please enter an ASX listed Company Name or Code: </label></td><td><input class='pinky' onchange='analyze(this);' id='company' type='text' size=100 value=''></input><br><br></td></tr>";
 echo "\n<tr><td><label>Will show you Company Code: </label></td><td><input id='code' type='text' value=''></input><br><br></td></tr>";
 echo "\n<tr><td><label>Will show you Industry Group: </label></td><td><input id='industrygroup' type='text' value=''></input>";
 if ($vstimeis != $timeis && file_exists("lastGo.is")) {
   if ($vstimeis == "") {
    $fl=fopen("lastGo.is", "r");
    $vstimeis = fread($fl, 100);
    fclose($fl);
   }
   echo "&nbsp;&nbsp;<a id='refresh' href='./asx_look.php?refresh=" . $timeis . "' title='Click if " . $dateis . "-" . str_replace("-", ":", $vstimeis) . " vs " . $timeis . " GMT is too stale for you'>Click if " . $dateis . "-" . str_replace("-", ":", $vstimeis) . " vs " . $timeis . " GMT is too stale for you</a>";
 }
 echo "<br><br></td></tr>";
 echo "\n</table>\n" . $selcompanyval . "&nbsp;&nbsp;" . $selindustrygroupval . "&nbsp;&nbsp;" . $selcodeval . "<script> selcompanyobj=document.getElementById('selcompanyval'); selcodeobj=document.getElementById('selcodeval'); selindustrygroupobj=document.getElementById('selindustrygroupval'); codeinput=document.getElementById('code'); industrygroupinput=document.getElementById('industrygroup'); </script></body></html>";
?>
