<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

	// load viewer library
	$libraryPath = 'admin/lib/viewer_functions.php';
	$dirsToCheck = array('########');
	foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
	if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

	// load globals
	list($globals, $globalsMetaData) = getRecords(array(
		'tableName'   => 'global',
		'allowSearch' => '0',
		'loadUploads' => '0',
		'limit'       => '1',
	));
	$globals = @$globals[0]; // get first record


	// site search stuff
	$searchOptions = array();
	$searchOptions['keywords'] = @$FORM['q'];
	$searchOptions['perPage'] = "20";
	$searchOptions['debugSql'] = "0";

	$searchTables = array();

	$searchTables['whyarcel_pages'] = array(
		'viewerUrl' => '/why-arcel-resins/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['markets_pages'] = array(
		'viewerUrl' => '/markets/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['techinfo_pages'] = array(
		'viewerUrl' => '/technical-information/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['sustainability_pages'] = array(
		'viewerUrl' => '/sustainability/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['newsevents_pages'] = array(
		'viewerUrl' => '/news-events/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['contact_pages'] = array(
		'viewerUrl' => '/contact/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['casestudies_pages'] = array(
		'viewerUrl' => '/casestudies-literature/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','navigation_text','content','sidebar','title_tag','site_search_keywords'),
		'field1' =>  'createdDate',
		);

	$searchTables['tech_info_documents'] = array(
		'viewerUrl' => '/technical-information/download.php',
		'titleField' => 'title',
		'summaryField' => '',
		'searchFields' => array('title','site_search_keywords'),
		'field1' =>  'createdDate',
		);
		
	// news
	$searchTables['news_items'] = array(
		'viewerUrl' => '/news-events/news.php',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','date','site_search_keywords','source','url'),
		'field1' =>  'createdDate',
		);

	// events
	$searchTables['events'] = array(
		'viewerUrl' => '/news-events/',
		'titleField' => 'title',
		'summaryField' => 'event_dates',
		'searchFields' => array('title','event_dates','event_location','event_website'),
		'field1' =>  'createdDate',
		);

	// case studies
	$searchTables['case_studies'] = array(
		'viewerUrl' => '/casestudies-literature/',
		'titleField' => 'title',
		'summaryField' => 'summary',
		'searchFields' => array('title','summary','site_search_keywords'),
		'field1' =>  'createdDate',
		);

		

	list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);


?>



			<h2>Search Results</h2>

			<?php
			  function maxWords($textOrHtml, $maxWords) {
				  $text = strip_tags($textOrHtml);
				  $words = preg_split("/\s+/", $text, $maxWords+1);
				  if (count($words) > $maxWords) { unset($words[$maxWords]); }
				  $output = join(' ', $words);
				  return $output;
			  }
			?>

			<div class="search-again">
				<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
					<input type="text" name="q" placeholder="<?php echo htmlspecialchars(@$FORM['q']); ?>">
					<input type="submit" name="" value="Go">
				</form>
				<p>
					<?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?> results were found for <strong><?php echo htmlspecialchars(@$FORM['q']); ?></strong>
					<!--
						Showing results <?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?> &ndash; <?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?> of <?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?>
					-->
				</p>
			</div>
	
	
	
				<!-- show errors -->
				<?php if ($searchDetails['invalidPageNum']): ?>
					Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
				<?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
					There were no results for the term or phrase you searched for.<br/><br/>
				<?php elseif ($searchOptions['keywords'] == ""): ?>
					Enter a term or phrase to search for.<br/><br/>
				<?php endif ?>

	
				<?php
					if($searchDetails['noRecordsFound']) {
						}
					else {
						function subval_sort($a,$subkey, $direction = "DESC") {
							foreach($a as $k=>$v) {
							$b[$k] = strtolower($v[$subkey]);
							}
							if ($direction == "DESC") {
							  arsort($b);
							}
							else {
							  asort($b);
							}
						foreach($b as $key=>$val) {
							$c[] = $a[$key];
							}
						return $c;
						}
	
						$searchRows = subval_sort($searchRows,'field1','DESC');
						}
				?>
	
				<div class="search-results">
					<?php foreach ($searchRows as $record): ?>
						<?php
							if($record['tablename']=="markets_pages") $title_prefix = "Markets";
							elseif($record['tablename']=="whyarcel_pages") $title_prefix = "Why ARCEL Resins";
							elseif($record['tablename']=="techinfo_pages") $title_prefix = "Technical Information";
							elseif($record['tablename']=="sustainability_pages") $title_prefix = "Sustainability";
							elseif($record['tablename']=="casestudies_pages") $title_prefix = "Case Studies / Literature";
							elseif($record['tablename']=="newsevents_pages") $title_prefix = "News &amp; Events";
							elseif($record['tablename']=="contact_pages") $title_prefix = "Contact Us";
							elseif($record['tablename']=="tech_info_documents") $title_prefix = "Technical Information";
							elseif($record['tablename']=="events") $title_prefix = "Events";
							elseif($record['tablename']=="news_items") $title_prefix = "News";
							elseif($record['tablename']=="case_studies") $title_prefix = "Case Studies";
							else $title_prefix = "";
						?>
						<div class="result">
							<p>
								<a class="search-result-title" href="<?php echo $record['_link'] ?>"><?php echo $title_prefix ?> &raquo; <strong><?php echo $record['_title'] ?></strong></a>
								<?php if ($record['_summary']): ?>
									<br><span class="search-result-summary"><?PHP  echo maxWords($record['_summary'], 30); ?>...</span>
								<?php endif ?>
							</p>
						</div>
					<?php endforeach ?>
				</div><!-- search-results -->
	
				<div class="pagination">
					<?php if ($searchDetails['prevPage']): ?>
						<a href="<?php echo $searchDetails['prevPageLink'] ?>">&lt;&lt; prev</a>
					<?php else: ?>
						&lt;&lt; prev
					<?php endif ?>
						- page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -
					<?php if ($searchDetails['nextPage']): ?>
						<a href="<?php echo $searchDetails['nextPageLink'] ?>">next &gt;&gt;</a>
					<?php else: ?>
						next &gt;&gt;
					<?php endif ?>
				</div>
	
	
