<?php
include "file_func.php";
include "ddfuncs/ad2.php";
require('../setClassPath.php');
require_once('db/TransManager.php');
require_once('util/InputHelper.php');

$input = InputHelper::getInstance();
$trans = TransManager::getInstance();

$key = $input->fullySanitize($_GET['key']);
if (strlen($key) == 0) $key = "singles-US";

$depth      = 1;                    // 1 for country, 2 for state, 3 for city
$keys       = explode("-", $key);
$cat        = $keys[0];             // this is cat_path
$country    = $keys[1];             // this is loc_path of country
$state      = "";                   // this is loc_path of state
$city       = "";                   // this is loc_path of city
$catdisp    = "";                   // this is display value for category, ex. "Christian Singles"
$peopledisp = "";                   // this is display value for people of above category, which may differ from above
$catlink    = "";                   // this is the category link html, ex. "United States / Nevada"
$locdesc    = "";                   // description of the top location, ex. "Reno, NV"
$countryname    = "";               // display name of country
$statename      = "";               // display name of state
$cityname       = "";               // display name of city
$toplocpath     = $country;
$toploctype     = 'COUNTRY';

if (count($keys) > 2) {
    $state      = $keys[2];
    $toplocpath = $state;
    $toploctype = 'STATE';
    $depth      = 2;
}
if (count($keys) > 3) {
    $city       = $keys[3] . '_' . strtolower($state);
    $toplocpath = $city;
    $toploctype = 'CITY';
    $depth      = 3;
}


$trans->beginTrans();
$pdo = $trans->getPDO();

//first are there any tips on this page? and it's a page of depth 3?
//if not, kill the page with a 404
if ($depth == 3) {
    $stmt = $pdo->prepare('SELECT 1 FROM tip a, location_cat b WHERE b.location_cat_id=a.location_cat_id AND b.loc_path = ? AND b.cat_path = ? AND a.admin_flag = \'A\' LIMIT 1');
    $stmt->bindParam(1, $toplocpath);
    $stmt->bindParam(2, $cat);
    $stmt->execute();

    //no tip? 404!
    if (!$stmt->fetch(PDO::FETCH_NUM)) {
        header('HTTP/1.1 404 Not Found', TRUE, 404);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>404 File Not Found</title>
</head>
<body>
  <h1>404 File Not Found</h1>

  <p>Oops. There is no page to display!</p>

</body>
</html>
<?php
        exit();
    }
}



$useragent = $input->fullySanitize($_SERVER['HTTP_USER_AGENT']);
if (strpos($useragent, 'Googlebot/2.1'))
    append_file("../referer_files/geospider.txt", date("m/d/y  H:i:s") . ' - Google spidered:  ' . $key . "\n");
else if (strpos($useragent, 'msnbot/1.0'))
    append_file("../referer_files/geospider.txt", date("m/d/y  H:i:s") . ' - MSN spidered:     ' . $key . "\n");
else if (strpos($useragent, 'Yahoo! Slurp'))
    append_file("../referer_files/geospider.txt", date("m/d/y  H:i:s") . ' - Yahoo spidered:   ' . $key . "\n");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="/geo.css">
<?php

$stmt = $pdo->prepare('SELECT display, people_disp FROM category WHERE cat_path = ?');
$stmt->bindParam(1, $cat);
$stmt->execute();
$myrow = $stmt->fetch(PDO::FETCH_NUM);
$catdisp = $myrow[0];
$peopledisp = $myrow[1];

if (strlen($country) != 0) {
    $stmt = $pdo->prepare('SELECT name FROM location WHERE loc_path = ?');
    $stmt->bindParam(1, $country);
    $stmt->execute();
    $myrow = $stmt->fetch(PDO::FETCH_NUM);
    $countryname = $myrow[0];
}
if (strlen($state) != 0) {
    $stmt = $pdo->prepare('SELECT name FROM location WHERE loc_path = ?');
    $stmt->bindParam(1, $state);
    $stmt->execute();
    $myrow = $stmt->fetch(PDO::FETCH_NUM);
    $statename = $myrow[0];
}
if ($city != "") {
    $stmt = $pdo->prepare('SELECT name FROM location WHERE loc_path = ?');
    $stmt->bindParam(1, $city);
    $stmt->execute();
    $myrow = $stmt->fetch(PDO::FETCH_NUM);
    $cityname = $myrow[0];
}

