<?php 
	include("includes/config.php");
	include("includes/func.php");
	
	if (($_SESSION['id'] == ""))
	{
		redirect("login");
	}
	
	include("templates/header.php");

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
?>
<script type="text/javascript">
$(document).ready(function(e) {

		$("#category").submit(function(){
		
					$('#First_Name_error').text('');

					
					var first_name		=	$('#desc').val();
					
					var email_reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;					

					if(first_name == "")
					{
						$('#First_Name_error').text(' *Please Enter Category Name.');
						$('#First_Name_error').css({color: "red", "font-size": "10px"});
						return false;
					}

					return true;		
		});	
});

function cancel_check()
{
		window.location.href='cat_edit.php'
}
function delete_check(del)
{
	if(confirm('Are you sure to want to delete!'))
	{
		window.location.href='cat_edit.php?u_idd='+del;
	}
	else{
		return false;
	}
}
</script>

<div id="mainPanel">
  <?php
  if(isset($_GET['update']))
  {
	  echo '<h2>'.$Get_XML_Responce->readXmlArrtibut('content.xml', 'Category').'</h2>';

		if(isset($_GET['u_id']))
		{
			$id = $_GET['u_id'];
		}
		
		if(isset($_POST['update']))
		{
			$desc = $_POST['desc'];
			$sql = "UPDATE `category` SET `desc`='$desc' where `id`=$id";
			mysql_query($sql);
			$_SESSION['msg'] = "Category has been updated Successfully.";
			redirect("cat_edit.php");
		}
		
		if(isset($_GET['u_id']))
		{
			$id= $_GET['u_id'];
			$sql = "SELECT * from category where id= $id";
			$results = mysql_query($sql);
			$row = mysql_fetch_array($results);
			echo '<form action="" method="post" id="category">';
			echo '<table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">';
			echo '<tr>';
				echo '<td nowrap width="34%" align="right">Name:<span style="color:red">*</span></td>';
				echo '<td width="66%" nowrap><input type="text" name="desc" id="desc" class="textfield1" value='.$row["desc"].'><span id="First_Name_error"></span></td>';
			echo '</tr>';
			echo '<tr>';
			echo '<td colspan="2" style="text-align:center"><input type="button" value="Cancel" class="form_button" onclick="cancel_check();"  ><input class="form_button" type="submit" name="update" id="update" value="Update"/></td>';
			echo '</tr>';
			echo '</table>';
			echo '</form>';
		}


  	}else if(isset($_GET['add'])){
		
	  	echo '<h2>Category</h2>';
		
		if(isset($_POST['submit']))
		{
				$desc = $_POST['desc'];
				$sql = "Insert into category(`desc`) values('$desc')";
				$results = mysql_query($sql);
				$_SESSION['msg'] = "Category has been added Successfully.";
				redirect("cat_edit.php");
		}
	?>
  
  			<form name="category" id="category" action="" method="post">
    			<table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">
      				<tr>
        				<td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_category.xml', 'name')?>:<span style="color:red">*</span></td>
        				<td width="66%" nowrap>&nbsp;
          					<input type="text" name="desc"  class="textfield1" id="desc"/><span id="First_Name_error"></span>
                         </td>
      				</tr>
      				
                    <tr>
        				<td width="66%" nowrap style="text-align:center" colspan="2">
                        	<input type="button" value="Cancel" class="form_button" onclick="cancel_check();"  >&nbsp;
          					<input class="form_button" type="submit" name="submit" id="submit" value="Save"/>
                    	</td>
      				</tr>
    			</table>
  			</form> 
  <?php
  }else{
	  echo '<h2>'.$Get_XML_Responce->readXmlArrtibut('content.xml', 'cateogrymg').'</h2>';
	  echo '<a href="?add=add_cat" class="form_button">'.$Get_XML_Responce->readXmlArrtibut('xml_category.xml', 'add_new_category').'</a>';
	  echo '<div style="margin-bottom:20px;"></div>';
  ?>
   
   <div style="text-align:left; padding-bottom:5px; color:green;">
    	<?php
        if ($_SESSION['msg'] != '') { 
			    print $_SESSION['msg'];
                $_SESSION['msg'] = '';
        }
		?>
    </div>
  
  <table width="100%" id="tabling1" cellpadding="0" cellspacing="0">
    <thead>
      <tr>
        <td class="col1" width="4%"><span class="blue">#</span>
          </th>
        <td class="col1" width="80%"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_category.xml', 'name')?></span>
          </th>
        <td class="col1" width="8%" style="text-align:center"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_category.xml', 'action')?></span>
          </th>
      </tr>
    </thead>
    <tbody>
      <?php
		if(isset($_GET['u_idd']))
		{
			$id = $_GET['u_idd'];
			$sql = "UPDATE `category` SET `delete`='1' where `id`=$id";
			mysql_query($sql);
			$_SESSION['msg'] = "Category has been deleted Successfully.";
		}

			$sql = "Select * from category WHERE `delete` != '1' Order By `desc` ASC";
			$results = mysql_query($sql);
			$count = 0;
			while($rows = mysql_fetch_array($results))
			{
				$count++;
				echo '<tr>';
				echo '<td class="col1">'.$count.'</td>';
				echo '<td class="col1">'.$rows['desc'].'</td>';
				echo '<td class="col1" style="text-align:center"><a class="update_back" href="?u_id='.$rows['id'].'&update=update" ><img src="images/edit.png" alt="Edit" border="0"></a><a class="update_back" href="#" onclick="delete_check('.$rows['id'].');"><img src="images/delete.png" alt="Delete" /></a></td>';
				echo '</tr>';
			}
  }
?>
    </tbody>
  </table>
</div>
<!-- Grid ends -->

</div>
<!-- Maindata box ends -->
<?php include("templates/footer.php"); ?>