<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}?>
<title>Kerala Khadi and Village Industries Board</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-latest.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
</script>
<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;

}
table.form{
font-weight: normal;border:1px;
} 
</style>
<body >
<table class="container"><tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px; color:black;">  &nbsp;&nbsp;Master >> Branch Master</font>
<br>

<link rel="stylesheet" href="style.css" type="text/css" />
<script>
function valid()
{ 
 subOK="false";
 x=confirm("Do you want to save?");
 if(x)
  subOK="true";
   if(subOK=="true")
     return true;
   else
     return false;
} 

function chkey(abc)
{
 yy=abc.value
 if(yy.length>35)
 { 
   alert('Name exeeds the limit');
   return;
 }

 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }
 if(yy.indexOf('"')>-1) 
  {
    alert("\" is not allowed");
    abc.value='';
  }

}
</script>
<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

</script>

<center>

<?
if(isset($_POST['submit']))
{
        $re12=mysqli_query($db,"select brgroup from branch where brcode ='$brcode'")or die(mysqli_error($db));
	$row12=mysqli_fetch_array($re12);
	$brname = $_REQUEST['brname'];
 	if(strlen($brname)<=0)
 		{
  			print "<script> alert('Invalid Branch Name'); </script>";
  			
 		}
	else
		{
 	$code=substr($brname,0,3).substr(time(),7,3); 
 	$code=str_replace(' ','-',$code);
 	$brcode=$code;
        $res=mysqli_query($db,"select * from branch where brname='$brname'");
 	if(mysqli_num_rows($res)>0)
 		{
  			print "<script> alert('Duplicate Entry'); history.back(1);</script>";
 		}
	$brman=$_REQUEST['brman'];
	$braddr=$_REQUEST['braddr'];
	$brcity=$_REQUEST['brcity'];
	$brphone=$_REQUEST['brphone'];
	$brpass1=$_REQUEST['brpass1'];
	$brtype=$_REQUEST['brtype'];
	$tin=$_REQUEST['tin'];
	$cst=$_REQUEST['cst'];
	$brstate=$_REQUEST['brstate'];
//'$code','".strtoupper($brname)."', '$brman','$braddr', '$brcity','$brphone','$brpass1','$btype','$tin','$cst','$brstate','0','0','0','0','0'
 	$sql="insert into branch (brcode,brname,brman,braddr,brcity,brphone,brpass,brtype,tin,cstno,brstate,pupass2,sapass2,jopass2,acpass2,otpass2,brgroup) values('$code','".strtoupper($brname)."', '$brman','$braddr', '$brcity','$brphone','$brpass1','$brtype','$tin','$cst','$brstate','0','0','0','0','0','$row12[0]')";
 	mysqli_query($db,$sql) or die(mysqli_error($db));

 	$led_code=$brname.substr(time(),7,3).date("s"); 
 	mysqli_query($db,"insert into ledger(led,grp,op,drcr,code,brcode) values('$brname','Branches','','Dr','$led_code','$brcode')") or die('1'.mysqli_error($db));
 	print "Branch and Ledgers have been Created";
	}
}
?>

<script>
function validate()
{
var p1=document.f1.brpass1.value;
var p2=document.f1.brpass2.value;

 submit="true";
 if(document.f1.brname.value.length<3)
  {submit="false"; alert('Invalid branch name');}

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

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

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

<body bgcolor=white>


<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return validate()">
<table cellspacing=1 cellpadding=2 class='form'>
<th colspan=2 bgcolor='steelblue'>Branch Creation</font></th>
<tr><td>Branch Name</td><td><input type=text name='brname' size=40></td></tr>
<tr><td>Branch Type </td><td><select name='brtype'>
<option>HO</option>
<option>BR</option>
</select></td></tr>

<tr><td>Branch Manager</td><td><input type=text name='brman' size=40></td></tr>
<tr><td>Address</td><td><input type=text name='braddr' size=40></td></tr>
<tr><td>City</td><td><input type=text name='brcity' size=40></td></tr>
<tr><td>State</td><td>
<select name='brstate'>
<option>Kerala</option>
<option>Karnataka</option>
</select>
</td></tr>
<tr><td>Phone</td><td><input type=password name='brphone' size=40></td></tr>

<tr><td>Tin No.</td><td><input type=tex name='tin' size=40></td></tr>
<tr><td>Cst No.</td><td><input type=text name='cst' size=40></td></tr>
<tr bgcolor='lightsteelblue'><td colspan=2 align=center><input type=submit name='submit' value='  S A V E  '> <input type=button value='   C A N C E L    ' onClick=window.location='createbr.php'></td></tr>
</table>
</form>
<?
include("listbr.php");
?>

