function find_selected_listing()
{
    global $listing;
    if (isset($_GET["listing"])) {
        /* search db for specified record */
        $listing = read_listing($_GET["listing"]);
        if (!$listing) {
            /* record not found */
            redirect_to("index.php");
        }
    } else {
        /* $_GET was not set */
        redirect_to("index.php");
    }
}
{
    $titleData = $row->find('.make_and_model > a');
    $priceData = $row->find('.price');
    $title = $titleData[0]->plaintext;
    $price = (int) str_replace(' ', '', $priceData[0]->plaintext);
    $link = $titleData[0]->href;
    $id = str_replace('/used/', '', $link);
    if (strpos($priceData[0]->plaintext, '20%') !== false) {
        $price *= 1.2;
    }
    return array('id' => $id, 'title' => $title, 'price' => $price, 'url' => $link, 'ts' => date('Y-m-d H:i:s'));
}
require 'scraperwiki/simple_html_dom.php';
define('RESULTS_PER_PAGE', 200);
$requestParams = array('ae' => 2, 'af' => RESULTS_PER_PAGE, 'f1' => 2007, 'f2' => 2010, 'g1' => 3000, 'g2' => 30000, 'ad' => 1, 'by' => 2, 'q' => 2, 'd' => range(4, 7));
$a24ksi = read_listing($requestParams);
$data = array('a24ksi_val' => $a24ksi['val'], 'a24ksi_n' => $a24ksi['n'], 'date' => date('Y-m-d'), 'ts' => time());
scraperwiki::save(array('date'), $data);
function read_listing($params, $url = 'http://www.auto24.ee/kasutatud/nimekiri.php')
{
    $endpoint = build_query($url, $params);
    $html = scraperWiki::scrape($endpoint);
    $dom = new simple_html_dom();
    $dom->load($html);
    $totalResultsEl = $dom->find('.paginator .current-range strong');
    $totalResults = $totalResultsEl[0]->plaintext;
    $medianItem = ($totalResults + 1) / 2;
    if ($medianItem > RESULTS_PER_PAGE) {
        $listingOffset = floor($medianItem / RESULTS_PER_PAGE) * RESULTS_PER_PAGE;
        $params['ak'] = $listingOffset;
        $medianItem -= $listingOffset;