<?
session_start();

$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
print $brcode;

if(strlen($user)<=0)
{
 print "<script> alert('Please login again'); parent.document.location='ind.php'; </script>";
 return;
}
print "User: $user <br> ";
 include("../opendb.php");
if(isset($_POST['submit']))
{
$username=$_REQUEST['username'];

 $code=substr($username,0,3).substr(time(),7,3); 
 $code=str_replace(' ','-',$code);
 $re=mysqli_query($db,"select * from bruser where username='$username'")or die(mysqli_error($db));
 if(mysqli_num_rows($re)>0)
 {
    print "<script> alert('User already Exists') </script>";
$sql="delete from bruser where username='$username'";
 mysqli_query($db,$sql) or die(mysqli_error($db));

     }
 $snre= mysqli_query($db,"select max(sno) from bruser")or die(mysqli_error($db));
 $snrow=mysqli_fetch_row($snre);
 if($snrow[0]>0)
 {
   $sno=($snrow[0]+1);
  }
  else 
    $sno=1;
 print $sno;

 //$sql="insert into bruser values('$brcode' ,'$username', '$level', '$utype','$dept', '$desig', '$addr', '$ph', '$pswd1','$sno','Y')";
 //mysqli_query($db,$sql) or die(mysqli_error($db));

 //$e=mysqli_query($db,"select brstate from branch where brcode='$brcode'")or die(mysqli_error($db));
 //$es=mysqli_fetch_row($e);




$sq="select brcode from branch ";
$re=mysqli_query($db,$sq);

$df=$_REQUEST['df'];
$mf=$_REQUEST['mf'];
$yf=$_REQUEST['yf'];
$level=$_REQUEST['level'];
$utype=$_REQUEST['utype'];
$dept=$_REQUEST['dept'];
$desig=$_REQUEST['desig'];
$addr=$_REQUEST['addr'];
$ph=$_REQUEST['ph'];
$pswd1=$_REQUEST['pswd1'];
$dt=$yf.'-'.$mf.'-'.$df;
$username=$_REQUEST['username'];


while($res=mysqli_fetch_array($re))
{

$snre= mysqli_query($db,"select max(sno) from bruser")or die(mysqli_error($db));
 $snrow=mysqli_fetch_row($snre);
 if($snrow[0]>0)
 {
   $sno=($snrow[0]+1);
  }
  else 
{
    $sno=1;
}

print $sql="insert into bruser values('$res[brcode]' ,'$username', '$level', '$utype','$dept', '$desig', '$addr', '$ph', '$pswd1','$sno','Y','$dt','0','')";
 mysqli_query($db,$sql) or die(mysqli_error($db));
}


 print "User has been created <br>";
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />

<script>
function validate()
{
var p1=document.f1.pswd1.value
var p2=document.f1.pswd2.value

 submit="true";
 if(document.f1.username.value.length<3)
  {submit="false"; alert('Invalid User Name');}

 if(document.f1.username.value=="admin")
  {submit="false"; alert('admin is reserved');}

 if(document.f1.pswd1.value.length<=0)
  {submit="false"; alert('Invalid Password');}

 if(document.f1.pswd2.value.length<=0)
  {submit="false"; alert('Invalid Password2');}
 if(p1!=p2)
  {submit="false"; alert('Passwords does not match');}

 if(document.f1.desig.selectedIndex<1)
  {submit="false"; alert('Invalid Designation');}

 if(submit=="false")
  return false;
}
</script>



<body bgcolor=fcfff1>
<font color=green size=4> <b>User Creation</font>
<hr bgcolor=red>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return validate()">

<table border=0  cellspacing=2 cellpadding=5>
<tr><td>Branch Name</td><td><select name='brcode'>
<?
$re=mysqli_query($db,"select brcode,brname from branch order by brname")or die(mysqli_error($db));
while($row=mysqli_fetch_row($re))
{
 print "<option value='$row[0]'>$row[1]</option>";
}
?>
</select>
</td></tr>
<tr><td>User Name</td><td><input type=text name='username' size=40></td></tr>
<tr><td>User Level</td><td><select name='level'>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
User Type <select name='utype'>
<option>Normal User</option>
<option>Administrator</option>
</select>

</td></tr>
<tr><td>Admin to Dept</td><td><select name='dept'>
<option>None</option>
<option>Accounts</option>
<option>Service</option>
<option>Sales</option>
<option>Spares</option>
<option>H.R</option>
</select> </td></tr>
<tr><td>Designation</td><td><select name='desig'>
<option></option>
<?
/*$re=mysqli_query($db,"select desig from desigmaster order by desig");
while($ro=mysqli_fetch_row($re))
{
 print "<option>$ro[0]</option>";
}*/
?>
<option>MD</option>
</select>
</td></tr>
<tr><td>Address</td><td><input type=text name='addr' size=40></td></tr>
<tr><td>Phone/Mobile</td><td><input type=text name='ph' size=40></td></tr>
<tr><td>Password</td><td><input type=password name='pswd1' size=40></td></tr>
<tr><td>Confirm Password</td><td><input type=password name='pswd2' size=40></td>
<td>
Date </td><td>dd<input type=text name='df' size=2 readonly value='<?print date('d',time())?>'>
	mm<input type=text name='mf' size=2 readonly value='<?print date('m',time())?>'>
	yy<input type=text name='yf' size=4 readonly value='<?print date('Y',time())?>'>
 </td></tr>
<tr><td colspan=2 align=center><input type=submit name='submit' value='      Save      '> 
<input type=button value='     Cancel    ' onClick=document.location='mainbr.htm'>
</td></tr>
</table>
</form>
