<?PHP

include("config.php");
require_once("rating/classes/include.all.php");

// string comparison match percent
$comp_pct = 80;

if (!$_POST){ header('Location: '.$site_url); }

// build the SQL query
$sql = "SELECT runshop_id, runshop_name, runshop_address, runshop_address_two, runshop_city, runshop_state, runshop_zip, runshop_phone, runshop_fax, runshop_email, runshop_website, runshop_cart FROM ".$prefix."Runshops WHERE runshop_valid=1 ";

if (isset($_POST[ecommerce])) {
  $sql = $sql." AND runshop_cart = 1";  
}

if (isset($_POST[wetsuits])) {
  foreach ($_POST[wetsuits] AS $value) {
	$sql = $sql." AND runshop_wetsuits LIKE '%,$value,%'";  
  }
}

if (isset($_POST[swimgear])) {
  foreach ($_POST[swimgear] AS $value) {
	$sql = $sql." AND runshop_swimgear LIKE '%,$value,%'";  
  }
}

if (isset($_POST[diagnostics])) {
  foreach ($_POST[diagnostics] AS $value) {
	$sql = $sql." AND runshop_diagnostics LIKE '%,$value,%'";  
  }
}

if (isset($_POST[shoes])) {
  foreach ($_POST[shoes] AS $value) {
	$sql = $sql." AND runshop_shoes LIKE '%,$value,%'";  
  }
}

if (isset($_POST[socks])) {
  foreach ($_POST[socks] AS $value) {
	$sql = $sql." AND runshop_socks LIKE '%,$value,%'";  
  }
}

if (isset($_POST[apparel_run])) {
  foreach ($_POST[apparel_run] AS $value) {
	$sql = $sql." AND runshop_apparel_run LIKE '%,$value,%'";  
  }
}

if (isset($_POST[apparel_tri])) {
  foreach ($_POST[apparel_tri] AS $value) {
	$sql = $sql." AND runshop_apparel_tri LIKE '%,$value,%'";  
  }
}

// regions are checkboxes that then check off all states within that region
// add states
if (isset($_POST[states])) {
  $glue = "','";
  $statelist = "('".implode($glue, $_POST[states])."')";
  $sql = $sql." AND runshop_state_tag IN $statelist";
}


$sql = $sql." ORDER BY runshop_name ASC";

//echo $sql;

$results = mysql_query($sql);

$none = FALSE;
if (mysql_num_rows($results) == 0) {
  $none = TRUE;
}
$match_count = mysql_num_rows($results);

//keyword search
// trim redundant keywords: triathlon, duathlon
$trim_words = array(" the ", " a ", " an ", "and ", " half ", "triathlon", "duathlon");
foreach ($trim_words AS $word) {
	$_POST['keyword'] = str_ireplace($word, " ", $_POST['keyword']);
}
$bool_Keywords = false;
if (isset($_POST['keyword']) && $_POST['keyword'] != "") {
	$bool_Keywords = true;
	while($row_runshopes = mysql_fetch_array($results)) {
		// build an array that we can loop through.
		$arr_runshopes[$i] = $row_runshopes;
		$i++;
	}
	
	$arr_Matches = array();
	foreach ($arr_runshopes AS $runshop) {	
			if (stripos($runshop['runshop_name'], $_POST['keyword']) !== false) {
				// push this onto the array of matches
				$runshop['percent'] = 100;
				array_push($arr_Matches, $runshop);
			} else {
				$name_nospace = str_replace(" ", "", $runshop['runshop_name']);
				$keyword_nospace = str_replace(" ", "", $_POST['keyword']);
				if (stripos($name_nospace, $keyword_nospace) !== false) {
					// push this onto the array of matches
					$runshop['percent'] = 90;
					array_push($arr_Matches, $runshop);
				} else {
					// trim redundant keywords
					$runshop_name = $runshop['runshop_name'];
					foreach ($trim_words AS $word) {
						$runshop_name = str_ireplace($word, " ", $runshop_name);
					}
					//$runshop_name = str_ireplace(" ", "", $runshop_name);
					$runshop_name = strtolower($runshop_name);
					$arr_runshop_name = explode(" ", $runshop_name);
					$_POST['keyword'] = strtolower($_POST['keyword']);
					$_POST['keyword'] = str_replace(" ", "", $_POST['keyword']);
					foreach ($arr_runshop_name AS $test_word) {					
						similar_text($test_word, $_POST['keyword'], $percent);
						if ($percent >= $comp_pct) {
							// push this onto the array of matches
							$runshop['percent'] = round($percent, 0);
							//$runshop['trimmed'] = $runshop_name;
							array_push($arr_Matches, $runshop);
							break;
						}
					}
				}
			}
	}
	
	if (count($arr_Matches) == 0) {
 		$none = TRUE;
 	}
 	$match_count = count($arr_Matches);
 	
 	//sort the array by keyword match success instead of by date
 	function cmp($a, $b)
	{
		if ($a['percent'] == $b['percent']) {
			return 0;
		}
		return ($a['percent'] > $b['percent']) ? -1 : 1;
	}
	
	usort($arr_Matches, "cmp");
}
//end keyword search

// set the page title
$pagetitle = "Search Results";

// set meta tags
$meta_keywords = "search, running stores, triathlon, results";
$meta_description = "Triathlon running stores matching a user's search query. A list of matching running stores.";
?>

<? include("include_common_head.php"); ?>

<body class="listings">
  <? include($common_path . "/ads/ad_wallpaper.html"); ?>

  <div class="container">
    <? include($common_path . "/templates/include_header.php"); ?>
    <div class="main">
      <div class="contentwrapper clearfix">
        <? include("include_breadcrumb.php"); ?>

        <section class="section listings section-has-widgets section-static remove-sidebar">
          <div class="sidebar-b">
            <? include("include_sidebar.php"); ?>
          </div>

          <div class="content content-has-widgets">
            <div class="grid">

              <div class="clearfix">
                <h1>Search Results</h1>
                <p><b><? echo $match_count; ?></b> Running Stores Match Your Search</p>

                <table border="0" cellspacing="0" cellpadding="1" width="580">
                <? 
                if ($bool_Keywords == true) {
                    foreach($arr_Matches AS $row){	
                        include("include_store.php");
                    } // end foreach arr_Matches 
                } else { //if bool_Keywords
                    while ($row = mysql_fetch_array($results)) {
                        include("include_store.php");
                    } // end foreach mysql_fetch_array ?>
                <? } // end else ?>
                </table>

              </div><!-- end col-2/3 -->

            </div><!-- end grid -->
          </div><!-- end content -->
        </section>

      </div><!-- end contentwrapper -->
    </div> <!-- end main -->

  <? include($common_path . "/templates/include_footer.php") ?>
  </div> <!-- container  -->
</body>
<? include($common_path . "/templates/include_global_js.php") ?>
</html>
