<?php 
include("includes/config.php");
include("includes/func.php");
include("classes/user.class.php");
include("classes/safeSQL.class.php");

	
if (($_SESSION['id'] == ""))
{
	redirect("login");
}

include("templates/header.php");




$user 				= new user;
$safeSQL 			= new safeSQL;

$action 			= '';
$actionToExec 		= '';
$errorMsg 			= '';
$_SESSION['msg'] 	= '';
$page_title 		= '';
$page_heading 		= '';
$mui 				= '';
$btn 				= '';
$alertcount			= '0';	

if (isset($_GET['act'])) {
	$action = $safeSQL->$_GET['act'];
}

$page_title 	= $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_New_Product');
$page_heading 	= $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_New_Product');
$mui 			= 'add';
$btn_txt		= $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Save');

if ($action == 'delete')
	{
		if(!is_numeric($safeSQL->$_GET['uid'])) {
			$_SESSION['msg'] = (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Error_occured_while_retreving_data');
			redirect("products_management.php");
			
		}
		
		$user_id = intval($safeSQL->$_GET['uid']);
		
		$res = $user->deleteProduct($user_id);
		
		if ($res === true) 
			{
				$_SESSION['msg'] = (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Product_Deleted');
				redirect("products_management.php");
				
			}
		else
			{
				$errorMsg = $res;
			}
	}
	
if ($action == 'edit')
	{
		if(!is_numeric($safeSQL->$_GET['uid'])) {
			$_SESSION['msg'] = (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Error_occured_while_retreving_data');
			redirect("products_management.php");
			
		}
		
		$user_id = intval($safeSQL->$_GET['uid']);
		
		$row = $user->getProductInfo($user_id);

		if ($row) 
			{				
				$producttitle 				= $row['producttitle'];
				$product_code 				= $row['product_code'];
				$catid 						= $row['catid'];
				$currentstocklevel 			= $row['currentstocklevel'];
				$alertcount 				= $row['alertcount'];
				$product_status_id 			= $row['product_status_id'];
			}

		$page_title 	= $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Edit_Product');
		$page_heading 	= $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Edit_Product');
		$mui 			= 'update';
		$btn_txt		= $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Edit_Update');
	}
	
	
if (isset($_POST['mui'])) {
	$actionToExec = $safeSQL->$_POST['mui'];
}

if ($actionToExec == 'add')
	{						
						
			$producttitle 				= $safeSQL->$_POST['producttitle'];
			$product_code 				= $safeSQL->$_POST['product_code'];
			$catid 						= $safeSQL->$_POST['catid'];
			$currentstocklevel 			= $safeSQL->$_POST['currentstocklevel'];
			$alertcount 				= $safeSQL->$_POST['alertcount'];
			$product_status_id 			= $safeSQL->$_POST['product_status_id'];
							
		
			if ($user->isProductCode($product_code)) {
				$errorMsg = (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Entered_Product_Code_already_exists')."<br/>";
			}
					
			if ($errorMsg == '') {
			
				$temp = array($producttitle, $product_code, $catid, $currentstocklevel, $alertcount, $product_status_id);
	
				$res = $user->addProduct($temp);
	
				if ($res === true) 
					{
						$_SESSION['msg'] = (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Product_Added');
						redirect("products_management.php");
						
					}
				else
					{
						$errorMsg = $res;
					}
			}	 
			
	}

if ($actionToExec == 'update')
	{
		$user_id 				= $safeSQL->$_POST['user_id'];
		$producttitle 				= $safeSQL->$_POST['producttitle'];
		$product_code 				= $safeSQL->$_POST['product_code'];
		$catid 						= $safeSQL->$_POST['catid'];
		$currentstocklevel 			= $safeSQL->$_POST['currentstocklevel'];
		$alertcount 				= $safeSQL->$_POST['alertcount'];
		$product_status_id 			= $safeSQL->$_POST['product_status_id'];
		
		
		
		$old_product_code = $user->getProductCode($user_id);
		
		if ($old_product_code != $product_code) {
			
			if ($user->isUserName($username)) {
				$errorMsg .= (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Entered_Product_Code_already_exists')."<br/>";
			}
		}


		if ($errorMsg == '') {

			$temp = array($producttitle, $product_code, $catid, $currentstocklevel, $alertcount, $product_status_id, $user_id);
			
			$res = $user->updateProduct($temp);
			
			if ($res === true) 
				{
					$_SESSION['msg'] = (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Product_Updated');
					redirect("products_management.php");
				}
			else
				{
					$errorMsg = $res;
				}
		} 
	}
		
?>
<script type="text/javascript" src="javascripts/jquery.maskedinput-1.3.js"></script>
<script type="text/javascript">

$(document).ready(function(){
		$("#currentstocklevel, #alertcount").keypress(function (e){
		  	var charCode = (e.which) ? e.which : e.keyCode;
		  	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
				return false;
		  	}
		});
 });
 
function product_validation(){
	var ptitle = document.getElementById("producttitle").value;
	var pcode = document.getElementById("product_code").value;
	var catid = document.getElementById("catid").value;
	var currentstocklevel = document.getElementById("currentstocklevel").value;
//	var alertcount = document.getElementById("alertcount").value;
//	var product_status_id = document.getElementById("product_status_id").value;
	if(ptitle == "" || ptitle == null)
	{
		document.getElementById("title_error").style.display = 'inline';
		document.getElementById("producttitle").focus();
		return false;
	}else{
		document.getElementById("title_error").style.display = 'none';
	}
	if(pcode == "" || pcode == null)
	{
		document.getElementById("pc_error").style.display = 'inline';
		document.getElementById("product_code").focus();
		return false;
	}else{
		document.getElementById("pc_error").style.display = 'none';
	}
	if(catid == "" || catid == null)
	{
		document.getElementById("sc_error").style.display = 'inline';
		document.getElementById("catid").focus();
		return false;
	}else{
		document.getElementById("sc_error").style.display = 'none';
	}
	if(currentstocklevel == "" || currentstocklevel == null || isNaN(currentstocklevel))
	{
		document.getElementById("csl_error").style.display = 'inline';
		document.getElementById("currentstocklevel").focus();
		return false;
	}else{
		document.getElementById("csl_error").style.display = 'none';
	}
//	if(alertcount == "" || alertcount == null || (parseInt(alertcount) >= parseInt(currentstocklevel)) || isNaN(alertcount))
    /* if(alertcount == "" || alertcount == null || isNaN(alertcount))
	{
		document.getElementById("ac_error").style.display = 'inline';
		document.getElementById("alertcount").focus();
		return false;
	}else{
		document.getElementById("ac_error").style.display = 'none';
	}*/
}
</script>
<?php $page_title ?>
<!-- Start main box area in contains form-->
<div id="mainPanel">
<!-- Start form -->
	<h2><?php echo $page_heading ?></h2>
    <br clear="all" />
<div id="form">
	<form id="frmUser" name="frmUser" action="" method="post" onsubmit="return product_validation();">
    	<input type="hidden" name="mui" id="mui" value="<?php echo $mui ?>" />
		<input type="hidden" name="user_id" id="user_id" value="<?php echo $user_id ?>" />
    <table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">
        
		<?php 
            if ($_SESSION['msg'] != '') { 
           		echo "<tr><td colspan='2' nowrap align='center' id='tableheader'><span class='text1Col'>" ;   
			    print $_SESSION['msg'];
                $_SESSION['msg'] = '';
				echo "</span></td>";
            }
			
			/*Full texts 	id 	producttitle 	product 	catid 	currentstocklevel 	alertcount 	product_status_id 	delete */
        ?>        
        

        <?php if ($errorMsg != '') { ?>
        <tr>
            <td colspan="2"><span style="color:#ff0000; line-height:20px"><?php echo $errorMsg; ?></span></td>
        </tr>
        <?php } ?>
        
        <tr> 
            <td nowrap width="34%" align="right"> <?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Edit_Title')?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="producttitle" type="text" class="textfield1" id="producttitle" value="<?php echo $producttitle; ?>" size="30" >&nbsp;&nbsp;<span id="title_error" style="display:none; color:red;"><?php echo (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Empty_Title_Error')?></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"> <?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Edit_Product_Code')?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="product_code" type="text" class="textfield1" id="product_code" value="<?php echo $product_code; ?>" size="30" >&nbsp;&nbsp;<span id="pc_error" style="display:none; color:red;"><?php echo (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Empty_Product_Code_Error')?></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"> <?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Edit_Category')?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<select name="catid" id="catid" class="textfield1">
                		<option value="">Select Category</option>
                        <?php
							$Sql_Cat	=	"SELECT * FROM `category` where `delete` != '1'";
							$Exe_Sql_Cat	=	mysql_query($Sql_Cat);
							while($Cat_Row	=	mysql_fetch_array($Exe_Sql_Cat))
							{
								$Sel	=	"";
								if($Cat_Row['id'] == $catid)
								{
									$Sel	=	"selected";	
								}
								echo "<option value='{$Cat_Row['id']}' $Sel>{$Cat_Row['desc']}</option>";	
							}
						?>
                	  </select>
                      &nbsp;&nbsp;<span id="sc_error" style="display:none; color:red;"><?php echo (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Empty_Category_Error')?></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"> <?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Edit_Current_Stock_Level')?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="currentstocklevel" type="text" class="textfield1" id="currentstocklevel" value="<?php echo $currentstocklevel ?>" size="30" >&nbsp;&nbsp;<span id="csl_error" style="display:none; color:red;"><?php echo (string)$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Empty_Current_Stock_Level')?></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"> <?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Edit_Alert_Count')?>:</td>
            <td width="66%" nowrap>
                &nbsp;<input name="alertcount" type="text" class="textfield1" id="alertcount" value="<?php echo $alertcount; ?>" size="30" >
            </td>
        </tr>
       
        <tr> 
            <td colspan="2" nowrap style="text-align:center">
                <input type="button" value="<?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_Edit_Add_Cancel')?>" class="form_button" onClick="cancelAct('products_management')" > 
                &nbsp; 
                <input type="submit" name="submitButton" class="form_button" id="submitButton" value="<?php echo $btn_txt ?>" >
            </td>
        </tr>
    </table>
		</form>
		
</div><!-- end form -->

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