<script>
window.onload = function(){
	setTimeout(function() {
    $('.success').hide();
  }, 3000);

}
</script>
<script type="text/javascript" language="javascript">
 	function getdate()
	{
		var quantity = $('#qty_').val();
		var rate = $('#rate_').val();
		var amount= quantity*rate;
		$('#amount_').attr('value',amount);
		$('#amount_').attr('readonly','true');
	
	}
function show_text_area1(str)
{
	if(str == 'select')
	{
		$('.hide1').show();
		$('.hide2').show();
	}
	if(str == 0)
	{
	//alert(str);
		//$('.hide1').attr('disabled','true');
		$('.hide1').hide();
		$('.hide2').show();
	}
	else
	{
		//$('.hide1').removeAttr('disabled');
		$('.hide2').hide();
		$('.hide1').show();
	}
}
</script>
<script type="text/javascript" language="javascript">
	function show_text_area(str)
	{
	if(str == 0)
	{
	var xmlhttp;
	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)
		{
		var data=xmlhttp.responseText.split("__");	
		document.getElementById("txthint").innerHTML=data[0];
		document.getElementById("txthint1").innerHTML=data[1];
		document.getElementById("txthint2").innerHTML=data[2];
		document.getElementById("txthint3").innerHTML=data[3];
		document.getElementById("txthint4").innerHTML=data[4];
		  }
	  }
	  xmlhttp.open("GET","get_textatea.php?site="+str,true);
	  xmlhttp.send();
	
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
	}	
	else
	{
		return false;
	}
	}
	
</script>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $page_title; ?></h1>
<?php $targetpage="index.php?route=PMS/Maintenance/MST-BOM".$rurl;
//echo $_REQUEST['id']."fggff";die;

$table='master_boq';
$PageSecurity = 15;
$action=$_REQUEST['action'];
  $daction=$_REQUEST['daction'];
$title = _('BOQ Master') ;
  $status=true;
 if($_REQUEST['Save'])
{ 
	if(empty($_REQUEST['group_title']))
	{
	   $msgErr[]='Please Enter Group Title';
	   $status=false;
	}
	if($_REQUEST['parentid']=="0")
	{
	
	$data['group_title'] = $_REQUEST['group'];
	$data['parentid'] = $_REQUEST['parentid'];
	$table = "master_boq";
	 $s->RecordInsert($table, $data);
	 unset($data);
	 }
	 else
	 {
	 	$data['group_title'] = $_REQUEST['group_name'];
		$data['parentid'] = $_REQUEST['parentid'];
		$data['description'] = $_REQUEST['description'];
		$data['rate'] = $_REQUEST['rate'];
		$data['qty'] = $_REQUEST['qty'];
		$data['amount'] = $_REQUEST['amount'];
		$data['unit'] = $_REQUEST['unit'];
		$row= $s->RecordInsert($table, $data);
	 }
 }

//************************Delete*********************************************** 
if( $action=='del')
{
	
 
		 $condition="id='".$_REQUEST['id']."'";
	     $s->DeleteRecord($table,$condition);
	     $s->pageLocation($targetpage.'daction=Deleted');
 }
 
  
//************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="id='".$_REQUEST['id']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	  $group_title=$RData['group_title'];
//	echo $description=$RData['description'];
	  $parentid=$RData['parentid'];
	$unit=$RData['unit'];
	$amount=$RData['amount'];
	$qty=$RData['qty']; $rate=$RData['rate'];
	$rate=$RData['rate'];
			 
}
 

 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status  )
{
	$data['group_title']=addslashes($_REQUEST['group_title']);
	$data['description']=$_REQUEST['description'];
	$data['rate']=$_REQUEST['rate'];
	 $data['unit']=$_REQUEST['unit'];	
	$data['qty']=$_REQUEST['qty'];
 	$data['amount']=$_REQUEST['amount'];
	$data['posted_date']=date('Y-m-d');
	$data['parentid']=$_REQUEST['parentid'];
	if($action=='Edit')
 	$row= $s->RecordEdit($table, $data,"id='".$_REQUEST['id']."'");
 	else
	$row= $s->RecordInsert($table, $data);
	 
	if(!$row) 
	{
	   die('Error'.DB_error());
	}
  $s->pageLocation($targetpage.'&daction=Inserted');
}?>
 
 
<form method="post" action="">

