<?
	include("head.php");
	include("../include/db.inc.php");
	include("../include/pager.php");		
	require_once("../include/bit_functions.inc.php");
	$db1=new db;
    $db1->open();mysql_query("SET NAMES 'utf8'");
if($_SESSION['uname']=='' || $_SESSION['pwd']=='')
		{
			header("location:login.php");
		}
		
if(isset($_POST['submit']))
{
		
		
		$cat1=$_POST['category1'];
		$cat2=$_POST['category2'];
		$cat3=$_POST['category3'];
		$cat4=$_POST['category4'];
		$cat5=$_POST['category5'];
				
		$cname=$_POST['cat_name'];
		
		
			
		if($cat5 > 0)
		$top = $cat5;
		else if($cat4 > 0)
		$top = $cat4;
		else if($cat3 > 0)
		$top = $cat3;
		else if($cat2 > 0)
		$top = $cat2;
		else if($cat1 > 0)
		$top = $cat1;
		else
		$top=0;
			if($_FILES['image']['tmp_name']!='' && is_uploaded_file($_FILES['image']['tmp_name']))
							{
								copy($_FILES['image']['tmp_name'],"../images/category/".$_FILES['image']['name']);
								$image=$_FILES['image']['name'];
							
				}
				
				
		$sql_chk="select id from add_category where name='$cname' and top='$top'";
		if(mysql_num_rows(mysql_query($sql_chk)) == 0)
		{
		mysql_query("insert into add_category set name='$cname',top='$top', image='$image'");
		header("location:addcategory.php?msg=added");
		}
		else
		{
		header("location:addcategory.php?msg=notadded");
		}		
				
}

if(!empty($_GET['dele']))
{
$id = $_GET['dele'];
mysql_query("delete from add_category where id='$id'") or die(mysql_error());
header("location:addcategory.php");
//exit();
}
if(!empty($_GET['edit']))
{
$id = $_GET['edit'];
$ids = $_GET['edit1'];
$query = ("select id, name,image from add_category where id='$id'") or die(mysql_error());
if (!($res=@mysql_query($query))) {error(mysql_error(),__LINE__,__FILE__);}
list($id,$name,$image) = mysql_fetch_array($res);
$edit = 1;
$text = "Edit";
}
else
{
$edit = 0;
$text = "Add";
}
if(!isset($_GET['edit'])) {
$id = $_GET['edit'];

}

	

?>
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Money Auctions | Admin Controls</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function validate()
{
    /*if(document.form1.category1.value=='0')
	{
		 alert("Select Your Main Category");
		 document.form1.category1.focus();
		return false;

	} */
}
</script>
<script language="javascript">
<?
for($a=1;$a<5;$a++)
{
$b=$a+1;
?>
function populate<?=$a?>(catid, form1)
{	
	for(x=form1['category<?=$b?>'].length; x>=0; x=x-1)
	{
		form1['category<?=$b?>'][x]=null;
	} 
	
		<?php 
		$sql=mysql_query("SELECT * FROM add_category");
		while($res=mysql_fetch_object($sql))
		{
			$c_id = $res->id;
			
		?>
		
		if(catid == '<?php echo $c_id; ?>')
			{
						
				<?php 
					$subsql=mysql_query("SELECT * FROM add_category WHERE top='$c_id' and top > 0");
				?>
				form1['category<?=$b?>'][form1['category<?=$b?>'].length]=new Option("--Select--", "0", "", false);
				<?php
				while($subres=mysql_fetch_object($subsql))
					{ 
						$s_id = $subres->id;
						$s_text = $subres->name;
				?>
						form1['category<?=$b?>'][form1['category<?=$b?>'].length]=new Option("<? echo $s_text; ?>", "<?php echo $s_id; ?>", "", false);
						
			<?php	} ?>
			}
<?php	} ?>

}
<? }?>

</script>