if ($depth == 1) {
    $catlink = $countryname;
    $locdesc = $countryname;
}
else if ($depth == 2) {
    $catlink = "<a href=\"/$cat-$country/main.html\">$countryname</a> / $statename";
    $locdesc = $statename;
}
else if ($depth == 3) {
    $catlink = "<a href=\"/$cat-$country/main.html\">$countryname</a> / <a href=\"/$cat-$country-$state/main.html\">$statename</a> / $cityname";
    $locdesc = $cityname . ", " . $state;
}

printf("<title>%s Dating &#9829; %s</title>", $locdesc, $catdisp);
?>
<script type="text/javascript">
var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-628020-4']);_gaq.push(['_trackPageview']);(function(){var ga=document.createElement('script');ga.type='text/javascript';ga.async=true;ga.src=('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})();
</script>
</head>
<body>
<div class="box full" style="background-color: #ffddcc;">
<h1>Dating in <?= $locdesc . ' &#9829; ' . $catdisp ?></h1>
<span class="sm"><a href="/">Dating Diversions</a> | 
<?php
$c = 0;
$stmt = $pdo->prepare('SELECT cat_path, display FROM category ORDER BY disp_order');
$stmt->execute();
while ($myrow = $stmt->fetch(PDO::FETCH_NUM)) {
    if ($myrow[0] != 'friends') {
        if ($c != 0)
            echo " | ";
        if ($cat == $myrow[0])
            echo $myrow[1];
        else
            printf("<a href=\"/%s-US/main.html\">%s</a>", $myrow[0], $myrow[1]);
        $c = $c + 1;
    }
}
?>
</span>
</div>
<?php

echo "<div class=\"box full\"><b>Category: $catlink</b><br>\r\n";

$stmt = $pdo->prepare('SELECT a.loc_path, a.name, a.population FROM location a, location_cat b WHERE a.loc_path=b.loc_path AND b.cat_path = ? AND a.parent_path = ? '. ($toplocpath == 'US' ? '' : 'AND EXISTS(SELECT 1 FROM tip t WHERE t.location_cat_id=b.location_cat_id and t.admin_flag = \'A\') ') .' ORDER BY a.name');
$stmt->bindParam(1, $cat);
$stmt->bindParam(2, $toplocpath);
$stmt->execute();

$rows = array();
while ($myrow = $stmt->fetch(PDO::FETCH_NUM))
    $rows[] = $myrow;
$num = count($rows);

if ($num != 0) {
    echo "<table class=\"dir\"><tr valign=\"top\"><td width=\"250\"><ul>";

    $c = 0;
    foreach ($rows AS $myrow) {
        $keyend = $myrow[0];
        if ($depth == 2)
            $keyend = substr($keyend, 0, strlen($keyend) - 3);
        printf("<li><a href=\"/%s/main.html\">%s</a></li>\r\n", $key . "-" . $keyend, $myrow[1]);
        $c = $c + 1;
        if ($c >= ($num / 2)) {
            echo "</ul></td><td width=\"250\"><ul>";
            $c = -10;
        }
    }

    echo "</ul></td></tr></table>";
}
?>
</div>
<div class="boxt fullnp">
  <div class="boxtr side flr">
    <h3>Nightclubs</h3>
    ** Coming Soon **<br><br>
    <h3>Restaurants</h3>
    ** Coming Soon **
    <br><br>
    <div class="ctr">
