<?
    require("util.php3");
    DoTitle("Browse Auctions by Distance");
?>

<form action="select-dist.php3" method=post>
<center>
<table border=1 width="75%" cellpadding=5>
<tr><th colspan=2 bgcolor="#ff6666">Locate Auctions by Distance</th></tr>
<tr><th>What is your ZipCode?</th>
    <td align=center>
        <input type=text name=ZipCode size=5 maxlength=5 value="<?echo($ZipCode);?>">
</td></tr>
<tr><th>How far are you willing to drive?</th>
    <td align=center>
        <input type=text name=Dist size=5 value="<?echo($Dist);?>">
<?
    if ($Units == "Km") {
        echo("<p><input type=radio name=Units value=\"Miles\">Miles\n");
        echo("<input type=radio name=Units checked value=\"Km\">Km\n");
    } else {
        echo("<p><input type=radio name=Units checked value=\"Miles\">Miles\n");
        echo("<input type=radio name=Units value=\"Km\">Km\n");
    }
?>
</td></tr>
<tr><td align=center colspan=2>
    <input type=submit value="Find Auctions">
    <input type=reset value="Reset Form">
</td></tr>
<tr><td colspan=2>
    <table cellpadding=5>
    <tr><td>
        Note, the distance are calculated "as the crow flies", or
        a direct line distance.  Since roads rarely take the most
        direct route, the real distances are usually a bit longer
        than they appear here.
    </td></tr>
    </table>
</td></tr>    
</table>
</form>

<?
    if ($ZipCode) {
        $sql = new MySQL_class;
        $sql->Create("churchil_demo");

        echo("<p><hr noshade>\n");

        /* Get the lat & long of the zipcode entered by the User */
        $sql->Query("select Lat,Lng from ZipCode where ZipCode = '$ZipCode'");
        if ($sql->rows == 0) {
            /* Zipcode not in database */
            echo("<p><font color=\"#ff0000\"><b>Error: Unable to find $ZipCode\n");
            echo("in ZipCode database.<br>Unable to calculate distances.</b></font>\n");
        } else {
            $sql->Fetch(0);
            $lat1 = $sql->data[Lat];
            $lng1 = $sql->data[Lng];

            /* Ok, the first thing to do here is loop through the Locations
             * table and determine which of the locations are within the distance
             * specified.  Note the use of a massive join.  That probably won't work
             * if the database gets a signifigant number of items in it.
             */
            $min = 99999999;
            $sl1 = Sin($lat1);
            $cl1 = Cos($lat1);
            $join = "select Auctions.ID, Auctions.Name, Auctions.StartTime, Locations.City, Locations.State, ZipCode.Lat, ZipCode.Lng from Locations, Auctions, ZipCode where Auctions.Location = Locations.ID and Locations.ZipCode = ZipCode.ZipCode";
            $sql->Query($join);
            for ($i = 0; $i < $sql->rows; $i++) {
                $sql->Fetch($i);
                $id    = $sql->data[0];
                $name  = $sql->data[1];
                $stime = $sql->data[2];
                $city  = $sql->data[3];
                $state = $sql->data[4];
                $lat2  = $sql->data[5];
                $lng2  = $sql->data[6];
    
                /* Distance between two points on a sphere in radians */
                $dist = Acos($sl1 * Sin($lat2) + $cl1 * Cos($lat2) * Cos($lng2 - $lng1));
    
                /* Convert to Miles or Km */
                if ($Units == "Miles") {
                    $dist = (int)(3963.0 * $dist);
                } else {
                    $dist = (int)(6377.83 * $dist);
                }
    
                if ($dist < $min) {
                    $min = $dist;
                }
    
                if ($dist <= $Dist) {
                    /* Ok, we found one, add it to the list.  Putting everything
                     * into a list like this allows us to do a sort on the data
                     * after we've finished looping through it all.
                     */
                    $Found[$id] = $dist;
                    $CitySt[$id] = "$city, $state";
                    $StartTime[$id] = $stime;
                    $Name[$id] = $name;
                }
            }

            /* Now that we've got the list of all the auctions within the specified
             * distance, sort them by distance, closest to furtest, and print'em
             * out.
             */
            if ($Found) {
                echo("<p><table border=1 cellpadding=5>\n");
                echo("<tr><th colspan=4 bgcolor=\"#ff6666\">Auctions within $Dist $Units</th></tr>\n");
                echo("<tr><th>Name</th><th>Start Time</th><th>Location</th><th>Distance ($Units)</th></tr>\n");
                asort($Found);
                reset($Found);
                while ($tmp = each($Found)) {
                    $id = $tmp[key];
                    echo("<tr><td><a href=\"show-auction.php3?ID=$id\">$Name[$id]</a></td>\n");
                    echo("    <td align=center>$StartTime[$id]</td>\n");
                    echo("    <td align=center>$CitySt[$id]</td>\n");
                    echo("    <td align=center>$Found[$id]</td></tr>\n");
                }
                echo("</table>\n");
            } else {
                /* If there weren't any auctions listed within the distance specified,
                 * tell the user how far the closest auction is to him/her.
                 */
                echo("<center><p>Sorry, No auctions found within $Dist $Units. \n");
                echo("The closest auction found is $min $Units from you.\n");
            }
        }
    }
    include("footer.php3");
?>
