<?php
session_start();
require("sajax/php/Sajax.php");
require("classes/mysql.php");
include_once("classes/globals.php");

sajax_init();

mysql_connect("****", "****", "****");
mysql_select_db("****");

function searchClients($search_string)
{
	$searchResult = "";
	$sql = mysql_query("SELECT * FROM products_mailorder_sugarcraft WHERE item_name LIKE \"%$search_string%\" OR item_number LIKE \"%$search_string%\" ORDER BY item_name ASC LIMIT 100");
	$searchResult = "<table>";
	while($result = mysql_fetch_array($sql))
	{
		$searchResult = $searchResult."<tr><td><em>".$result["item_number"]."</em></td><td><a href=\"editsugar.php?item_number=".$result["item_number"]."\">".substr($result["item_name"],0,50)."...</a></td></tr>";
	}
	$searchResult = $searchResult."</table>";
	return $searchResult;
}

sajax_export("searchClients");
sajax_handle_client_request();
?>
<html>
<head>
<title>-</title>
<link type="text/css" href="style.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="tabcontent.css" />
<script type="text/javascript" src="tabcontent.js"></script>
<script>
	<?php sajax_show_javascript(); ?>

	function show_results(result) 
	{
		document.getElementById("search_results").innerHTML = result;
	}
	
	function do_search()
	{
		var searchString;
		searchString = document.getElementById("searchfor").value;
		x_searchClients(searchString, show_results);
	}
	
function hide(which)
	{
		if (!document.getElementById)
			return
		which.style.display="none";
	}

function show(which)
	{
		if (!document.getElementById)
			return
		which.style.display="block";
	}
</script>
</head>

<body><div style="width:800px">
<table width="100%"><tr><td><img src="images/jane.jpg"></td><td align="right"><h1>NetPublish : JaneAsher.com</h1></td></table>
<br><br>

<ul id="maintab" class="shadetabs">
<li class="selected"><a href="#" rel="tcontent1">Search Sugarcraft</a></li>
<li><a href="#" rel="tcontent2">Browse Sugarcraft</a></li>
<li><a href="#" rel="tcontent3">Create Category</a></li>
<!--<li><a href="http://www.net-root.com">NetRoot</a></li>-->
<li><a href="menu.php"><span class="barLink">Menu</span></a></li>
</ul>

<div class="tabcontentstyle">

<div id="tcontent1" class="tabcontent">
<p>As you type the text entered so far will be checked against current items in the sugarcraft database. Clicking on an item description will take you to the editor for it. Searches work for both text in the item's name and the reference code itself. In order to limit the amount of data sent to & from the server, searches will return only the first 100 matches.</p>
<table width="90%">
<tr>
	<td width="30%" valign="top"><input type="text" name="searchfor" id="searchfor" size="20" onkeyup="do_search(); return false;"><br><br><img src="images/Search.jpg" width="128" height="128"></td>
	<td valign="top" width="70%"><div id="search_results">Search results will appear here</div></td>
</tr>

</table>

</div>

<div id="tcontent2" class="tabcontent">
<p>Search the categorys of sugarcraft items by choosing from the main & sub categorys below. The lists will update automatically and when you click on an item's name you'll be taken to the edit page for it.</p>
<table>
<tr>
	<td valign="top">
<form action="sugarcraft.php" name="cat1" method="post">
	<select name="category" onchange="cat1.submit();">
		<option value=""></option>
<?php
	$query_getCategorys = "SELECT DISTINCT category FROM products_mailorder_sugarcraft ORDER BY category ASC";
	$getCategorys = mysql_query($query_getCategorys) or die(mysql_error());
?>
<?php while ($row = mysql_fetch_array($getCategorys)) { ?>
		<option value="<?php echo $row['category']; ?>" <?php if ($row['category'] == $_POST['category']) { echo "selected"; }?>><?php echo $row['category']; ?></option>
<?php } ?>
	</select>
