<?php 

$var_array = explode("/",$PATH_INFO);
$page = $var_array[0];
$statefull = $var_array[1];
$county = $var_array[2];
$city = $var_array[3];
$term = $var_array[4];
$termID = $var_array[5];

$sepage = 1;

include "config.php";
include "header.php"; 



if ($statefull != "") {
		
		$statefull_db = str_replace("-"," ",$statefull);
		$pre_sql = "select distinct state from cm_zipcodes where statefull = \"$statefull_db\"";
			if ($pre_result = @mysql_query ("$pre_sql")) {
				while ($pre_row = @mysql_fetch_array ($pre_result)) {
					$state = $pre_row[state];
				}
			}
}

if ( ($statefull != "") && ($county != "") && ($city != "") ) {

		$pre_sql = "select * from cm_zipcodes where statefull = \"$statefull\" and county = \"$county\" and city = \"$city\"";
			if ($pre_result = @mysql_query ("$pre_sql")) {
				while ($pre_row = @mysql_fetch_array ($pre_result)) {
					$zip = $pre_row[zip];
					$state = $pre_row[state];
					$areacode = $pre_row[areacode];
				}
			}
			
}


?>
	
<h1>

<?php 

if ($term == "") {
	echo $seo_title;
} else {
	$display_term = str_replace("-"," ",$term); echo $display_term;
}
	if (($statefull) && ($term == "") ) { echo " in "; }
	if (($statefull) && ($term != "") ) { echo " in "; }	
	
	if ($city) { $display_city = str_replace("-"," ",$city); echo $display_city.", "; } 
	
	if (($county) && ($city == "") ) { $display_county = str_replace("-"," ",$county); echo $display_county." County, "; } 
	
	if ($statefull) { $display_statefull = str_replace("-"," ",$statefull); echo "<a class='h1link' href='".$site_domain.$seo_root_page_name."/".$statefull."'>".$display_statefull."</a>"; } 
	


?>

</h1>

<?php if ($cdgCMadmin == "admin") { echo "<p>[ <a href='".$site_domain."admin_menu.php'>Admin Menu</a> | <a href='".$site_domain."logout.php'>Logout</a>"; } ?>






<?php

