 <?php $table='company_branch';
include('includes/Class_location.php');
?>
<script language="javascript" type="text/javascript" >
function fun_validation()
{

	var compid=  document.getElementById(companyid).value;
	alert(compid)
	if(compid==0)
	{
	   alert("Please select Company name");
	   return false;
	}
return true;
}
function Company_branch(str,hiddiv, url )
{
   if (str=="")
  {
  document.getElementById(hiddiv).innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(hiddiv).innerHTML=xmlhttp.responseText;
    }
  }
 xmlhttp.open("GET",url+"?q="+str,true);
xmlhttp.send();
}

</script>
<?php 
 $daction=$_REQUEST['daction'];
  $query = "SELECT COUNT(*) as num FROM modules_form";
$total_pages =DB_fetch_array(DB_query($query,$db));
  $total_pages = $total_pages['num'];
 	/* Setup vars for query. */
	$targetpage = "ControlPanel.php?route=CompanyBranch"; 	//your file name  (the name of this file)
	$limit = 10; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0



 

	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	include('paging.php');
//*************************Search********************************************************************
if($_REQUEST['Search'])
{
	  $filter=array();
      if($_REQUEST['loccode']!='')
	  $filter[]="loccode='".$_REQUEST['loccode']."'";
	  if($_REQUEST['locationname']!='')
	  $filter[]="locationname='".$_REQUEST['locationname']."'";
 		if(count($filter)>0)
		{
		   $filterval=implode(' and ',$filter );
		    $GetRecord = "Select * from $table where  $filterval LIMIT $start, $limit";
		}
		else
		{
		   $GetRecord = "Select * from $table where 1=1 LIMIT $start, $limit";
		}
		 
}
else
{
	    $GetRecord = "Select * from $table  LIMIT $start, $limit ";
}  
//**************************************************************************************************  
 

$msgErr=array();
$fstatus=true;



 //*********************************GET DATA FROM DATA BASE*****************************
if($_REQUEST['action']=='Edit'  && $_REQUEST['Id']!='' && !$_REQUEST['Save'])
{
  	$condition="id='".$_REQUEST['Id']."'";
  	$query=DB_query("select * from $table where $condition ",$db);
	$RData=DB_fetch_array($query);	
	
	 $companyid=$RData['companyid'] ; 
	$loccode=$RData['loccode'];
	$prefix=$RData['prefix'];
	$locationname=$RData['locationname']; 
	$deladd1=$RData['deladd1'];	 
	$deladd2=$RData['deladd2'];	 
	$deladd3=$RData['deladd3'];	 
	$extension=$RData['extension'];
	$phone1=$RData['phone1'];
	$phone2=$RData['phone2'];
	$country=$RData['country'];
	$state=$RData['state'];
	$fax=$RData['fax'];
	$city=$RData['city'];
	$pincode=$RData['pincode'];
	$email=$RData['email'];
	$tinno=$RData['tinno'];
	$modified_date=date('Y-m-d');
 }else{
 
   $companyid=trim($_REQUEST['companyid']); 
	$loccode=trim($_REQUEST['loccode']); 
	$locationname=trim($_REQUEST['locationname']); 
	$prefix=trim($_REQUEST['prefix']); 
	$deladd1=trim($_REQUEST['deladd1']); 
	$extension=trim($_REQUEST['extension']); 
	$deladd2=trim($_REQUEST['deladd2']); 
	$phone1=trim($_REQUEST['phone1']); 
	$deladd3=trim($_REQUEST['deladd3']); 
	$phone2=trim($_REQUEST['phone2']); 
	$country=trim($_REQUEST['country']); 
	
	$mobile=trim($_REQUEST['mobile']); 
	$state=trim($_REQUEST['state']);
	$fax=trim($_REQUEST['fax']); 
	$city=trim($_REQUEST['city']); 
	$email=trim($_REQUEST['email']); 
	$pincode=trim($_REQUEST['pincode']); 
	$tinno=trim($_REQUEST['tinno']); 
 	$modified_date=date('Y-m-d');
 }
 
if($_REQUEST['Save'] )
{
 
 	if($companyid=='0')
	{
	   $msgErr[]='Please Select CompanyId';
	   $fstatus=false;
	}
	
	if($loccode=='')
	{
	   $msgErr[]='Please Enter Branch Area';
	   $fstatus=false;
	}
	
	if($locationname=='')
	{
	   $msgErr[]='Please Enter Branch Name';
	   $fstatus=false;
	}
	
	if($prefix=='')
	{
	   $msgErr[]='Please Enter Prefix';
	   $fstatus=false;
	}
	if($email=='')
	{
	   $msgErr[]='Please Enter EmailId';
	   $fstatus=false;
	}
}
 

