include "_header.php";
// DEFINE DEFAULT SEARCH VALUES
$totalMatches=0;
if(!isset($_GET['pg']) || !is_numeric($_GET['pg']))$_GET['pg']=1;
if(!isset($ctxt))$ctxt="";
if(!isset($cfield))$cfield="last";
if(!isset($ctype))$ctype="1";
$ctxt=stripslashes($ctxt);
$indextype=false;
function outputPageLinks($page,$total,$url){
  $pg_start=$page-5;
  if($total<=10 || $pg_start<1)$pg_start=1;
  echo("Goto Page:  ");
  if($pg_start>1)echo("First  ");
  for ($count=$pg_start;$count<=$total;$count++) {  // loop thru 
    if ($count < (10+$pg_start)){
		$iMod = 1;
 	} elseif ($count < (50+$pg_start)){
		$iMod = 10;
	} else {
		$iMod=50;
	}
    if ($count % $iMod==0){
		if($count!=$page){
      			print  "$count \n";
		} else {
			print "$count \n";
		}
		$pg_last=$count;
	}
  }
  if($pg_last<$total)echo(" Last");
  echo("");
}
if($cid>0){
	$page_title=$cnty['CT_Name']." County, ".$_VAR['STATE_NAME'];
} else {
	$page_title="All ".$_VAR['STATE_NAME'];
	$indextype=true;
}
$heading_title=$_VAR['PROJECT_LONG_NAME'].' - '.$page_title;
DrawHeader($heading_title,'',"Search...");
startFrame('100%');
?>
// BUILD SEARCH STRING
if(!empty($cfield) && !empty($ctype) && !empty($ctxt)){
  $searchOffset=($_GET['pg']-1)*$_VAR['SEARCH_MAX_MATCHES'];
  
  $querysql ="	SELECT 
					SQL_CALC_FOUND_ROWS gravestone.*,CT_Name,cemName 
				FROM 
					gravestone,countylist,cemeteries 
				WHERE 
					CID=CountyID AND CID=cemCID AND cemeteryID=cemID AND approved='Y'  
				";
				
	$sstring=mysql_escape_string(stripslashes($ctxt));
  
	if($ctype=="1"){
		$querysql.=" AND ($cfield like '$sstring%') ";
	} elseif($ctype=="2"){
		$querysql.=" AND $cfield like '%$sstring%' ";
	} elseif($ctype=="3"){
		$querysql.=" AND soundex($cfield)=soundex('$sstring') ";
	} elseif($ctype=="4"){
		$querysql=" AND $cfield='$sstring' ";
	}
	if($cid>0)$querysql.=" AND CID=".floor($cid); 
	$querysql.=" ORDER BY last,first";
	
	$sql=$querysql." LIMIT $searchOffset,{$_VAR['SEARCH_MAX_MATCHES']}";
	$result = mysql_query($sql) or die("Query failed: $sql");
	
	$row=mysql_fetch_row(mysql_query("Select FOUND_ROWS()")) or die($DB->ErrorMsg());
	$totalMatches=$row[0];
	$totalPages=ceil($totalMatches/$_VAR['SEARCH_MAX_MATCHES']);
	$queryURL="cfield=$cfield&ctype=$ctype&ctxt=".urlencode($ctxt);
}
//*** DISPLAY SEARCH TIPS  IF NO RECORDS FOUND ****
if($querysql!='' && $totalMatches==0){
	if($cid>0 && empty($cnty['CT_QtyTotal'])){
		echo "".
		"".
		"Be the first to submit a photo for ".$cnty['CT_Name'].
		"!
";
	} else if($cid>0){
		echo "".
		"Sorry no entries matching your search were found in ".$cnty['CT_Name'].
		".   You may want to check your spelling, try searching additional Parishes, locations in other countries, ".
		"or user a different type of search.
";
	} else {
		echo "".
		"Sorry no entries matching your search were found.  ".
		"You may want to check your spelling, or use a different type of search.
";
	}
} 
if($totalMatches>0){
	if($totalMatches<=$_VAR['SEARCH_MAX_MATCHES']){
		print "Total Matches: {$totalMatches}
";
	} else {
		$firstGroupRecord=$searchOffset+1;
		$lastGroupRecord=$searchOffset+$_VAR['SEARCH_MAX_MATCHES'];
		if($lastGroupRecord>$totalMatches)$lastGroupRecord=$totalMatches;
		print "{$firstGroupRecord} - {$lastGroupRecord} of {$totalMatches} matches
";
	}
	print "";
	print "";
	print "| Name | ";
	print "Cemetery | ";
	print "County | ";
	print "Updated | ";
	print "
";
	$toggle=1;
	while($row = mysql_fetch_assoc($result)){ 
	    $toggle=1-$toggle;
	    if($toggle==0){
			$back="bline";
	    } else {
			$back="bline shaded";
	    }
		$name=htmlout(strtoupper($row["last"]).", ".$row["first"]);
		
		print "| {$name} | ";
		print "";
		print "".htmlout($row["cemName"])." Cemetery | ";
		print "".htmlout($row["CT_Name"])." | ";
		print "".htmlout($row["dt_updated"])." | 
";
	}
 
	print "
";
	if($totalPages>1)
		outputPageLinks($_GET['pg'],$totalPages,$_VAR['RELATIVE_URL']."/search.php?{$queryURL}&pg=");
} else {
  // DISPLAY NAV LINKS
  include "_navlinks.html";
} 
?>
 include "_footer.php"; ?>