<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
if(isset($_POST['Submit']))
{
 $insert="insert into prd_safty set description='".$_REQUEST['description']."',site='".$_REQUEST['site']."',seq='".$_REQUEST['sequence']."',CompanyId='".$_SESSION['ACGCompanyId']."',Created_by='".$_SESSION['UserID']."',Created_date='".date('y-m-d')."'";
$query=Db_query($insert,$db);
}
if(isset($_POST['Update']))
{
$update=DB_query("update prd_safty set description='".$_REQUEST['description']."'
,site='".$_REQUEST['site']."',seq='".$_REQUEST['sequence']."',
 Modified_by='".$_SESSION['UserID']."',Modified_date='".date('y-m-d')."' where id='".$_REQUEST['Id']."'",$db);	
$s->pageLocation("index.php?route=Production/Plant/break_down_maintanance_safty".$rurl);	
}
if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['leadid']);$i++){
	$del=DB_query("delete from prd_safty where id='".$_REQUEST['leadid'][$i]."'",$db);	
	}
$s->pageLocation("index.php?route=Production/Plant/break_down_maintanance_safty".$rurl);	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['action']==''){ ?>
<h1>Break Down Safty Master</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
 <td class="tabledata1">Branch</td>	
	  <td>
	  
	   <select name="site" style="width:150px;" id="site">
	  	<option value="000">--Select---</option>
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$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 class="tabledata1">Descriptions</td>	
	  <td> <input type="text" name="description" style="width:150px;" id="description" ></td>

</tr>
<tr class="row1">
	<td class="tabledata1">Sequence</td>
	<td colspan="3"><input name="sequence" id="seq" style="width:150px;" type="text"></td>
</tr>  
<tr class="row1">
	<td colspan="4">
		<p align="center"><input type="submit" value="Save" name="Submit" onclick="return valid();" class="search"></p>
	</td>
</tr>

</table>
</form>
<?php } if($_REQUEST['action']=='edit' and $_REQUEST['Id']!=''){
 	$sql=DB_query("select * from prd_safty where id='".$_REQUEST['Id']."'",$db);
	$edit_row=DB_fetch_array($sql);
	
	?>
<h1>Edit Break Down Reason</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
  <td class="tabledata1">Branch</td>	
	  <td><select name="site" style="width:150px;">
<?php $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
            while($site_row=DB_fetch_array($site_sql)){
            $k='';
			if($site_row['loccode']==$edit_row['site']){
			$k="selected";	} ?>
         <option value="<?php echo $site_row['loccode'];?>" <?php echo $k; ?> ><?php echo $site_row['locationname'];?></option>
                      <?php } ?>		
		</option></select></td>
		<td class="tabledata1">Break Down Type</td>
	  <td><input type="text" name="description" value="<?php echo $edit_row['description']; ?>"> </td>
</tr>
<tr class="row1">
	<td class="tabledata1">Break Down Reason</td>
	<td colspan="3"><input type="text" name="sequence" value="<?php echo $edit_row['seq']; ?>"	</td>
</tr>  
<tr class="row1">
	<td colspan="4">
		<p align="center"><input type="submit" value="Update" name="Update" class="search"></p>
	</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>
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr>
<td class="tabledata"><input name="leadid" type="checkbox"   id="leadid" onclick="LeadData()"    /></td>
<td class="tabledata">Branch</td>
<td class="tabledata">Break Down Type  </td>
<td class="tabledata">Sequence </td>
</tr>
<?php $i=0;
$sql="select * from prd_safty where CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr class="row1">
<td style="white-space:nowrap;"><input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>"   value="<?php  echo $row['id']?>" />
	
	<a href="index.php?route=Production/Plant/break_down_maintanance_safty&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&Id=<?php echo $row['id'];?>"><img src="images/icon_edit.gif" width="12px"  height="12" /></a>
		
	</td>	
<td><?php	echo $location=$s->getField('locations','loccode',$row['site'],'locationname');	 ?></td>
<td><?php echo $row['description'];	 ?></td>
<td><?php echo $row['seq']; ?></td>
</tr>
<?php $i++;} ?>
</table>
</form>
</div></div>



<?php 
//include('includes/footer.inc');
?>
<script language="JavaScript">
function valid()
{
	if(document.getElementById("site").value=='000')
	{
	alert("Must Select Branch Name");
	document.getElementById("site").focus();
	return false;	
	}
	if(document.getElementById("shift_category").value=='')
	{
	alert("Please Enter Break Down Reason");
	document.getElementById("shift_category").focus();
	return false;	
	}
	if(document.getElementById("shift_category").value=='')
	{
	alert("Please Enter Break Down Reason");
	document.getElementById("shift_category").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>