<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);

if(isset($_GET["mode"])=="true"){
    $addflag = 1;
        $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM itemdesc LIMIT 0,1 ") or die(mysqli_error($conn));
    if (mysqli_num_rows($gen1) > 0) {
        $vvouch = mysqli_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else{
        $qvouch = '51';
    }
    
}
else{
    $addflag= 0;
    $qvouch = mysqli_real_escape_string($conn, $_GET["vouchno"]);
}

if(isset($_POST["save"])){

    
    $a = 1;

    $itemdesc =  $_POST["itemdesc"];

    if ($itemdesc == " ") {
        browse("Item Description Cannot Be Empty");
        $a = 0;
        if ($addflag == 1) {
            header("refresh: 1; itemdesc.php?mode=true");
        }else{
            header("refresh: 1; itemdesc.php?vouchno=$_GET[vouchno]");
        }
    }

    if ($a == 1) {
        $result = mysqli_query($conn, "SELECT * FROM `itemdesc` WHERE `name`='" . $itemdesc . "' AND code != '" . $qvouch . "'");
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Item Description Already Exist");
            if ($addflag == 1) {
                header("refresh: 1; itemdesc.php?mode=true");
            }else{
                header("refresh: 1; itemdesc.php?vouchno=$_GET[vouchno]");
            }
        }
    }
    
    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `itemdesc`(code, `name`, `username`)
                                      VALUES('$qvouch', '$itemdesc', '$_SESSION[username]')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; itemdesc.php?mode=true");
        } else {
            $mvouch = $_POST["vouch"];
            mysqli_query($conn, "UPDATE `itemdesc` SET `name`='$itemdesc', `username`='$_SESSION[username]' where code = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; itemdescmain.php");
        }
    }
}

if($addflag==0){
        $getvouch = $_GET["vouchno"];
        $result = mysqli_query($conn, "SELECT * FROM itemdesc WHERE code='$getvouch'")or die(mysqli_error($conn));
        $get = mysqli_fetch_array($result);
}
?>

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>Item Group</title>
	
	<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
	<link rel="stylesheet" href="alight/reset.css" />
	<link rel="stylesheet" href="light/general-light.css" />
        <link rel="stylesheet" href="css/chosen.css" />
	
	<!--[if lt IE 9]>
	    	<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
	    	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	 <style>
    
  </style>

</head>
<body>

	
	<!--<center><font size="5px" color="#6B6A68"><strong>Customer - Add</strong></font></center>  -->
	<form action="" class="general" align="left" name="itemdesc" method="POST">
               <input type="hidden" name="vouch" value="<?php echo ($addflag == 0) ? $get['code'] : " "; ?>">
                <table border="0px" width="100%">
                    
                    <tr>
                        <td><br/></td>
                    </tr>
                <tr>
                    <td width="200px" align="right"><label for=""><font color="#009900" size="3px">Item Description</font><span></span></label></td>
                    <td align="left"><input type="text" size="40" name="itemdesc" value="<?php echo ($addflag == 0) ? $get['name'] : " "; ?>" class="form-input-add" required autofocus /></td>
                </tr>
                <tr>
                    <td><br/></td>
                </tr>
                <tr>
                    <td></td><td>
                        <input class="cmdsave" type="submit" name="save" value="Save" />&nbsp; <input class="cmdexit" type="button" onClick="window.location.href='itemdescmain.php'"value="Exit" />                    <td>
                </tr>
            </table>
                
	</form>
	
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
	<script>
		$(function(){
			if( $.browser.msie && $.browser.version < 9 ) {
				$('html').addClass('ie');
				
				$('div.option-group').each(function(){
					var $this = $(this);
					
					$this.find('label').append('<span class="before"></span> <span class="after"></span>');
					
					// radio button styling
					if( $this.hasClass('radio') ) {
						$this.delegate('input[type="radio"]', 'click', function(){
							if( this.checked ) {
								$(this).siblings('label').removeClass('checked').end().next('label').addClass('checked');
							}
							else {
								$(this).next('label').removeClass('checked');
							}
						});
					}
					
					// checkbox styling
					else if( $this.hasClass('check') ) {
						$this.delegate('input[type="checkbox"]', 'click', function(){
							if( this.checked ) {
								$(this).next('label').addClass('checked');
							}
							else {
								$(this).next('label').removeClass('checked');
							}
						});
					}
				});
				
			}
			
			
			// select box styling
			if( $.browser.msie && $.browser.version <= 9 ) {
				$('html').addClass('ie9');
				
				$('form.general')
					.find('select')
					.css({ 'opacity': '0', 'position': 'relative', 'z-index': '10' })
					.after('<span class="selectTop"/>')
					.change(function(){
						$(this).next().text( $('option:selected', this).text() );
					})
					.trigger('change');
			}
			
			// add 'invalid' class when HTML5 form valiation fails
			if( !$.browser.firefox ) {
				$('form.general').each(function(){
					$(this).find('input.form-input').bind('invalid', function(){
						$(this).addClass('invalid');
					});
				});
			}
		});
	</script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
  <script src="js/chosen.jquery.js" type="text/javascript"></script>
  <script type="text/javascript"> $(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({allow_single_deselect:true}); </script>

</body>
</html>