</form>

</td> <td valign="top">

<?php if (!(isset($_POST['category']))) { ?>
	<select disabled="disabled">
		<option value="">Pick a category first</option>
	</select>
<?php } else { ?>
<form action="sugarcraft.php" name="cat2" method="post">
	<select name="sub_category" onchange="cat2.submit();">
		<option value=""></option>
<?php
	$query_getSubCategorys = "SELECT DISTINCT sub_category FROM products_mailorder_sugarcraft WHERE category=\"".$_POST['category']."\" ORDER BY sub_category ASC";
	$getSubCategorys = mysql_query($query_getSubCategorys) or die(mysql_error());
?>
<?php while ($row = mysql_fetch_array($getSubCategorys)) { ?>
		<option value="<?php echo $row['sub_category']; ?>" <?php if ($row['sub_category'] == $_POST['sub_category']) { echo "selected"; }?>><?php echo $row['sub_category']; ?></option>
<?php } ?>
	</select>
	<input type="hidden" value="<?php echo $_POST['category'];?>" name="category" />
</form>
<?php } ?>
	</td>
	
 <td valign="top">

<?php if (!(isset($_POST['sub_category']))) { ?>
	<select disabled="disabled">
		<option value="">Pick a sub-category first</option>
	</select>
<?php } else { ?>
<form action="editsugar.php" name="cat3" method="GET">
	<select name="item_number" onchange="cat3.submit();">
		<option value=""></option>
<?php
$query_getItems = "SELECT item_number, item_name FROM products_mailorder_sugarcraft WHERE category=\"".$_POST['category']."\" AND sub_category=\"".$_POST['sub_category']."\" ORDER BY item_number ASC";
$getItems = mysql_query($query_getItems) or die(mysql_error());
?>		
<?php while ($row = mysql_fetch_array($getItems)) { ?>
		<option value="<?php echo $row['item_number']; ?>"><?php echo $row['item_number']." (".substr($row['item_name'],0,20)."...)"; ?></option>
<?php } ?>
	</select><!--
	<input type="hidden" value="<?php echo $_POST['category'];?>" name="category" />
	<input type="hidden" value="<?php echo $_POST['sub_category'];?>" name="sub_category" />-->
</form>
<?php } ?>
	</td>
</tr>
</table>
</div>

<div id="tcontent3" class="tabcontent">
<p>Create a new category using the first input box below. Choose whether to make it a sub or main category with the radio buttons.</p>
<form method="POST" name="createcat" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table>
	<tr>
		<td width="200"><input type="text" size="30" name="categoryname"></td>
		<td width="50" align="left"><input type="radio" name="type" value="main" checked="checked" onclick="javascript:hide(document.getElementById('create_main'));">Main</td>
		<td width="250" align="left"><input type="radio" name="type" value="sub" onclick="javascript:show(document.getElementById('create_main'));">Sub</td>
	</tr><tr>
		<td colspan="3"><div id="create_main" style="display:none;">
			<select name="category">
				<option value=""></option>
		<?php
			$query_getCategorys = "SELECT DISTINCT category FROM products_mailorder_sugarcraft ORDER BY category ASC";
			$getCategorys = mysql_query($query_getCategorys) or die(mysql_error());
		?>
		<?php while ($row = mysql_fetch_array($getCategorys)) { ?>
				<option value="<?php echo $row['category']; ?>" <?php if ($row['category'] == $_POST['category']) { echo "selected"; }?>><?php echo $row['category']; ?></option>
		<?php } ?>
			</select>
			<span style="color:red;">Choose the main category for the new sub!</span>
		</div></td>
	</tr>
	</tr><tr>
		<td colspan="3"><input type="submit" name="Create" value="Create"></td>
	</tr>
</table>
</form>

</div>

<div id="tcontent4" class="tabcontent">
</div>

</div>

<?php include("footer.php"); ?>

syntax highlighted by Code2HTML, v. 0.9.1