<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
if(isset($_POST['Submit']))
{
$check=DB_query("select * from machine_group_master where machine_gp_location='".$_REQUEST['machine_location']."' ",$db);
$num=DB_num_rows($check);
if($num<=0)
{		
 $insert ="insert into machine_group_master set branch_code='".$_REQUEST['site']."',machine_gp_location='".$_REQUEST['machine_location']."',CompanyId='".$_SESSION['ACGCompanyId']."'";

$query=DB_query($insert,$db);
}
}
if(isset($_POST['Update']))
{
//$check=DB_query("select * from machine_group_master where machine_gp_location='".$_REQUEST['machine_location']."'",$db);
//$num=DB_num_rows($check);
//if($num<=0)
//{			
$update="update machine_group_master set branch_code='".$_REQUEST['site']."',machine_gp_location='".$_REQUEST['machine_location']."' where id='".$_REQUEST['Id']."'";
$query=DB_query($update,$db);	
//}	
$s->pageLocation("index.php?route=ToolPlants/MaterialCategoryDefinition&daction=Updated".$rurl);	
}

if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['leadid']);$i++){
	$del="delete from machine_group_master where id='".$_REQUEST['leadid'][$i]."'";
	$query=DB_query($del,$db);
	}
	$s->pageLocation("index.php?route=ToolPlants/MaterialCategoryDefinition&daction=Updated".$rurl);	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['action']==''){?>
<h1>Asset Group Center</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
 	<td width="13%" align="right">Branch</td>
  	<td width="34%"><select name="site"  id="site">
	  	<option value="0">-----Select-----</option>
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
            while($site_row=DB_fetch_array($site_sql)){
            ?>
            <option value="<?php echo $site_row['loccode'];?>"><?php echo $site_row['locationname'];?></option>
            <?php } ?>		
		</option></select></td>
 	<td width="20%" align="right">Asset Group Center Location</td>	
  	<td><input type="text" name="machine_location"  id="machine_location" ></td>
</tr>
<tr class="row1">
	<td colspan="4" align="center"><input type="submit" value="Save" name="Submit" onclick="return valid();" class="search" /></td>
</tr>	  
</table>

</form>
<?php }
if($_REQUEST['action']=='edit' and $_REQUEST['Id']!='')
{
$sql="select * from machine_group_master where id='".$_REQUEST['Id']."'";
$query=DB_query($sql,$db);
$row1=Db_fetch_array($query,$db);
 ?>
<h1>Edit Asset Group Center</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
  	<td width="13%" align="right">Branch</td>
  	<td width="35%"><select name="site" id="site" >
	  	<option value="0">-----Select-----</option>
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
            while($site_row=DB_fetch_array($site_sql)){
            	$k='';
				if($site_row['loccode']==$row1['branch_code'])
				{
				$k="selected";	
				}				
            ?>
            <option value="<?php echo $site_row['loccode'];?>"<?php echo $k; ?>><?php echo $site_row['locationname'];?></option>
            <?php } ?>		
		</option></select></td>
	<td width="20%" align="right">Asset Group Center Location</td>
	<td><input type="text" name="machine_location"  id="machine_location" value="<?php echo $row1['machine_gp_location']; ?>"></td>
</tr>
<tr class="row1">
	<td colspan="4" align="center"><input type="submit" value="Update" name="Update" onclick="return valid();" class="search" /></td>
</tr>      
</table>

</form><?php } ?>

<form name="myform" method="post">
<p align="left">
<input type="submit" name="Delete" value="Delete" class="search" onclick="return confirm('Are you sure you want to delete?')" />
</p>
<div class="overpan">
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
	<th width="3%"><input name="leadid" type="checkbox" id="leadid" onclick="LeadData()" /></th>
	<th>Branch</th>
	<th width="16%">Asset Group Center Location</th>
	<th style="text-align:center;" width="5%">Edit</th>
</tr>
<?php $i=0;
 $sql="select * from machine_group_master where CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr>
	<td><input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>"   value="<?php  echo $row['id']?>" /></td>
	<td><?php $db1="select * from locations where loccode='".$row['branch_code']."'";
	$query1=Db_query($db1,$db);
	$row11=Db_fetch_array($query1); 
	echo $row11['locationname']; ?></td>
	<td><?php echo $row['machine_gp_location']; ?></td>
	<td style="text-align:center;"><a href="index.php?route=ToolPlants/MaterialCategoryDefinition&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&Id=<?php echo $row['id'];?>"><img src="images/icon-edit.png" width="15" height="15" title="Edit" /></a></td>
</tr>
<?php $i++;} ?>
</table>
</div>
</form>
</div></div>


<?php 
//include('includes/footer.inc');
?>

<script language="JavaScript">
function valid()
{
if(document.getElementById("site").value==0)
{
alert("Must Select Branch Name");
document.getElementById("site").focus();
return false;	
}
if(document.getElementById("machine_location").value=='')
{
alert("Must Enter Machine Location");
document.getElementById("machine_location").focus();
return false;	
}
return true;	
}	

function LeadData()
{	
	myCheckboxArray = document.myform.elements["leadid[]"];
	var t=myCheckboxArray.length;
	//alert(myCheckboxArray);
	if(document.getElementById('leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=false;	
	}
	}
}		
</script>