<table width="60%" border="1" align="center" cellspacing="0" cellpadding="3">
 
  <?php if(isset($daction))
  {?>
    <tr>
  
    <td colspan="2" ><div class="success">
	<?php 
 
	 echo "Record ".$daction." Successfully";
 
		 ?>
	</div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>
  
    <td colspan="2" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
<tr>
  <td class="label">BOQ Group</td>
  <td><select name="parentid"  style="width:200px" onchange="return show_text_area1(this.value);">
  <option value="select">--Select--</option>
  <option value="0">Root</option>
  
  <?php 
 
  $sql_dept=DB_query("select * from master_boq where parentid=0",$db);
  if(DB_num_rows($sql_dept)>0){
	while($dept_row=DB_fetch_array($sql_dept))	
	{	
	$s='';
	if($dept_row['id']==$parentid)
	{
		$s='selected';
	}
?>
  <option value="<?php echo $dept_row['id']; ?>" <?php echo $s;?>>
  <?php echo $dept_row['group_title']; ?>  </option>
  <?php }} ?>
</select></td>
</tr>
<tr class="hide2">
	<td class="label">Group Name</td>
	<td><input type="text" name="group_name" class="hide9" style="width:200px"></td>
</tr>
<tr class="hide1">
<td class="label">Activity</td>
<td>
<select name="group_title" style="width:200px">
<option value="">--select--</option>
<?php 
	$query = "SELECT * FROM sub_activity_group where status = 'Active'";
	$res = DB_query($query,$db) or die(DB_error());
	while($result = DB_fetch_array($res,$db)):
?>
<option value="<?php echo $result['group_name']; ?>"><?php echo $result['group_name']; ?></option>
<?php endwhile; ?>
</select>
<!--<div id = "txthint">
	<textarea name="group_title" cols="22" rows="2"s><?php //secho $group_title?></textarea>
</div> 
 -->


</td>
</tr> 
<tr class="hide1">
<td class="label">BOQ Description</td>
<td><textarea  style="width:200px" name="description" ></textarea></td>
</tr>
<tr class="hide1">
  <td class="label">Qty</td>
  
  <td><div id = "txthint1"><input name="qty"  type="text" id="qty_"  value="<?php echo $qty?>"  size="43" /></div></td>
  
</tr>

 <tr class="hide1">
  <td class="label">Rate</td>
  <td><div id = "txthint2"><input name="rate" type="text" id="rate_"  class="hide1" value="<?php echo $rate?>"  size="43" /></div></td>
</tr>
<tr class="hide1">
  <td class="label">Unit</td>
  <td>
  <div id="txthint3">
  	<?php 
	$unit_master=DB_query("SELECT * FROM unitsofmeasure ",$db); ?>
  <select name="unit" id="unit" style="width:200px;" class="" >
  <option value="">--Select--</option>
  <?php while($unitRow=DB_fetch_array($unit_master)) {
  		$secl='';
		if($unitRow['unitid']==$unit)
  		$secl='selected';
  ?>
		<option value="<?php echo $unitRow['unitid']; ?>"<?php echo $secl; ?>><?php echo $unitRow['unitname']; ?></option>
		<?php } ?>
  </select>
  </div>
  </td>
</tr> 
<tr class="hide1">
  <td class="label">Amount</td>
  <td><div id="txthint4"><input name="amount" type="text" id="amount_" class="" value="<?php echo $amount?>" onfocus="getdate();"  size="43" /></div></td>
</tr>

</table>
  
<input type="submit" name="Save"  value="Save" style="cursor:pointer;corsor:hand;" class="search"/>
<a href="index.php?route=PMS/Maintenance/MST-BOM<?php echo $rurl;?>" style="text-decoration:none;">
<input type="submit" name="cancel"  value="Cancel" style="cursor:pointer;corsor:hand;" class="search"/> 
</a>
</form>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr class="row1"><td class="label">Sno.</td>
 
<td class="label" style="width:650px;">BOQ Name</td>
 <td class="label">Qty </td>
 <td class="label">Rate </td>
 
<td class="label">Unit </td>
 <td class="label">Amount </td>
<td class="label">EDIT</td>
<td class="label">DELETE</td>
</tr>
<?php $s=new db_query();
$SQL ="select * from master_boq";	
$result=DB_query($SQL,$db,$ErrMsg); 
while($resrow=DB_fetch_array($result)){
if($resrow['parentid']==0)
{
 $i=1;
  echo "<tr><td colspan=9><h3>". $s->GetFieldValue($table,"id='".$resrow['id']."'" ,'group_title')."</h3></td></tr>";
}
 if($resrow['parentid']!=0){
?>

<tr bgcolor="#FFFFFF" onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';" >
<td><?php echo $i++;?></td>
 <td><?php 
 
//  $s->GetFieldValue($table,"id='".$resrow['parentid']."'" ,'group_title');
   echo $resrow['group_title']?></td>
 
   <td class="tabledata1"><?php     echo $resrow['qty']?></td>

 <td class="tabledata1"><?php     echo $resrow['rate']?></td>
<td class="tabledata1"><?php  echo  $s->GetFieldValue('unitsofmeasure',"unitid='".$resrow['unit']."'",'unitname');?></td>
<td class="tabledata1"><?php      echo $resrow['amount']?></td>
<td class="tabledata1">
 <a href="index.php?route=PMS/Maintenance/MST-BOM<?php echo $rurl;?>&action=Edit&id=<?php echo $resrow['id']?>" class="link"><img src="images/search.gif" width="15" height="15"></a> 
</td>
<td class="tabledata1">
 <a href="index.php?route=PMS/Maintenance/MST-BOM<?php echo $rurl;?>&action=del&id=<?php echo $resrow['id']?>" onclick="return confirm('Are you sure you want to delete this record?')" class="link"><img src="images/Icon_Delete.png" /></a> 
</td>

	<?php } ?>
</tr>
<?php }?>
</table>
 </div>