<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Custom Live Search</title>
</head>

<body>
	<h1>Custom Live Search</h1>
	<div style="padding: 10pt; border: medium red solid;">
		<form method="post" action="index.php">	
			<input type="text" id="txtSearch" name="txtSearch" /><br />
			<fieldset id="grpSourceType">
				<legend>Prohledat</legend>
				<input name="searchType" type="radio" value="web" checked="checked"  /> stránky
				<input name="searchType" type="radio" value="image" /> obrázky
			</fieldset>
			<input type="submit" id="submit" name="submit" value="Hledat" />
		</form>
	</div>
	<div>
		<?php
			$APPID = "";
			$VALID_SEARCH_TYPES = array ('web', 'image');
			
			if (isset($_POST['submit']) && isset($_POST['searchType']) && isset($_POST['txtSearch']))
			{
				echo '<h1>Hledaný výraz: ' . $_POST['txtSearch'] . '</h1>';
				if (in_array($_POST['searchType'], $VALID_SEARCH_TYPES))
				{
					$searchTerm = str_replace('+', '%20', urlencode($_POST['txtSearch']));
					$searchUrl = "http://api.search.live.net/xml.aspx?AppId=$APPID&Market=cs-CZ&Query=$searchTerm&Sources=" . $_POST['searchType'];
					
					$xsl = new DOMDocument();
					$xsl->load('xslt/' . $_POST['searchType'] . '.xslt');
					
					$xslt = new XSLTProcessor();					
					$xslt->importStylesheet($xsl);
					
					$xml = new DOMDocument();
					$xml->load($searchUrl);

					echo '<div>' . $xslt->transformToXML($xml) . '</div>';
				}
				else
				{
					echo "Vyberte platný typ hledání.";
				}				
			}
		?>
	</div>
</body>

</html>