if ( ($statefull == "") && ($county == "") ) { //if no state variable, then build state list

		$sql = "select distinct statefull from cm_zipcodes order by statefull asc";
			if ($result = @mysql_query ("$sql")) {
				
				if ($cdgCMadmin == "admin") { echo " | <a href='".$site_domain."edit_sepage.php?sepageName=none'>Edit SEO Page Text</a> ]</p>"; }
				
				$sql2 = "select * from cm_sepages where sepageName = \"none\"";
					if ($result2 = @mysql_query ("$sql2")) {
						while ($row2 = @mysql_fetch_array ($result2)) {
							$page_text = $row2[sepageText];
						}
					}

				echo $page_text;				
				
				
				echo "<ul>";
				while ($row = @mysql_fetch_array ($result)) {
					$statefull = str_replace(" ","-",$row[statefull]);																	
					echo "<li><a href='".$site_domain.$seo_root_page_name."/".$statefull."'>".$row[statefull]."</a></li>";
				}
				echo "</ul>";
		}

} elseif ( ($county == "") && ($city == "") ) { // if no county variable, then build county list

		$statefull_db = str_replace("-"," ",$statefull);
		$sql = "select distinct county from cm_zipcodes where statefull = \"$statefull_db\" order by county asc";
			if ($result = @mysql_query ("$sql")) {
				if ($cdgCMadmin == "admin") { echo " | <a href='".$site_domain."edit_sepage.php?sepageName=state'>Edit SEO Page Text</a> ]</p>"; }
				
				$sql2 = "select * from cm_sepages where sepageName = \"state\"";
					if ($result2 = @mysql_query ("$sql2")) {
						while ($row2 = @mysql_fetch_array ($result2)) {
							$page_text = $row2[sepageText];
						}
					}
				
				$page_text = str_replace("<state>",$display_statefull,$page_text);
				echo $page_text;	
				
				
				echo "<ul>";
				while ($row = @mysql_fetch_array ($result)) {
					$county = str_replace(" ","-",$row[county]);																	
					echo "<li><a href='".$site_domain.$seo_root_page_name."/".$statefull."/".$county."'>".$row[county]." County, ".$statefull_db."</a>";
					if ($cdgCMadmin == "admin") { echo " [ <a onClick=\"javascript: return confirm('Are you sure you want to delete ".$county." County, ".$statefull."? This CANNOT be undone.');\" href='".$site_domain."delete_se_county.php?statefull=".$statefull."&county=".$row[county]."'>Delete County</a> ]"; }
					echo "</li>";
				}
				echo "</ul>";				
		}

} elseif (($city == "") && ($term == "") ) { // if no city variable, then build city list


		$statefull_db = str_replace("-"," ",$statefull);
		$county_db = str_replace("-"," ",$county);
		$sql = "select distinct city from cm_zipcodes where county = \"$county_db\" order by city asc";
			if ($result = @mysql_query ("$sql")) {
				if ($cdgCMadmin == "admin") { echo " | <a href='".$site_domain."add_zip_code.php?statefull=".$statefull."&state=".$state."&county=".$county."'>Add Zip Code</a> | <a href='".$site_domain."edit_sepage.php?sepageName=county'>Edit SEO Page Text</a> ]</p>"; }

				
				$sql2 = "select * from cm_sepages where sepageName = \"county\"";
					if ($result2 = @mysql_query ("$sql2")) {
						while ($row2 = @mysql_fetch_array ($result2)) {
							$page_text = $row2[sepageText];
						}
					}
				
				$page_text = str_replace("<state>",$statefull_db,$page_text);
				$page_text = str_replace("<county>",$county_db,$page_text);				
				echo $page_text;


				echo "<ul>";			
				while ($row = @mysql_fetch_array ($result)) {
					$city = str_replace(" ","-",$row[city]);	
					$display_county = str_replace("-"," ",$county);																
					echo "<li><a href='".$site_domain.$seo_root_page_name."/".$statefull."/".$county."/".$city."'>".$row[city]." - ".$display_county." County, ".$statefull_db."</a></li>";
				}
				echo "</ul>";				
		}


} elseif (($term == "") && ($city != "") ) { //if no term variable, then build term list


		$sql = "select * from cm_sekeywords order by sekeyword asc";
			if ($result = @mysql_query ("$sql")) {
				if ($cdgCMadmin == "admin") { echo " | <a href='".$site_domain."add_keyword.php'>Add Keyword</a> | <a href='".$site_domain."edit_sepage.php?sepageName=city'>Edit SEO Page Text</a> ]</p>"; }
				
				
				$statefull_db = str_replace("-"," ",$statefull);
				$county_db = str_replace("-"," ",$county);
				$city_db = str_replace("-"," ",$city);
				$sql2 = "select * from cm_sepages where sepageName = \"city\"";
					if ($result2 = @mysql_query ("$sql2")) {
						while ($row2 = @mysql_fetch_array ($result2)) {
							$page_text = $row2[sepageText];
						}
					}
				
				$page_text = str_replace("<state>",$statefull_db,$page_text);
				$page_text = str_replace("<county>",$county_db,$page_text);				
				$page_text = str_replace("<city>",$city_db,$page_text);						
				echo $page_text;				
				
				
				echo "<ul>";			
				while ($row = @mysql_fetch_array ($result)) {
					$term = str_replace(" ","-",ucwords($row[sekeyword]));																	
					echo "<li><a  href='".$site_domain.$seo_root_page_name."/".$statefull."/".$county."/".$city."/".$term."/".$row[keywordID]."'>".ucwords($row[sekeyword])."</a></li>";
				}
				echo "</ul>";				
		}







} else { //if all variables assigned, then build keyword term page


		$sql = "select * from cm_sekeywords where keywordID = \"$termID\"";
			if ($result = @mysql_query ("$sql")) {
				while ($row = @mysql_fetch_array ($result)) {
				
					$display_city = str_replace("-"," ",$city);
					$display_county = str_replace("-"," ",$county);
					$display_statefull = str_replace("-"," ",$statefull);				
				
					$sql2 = "select * from cm_sekeywords order by sekeyword asc";
						if ($result2 = @mysql_query ("$sql2")) {
							$term_list_block = "<ul>\n";			
							while ($row2 = @mysql_fetch_array ($result2)) {
								$term2 = str_replace(" ","-",ucwords($row2[sekeyword]));																	
								$term_list_block .= "<li><a  href='".$site_domain.$seo_root_page_name."/".$statefull."/".$county."/".$city."/".$term2."/".$row2[keywordID]."'>".ucwords($row2[sekeyword])." for ".$display_city.", ".$display_statefull."</a></li>\n";
							}
						$term_list_block .= "</ul>\n";				
					}
				
					$text = str_replace("<state>",$display_statefull,$row[sekeywordText]);	
					$text = str_replace("<county>",$display_county,$text);						
					$text = str_replace("<city>",$display_city,$text);						
					$text = str_replace("<term>",str_replace("-"," ",ucwords($term)),$text);	
					$text = str_replace("<term_list_block>",$term_list_block,$text);					
					
					if ($cdgCMadmin == "admin") { echo " | <a href='".$site_domain."edit_keyword.php?keywordID=".$termID."&statefull=".$statefull."&county=".$county."&city=".$city."&term=".$term2."'>Edit Keyword</a> | <a href='".$site_domain."delete_keyword.php?keywordID=".$row[keywordID]."' onClick=\"javascript: return confirm('Are you sure you want to delete this keyword phrase? This CANNOT be undone.');\">Delete</a> ]</p>"; }
					
					echo $text;
					
					}
		}


}

?>



<p><a href="<?php echo $site_domain; ?>c/8/contact-us">Contact us today!</a></p>

		



<?php include "footer.php"; ?>