<?php
    $cookieval = $input->fullySanitize($_COOKIE['mystic']);
    if ($cookieval == '') {
        if ($cat == 'christian') {
            echo '<a href="http://www.kqzyfj.com/9i98tenkem1388BB781325B3779" target="_blank" onmouseover="window.status=\'http://www.christiancafe.com\';return true;" onmouseout="window.status=\' \';return true;">';
            echo '<img src="http://www.ftjcfx.com/hi117p59y31NPUUXXTUNPORXPTTV" alt="ChristianCafe.com - All Christian. All Single!" border="0"/></a>';
        }
        else if ($cat == 'gay') {
            echo '<a href="http://www.kqzyfj.com/d977qgpmgo35AADD9A3547585BC" target="_blank" onmouseover="window.status=\'http://www.gay.com\';return true;" onmouseout="window.status=\' \';return true;">';
            echo '<img src="http://www.lduhtrp.net/q997y7B-53PRWWZZVWPRQTRURXY" alt="gay.com Premium Personals Free Week Offer" border="0"/></a>';
        }
        else {
            echo getAd($cookieval, '120x240', 'f', $pdo);
        }
    }
    else
        echo getAd($cookieval, '120x240', 'f', $pdo);
?>
    </div>
  </div>
  <div class="main pad">
    <h2>Online Dating Services Serving <?= $locdesc ?></h2>
<?php
/*
$stmt = $pdo->prepare('SELECT a.body FROM ad a, ad_map b, location_cat c WHERE c.cat_path = ? AND c.loc_path = ? AND b.location_cat_id=c.location_cat_id AND a.ad_id=b.ad_id');
$stmt->bindParam(1, $cat);
$stmt->bindParam(2, $toplocpath);
$stmt->execute();
$countad = 0;
while ($myrow = $stmt->fetch(PDO::FETCH_NUM)) {
    if ($countad == 0)
        echo '<ul class="ad">';
    printf("<li>%s</li>", $myrow[0]);
    $countad++;
}
if ($countad != 0)
    echo '</ul>';
*/    
?>
    <ul class="ad">
      <li><a href="http://www.dpbolvw.net/click-1669956-1515339" target="_blank" onmouseover="window.status='http://www.metrodate.com/';return true;" onmouseout="window.status=' ';return true;">Metrodate.com</a> - The Ultimate Singles Resource!
      <img src="http://www.tqlkg.com/image-1669956-1515339" width="1" height="1" border="0"/></li>
      <li><a href="http://www.anrdoezrs.net/a1107shqnhp46BBEEAB4968A876" target="_blank" onmouseover="window.status='http://www.date.com';return true;" onmouseout="window.status=' ';return true;">Find a Date - Date.com</a> personals
      <img src="http://www.awltovhc.com/od98c37w1-LNSSVVRSLQNPRPON" width="1" height="1" border="0"/></li>
    </ul>
    <h2>Dating Tips and Advice for <?= $peopledisp . " in " . $locdesc ?></h2>
<?php
$stmt = $pdo->prepare('SELECT a.body, a.name, a.location, a.tip_id FROM tip a, location_cat b WHERE b.location_cat_id=a.location_cat_id AND b.loc_path = ? AND b.cat_path = ? AND a.admin_flag = \'A\' ORDER BY a.tip_id DESC LIMIT 30');
$stmt->bindParam(1, $toplocpath);
$stmt->bindParam(2, $cat);
$stmt->execute();
$tipcount = 0;
while ($myrow = $stmt->fetch(PDO::FETCH_NUM)) {
    if ($tipcount == 1) {
?>
    <div style="width: 312px; height: 255px; float: left;">
      <script type="text/javascript"><!--
google_ad_client = "pub-7795630243408100";
/* Dating Diversions 300x250, created 4/22/10 */
google_ad_slot = "2191523928";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

    </div>
<?php
    }
    printf("<p>\"%s\" <i>-%s, %s</i></p>\r\n<hr>", $myrow[0], $myrow[1], $myrow[2]);
    $tipcount++;
}
if ($tipcount == 0)
    echo '<p><i>No advice yet, be the first to add advice to this location!</i></p>';
?>
    <div style="clear: both;"></div>
    <a href="post.html">Add your dating tips or advice for <?= strtolower($peopledisp) ?> in <?= $locdesc ?></a>
  </div>
</div>
View our <a href="/privacy.html">Privacy Policy</a>

</body></html>

<?php
$trans->commit();
$trans->disconnect();
?>