//**********************DELETE****************************************************
if($_REQUEST['action']=='del')
{
			$condition="id='".$_REQUEST['Id']."'";
			$s->DeleteRecord($table,$condition);
			$s->pageLocation('ControlPanel.php?route=CompanyBranch&daction=Deleted');
}

 
//*************************Update DATA FROM DATA BASE***************************************
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit' && $fstatus )
{
	$data['companyid']=$companyid;
	$data['loccode']=$loccode; 
	$data['prefix']=$prefix;
	$data['locationname']=$locationname; 
	$data['deladd1']=$deladd1;	 
	$data['deladd2']=$deladd2;	 
	$data['deladd3']=$deladd3;	 
	$data['extension']=$extension;
	$data['phone1']=$phone1;
	$data['phone2']=$phone2;
	$data['country']=$country;
	$data['state']=$state;
	$data['fax']=$fax;
	$data['city']=$city;
	$data['pincode']=$pincode;
	$data['email']=$email;
	$data['tinno']=$tinno;
	$data['modified_date']=$modified_date;


 	$cond="id='".$_REQUEST['Id']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
  $s->pageLocation('ControlPanel.php?route=CompanyBranch&daction=Updated');
}


//*************************INSERT************************************
 if($_REQUEST['Save'] && $_REQUEST['action']=='add'&& $fstatus)
  {
 	$getlastid=DB_query("select * from $table order by id desc limit 1",$db) ;
	$rid=DB_fetch_array($getlastid);
    $lastid= $rid['id']+1;

	$data['companyid']=$companyid; 
	$data['loccode']=$loccode; 
	$data['prefix']=$prefix;
	$data['locationname']=$locationname; 
	$data['deladd1']=$deladd1;	 
	$data['deladd2']=$deladd2;	 
	$data['deladd3']=$deladd3;	 
	$data['extension']=$extension;
	$data['phone1']=$phone1;
	$data['phone2']=$phone2;
	$data['country']=$country;
	$data['state']=$state;
	$data['fax']=$fax;
	$data['city']=$city;
	$data['pincode']=$pincode;
	$data['email']=$email;
	$data['tinno']=$tinno;
	$data['modified_date']=$modified_date;


	
    $insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
	$s->pageLocation('ControlPanel.php?route=CompanyBranch&daction=Inserted');
  }
?>




<link href="css/pagnation.css" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="left">
   <tr>
    <td colspan="2"><h2>Company Branch  Master </h2></td>
	<td><div align="right">
	 <?php if($_REQUEST['action']==''){	?>
	<a href="ControlPanel.php?route=CompanyBranch&action=add">AddNew</a>
	<?php } else{?>
	<A HREF="javascript:history.go(-1)"> [Go Back]</A> 
	<?php } ?> </div>
	</td>
   </tr>
   
     
   
   <?php
//**************************Display Mess********************************************     
   
     if(isset($daction))
	 { ?>
	  <tr>
    <td colspan="3" ><div align="left" class="success" >Record <?php echo $_REQUEST['daction']; ?> Successfully </div>    </td>
	   </tr>
	 <?php 
	 }
	 
	 
