<?
$rez=include("setup.php");
if (! $rez) //if can't find setup.php or not chmod'ed 
 {
  echo "<font color=red>Check Your setup.php file !</font>";
	exit;
 }

$dir='admin';
checkauth($dir);

include "menu.htm";

//-------------- ADD new category ---------------------
if (isset($insertme))		
{
       if( check_if_exist($namedirNew,"category","CategoryName"))
	print "This category exists";
       else
       {	
       $father=0;
       $result=mysql_query("
              insert into category(ID, CategoryName,Father,DateOfCreation) 
              values (null, '$namedirNew',$father,CURDATE() )") or die (mysql_error(). " : ".mysql_errno());
       }
}
//----------------------- and insert new ----------------

//------delete dirs-categories --------------
   if (($deleteme="delete") && isset($cremove[0]))
     {	
	$result=0;
        $i=0;  
        $flag=1; //to know if this category contains products,1 if not
     while($elements = each ($cremove))
     {
        $whois=$elements["value"];	
       if( check_if_exist($elements["value"],"product","CategoryID")>=1)
       {
	 echo "This category contains products.Can't be deleted<br>";
         $flag=0;
       }
       else
       {
        $query="delete from category where ID=$whois";
        $result=$result . mysql_query($query)or die (mysql_error(). " : ".mysql_errno());     
        $i++;
       }
     }
	if ($result==$i && $flag)
          print "Operation succeeded<br><br>";


     }

//----------- end remove dirs-categories--------

// ------------- remove -------------------
print "<br><br><br><br>"; 	//---------------  vse dir v nyneshey dir  -----------------

print "<form action=$PHP_SELF method=post name=allcategories>
       <input type=hidden name=deleteme value=delete>";
$result=mysql_query("select * from category order by CategoryName") or die (mysql_error(). " : ".mysql_errno());
print "<table border=0  cellpadding=3 cellspacing=0 width=300>";

       print "<tr><td>Remove</td><td>Name</td></tr><tr>";

while($row=mysql_fetch_object($result))
{
       print "<td><input type=checkbox name=cremove[] value=$row->ID></td> ";
       print "<td><a href=\"javascript:showuserdata('one_category.php','$row->ID')\" title=\"Show about $row->CategoryName\">$row->CategoryName</a></td>";
       print "</tr>";
}
print "</table>";
print "<br><input type=reset name=reset value='Reset'> ";
print "<input type=submit value=Delete onclick='return del()'></form>";
echo "Category will not be deleted if it contains products";
//------------------ remove --------------------------

?>
<br>
<hr>
<form action=<?=$PHP_SELF?> method=post>
<input type="hidden" name=insertme><BR>
Insert new category <input type=text name=namedirNew>
<input type=Submit name=submit value=Submit>
</form>