<html>
	<head>

<?
	// have to make this avoid highlighting parts of hrefs!
  function highlight( $needle, $haystack ) {
   $parts = explode( strtolower($needle), strtolower($haystack) );

   $pos = 0;

   foreach( $parts as $key=>$part ){
   
     $parts[ $key ] = substr($haystack, $pos, strlen($part));
     $pos += strlen($part);

		 // check that not inside a <>
		 $hitlt = false;
		 for($i=$pos; $i>=0; $i--) {
		 	 $ch = substr($haystack, $i, 1);
			 if($ch == '>') break;
			 if($ch == '<') {
			  	$hitlt = true; 
			  	break; 
			 }
		 }
		 if($hitlt) {
     	 $parts[ $key ] .= substr($haystack, $pos, strlen($needle));
		 }
		 else {
     	 $parts[ $key ] .= '<FONT COLOR="#008000"><B><u>'.substr($haystack, $pos, strlen($needle)).'</u></B></FONT>';
     }
     $pos += strlen($needle);
   }

   return( join( '', $parts ) );
  }
?>
<script type="text/javascript" language="JavaScript">
	
	// change this l8r!
	
	var queryVar = this.location.href;
	var pos      = queryVar.indexOf('?');
	if(pos != -1) queryVar = queryVar.substring(pos);
	else          queryVar = '';
	
	if (top == self || parent.frames[2].name != 'fhcontent')
    top.location.href = 'indexS.htm'+queryVar;
	
	//var url = parent.frames[1].location.href;
	//if (url.substring(url.length-5) != 'S.htm') 
	//	parent.frames[1].location.href = 'leftS.htm';
</script>

	  <title>Forgotten History</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta name="Keywords"    content="Facts,US,UN,Intervention,Foreign,Policy">
		<meta name="Description" content="Forgotten History - a few facts that everyone should know">

		<link rel="stylesheet" type="text/css" href="stylesheet.css">
	</head>
	
	<body>
		<table cellspacing=0 cellpadding=0 border=0 height=100%>
			<tr>
				<a name="site"></a>
				<td valign=top><table cellspacing=0 cellpadding=0>
					<br>
					<table border=0 cellspacing=0 cellpadding=0 width=720>
						<tr><td rowspan=5 height=30>&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
						<tr><td height=20>&nbsp;</td></tr>
						<tr><td colspan=2><table border=0 cellpadding=3 width=720><tr><td class=tablehead2><b>Search Results</b></td></tr></table></td></tr>
						<tr><td height=5></td></tr>
						<tr>
							<td>&nbsp;</td>
							<td>
								<table border=0 background="usflag.jpg"  cellpadding=2 width=720>
									<tr height="5"><td height="5"></td></tr> 
	
									<?
									  $timeparts = explode(" ",microtime());
  									$starttime = $timeparts[1].substr($timeparts[0],1);
  									
  									//error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
										error_reporting  (E_ERROR | E_WARNING | E_PARSE);
										
										$output = "";
										$noresult = "";
										
										$startitem = urldecode($HTTP_GET_VARS['start']); //$_GET['start'];
										if(strlen($startitem) == 0) $startitem = 1;

										$enditem   = $startitem+9;
										$searchstring = urldecode($HTTP_GET_VARS['searchstring']); //$_GET['searchstring'];

										$validsearchstring = true;
										if(strlen($searchstring) == 0) $validsearchstring = false;
										else if(substr($searchstring, 0, 1) == '?' || substr($searchstring, 0, 1) == '*') $validsearchstring = false;
																				
										// open all.htm
										$filename = 'all.htm';
										$fp = fopen($filename, "r");
										$contents = fread($fp, filesize($filename));
										fclose($fp);
										//echo $contents;
										
										// split with regexp
										$start = -1;
										$end   = -1;
										$found = 0;
										$lastitemshowing = -1;
										$factno = 0;
										
										$i = 0;
										
										// find all facts
										for (;;) {
											$start = strpos($contents, "<!--factbegins", $i);
										 	if($start == false) break;
										 	$end = strpos($contents, "<!--factend", $start);
										 	if($end == false) break;
										 	
								 			$end = $end + 15;
								 			$i   = $end;	
								 			
								 			$fact = substr($contents, $start, $end-$start);
								 			
											$factno++;
											if($factno == 2) {
												$noresult = $fact;
											}
											else {
												// if fact contains the searchstring															 			
									 			$contains = false;
									 			if($searchstring == "-- checklinks --") {
									 				$endofurl   = strpos($fact, "\">cached");
									 				if($endofurl != false) {
									 					$startofurl = strrpos(substr($fact,0,$endofurl), "\""); 
									 					$cacheurl = substr($fact, $startofurl+1, $endofurl-$startofurl-1);
									 					//echo "$startofurl $endofurl url = $cacheurl<br>";
									 					if(!file_exists($cacheurl)) $contains = true;
									 				}
									 			}
									 			else {
									 				if($validsearchstring && eregi($searchstring, $fact)) 
									 					$contains = true;
									 			}
									 			if($contains) {
									 				$found++;
									 				if($found >= $startitem && $found <= $enditem) {
	
											 			$link = "";
									 					$topic = "";
									 					$subtopic = "";
									 					{
									 						$part = 0;
									 						for ($j = 14; $j < strlen($fact); $j++) {
									 							$c = substr($fact, $j, 1);
								 								if($c == "|") {
								 									$part++;
																	if($part>2) break;
																}
										 						else if($part == 0) $link     .= $c;
										 						else if($part == 1) $topic    .= $c;
										 						else if($part == 2) $subtopic .= $c;
									 						}
									 					}
									 					$output .= "<tr><td colspan=2><img src=\"bullet.gif\"><b><a href=\"$link\">Result $found</a></b> (from section $topic - $subtopic)</td></tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><table>";
									 					
									 					// highlight (but not in links!?)
									 					$output .= highlight($searchstring, $fact); //ereg_replace($searchstring, "<font color=#007700><b><u>$searchstring</u></b></font>", $fact);
									 					$output .= "</table></td></tr>";
									 					$lastitemshowing = $found;
									 				}
									 			}
									 		}
										}
										
									  $timeparts = explode(" ",microtime());
										$endtime = $timeparts[1].substr($timeparts[0],1);
										
										// output results
										echo "<tr><td colspan=2><br>";
										if($lastitemshowing != -1) {
											echo "Displaying $startitem to ";
											echo $lastitemshowing;
											echo " from $found results for '$searchstring'.";
											// (search took ";
											//echo bcsub($endtime,$starttime,4);
											//echo "s).";
										}
										else if(!$validsearchstring) {
											if(strlen($searchstring) > 0)
												echo "Please enter a valid searchstring!";
											else
												echo "Please enter a non-empty searchstring!";
										}
										else if(strlen($searchstring) > 0) {
											echo "No items found for '$searchstring'!";
										}
										
										$nextpagestart = $startitem + 10;
										$prevpagestart = $startitem - 10;
										if($startitem > 1) echo "&nbsp;&nbsp;&nbsp;<a href=\"search.php?start=$prevpagestart&searchstring=$searchstring\">Prev 10</a>&nbsp;";
										if($lastitemshowing < $found && $lastitemshowing != -1) 
											echo "&nbsp;&nbsp;&nbsp;<a href=\"search.php?start=$nextpagestart&searchstring=$searchstring\">Next 10";
										
										echo "<br><br><br></td></tr>$output";
										
										if($lastitemshowing == -1) echo $noresult;
									?>
		
	  						</table>
	  					</td>
	  				</tr>
	  			</table><br>
				</td>
			</tr>
		</table>
	</body>
</html>