</head>
<body>
<div class="admin_wrapper">
<div class="logo_show"><? include("minorlogout.php");?></div>
<div style="float:left; width:200px">
<div class="topg_links_show"><? include("left.php");	?></div>
<div class="top_links_show"><? include("belowleft1.php");	?></div></div>
<div style="float:left; width:800px">
<div class="maximum"><br />
<br />
          

        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="bodytxt">
          <tr>
            <td width="33" valign="top"><img src="img/03.gif" width="33" height="23"></td>
            <td background="img/bg01.gif"><strong>ADDCATEGORY</strong></td>
            <td width="35"><img src="img/04.gif" width="35" height="23"></td>
          </tr>
        </table>
        <table cellpadding="0" cellspacing="0" width="100%" align="center" >
          <tr>
            <td width="22" valign="top" background="img/tbl-left04.gif"><img src="img/tbl-left04.gif" width="22" height="13"></td>
            <td valign="top" >
            <form action="" method="post" name="form1" enctype="multipart/form-data" onsubmit="return validate();">
                <table width="90%" align="center" border="0" cellspacing="0" cellpadding="2">
                  <tr>
                    <td colspan="2" align="center"><h3>
                        <? if($_GET['msg']=='added')
						   echo "Category added successfully.";
						   else if($_GET['msg']=='notadded')
						   echo "Category exists.";
						?>
                      </h3></td>
                  </tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold">CATEGORIES</div></th>
                    <td width="71%" style="padding-left:20px">
                    <select name="category1"   id="category1" onChange="populate1(this.options[this.selectedIndex].value, document.form1);" style="width:250px" />
                      <option value="0">--Select--</option>
                      <? $sql="select * from add_category where top='0' order by name";
					$res=mysql_query($sql);
					while($row=mysql_fetch_array($res))
						{?>
                      <option value="<?php echo $row['id'];?>" <? if($row['id']==$_GET['edit']) echo "selected"; ?> > <?php echo $row['name'];?> </option>
                      <?
						}?>
                      </select>
                    </td>
                  </tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold" >SUB CATEGORIE 1</div></th>
                    <td width="71%" style="padding-left:20px">
                    <select name="category2"  style="width:250px" id="category2"  onChange="populate2(this.options[this.selectedIndex].value, document.form1);"/>
                    <option value="0">--Select--</option>                     
                    </select>
                    </td>
                  </tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold">SUB CATEGORIE 2</div></th>
                    <td width="71%" style="padding-left:20px">
                     <select name="category3"  style="width:250px" id="category3"  onChange="populate3(this.options[this.selectedIndex].value, document.form1);"/>
                     <option value="0">--Select--</option>                    
                      </select>
                    </td>
                  </tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold">SUB CATEGORIE 3</div></th>
                    <td width="71%" style="padding-left:20px">
                    <select name="category4"  style="width:250px" id="category4"  onChange="populate4(this.options[this.selectedIndex].value, document.form1);"/>
                    <option value="0">--Select--</option>                    
                    </select>
                    </td>
                  </tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold">SUB CATEGORIE 4</div></th>
                    <td width="71%" style="padding-left:20px">
                     <select name="category5"  style="width:250px" id="category5"  />
                    <option value="0">--Select--</option>
                      </select>
                    </td>
                  </tr>
                 
                  <tr><td colspan="2">&nbsp;</td></tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold">Category Name</div></th>
                    <td width="71%" style="padding-left:20px">
                    <input name="cat_name" type="text" class="whiteselect"  />
                     
                    </td>
                  </tr>
                  
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold">Category Image</div></th>
                    <td width="71%" style="padding-left:20px"><input name="image" type="file" class="whiteselect" id="image"  />
                     
                    </td>
                  </tr>
                  <tr><td colspan="2">&nbsp;</td></tr>
                  <tr>
                    <th width="29%" scope="row" align="center"><div align="right" class="normaltextblackbold"></div></th>
                    <td width="71%" style="padding-left:20px"><input name="submit" type="submit" class="inputsubmit"   value="Add Category" />
                    </td>
                  </tr>
                </table>
              </form>
              <br />
             
            </td>
            <td width="19" valign="top" background="img/tbl-right04.gif"><img src="img/tbl-right04.gif" width="19" height="9"></td>
          </tr>
        </table>
        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="bodytxt">
          <tr>
            <td width="33"><img src="img/tbl-bot01.gif" width="33" height="12"></td>
            <td width="95%" align="right" background="img/tbl-bg.gif"></td>
            <td width="33"><img src="img/tbl-right05.gif" width="35" height="12"></td>
          </tr>
        </table>
</div>


      
            <br />
<br />

</div></div>
<div class="bottom_links_show"><? include("footer.php");	?></div>

</html>
</div>
</body>
</html>
