<?php
/*
#****************************************************************************#
# Ultimate Locator                                                           #
# Professional Zip Code Locator Systems          support@ultimatelocator.com #
# Copyright 2004 PageSkills.com Internet Services    www.ultimatelocator.com #
# Release Date: 2/01/2004                                                    #
#                                                                            #
#   Ultimate Locator is protected by the copyright                           #
#   laws and international copyright treaties, as well as other              #
#   intellectual property laws and treaties.                                 #
#   Distribution without approved license is illegal and is                  #
#   punishable by law.                                                       #
#                                                                            #
#   A license is good toward one domain only.  You must purchase             #
#   additional licenses if you wish to install this software on              #
#   additional domains (websites). We do offer Reseller Packages.            #
#****************************************************************************#
*/

require_once("admin/config.php");

$fp5=fopen("$user_path/results_list.htm","r");
$str5=fread($fp5,filesize("$user_path/results_list.htm"));

$queryh="Select hide as h1 from setfield";
$datah=mysql_query($queryh,$zip_connection);


// Limit Searches by IP -------------------------------------------------------------------------------
$ext_chksearch = mysql_query("select * from limitsearches") or die(mysql_error());
$ext_chk=mysql_fetch_assoc($ext_chksearch) or die(mysql_error());
if($ext_chk["onoff"]=="on")
{
        $ip=$_SERVER['REMOTE_ADDR'];
        $lastaccess= time()+date("Z");
        $ext_hitsrec=mysql_query("select * from hitcounter where ip='$ip'") or die(mysql_error());
        $ext_totrec= @mysql_num_rows($ext_hitsrec);
        if($ext_totrec > 0)
        {
                $ext_hits=mysql_fetch_assoc($ext_hitsrec) or die(mysql_error());
                $hitstemp = $ext_hits["hits"];
                $hits=$hitstemp+1;
                if($ext_hits["hits"]>=$ext_chk["maxsearch"])
                {
                        $lastacc=$ext_hits["lastaccess"];
                        $today = time()+date("Z");
                        $timediff = ($today-$lastacc)/60;
                        if ($timediff <= $ext_chk["minutes"])
                        {

                                $fp_ext=fopen("$user_path/search_error.htm","r");
                                $str_ext=fread($fp_ext,filesize("$user_path/search_error.htm"));
                                fclose($fp_ext);
                                $msg = stripslashes($ext_chk["msg"]);
                                $str=str_replace("{msg}",$msg,$str_ext);
                                $str=str_replace("{headtext}",$headtext,$str);
                                $str=str_replace("{images}",$image_path,$str);
                                $str=str_replace("{site}",$site,$str);
                                echo $str;
                                exit;
                        }
                        else
                        {
                                $hits=1;
                        }

                }
                mysql_query("update hitcounter set lastaccess='$lastaccess', hits='$hits' where ip='$ip'")  or die(mysql_error());
        }
        else
        {
                mysql_query("insert into hitcounter (ip,lastaccess,hits) values ('$ip','$lastaccess','1')") or die(mysql_error());
        }
} // End Limit Searches by IP ----------------------------------------------------------------------


// Check for user input of radius.  If blank use admin default
if (isset($radius) && $radius != "")
{
$searchlimit = $radius;
}
else
{
$searchlimit = $searchlimit;
}

// Build Next and Previous Page Links Image Tags
$previmage="<img alt=\"Previous Page\" src=\"$image_path/btn_previous.gif\" border=\"0\" align=\"absmiddle\">";
$nextimage="<img alt=\"Next Page\" src=\"$image_path/btn_next.gif\" border=\"0\" align=\"absmiddle\">";
$nextgroupimage="<img alt=\"Next $max_page Pages\" src=\"$image_path/btn_next_group.gif\" border=\"0\" align=\"absmiddle\">";
$previousgroupimage="<img alt=\"Previous $max_page Pages\" src=\"$image_path/btn_previous_group.gif\" border=\"0\" align=\"absmiddle\">";

if(!isset($zipsearch))
{
$zipsearch="";
}

if(!isset($order))
{
$order = "id";
}

if(isset($city))
{
$citystring = str_replace(' ', '+', $city );
}

if(isset($name))
{
$namestring = str_replace(' ', '+', $name );
}

if(isset($statename))
{
$statenamestring = str_replace(' ', '+', $statename );
}

if(isset($countryname))
{
$countrynamestring = str_replace(' ', '+', $countryname );
}

if(isset($zip))  // Added to support Canadian postal codes with spaces in page links
{
$formattedzip = str_replace(' ', '%20', $zip );
}


$str5=str_replace("{statesort}",$state,$str5);
$str5=str_replace("{statenamesort}",$statename,$str5);
$str5=str_replace("{countrysort}",$country,$str5);
$str5=str_replace("{countrynamesort}",$countryname,$str5);
$str5=str_replace("{citysort}",$city,$str5);
$str5=str_replace("{namesort}",$name,$str5);
$str5=str_replace("{radiussort}",$searchlimit,$str5);
$str5=str_replace("{zipsort}",$zip,$str5);
$str5=str_replace("{zipsearchsort}",$zipsearch,$str5);
$str5=str_replace("{user1sort}",$user1,$str5);
$str5=str_replace("{user2sort}",$user2,$str5);
$str5=str_replace("{user3sort}",$user3,$str5);
$str5=str_replace("{user4sort}",$user4,$str5);
$str5=str_replace("{user5sort}",$user5,$str5);

if(isset($showsurrounding))
{
        $str5=str_replace("{surroundingsort}",$showsurrounding,$str5);
        $strdata="zipsearch=$zipsearch&zip=$formattedzip&showsurrounding=$showsurrounding&state=$state&statename=$statenamestring&city=$citystring&name=$namestring&country=$country&countryname=$countrynamestring&order=$order&radius=$searchlimit&user1=$user1&user2=$user2&user3=$user3&user4=$user4&user5=$user5";
}
else
{
        $showsurrounding="";
        $str5=str_replace("{surroundingsort}",$showsurrounding,$str5);
        $strdata="zipsearch=$zipsearch&zip=$formattedzip&showsurrounding=$showsurrounding&state=$state&statename=$statenamestring&city=$citystring&name=$namestring&country=$country&countryname=$countrynamestring&order=$order&radius=$searchlimit&user1=$user1&user2=$user2&user3=$user3&user4=$user4&user5=$user5";
}

$ts=explode("{##}",$str5);

$fp=fopen("$user_path/results_list_format.htm","r");
$str=fread($fp,filesize("$user_path/results_list_format.htm"));

$st=explode("{###}",$str);
$st[3]="";
$st[4]=$st[1];

