<? session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
if($submit)
{
 $code=substr($brname,0,3).substr(time(),7,3); 
 $code=str_replace(' ','-',$code);
  $re=mysqli_query($db,"select * from br_master where brname='$brname' and brcode='$brcode'");
  if(mysqli_num_rows($re)>0)
  {
    print "<script> alert('Duplicate entry'); history.back(1); </script>";
    return;
  }

 $sql="insert into br_master values('$code','$brname','$addr1','$addr2','$city','$dist', '$stat','$pin','$phone1','$phone2','$email','$web','$brcode','$brpass','$tin','$cst','$secbrpass')";
 mysqli_query($db,$sql)or die(mysqli_error());

 $sql="insert into ledger values('$brname','Branches','0','Dr','$code','$brcode')";
 mysqli_query($db,$sql)or die(mysqli_error());
 
}
?>
<script>
function valid()
{
  submitOK="true";

 if(document.f1.brname.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Branch name');
 }

  if(submitOK=="true")
   return true;
  else
   return false;
}
</script>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<CENTER>
<BR>
<form name='f1' action='<? print $PHP_SELF?>' method='Post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr><td align=center>
<table>
<tr><td colspan=2 align=center>BRANCH MASTER</TD></TR>
<tr style={background:yellow}><td><font color=red>Branch Name <BR>
 <FONT SIZE=1 color=brown>(Non editable)</font> </td>
<td><input type=text name='brname' size=40 onKeyDown=chkey(this); style={color:red} title='As it will create A/c head, it cannot be edited'></td></tr>
<tr><td>Address1 </td><td><input type=text name='addr1' size=40></td></tr>
<tr><td>Address2 </td><td><input type=text name='addr2' size=40></td></tr>
<tr><td>City </td><td><input type=text name='city' size=40></td></tr>
<tr><td>District </td><td><input type=text name='dist' size=40></td></tr>
<tr><td>State </td><td><input type=text name='stat' size=40 value='KERALA'></td></tr>
<tr><td>PinCode </td><td><input type=text name='pin' size=40></td></tr>
<tr><td>Tin No </td><td><input type=text name='tin' size=40></td></tr>
<tr><td>CST No </td><td><input type=text name='cst' size=40></td></tr>
<tr><td>Phone1 </td><td><input type=text name='phone1' size=40></td></tr>
<tr><td>Phone2 </td><td><input type=text name='phone2' size=40></td></tr>
<tr><td>Email_id </td><td><input type=text name='email' size=40></td></tr>
<tr><td>Web</td><td><input type=text name='web' size=40></td></tr>
<tr><td>Password</td><td><input type=password name='brpass' size=10></td></tr>
<tr><td>Sec.Password</td><td><input type=password name='secbrpass' size=10></td></tr>
<TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT name='submit' VALUE='  SAVE  '></TD></TR>

</table>
</td></tr>
</table>
</form>
<br>
<?
$result=mysqli_query($db,"select * from br_master where brcode='$brcode' order by brname")or die(mysqli_error());
print "<table cellspacing=1 cellpadding=1 border=1>";
print "<th> No.<th>Name <th>Addr1 <th>Addr 2 <th>City <th>Dist. <th>State <th>Pin <th>Phone1 <th>Phone 2";
print " <th>Email <th>Web <th>Edit";
$i=1;
while($row=mysqli_fetch_row($result))
{
 print "<tr><td>".$i++."</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>";
 print "<td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td>";
 print "<td>$row[9]</td><td>$row[10]</td><td>$row[11]</td>";
 print "<td><a href='editbranch.php?code=$row[0]'> <img src='../img/signit.gif' border=0></a> </td> </tr>";
}
?>
</table>
<script>
function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }

}
</script>