//**************************Error Section*********************************************     
	
	 
if(isset($msgErr)&&$msgErr!='' && count($msgErr)>0 )
{?>
    <tr>
     <td colspan="3" ><div class="error">
  <?php 
	for($kp=0;$kp<count($msgErr);$kp++)
	 {
		echo  $msgErr[$kp].'<br>';
	 }
  ?></div></td>
  </tr>
  
   <?php } 
 //**************************Error Section*********************************************     
 ?>
 
 
  <?php  if($_REQUEST['action']==''){?>	

 <tr><td colspan="3" >
 <form action="" method="post">
 	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td> Branch Code :
      <input name="loccode" type="text" value="<?php echo $_REQUEST['loccode'] ?>"></td>
    <td> Branch Name  :
      <input name="locationname" type="text" value="<?php echo $_REQUEST['locationname'] ?>"></td>
	<td><input name="Search"  value="Search" type="submit"></td>
  </tr>
</table>
</form>
 </td>
</tr>
<?php } else{ ?>





<tr><td colspan="3" >
<form action="" method="post" onsubmit="return fun_validation()">
<table width="100%" border="0">
	 <tr>
		<td width="12%"><?php echo _('Company') . ':'; ?></td>
		<td width="43%">
		<select name="companyid" onchange="Company_branch(this.value,'hidbranch','Branch.php')">
		<option value="0">--Select Company--</option>
 		<?php
		  $query=DB_query("select * from companies",$db);
		  while($row=DB_fetch_array($query))
		  {
		  
		  	$p='';
			if($row['CompanyId']==$companyid)
			{
			   $p="selected";
			}

		 ?>
		<option value="<?php echo $row['CompanyId'] ?>" <?php echo $p; ?>><?php echo $row['CompanyName'] ?></option>
		<?php } ?>
		</select>
		
		</td>
		<td width="15%">&nbsp;</td>
		<td width="30%">&nbsp;</td>
	  </tr>
	</table>
<div id='hidbranch' >	
<table width="100%" border="0">
   <tr>
    <td>
	
	
	<table width="100%" border="0">
	<?php  if($_GET['SelectedLocation']!="") { ?>
	  <tr>
		<td width="15%"><?php echo _('Branch Alise') . ':'; ?></td>
		<td width="30%"><input type="Text" name="loccode" value="<?php echo $loccode; ?>" size=15 maxlength=50 ></td>
		<td width="15%">&nbsp;</td>
		<td width="30%">&nbsp;</td>
	  </tr>
	<?php }else { ?>
	 <tr>
		<td width="15%"><?php echo _('Branch Area') . ':'; ?></td>
		<td width="30%"><input type="Text" name="loccode" value="<?php echo $loccode; ?>" size=15 maxlength=50></td>
		<td width="15%">&nbsp;</td>
		<td width="30%">&nbsp;</td>
	  </tr>
	  <?php } ?>
	  <tr>
		<td><?php echo _('Branch Name') . ':'; ?></td>
		<td><input type="Text" name="locationname" value="<?php echo $locationname; ?>" size=41 maxlength=50></td>
		<td><?php echo _('Prefix') . ':'; ?></td>
		<td><input type="Text" name="prefix" value="<?php echo $prefix; ?>" size=20 maxlength=50></td>
	  </tr>
     </table>    </td>
  </tr>
   <tr>
    <td>
	  	<table width="100%" border="0">
	  <tr>
		<td width="15%" rowspan="3" valign="top"><?php echo _('Address') . ':'; ?></td>
		<td width="30%"><input type="Text" name="deladd1" value="<?php echo $deladd1; ?>" size=41 maxlength=50></td>
		<td width="15%"><?php echo _('Extension') . ':'; ?></td>
		<td width="30%"><input type="Text" name="extension" value="<?php echo $extension; ?>" size=15 maxlength=50></td>
	  </tr>
	  <tr>
		
		<td><input type="Text" name="deladd2" value="<?php echo $deladd2; ?>" size=41 maxlength=50></td>
		<td><?php echo _('Phone1') . ':'; ?></td>
		<td><input type="Text" name="phone1" value="<?php echo $phone1; ?>" size=21 maxlength=50></td>
	  </tr>
	  <tr>
		
		<td><input type="Text" name="deladd3" value="<?php echo $deladd3; ?>" size=41 maxlength=50></td>
		<td><?php echo _('Phone2') . ':'; ?></td>
		<td><input type="Text" name="phone2" value="<?php echo $phone2; ?>" size=21 maxlength=50></td>
	  </tr>
	  <tr>
		<td><?php echo _('Country') . ':'; ?></td>
		<td><?php 	$location->Country($country);?></td>
		<td><?php echo _('Mobile') . ':'; ?></td>
		<td><input type="Text" name="mobile" value="<?php echo $mobile; ?>" size=21 maxlength=50></td>
	  </tr>
	  <tr>
		<td><?php echo _('State') . ':'; ?></td>
		<td><div id='txtstate'><?php 	$location->State($country,$state);?></div> 		</td>
		<td><?php echo _('Fax') . ':'; ?></td>
		<td><input type="Text" name="fax" value="<?php echo $fax; ?>" size=21 maxlength=50></td>
	  </tr>
	   <tr>
		<td><?php echo _('City') . ':'; ?></td>
		<td>
		<div id='txtcity'>
		<?php 	$location->City($country,$state ,$city);?>
		</div>	 </td>
		<td><?php echo _('E-mail') . ':'; ?></td>
		<td><input type="Text" name="email" value="<?php echo $email; ?>" size=20 maxlength=50></td>
	  </tr>
	  <tr>
		<td><?php echo _('Pincode') . ':'; ?></td>
		<td><input type="Text" name="pincode" value="<?php echo $pincode; ?>" size=15 maxlength=50></td>
		<td><?php echo _('Tinno.') . ':'; ?></td>
		<td><input type="Text" name="tinno" value="<?php echo $tinno; ?>"  /></td>
	  </tr>
     </table>	</td>
  </tr>
   <tr> 
        <td><div class="centre"><input type="Submit" name="Save" value="Save">
       </div></td>
        
   </tr>
 </table>
</div>
 
 </form>


</td>
</tr>
<?php } ?>

</table>
 </td>
   
  </tr>
  <tr>
    <td>
 <?php if($_REQUEST['action']==''){	?>
 	<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr> <th scope="col">Loccode </th>
    <th scope="col">Location Name  </th>
	<th scope="col">prefix</th>
	<th scope="col">extension</th>	    
	<th scope="col">Contact Details</th>	  
	<th scope="col">Email </th>	  
  	<th scope="col">Edit</th>
    <th scope="col">Delete</th>
  </tr>
 
  <tr>
   <?php 
  
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='3'>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {?>  
   <td><?php echo $RowData['loccode']; ?>	  </td>
   <td><?php echo $RowData['locationname']; ?>	  </td>
    <td><?php echo $RowData['prefix']; ?>	  </td>
   
    <td><?php echo $RowData['extension'];?>	  </td>
	  <td><?php echo $RowData['phone1'].'<br>'.$RowData['phone2'];?>	  </td>
      <td><?php echo $RowData['email'];?>	  </td>
   <td><div align="center"><a href="ControlPanel.php?route=CompanyBranch&action=Edit&Id=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><a href="ControlPanel.php?route=CompanyBranch&action=del&Id=<?php echo $RowData['id']; ?>" onClick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></div></td>	
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><?=$pagination?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
	<?php $i++;}   
   ?> 
</table>
<?php } ?>
     </td>
   </tr>
</table>