//========added evolve============21st-August-2004==START
$sth=explode("{####}",$st[0]);
//========added evolve============21st-August-2004==END

//==========================================================================================
//==========================================================================================
// Zip Code Search NO surroundings (exact matches only)
//==========================================================================================
//==========================================================================================

if(isset($zip) && $zip!= "")
{
if(isset($showsurrounding) && $showsurrounding =="")
{
$query="select * from zipcode where zipcode='$zip'";
$data=mysql_query($query,$zip_connection);
$total=mysql_num_rows($data);
        if($total > 0)
        {
$rlongitude=mysql_result($data,0,"longitude");
$rlatitude=mysql_result($data,0,"latitude");
$resultmiles=distance($rlatitude, $rlongitude, $rlatitude, $rlongitude);
$resultmiles=round($resultmiles);
        }

//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "select * from location where zip='$zip' ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,$order";
}
else
{
$query="select * from location where zip='$zip' and hide=0 and aprove=0 order by pref asc,$order";
}



$alldata = mysql_query($query,$zip_connection);
$total_rows=mysql_num_rows($alldata);


        if($total_rows>0)
        {
                if($total_rows > $max_data)
                {
                $total_rows=$max_data;
                }
                $numofpages =ceil($total_rows/$front_max_records);

                        if(!isset($pageno))
                        {
                        $start_rec = 0;
                        }
                        if(isset($pageno))
                        {
                        $start_rec=($pageno-1)*$front_max_records;
                                        if($start_rec+$front_max_records > $total_rows)
                                        {
                                        $front_max_records=$total_rows-$start_rec;
                                        }
                        }


$query.=" limit $start_rec,$front_max_records";
$data=mysql_query($query,$zip_connection);
$recordset=mysql_fetch_assoc($data);
$total_rec=mysql_num_rows($data);

$pageslist="";

if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
        if($pageno==1)
        {
        $pageslist.=" ";
        $pageno-=1;
        }
        else
        {
        $pageno--;
$pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$previmage</a>&nbsp;&nbsp;";
        }
}

if(!isset($pageno))
{
$pageno=0;
}
$pageno+=1;

        for($i=1;$i<=$numofpages;$i++)
        {
        if(!isset($pageno) && $i==1)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        elseif(isset($pageno) && $pageno == $i)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        else
        {
        $pageslist.="<a class=\"pageLink\" href=results_list.php?pageno=$i&$strdata>$i</a>&nbsp;&nbsp;";
        }
        }

if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
$pageno++;
        if($pageno-1==$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {
$pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$nextimage</a>&nbsp;&nbsp;";
        }
}

        if(!isset($resultmiles))
        {
        $resultmiles=0;
        }

$x=$start_rec+1;
do{
$st[1]=str_replace("{displaynumber}",$x,$st[4]);
$st[1]=str_replace("{id}",$recordset['id'],$st[1]);

// Now supports {maptext} in the results_list.php - Added by Brian J. on July 30, 2004
if ($recordset['zip'] != "-") {
         if($map_link_text!="")
         {

           if ($show_map_link == 1) {
             if ($map_target==1) {$target="_blank";}
             if ($map_target==0){$target="";}

        $streetY = $recordset['address1'];
        $cityY = $recordset['city'];
        $stateY = $recordset['state'];
        $zipY = $recordset['zip'];
        $countryY = $recordset['country'];
        $streetMQ = str_replace(" ","+",$recordset['address1']);
        $cityMQ = str_replace(" ","+",$recordset['city']);
        $stateMQ = str_replace(" ","+",$recordset['state']);
        $zipMQ = str_replace(" ","+",$recordset['zip']);
        $countryMQ = str_replace(" ","+",$recordset['country']);

        if ($map_service == "mapquest")
            {
              if ($recordset['country'] == "US")
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$mapquest_link?city=$cityMQ&state=$stateMQ&address=$streetMQ&zip=$zipMQ&zoom=5\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        elseif ($map_service == "yahoo")
            {
              if (($recordset['country'] == "US") || ($recordset['country'] == "CA"))
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$yahoo_link?addr=$streetMQ&csz=$cityY+$stateY+$zipY&country=$countryY\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
        }
        }
}
else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
// End map links

        if(mysql_result($datah,0,"h1")==0)
        {
        $st[1]=str_replace("{name}",$recordset['name'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{name}","",$st[1]);
        }
        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address1}",$recordset['address1'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address1}","",$st[1]);
        }
        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address2}",$recordset['address2'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address2}","",$st[1]);
        }
        if(mysql_result($datah,2,"h1")==0)
        {
        $st[1]=str_replace("{city}",$recordset['city'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{city}","",$st[1]);
        }
        if(mysql_result($datah,3,"h1")==0)
        {
        $st[1]=str_replace("{state}",$recordset['state'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{state}","",$st[1]);
        }
        if(mysql_result($datah,4,"h1")==0)
        {
        $st[1]=str_replace("{country}",$recordset['country'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{country}","",$st[1]);
        }

        if(mysql_result($datah,5,"h1")==0)
        {
        $st[1]=str_replace("{zip}",$recordset['zip'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{zip}","",$st[1]);
        }

        if(mysql_result($datah,6,"h1")==0) // THE FOLLOWING WAS ADDED BY BRIAN TO ALLOW ALL FIELDS
        {
        $st[1]=str_replace("{telephone}",$recordset['telephone'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{telephone}","",$st[1]);
        }

        if(mysql_result($datah,7,"h1")==0)
        {
        $st[1]=str_replace("{fax}",$recordset['fax'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{fax}","",$st[1]);
        }

        if(mysql_result($datah,8,"h1")==0)
        {
        $st[1]=str_replace("{email}",$recordset['email'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{email}","",$st[1]);
        }

        if(mysql_result($datah,9,"h1")==0)
        {
        $st[1]=str_replace("{website}",$recordset['website'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{website}","",$st[1]);
        }

        if(mysql_result($datah,10,"h1")==0)
        {
        $st[1]=str_replace("{user1}",$recordset['user1'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user1}","",$st[1]);
        }

        if(mysql_result($datah,11,"h1")==0)
        {
        $st[1]=str_replace("{user2}",$recordset['user2'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user2}","",$st[1]);
        }

        if(mysql_result($datah,12,"h1")==0)
        {
        $st[1]=str_replace("{user3}",$recordset['user3'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user3}","",$st[1]);
        }

        if(mysql_result($datah,13,"h1")==0)
        {
        $st[1]=str_replace("{user4}",$recordset['user4'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user4}","",$st[1]);
        }

        if(mysql_result($datah,14,"h1")==0)
        {
        $st[1]=str_replace("{user5}",$recordset['user5'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user5}","",$st[1]);
        }


$st[1]=str_replace("{distance}",$resultmiles." miles",$st[1]);

$x++;
$st[3]=$st[3].$st[1];
}while($recordset=mysql_fetch_assoc($data));

//========added evolve============21st-August-2004==START
//$str=$st[0].$st[3].$st[2];
$str=$sth[0].$sth[1].$sth[2].$st[3].$st[2];
//========added evolve============21st-August-2004==End
$str=str_replace("{querytype}","Searching for $headtext in $zip",$str);
$str=str_replace("{matches}","Displaying $total_rows matches",$str);

        }
        else        {
        $pageslist="";
        $st[1]=str_replace("{displaynumber}","",$st[1]);
        $st[1]=str_replace("{id}","",$st[1]);
        $st[1]=str_replace("{name}","",$st[1]);
        $st[1]=str_replace("{address1}","",$st[1]);
        $st[1]=str_replace("{address2}","",$st[1]);
        $st[1]=str_replace("{city}","",$st[1]);
        $st[1]=str_replace("{state}","",$st[1]);
        $st[1]=str_replace("{country}","",$st[1]);
        $st[1]=str_replace("{zip}","",$st[1]);
        $st[1]=str_replace("{telephone}","",$st[1]);
        $st[1]=str_replace("{fax}","",$st[1]);
        $st[1]=str_replace("{email}","",$st[1]);
        $st[1]=str_replace("{website}","",$st[1]);
        $st[1]=str_replace("{user1}","",$st[1]);
        $st[1]=str_replace("{user2}","",$st[1]);
        $st[1]=str_replace("{user3}","",$st[1]);
        $st[1]=str_replace("{user4}","",$st[1]);
        $st[1]=str_replace("{user5}","",$st[1]);
        $st[1]=str_replace("{distance}","",$st[1]);
        $st[1]=str_replace("{maptext}","",$st[1]);

//========added evolve============21st-August-2004==START

//        $str=$st[0].$st[1].$st[2];
                $sth[1]="";
                $st[0]=$sth[0].$sth[1].$sth[2];
        $str=$st[0].$st[1].$st[2];

//========added evolve============21st-August-2004==END
        $fpx=fopen("nomatch.txt","r");
        $stx=fread($fpx,filesize("nomatch.txt"));
        $str=str_replace("{matches}",$stx,$str);
        $str=str_replace("{admin_email}",$organizationemail,$str);
        fclose($fpx);
        }
}
}


//==========================================================================================
//==========================================================================================
// Zip Code Search with surroundings
//==========================================================================================
//==========================================================================================

//==========================================================================================
// First Search Process --------------------------------------------------------------------
//==========================================================================================
$rmile=array();
if(isset($zip) && isset($showsurrounding) && $zip !="" && $showsurrounding!="")
{

$query="select * from zipcode where zipcode='$zip'";
$data=mysql_query($query,$zip_connection);
$tot=mysql_num_rows($data);

        if($tot > 0)
        {

if($order=="id")
        {
$rlongitude=mysql_result($data,0,"longitude");
$rlatitude=mysql_result($data,0,"latitude");

$query="select latitude,longitude,zipcode from zipcode,location where zipcode=zip and hide=0";    // where zipcode != '$zip'";
$data=mysql_query($query,$zip_connection);
$total_rows=mysql_num_rows($data);

        for($i=0; $i<$total_rows; $i++)
        {
        $reslatitude=mysql_result($data,$i,"latitude");
        $reslongitude=mysql_result($data,$i,"longitude");
        $resultmiles=distance($rlatitude, $rlongitude, $reslatitude, $reslongitude);

        if($resultmiles <= $searchlimit)
        {
        $zp=mysql_result($data,$i,"zipcode");
        //$final[$k]=mysql_result($data,$i,"zipcode");
        $rmile["$zp"]= $resultmiles;
        }
        }


asort($rmile);

$s=implode("','",array_keys($rmile));
$s="('$s')";
//echo $s;

//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "Select * from location where zip in $s ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,'$order'";
}
else
{
$query="Select * from location where zip in $s and hide=0 and aprove=0 order by pref asc,'$order'";
}



$alldata = mysql_query($query,$zip_connection);
$recordset=mysql_fetch_assoc($alldata);
$total_rows=mysql_num_rows($alldata);

//========added evolve============21st-August-2004==START
if($total_rows>0)
{
//========added evolve============21st-August-2004==END
        if($total_rows > $max_data)
        {
        $total_rows=$max_data;
        }

$m=0;

        do{
        $ziptemp=$recordset['zip'];
        $finalresult[$m]=array($recordset['pref'],$rmile["$ziptemp"],$recordset['name'],$recordset['address1'],$recordset['address2'],$recordset['city'],$recordset['state'],$recordset['country'],$recordset['zip'],$recordset['id'],$recordset['telephone'],$recordset['fax'],$recordset['email'],$recordset['website'],$recordset['user1'],$recordset['user2'],$recordset['user3'],$recordset['user4'],$recordset['user5']);
        $m++;
        }while($recordset=mysql_fetch_assoc($alldata));
        asort($finalresult);

        $i=0;
        while (list($key, $val) = each($finalresult)){
        $actres[$i] = $val;
           $i++;
        }

if(!isset($pageno))
{
$start_rec = 0;
}

if(isset($pageno))
{
$start_rec=($pageno-1)*$front_max_records;
}

$cnt1=count($actres);
        if($cnt1 > $max_data)
        {
        $cnt1=$max_data;
        }
$numofpages =ceil($cnt1/$front_max_records);
$pageslist="";

if($numofpages>$max_page)
{
                if(!isset($pageno))
                {
                $pageno=1;
                }

                if($pageno<=$max_page)
                {
                $pageslist.="";
                }
                else
                {
                                if(($pageno%$max_page)==0)
                                {
                                $start1=(int)($pageno/$max_page)-1;
                                }
                                else
                                {
                                $start1=(int)($pageno/$max_page);
                                }
                                $pagep=($start1 * $max_page)+1;
//                                $pages=($start1*$max_page)+10;


                $pagep-=$max_page;
                $pageslist.="<a href=results_list.php?pageno=$pagep&$strdata>$previousgroupimage</a>&nbsp;&nbsp;";
                }
}



if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
        if($pageno==1)
        {
        $pageslist.=" ";
        $pageno-=1;
        }
        else
        {
        $pageno--;
        $pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$previmage</a>&nbsp;&nbsp;";
        }
}

if(!isset($pageno))
{
$pageno=0;
}
$pageno+=1;

$start1=(int)($pageno/$max_page);
$start2=$pageno%$max_page;
if($start2==0)
{
$startpage=(($start1-1)*$max_page)+1;
$endpage=(($start1-1)*$max_page)+$max_page;
}
else
{
$startpage=($start1*$max_page)+1;
$endpage=($start1*$max_page)+$max_page;
}
if($endpage>$numofpages)
{
$endpage=$numofpages;
}

for($i=$startpage;$i<=$endpage;$i++)
{
        if(!isset($pageno) && $i==1)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        elseif(isset($pageno) && $pageno == $i)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        else
        {
        $pageslist.="<a class=\"pageLink\" href=results_list.php?pageno=$i&$strdata>$i</a>&nbsp;&nbsp;";
        }
}



if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
        $pageno++;
        if($pageno-1==$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {
$pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$nextimage</a>&nbsp;&nbsp;";
        }
}

if($numofpages > $max_page)
{
    $page=$pageno-1;
//        echo $page;
        if(($page%$max_page)==0)
        {
        $start1=(int)($page/$max_page)-1;
        }
        else
        {
        $start1=(int)($page/$max_page);
        }
        $pagen=($start1 * $max_page)+1;
        $pages=($start1*$max_page)+$max_page;

        if($pages>$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {
//echo $pageno;
$pagen+=$max_page;
$pageslist.="<a href=results_list.php?pageno=$pagen&$strdata>$nextgroupimage</a>&nbsp;&nbsp;";
        }
}

$res=array();
$fin="";
        $cnt=count($actres);
        if($cnt > $max_data)
        {
        $cnt=$max_data;
        }
        if($cnt>$front_max_records)
        {
                $end_rec=$start_rec+$front_max_records;
                if($end_rec>$cnt)
                {
                $end_rec=$cnt;
                }
        }
        else
        {
        $end_rec=$cnt;
        }

for($i=$start_rec;$i<$end_rec;$i++)
{
$res=$actres[$i];

        $st[1]=str_replace("{id}",$res[9],$st[4]);

// Now supports {maptext} in the results_list.php - Added by Brian J. on July 30, 2004
if ($res[8] != "-") {
         if($map_link_text!="")
         {

           if ($show_map_link == 1) {
             if ($map_target==1) {$target="_blank";}
             if ($map_target==0){$target="";}

        $streetY = $res[3];
        $cityY = $res[5];
        $stateY = $res[6];
        $zipY = $res[8];
        $countryY = $res[7];
        $streetMQ = str_replace(" ","+",$res[3]);
        $cityMQ = str_replace(" ","+",$res[5]);
        $stateMQ = str_replace(" ","+",$res[6]);
        $zipMQ = str_replace(" ","+",$res[8]);
        $countryMQ = str_replace(" ","+",$res[7]);

        if ($map_service == "mapquest")
            {
              if ($res[7] == "US")
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$mapquest_link?city=$cityMQ&state=$stateMQ&address=$streetMQ&zip=$zipMQ&zoom=5\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        elseif ($map_service == "yahoo")
            {
              if (($res[7] == "US") || ($res[7] == "CA"))
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$yahoo_link?addr=$streetMQ&csz=$cityY+$stateY+$zipY&country=$countryY\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
        }
        }
}
else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
// End map links

        if(mysql_result($datah,0,"h1")==0)
        {
        $st[1]=str_replace("{name}",$res[2],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{name}","",$st[1]);
        }

        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address1}",$res[3],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address1}","",$st[1]);
        }

        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address2}",$res[4],$st[1]);
        }
        else
        {

        $st[1]=str_replace("{address2}","",$st[1]);
        }
        if(mysql_result($datah,2,"h1")==0)
        {
        $st[1]=str_replace("{city}",$res[5],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{city}","",$st[1]);
        }

        if(mysql_result($datah,3,"h1")==0)
        {
        $st[1]=str_replace("{state}",$res[6],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{state}","",$st[1]);
        }

        if(mysql_result($datah,4,"h1")==0)
        {
        $st[1]=str_replace("{country}",$res[7],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{country}","",$st[1]);
        }


        if(mysql_result($datah,5,"h1")==0)
        {
        $st[1]=str_replace("{zip}",$res[8],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{zip}","",$st[1]);
        }

        if(mysql_result($datah,6,"h1")==0)
        {
        $st[1]=str_replace("{telephone}",$res[10],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{telephone}","",$st[1]);
        }

        if(mysql_result($datah,7,"h1")==0)
        {
        $st[1]=str_replace("{fax}",$res[11],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{fax}","",$st[1]);
        }

        if(mysql_result($datah,8,"h1")==0)
        {
        $st[1]=str_replace("{email}",$res[12],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{email}","",$st[1]);
        }

        if(mysql_result($datah,9,"h1")==0)
        {
        $st[1]=str_replace("{website}",$res[13],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{website}","",$st[1]);
        }

        if(mysql_result($datah,10,"h1")==0)
        {
        $st[1]=str_replace("{user1}",$res[14],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user1}","",$st[1]);
        }

        if(mysql_result($datah,11,"h1")==0)
        {
        $st[1]=str_replace("{user2}",$res[15],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user2}","",$st[1]);
        }

        if(mysql_result($datah,12,"h1")==0)
        {
        $st[1]=str_replace("{user3}",$res[16],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user3}","",$st[1]);
        }

        if(mysql_result($datah,13,"h1")==0)
        {
        $st[1]=str_replace("{user4}",$res[17],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user4}","",$st[1]);
        }

        if(mysql_result($datah,14,"h1")==0)
        {
        $st[1]=str_replace("{user5}",$res[18],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user5}","",$st[1]);
        }

        $st[1]=str_replace("{distance}",round($res[1])." miles",$st[1]);
        $st[1]=str_replace("{displaynumber}",$i+1,$st[1]);

        $fin.=$st[1];
}


//========added evolve============21st-August-2004==START
//$str=$st[0].$fin.$st[2];
$str=$sth[0].$sth[1].$sth[2].$fin.$st[2];
//========added evolve============21st-August-2004==END



$str=str_replace("{querytype}","Searching for $headtext within $searchlimit miles of $zip",$str);
$str=str_replace("{matches}","Displaying $total_rows matches",$str);

}
//========added evolve============21st-August-2004==START
        else        {
        $pageslist="";
        $st[1]=str_replace("{displaynumber}","",$st[1]);
        $st[1]=str_replace("{id}","",$st[1]);
        $st[1]=str_replace("{name}","",$st[1]);
        $st[1]=str_replace("{address1}","",$st[1]);
        $st[1]=str_replace("{address2}","",$st[1]);
        $st[1]=str_replace("{city}","",$st[1]);
        $st[1]=str_replace("{state}","",$st[1]);
        $st[1]=str_replace("{country}","",$st[1]);
        $st[1]=str_replace("{zip}","",$st[1]);
        $st[1]=str_replace("{telephone}","",$st[1]);
        $st[1]=str_replace("{fax}","",$st[1]);
        $st[1]=str_replace("{email}","",$st[1]);
        $st[1]=str_replace("{website}","",$st[1]);
        $st[1]=str_replace("{user1}","",$st[1]);
        $st[1]=str_replace("{user2}","",$st[1]);
        $st[1]=str_replace("{user3}","",$st[1]);
        $st[1]=str_replace("{user4}","",$st[1]);
        $st[1]=str_replace("{user5}","",$st[1]);
        $st[1]=str_replace("{distance}","",$st[1]);
        $st[1]=str_replace("{maptext}","",$st[1]);


//========added evolve============21st-August-2004==START

//        $str=$st[0].$st[1].$st[2];
                $sth[1]="";
                $st[0]=$sth[0].$sth[1].$sth[2];
        $str=$st[0].$st[1].$st[2];

//========added evolve============21st-August-2004==END


        $fpx=fopen("nomatch.txt","r");
        $stx=fread($fpx,filesize("nomatch.txt"));
        $str=str_replace("{matches}",$stx,$str);
        $str=str_replace("{admin_email}",$organizationemail,$str);
        fclose($fpx);
        }

}
//========added evolve============21st-August-2004==END

else
        {

//==========================================================================================
// Second Search Process from the Sort By selection ----------------------------------------
//==========================================================================================

$rlongitude=mysql_result($data,0,"longitude");
$rlatitude=mysql_result($data,0,"latitude");

$query="select latitude,longitude,zipcode from zipcode,location where zipcode=zip";    // where zipcode != '$zip'";
$data=mysql_query($query,$zip_connection);
$total_rows=mysql_num_rows($data);

$k=1;
        for($i=0; $i<$total_rows; $i++)
        {
        $reslatitude=mysql_result($data,$i,"latitude");
        $reslongitude=mysql_result($data,$i,"longitude");
        $resultmiles=distance($rlatitude, $rlongitude, $reslatitude, $reslongitude);

        if($resultmiles <= $searchlimit)
        {
        $zp=mysql_result($data,$i,"zipcode");
        $rmile["$zp"]= $resultmiles;
        $final[$k]=mysql_result($data,$i,"zipcode");
        $k=$k+1;
        }

        }

$s=implode("','",$final);
$s="('$s')";

//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "Select * from location where zip in $s ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,'$order'";
}
else
{
$query="Select * from location where zip in $s and hide=0 and aprove=0 order by pref asc,'$order'";
}

$alldata = mysql_query($query,$zip_connection);
$total_rows=mysql_num_rows($alldata);

        if($total_rows > $max_data)
        {
        $total_rows=$max_data;
        }
$numofpages =ceil($total_rows/$front_max_records);

if(!isset($pageno))
{
$start_rec = 0;
}

if(isset($pageno))
{
$start_rec=($pageno-1)*$front_max_records;
        if($start_rec+$front_max_records > $max_data)
        {
        $front_max_records=$max_data-$start_rec;
        }

}



$query.=" limit $start_rec,$front_max_records";
$data=mysql_query($query,$zip_connection);
$recordset=mysql_fetch_assoc($data);
$total_rec=mysql_num_rows($data);


$pageslist="";



if($numofpages>$max_page)
{
                if(!isset($pageno))
                {
                $pageno=1;
                }

                if($pageno<=$max_page)
                {
                $pageslist.="";
                }
                else
                {
                                if(($pageno%$max_page)==0)
                                {
                                $start1=(int)($pageno/$max_page)-1;
                                }
                                else
                                {
                                $start1=(int)($pageno/$max_page);
                                }
                                $pagep=($start1 * $max_page)+1;
//                                $pages=($start1*$max_page)+10;


                $pagep-=$max_page;
                $pageslist.="<a href=results_list.php?pageno=$pagep&$strdata>$previousgroupimage</a>&nbsp;&nbsp;";
                }
}




if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
        if($pageno==1)
        {
        $pageslist.=" ";
        $pageno-=1;
        }
        else
        {
        $pageno--;
        $pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$previmage</a>&nbsp;&nbsp;";
        }
}

if(!isset($pageno))
{
$pageno=0;
}
$pageno+=1;



$start1=(int)($pageno/$max_page);
$start2=$pageno%$max_page;
if($start2==0)
{
$startpage=(($start1-1)*$max_page)+1;
$endpage=(($start1-1)*$max_page)+$max_page;
}
else
{
$startpage=($start1*$max_page)+1;
$endpage=($start1*$max_page)+$max_page;
}
if($endpage>$numofpages)
{
$endpage=$numofpages;
}

for($i=$startpage;$i<=$endpage;$i++)
{
        if(!isset($pageno) && $i==1)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        elseif(isset($pageno) && $pageno == $i)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        else
        {
        $pageslist.="<a class=\"pageLink\" href=results_list.php?pageno=$i&$strdata>$i</a>&nbsp;&nbsp;";
        }
}



if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
$pageno++;
        if($pageno-1==$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {
$pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$nextimage</a>&nbsp;&nbsp;";
        }
}


if($numofpages > $max_page)
{
    $page=$pageno-1;
//        echo $page;
        if(($page%$max_page)==0)
        {
        $start1=(int)($page/$max_page)-1;
        }
        else
        {
        $start1=(int)($page/$max_page);
        }
        $pagen=($start1 * $max_page)+1;
        $pages=($start1*$max_page)+$max_page;

        if($pages>$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {
//echo $pageno;
$pagen+=$max_page;
$pageslist.="<a href=results_list.php?pageno=$pagen&$strdata>$nextgroupimage</a>&nbsp;&nbsp;";
        }
}




$x=$start_rec+1;
        do{
$st[1]=str_replace("{displaynumber}",$x,$st[4]);
$st[1]=str_replace("{id}",$recordset['id'],$st[1]);

// Now supports {maptext} in the results_list.php - Added by Brian J. on July 30, 2004
if ($recordset['zip'] != "-") {
         if($map_link_text!="")
         {

           if ($show_map_link == 1) {
             if ($map_target==1) {$target="_blank";}
             if ($map_target==0){$target="";}

        $streetY = $recordset['address1'];
        $cityY = $recordset['city'];
        $stateY = $recordset['state'];
        $zipY = $recordset['zip'];
        $countryY = $recordset['country'];
        $streetMQ = str_replace(" ","+",$recordset['address1']);
        $cityMQ = str_replace(" ","+",$recordset['city']);
        $stateMQ = str_replace(" ","+",$recordset['state']);
        $zipMQ = str_replace(" ","+",$recordset['zip']);
        $countryMQ = str_replace(" ","+",$recordset['country']);

        if ($map_service == "mapquest")
            {
              if ($recordset['country'] == "US")
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$mapquest_link?city=$cityMQ&state=$stateMQ&address=$streetMQ&zip=$zipMQ&zoom=5\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        elseif ($map_service == "yahoo")
            {
              if (($recordset['country'] == "US") || ($recordset['country'] == "CA"))
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$yahoo_link?addr=$streetMQ&csz=$cityY+$stateY+$zipY&country=$countryY\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
        }
        }
}
else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
// End map links

        if(mysql_result($datah,0,"h1")==0)
        {
        $st[1]=str_replace("{name}",$recordset['name'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{name}","",$st[1]);
        }
        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address1}",$recordset['address1'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address1}","",$st[1]);
        }
        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address2}",$recordset['address2'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address2}","",$st[1]);
        }
        if(mysql_result($datah,2,"h1")==0)
        {
        $st[1]=str_replace("{city}",$recordset['city'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{city}","",$st[1]);
        }
        if(mysql_result($datah,3,"h1")==0)
        {
        $st[1]=str_replace("{state}",$recordset['state'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{state}","",$st[1]);
        }
        if(mysql_result($datah,4,"h1")==0)
        {
        $st[1]=str_replace("{country}",$recordset['country'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{state}","",$st[1]);
        }
        if(mysql_result($datah,5,"h1")==0)
        {
        $st[1]=str_replace("{zip}",$recordset['zip'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{zip}","",$st[1]);
        }

        if(mysql_result($datah,6,"h1")==0) // THE FOLLOWING WAS ADDED BY BRIAN TO ALLOW ALL FIELDS
        {
        $st[1]=str_replace("{telephone}",$recordset['telephone'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{telephone}","",$st[1]);
        }

        if(mysql_result($datah,7,"h1")==0)
        {
        $st[1]=str_replace("{fax}",$recordset['fax'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{fax}","",$st[1]);
        }

        if(mysql_result($datah,8,"h1")==0)
        {
        $st[1]=str_replace("{email}",$recordset['email'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{email}","",$st[1]);
        }

        if(mysql_result($datah,9,"h1")==0)
        {
        $st[1]=str_replace("{website}",$recordset['website'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{website}","",$st[1]);
        }

        if(mysql_result($datah,10,"h1")==0)
        {
        $st[1]=str_replace("{user1}",$recordset['user1'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user1}","",$st[1]);
        }

        if(mysql_result($datah,11,"h1")==0)
        {
        $st[1]=str_replace("{user2}",$recordset['user2'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user2}","",$st[1]);
        }

        if(mysql_result($datah,12,"h1")==0)
        {
        $st[1]=str_replace("{user3}",$recordset['user3'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user3}","",$st[1]);
        }

        if(mysql_result($datah,13,"h1")==0)
        {
        $st[1]=str_replace("{user4}",$recordset['user4'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user4}","",$st[1]);
        }

        if(mysql_result($datah,14,"h1")==0)
        {
        $st[1]=str_replace("{user5}",$recordset['user5'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user5}","",$st[1]);
        }


$zips=$recordset['zip'];
$st[1]=str_replace("{distance}",round($rmile["$zips"])." miles",$st[1]);

$x++;
$st[3]=$st[3].$st[1];
        }while($recordset=mysql_fetch_assoc($data));


//========added evolve============21st-August-2004==START

//$str=$st[0].$st[3].$st[2];
$str=$sth[0].$sth[1].$sth[2].$st[3].$st[2];
//========added evolve============21st-August-2004==END



$str=str_replace("{querytype}","Searching for $headtext within $searchlimit miles of $zip",$str);
$str=str_replace("{matches}","Displaying $total_rows matches",$str);

        }

        }
        else        {
        $pageslist="";
        $st[1]=str_replace("{displaynumber}","",$st[1]);
        $st[1]=str_replace("{id}","",$st[1]);
        $st[1]=str_replace("{name}","",$st[1]);
        $st[1]=str_replace("{address1}","",$st[1]);
        $st[1]=str_replace("{address2}","",$st[1]);
        $st[1]=str_replace("{city}","",$st[1]);
        $st[1]=str_replace("{state}","",$st[1]);
        $st[1]=str_replace("{country}","",$st[1]);
        $st[1]=str_replace("{zip}","",$st[1]);
        $st[1]=str_replace("{telephone}","",$st[1]);
        $st[1]=str_replace("{fax}","",$st[1]);
        $st[1]=str_replace("{email}","",$st[1]);
        $st[1]=str_replace("{website}","",$st[1]);
        $st[1]=str_replace("{user1}","",$st[1]);
        $st[1]=str_replace("{user2}","",$st[1]);
        $st[1]=str_replace("{user3}","",$st[1]);
        $st[1]=str_replace("{user4}","",$st[1]);
        $st[1]=str_replace("{user5}","",$st[1]);
        $st[1]=str_replace("{distance}","",$st[1]);
        $st[1]=str_replace("{maptext}","",$st[1]);

//========added evolve============21st-August-2004==START

//        $str=$st[0].$st[1].$st[2];
                $sth[1]="";
                $st[0]=$sth[0].$sth[1].$sth[2];
        $str=$st[0].$st[1].$st[2];

//========added evolve============21st-August-2004==END


        $fpx=fopen("nomatch.txt","r");
        $stx=fread($fpx,filesize("nomatch.txt"));
        $str=str_replace("{matches}",$stx,$str);
        $str=str_replace("{admin_email}",$organizationemail,$str);
        fclose($fpx);
        }
}

//==========================================================================================
//==========================================================================================
// Name, City, State and Country Searches:
//==========================================================================================
//==========================================================================================

if((isset($name) && $name !="") || (isset($city) && $city !="") || (isset($state) && $state !="") || (isset($country) && $country !=""))
{

if (isset($name) && $name!="") {
//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "select * from location where name like '%$name%' ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,'$order'";
}
else
{
$query="select * from location where name like '%$name%' and hide=0 and aprove=0 order by pref asc,'$order'";
}
}

if (isset($city) && $city!="") {
//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "select * from location where city like '%$city%' ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,'$order'";
}
else
{
$query="select * from location where city like '%$city%' and hide=0 and aprove=0 order by pref asc,'$order'";
}
}

if (isset($state) && $state!="") {
//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "select * from location where state='$state' ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,'$order'";
}
else
{
$query="select * from location where state='$state' and hide=0 and aprove=0 order by pref asc,'$order'";
}
}

if(isset($country) && $country != "") {
//Brian - User Fields Searching
if(isset($user1) || isset($user2) || isset($user3) || isset($user4) || isset($user5)) {
        $query = "select * from location where country='$country' ";
        if($user1!="")        { $query = $query."and user1='$user1' ";        }
        if($user2!="")        { $query = $query."and user2='$user2' ";        }
        if($user3!="")        { $query = $query."and user3='$user3' ";        }
        if($user4!="")        { $query = $query."and user4='$user4' ";        }
        if($user5!="")        { $query = $query."and user5='$user5' ";        }
        $query = $query."and hide=0 and aprove=0 order by pref asc,'$order'";
}
else
{
$query="select * from location where country='$country' and hide=0 and aprove=0 order by pref asc,'$order'";
}
}

$alldata = mysql_query($query,$zip_connection);
$total_rows=mysql_num_rows($alldata);
$max_data=$total_rows;

if($total_rows > $max_data)
{
$total_rows=$max_data;
}

        if($total_rows>0)
        {

$numofpages =ceil($total_rows/$front_max_records);
if(!isset($pageno))
{
$start_rec = 0;
}

if(isset($pageno))
{
$start_rec=($pageno-1)*$front_max_records;

        if($start_rec+$front_max_records > $max_data)
        {
        $front_max_records=$max_data-$start_rec;
        }
}
$query.=" limit $start_rec,$front_max_records";
$data=mysql_query($query,$zip_connection);
$recordset=mysql_fetch_assoc($data);
$total_rec=mysql_num_rows($data);



$pageslist="";


if($numofpages>$max_page)
{
                if(!isset($pageno))
                {
                $pageno=1;
                }

                if($pageno<=$max_page)
                {
                $pageslist.="";
                }
                else
                {
                                if(($pageno%$max_page)==0)
                                {
                                $start1=(int)($pageno/$max_page)-1;
                                }
                                else
                                {
                                $start1=(int)($pageno/$max_page);
                                }
                                $pagep=($start1 * $max_page)+1;

                $pagep-=$max_page;
                $pageslist.="<a href=results_list.php?pageno=$pagep&$strdata>$previousgroupimage</a>&nbsp;&nbsp;";
                }
}

if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
        if($pageno==1)
        {
        $pageslist.=" ";
        $pageno-=1;
        }
        else
        {
        $pageno--;
$pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$previmage</a>&nbsp;&nbsp;";
        }
}

if(!isset($pageno))
{
$pageno=0;
}
$pageno+=1;


$start1=(int)($pageno/$max_page);
$start2=$pageno%$max_page;
if($start2==0)
{
$startpage=(($start1-1)*$max_page)+1;
$endpage=(($start1-1)*$max_page)+$max_page;
}
else
{
$startpage=($start1*$max_page)+1;
$endpage=($start1*$max_page)+$max_page;
}
if($endpage>$numofpages)
{
$endpage=$numofpages;
}

for($i=$startpage;$i<=$endpage;$i++)
{
        if(!isset($pageno) && $i==1)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        elseif(isset($pageno) && $pageno == $i)
        {
        $pageslist.="<b><u>$i</u></b>&nbsp;&nbsp;";
        }
        else
        {
        $pageslist.="<a class=\"pageLink\" href=results_list.php?pageno=$i&$strdata>$i</a>&nbsp;&nbsp;";
        }
}


if($numofpages > 1)
{
        if(!isset($pageno))
        {
        $pageno=1;
        }
$pageno++;
        if($pageno-1==$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {

$pageslist.="<a href=results_list.php?pageno=$pageno&$strdata>$nextimage</a>&nbsp;&nbsp;";
        }
}

if($numofpages > $max_page)
{
    $page=$pageno-1;
        if(($page%$max_page)==0)
        {
        $start1=(int)($page/$max_page)-1;
        }
        else
        {
        $start1=(int)($page/$max_page);
        }
        $pagen=($start1 * $max_page)+1;
        $pages=($start1*$max_page)+$max_page;

        if($pages>$numofpages)
        {
        $pageslist.=" ";
        }
        else
        {
$pagen+=$max_page;
$pageslist.="<a href=results_list.php?pageno=$pagen&$strdata>$nextgroupimage</a>&nbsp;&nbsp;";
        }
}


$x=$start_rec+1;
do{

$st[1]=str_replace("{displaynumber}",$x,$st[4]);
$st[1]=str_replace("{id}",$recordset['id'],$st[1]);

// Now supports {maptext} in the results_list.php - Added by Brian J. on July 30, 2004
if ($recordset['zip'] != "-") {
         if($map_link_text!="")
         {

           if ($show_map_link == 1) {
             if ($map_target==1) {$target="_blank";}
             if ($map_target==0){$target="";}

        $streetY = $recordset['address1'];
        $cityY = $recordset['city'];
        $stateY = $recordset['state'];
        $zipY = $recordset['zip'];
        $countryY = $recordset['country'];
        $streetMQ = str_replace(" ","+",$recordset['address1']);
        $cityMQ = str_replace(" ","+",$recordset['city']);
        $stateMQ = str_replace(" ","+",$recordset['state']);
        $zipMQ = str_replace(" ","+",$recordset['zip']);
        $countryMQ = str_replace(" ","+",$recordset['country']);

        if ($map_service == "mapquest")
            {
              if ($recordset['country'] == "US")
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$mapquest_link?city=$cityMQ&state=$stateMQ&address=$streetMQ&zip=$zipMQ&zoom=5\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        elseif ($map_service == "yahoo")
            {
              if (($recordset['country'] == "US") || ($recordset['country'] == "CA"))
              {
              $stn="<a class=\"mapLink\" target=\"$target\" href=\"http://$yahoo_link?addr=$streetMQ&csz=$cityY+$stateY+$zipY&country=$countryY\">$map_link_text</a>";
              $st[1]=str_replace("{maptext}",$stn,$st[1]);
              }
              else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
            }
        else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
        }
        }
}
else { $st[1]=str_replace("{maptext}"," ",$st[1]); }
// End map links

        if(mysql_result($datah,0,"h1")==0)
        {
        $st[1]=str_replace("{name}",$recordset['name'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{name}","",$st[1]);
        }
        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address1}",$recordset['address1'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address1}","",$st[1]);
        }
        if(mysql_result($datah,1,"h1")==0)
        {
        $st[1]=str_replace("{address2}",$recordset['address2'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{address2}","",$st[1]);
        }
        if(mysql_result($datah,2,"h1")==0)
        {
        $st[1]=str_replace("{city}",$recordset['city'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{city}","",$st[1]);
        }
        if(mysql_result($datah,3,"h1")==0)
        {
        $st[1]=str_replace("{state}",$recordset['state'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{state}","",$st[1]);
        }
        if(mysql_result($datah,4,"h1")==0)
        {
        $st[1]=str_replace("{country}",$recordset['country'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{country}","",$st[1]);
        }
        if(mysql_result($datah,5,"h1")==0)
        {
        $st[1]=str_replace("{zip}",$recordset['zip'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{zip}","",$st[1]);
        }

        if(mysql_result($datah,6,"h1")==0)  // THE FOLLOWING WAS ADDED BY BRIAN TO ALLOW ALL FIELDS
        {
        $st[1]=str_replace("{telephone}",$recordset['telephone'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{telephone}","",$st[1]);
        }

        if(mysql_result($datah,7,"h1")==0)
        {
        $st[1]=str_replace("{fax}",$recordset['fax'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{fax}","",$st[1]);
        }

        if(mysql_result($datah,8,"h1")==0)
        {
        $st[1]=str_replace("{email}",$recordset['email'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{email}","",$st[1]);
        }

        if(mysql_result($datah,9,"h1")==0)
        {
        $st[1]=str_replace("{website}",$recordset['website'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{website}","",$st[1]);
        }

        if(mysql_result($datah,10,"h1")==0)
        {
        $st[1]=str_replace("{user1}",$recordset['user1'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user1}","",$st[1]);
        }

        if(mysql_result($datah,11,"h1")==0)
        {
        $st[1]=str_replace("{user2}",$recordset['user2'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user2}","",$st[1]);
        }

        if(mysql_result($datah,12,"h1")==0)
        {
        $st[1]=str_replace("{user3}",$recordset['user3'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user3}","",$st[1]);
        }

        if(mysql_result($datah,13,"h1")==0)
        {
        $st[1]=str_replace("{user4}",$recordset['user4'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user4}","",$st[1]);
        }

        if(mysql_result($datah,14,"h1")==0)
        {
        $st[1]=str_replace("{user5}",$recordset['user5'],$st[1]);
        }
        else
        {
        $st[1]=str_replace("{user5}","",$st[1]);
        }

$st[1]=str_replace("{distance}","",$st[1]);

$x++;
$st[3]=$st[3].$st[1];
}while($recordset=mysql_fetch_assoc($data));


//========added evolve============21st-August-2004==START

//$str=$st[0].$st[3].$st[2];
$str=$sth[0].$sth[1].$sth[2].$st[3].$st[2];
//========added evolve============21st-August-2004==END

if (isset($statename) && $statename!="") { $printstate = $statename; } else { $printstate = $state; }
if (isset($countryname) && $countryname!="") { $printcountry = $countryname; } else { $printcountry = $country; }

if (isset($state) && $state!="") { $str=str_replace("{querytype}","Searching for $headtext in $printstate ",$str); }
elseif (isset($country) && $country!="") { $str=str_replace("{querytype}","Searching for $headtext in $printcountry ",$str); }
elseif (isset($city) && $city!="") { $str=str_replace("{querytype}","Searching for $headtext in $city",$str); }
elseif (isset($name) && $name!="") { $str=str_replace("{querytype}","Searching for $headtext named $name",$str); }

$str=str_replace("{matches}","Displaying $total_rows matches",$str);
        }
        else        {
        $pageslist="";
        $st[1]=str_replace("{displaynumber}","",$st[1]);
        $st[1]=str_replace("{id}","",$st[1]);
        $st[1]=str_replace("{name}","",$st[1]);
        $st[1]=str_replace("{address1}","",$st[1]);
        $st[1]=str_replace("{address2}","",$st[1]);
        $st[1]=str_replace("{city}","",$st[1]);
        $st[1]=str_replace("{state}","",$st[1]);
        $st[1]=str_replace("{country}","",$st[1]);
        $st[1]=str_replace("{zip}","",$st[1]);
        $st[1]=str_replace("{telephone}","",$st[1]);
        $st[1]=str_replace("{fax}","",$st[1]);
        $st[1]=str_replace("{email}","",$st[1]);
        $st[1]=str_replace("{website}","",$st[1]);
        $st[1]=str_replace("{user1}","",$st[1]);
        $st[1]=str_replace("{user2}","",$st[1]);
        $st[1]=str_replace("{user3}","",$st[1]);
        $st[1]=str_replace("{user4}","",$st[1]);
        $st[1]=str_replace("{user5}","",$st[1]);
        $st[1]=str_replace("{distance}","",$st[1]);
        $st[1]=str_replace("{maptext}","",$st[1]);

//========added evolve============21st-August-2004==START
//        $str=$st[0].$st[1].$st[2];
                $sth[1]="";
                $st[0]=$sth[0].$sth[1].$sth[2];
        $str=$st[0].$st[1].$st[2];

//========added evolve============21st-August-2004==END


        $fpx=fopen("nomatch.txt","r");
        $stx=fread($fpx,filesize("nomatch.txt"));
        $str=str_replace("{matches}",$stx,$str);
        $str=str_replace("{admin_email}",$organizationemail,$str);
        fclose($fpx);
        }
}


//==========================================================================================
//==========================================================================================
// End all specific search types:
//==========================================================================================
//==========================================================================================




// DISTANCE CALCULATIONS-----------------------
function distance($lat1, $lon1, $lat2, $lon2) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;

        return $miles;

}

if(!isset($numofpages))
{
$numofpages="";
}

$str=$ts[0].$str.$ts[2];

if($numofpages=="")
{
$str=str_replace("{headtext}","",$str);
$str=str_replace("{pages}","",$str);
$str=str_replace("{paget}","",$str);
$str=str_replace("{querytype}","",$str);
}
elseif($numofpages=="1")
{
$str=str_replace("{headtext}","$headtext",$str);
$str=str_replace("{pages}","",$str);
$str=str_replace("{paget}","",$str);
}
else
{
$str=str_replace("{headtext}",$headtext,$str);
$str=str_replace("{paget}","Pages ",$str);
$str=str_replace("{pages}","(".$numofpages."): ".$pageslist,$str);
//$str=str_replace("{list}"," (".$numofpages."): ".$pageslist,$str);
}

if($headerfooter==1)
{
// Print Header
   $hdr=fopen("$user_path/header.htm","r");
   $stx=fread($hdr,filesize("$user_path/header.htm"));
   $str=str_replace("{header}",$stx,$str);
   fclose($hdr);
// Print Footer
   $ftr=fopen("$user_path/footer.htm","r");
   $stx=fread($ftr,filesize("$user_path/footer.htm"));
   $str=str_replace("{footer}",$stx,$str);
   fclose($ftr);
}
else 
{
$str=str_replace("{header}","",$str);
$str=str_replace("{footer}","",$str);
}

$str=str_replace("{site}",$site,$str);
$str=str_replace("{images}",$image_path,$str);
echo $str;
?>