<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php require_once "D:/Inetpub/piedmontparent/cmsAdmin/lib/viewer_functions.php"; ?>

 <?php list($mag_locaterRecords, $mag_locaterMetaData) = getRecords(array(
    'tableName'   => 'mag_locater',
	  ));
?>

<?php if (!@$GLOBALS['GEOCODER_PLUGIN']) { die("You must activate the Geocoder plugin before you can access this page."); } ?>
<?php

  $kmOrMiles = 'miles'; // can be 'miles' or 'km'

  // set default values
  if (!array_key_exists('fromAddress', $_REQUEST)) { $_REQUEST['fromAddress'] = '10001'; }      // this is the default address/postalcode if none entered
  if (!@$_REQUEST['maxDistance'])                  { $_REQUEST['maxDistance'] = '50000'; } // this will include all results

  // get coordinates
  list($myLat, $myLng) = geocodeAddress( @$_REQUEST['fromAddress'] );

  // error checking
  $errorsAndAlerts = '';
  if (@$_REQUEST['search']) {
    if     (!@$_REQUEST['fromAddress']) { $errorsAndAlerts .= "No address entered!<br/>\n"; }
    elseif (!$myLat || !$myLng)         { $errorsAndAlerts .= "We couldn't determine your location, please try again!<br/>\n"; }
  }

  // get records
  $addressRecords = array();
  if ($myLat && $myLng) {
    $maxDist = floatval(@$_REQUEST['maxDistance']);
   	list($mag_locaterRecords, $mag_locaterMetaData) = getRecords(array(
      'tableName'     => $GLOBALS['mag_locater'],
      'addSelectExpr' => geocoder_getSelectExprForDistance($myLat, $myLng, '_distance', $kmOrMiles), // adds '_distance' field to output records
      'where'         => geocoder_getWhereForDistanceWithin($myLat, $myLng, $maxDist, $kmOrMiles),   // optimization: remove results outside of minimum bounding rectangle
      'having'        => "_distance <= " . $maxDist, // only show results within max distance
      'orderBy'       => 'ISNULL(_distance), _distance', // sort nearest records first -and- unknown or undefined distances last
    ));
  }

?>
<!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" />
<title>Proximity search with results sorted by distance</title>
<style type="text/css">
  body, td { font-family: arial; font-size: 14px; }
</style>
</head>
<body>

  <h1>Proximity search with results sorted by distance</h1>

  <?php if (@$errorsAndAlerts): ?>
    <div style="color: #C00; font-weight: bold; font-size: 12px; font-family: arial;">
      <?php echo $errorsAndAlerts; ?><br/>
    </div>
  <?php endif ?>

  Enter your address or postal code to see nearby results:<br/>

  <form method="post" action="?">
    <input type="hidden" name="search" value="1" />
    <input type="text" name="fromAddress" value="<?php echo htmlspecialchars(@$_REQUEST['fromAddress']); ?>" size="30" />

    <select name="maxDistance">
      <option value="">at any distance</option>
      <option value="100" <?php selectedIf(100, @$_REQUEST['maxDistance']) ?> >within 100 miles</option>
      <option value="25"  <?php selectedIf( 25, @$_REQUEST['maxDistance']) ?> >within 25 miles</option>
      <option value="10"  <?php selectedIf( 10, @$_REQUEST['maxDistance']) ?> >within 10 miles</option>
      <option value="5"   <?php selectedIf( 5,  @$_REQUEST['maxDistance']) ?> >within 5 miles</option>
    </select>

    <input type="submit" value="Search" />
  </form>

  <?php if (@$_REQUEST['search'] && !$errorsAndAlerts): ?>
    <ul style="line-height: 1.5">
    
     <?php foreach ($mag_locaterRecords as $record): ?>
      <li>
        <a href="<?php echo $record['_link']; ?>">
          <?php echo htmlspecialchars($record['address']); ?>,
          <?php echo htmlspecialchars($record['city']); ?>,
          <?php echo htmlspecialchars($record['state']); ?>
          <?php echo htmlspecialchars($record['zipcode']); ?>,
         
        </a>
        (<?php echo floor($record['_distance'] * 10) / 10; ?> <?php echo $kmOrMiles ?> away)
      </li>
    <?php endforeach ?>

    <?php if (!$mag_locaterRecords): ?>
      <li>No results found!</li>
    <?php endif ?>

    </ul>
  <?php endif ?>

</body>
</html>
