<?php
  //use environmental variables for my path
// Works as of PHP 4.3.0
$home = getenv('WEB');
$search=getenv('SEARCH');

#home=$_ENV['WEB'];
set_include_path("$home/html/iscbulletin/include");
// Works in all PHP versions
//ini_set('include_path', "$home/html/include");

//set_include_path(get_include_path() . PATH_SEPARATOR ."$home/html/iscbulletin/include");
// Works in all PHP versions
//ini_set('include_path', "$home/html/iscbulletin/include");



// Get a list of all agencies
# DB
$user = "web";
$pass = "webuser";
$host = getenv('PGHOSTADDR');
$port = "5432";
$dbne = "isc";

#Open database connection	
$dbh = pg_connect("host=$host port=$port dbname=$dbne user=$user password=$pass") or die("Could not connect") ;
pg_query($dbh, "set search_path to isc");

#Database command 
$cmd = "SELECT distinct code 
FROM isc.agency 
WHERE code is not null";
#Execute the query
$result = pg_query($dbh, $cmd);
$allagencies= array();
// fetch_row is supposed to be faster than fetch_array
while ($row = pg_fetch_row($result)) {

  $agency  = $row[0];

  # Push the network into an array
  array_push($allagencies, $agency);
 }


$cmd = "SELECT MAX(date(last_date)) 
FROM isc.last_date 
WHERE reint_date IS NOT NULL";
#Execute the query
$result = pg_query($dbh, $cmd);
// fetch_row is supposed to be faster than fetch_array
$row = pg_fetch_row($result);
$lastdate  = $row[0];

//error_log($lastdate);
date_default_timezone_set('UTC');
$date = new DateTime('2000-01-01');
$interval = new DateInterval('P1M');
$date->add($interval);

$endyr = date("Y",strtotime($lastdate));
$endm = date("m",strtotime($lastdate));
$endd = date("d",strtotime($lastdate));
$start = mktime(0,0,0,$endm,$endd-1,$endyr);
$startdate=date("Y/m/d",$start);
$startyr = date("Y",$start);
$startm = date("m",$start);
$startd = date("d",$start);


pg_close($dbh);

?>
<!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>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <title>Collected readings search</title>
    <link rel="icon" href="/images/favicon.ico" type="image/ico" />
    <link rel="stylesheet" type="text/css" href="/css/iscschema.css">
    <script type="text/javascript" src="/js/iscsearchfuncs.js"></script>

  </head>
   <body>
  <div id="container">
   <?php include("$home/html/include/header.php"); ?>

   <div id="maingeneral">
	 <div id="content">
  <?php include("leftmenu.php"); ?>
    <div id="rightinner">
  <h3>Collected readings</h3>
  <p>Search stations for readings collected and stored in the ISC database. This includes readings not associated to events (hence not within the ISC Bulletin). N.B. searching arrival readings by time may be slow.</p>

<p>The search is limited to a maximum of 20 stations and a time period of up to 1 year.</p> 

  <p>To search for arrivals associated to events within the ISC Bulletin, please use the <a href=/iscbulletin/search/arrivals/>ISC Bulletin arrivals search</a>.</p>

  
 <br/> <br/>	
  </div>
   <form name="standardsearch" method="get" target="_blank" action="/cgi-bin/web-db-v4">
  <div id=options>

    <br/>
    
    <?php include("$search/include/rawarrivaloutputformat.php");?>
    

    <br/><br/>
     
<?php include("$search/include/stnregionselect.php");?>

<?php include("$search/include/stnreadtimeselect.php");?>

<?php include("$search/include/rawarrivaloutputoptions.php");?>

</div>

<br/><br/>
<input type="hidden" name="request" value="ARRIVALS"/> 
<center>
<input type="submit" class="button white medium num" value="Search arrivals"/>
</center>

  </form>		     
  </div>

  </div>


   <?php 
//close the container div 
?>
<?php include("$home/html/include/footer.php"); ?>
</div>
</body>
</html>
