<?php

if(isset($_POST['prepare']))
{

 


    $data['branch']=$_REQUEST['site'];
	$data['sheet_date']=FormatDateFORSQL($_REQUEST['sheet_date']);
	$data['client']=$_REQUEST['client'];
	$data['lead_ref']=$_REQUEST['lead_ref_no'];
	$data['lead_ref_date']=FormatDateFORSQL($_REQUEST['lead_ref_date']);
	$data['type']=$_REQUEST['lead_ref_type'];
	$data['sub_group']=$_REQUEST['sub_group'];	
	$data['primary_qty']=$_REQUEST['pm_qty'];
	$data['secondary_qty']=$_REQUEST['sm_qty'];
	$data['grossamt']=$_REQUEST['grossamt'];
	$data['remark']=$_REQUEST['remark'];
	$data['prepared_by']=$_SESSION['UserID'];	
	$data['totalAmount']=$_REQUEST['entire_amt'];
	$data['modified_date']=date('Y-m-d');
	$data['modified_by']=$_SESSION['UserID'];
	$cond="id='".$_REQUEST['SID']."'";
	
	
	
	$s->RecordEdit('cstest_estimation_bom',$data,$cond);




        $detailsid=array();
		
		$sql_details=DB_query("select id from cstest_estimation_bom_details where master_id='".$_REQUEST['SID']."'",$db);
		while($row2=DB_fetch_array($sql_details))
		{
		$detailsid[]=$row2['id'];
		}
		if(!empty($detailsid))
		{
		$masterid_details=implode("','",$detailsid);
		}
		else
		$masterid_details=0;
		
		 $masterid_details;
		

		$del_cst_est_bom_details=DB_query("delete from cstest_estimation_bom_details 
		where master_id='".$_REQUEST['SID']."'",$db);
		
		$del_cst_est_bom_details=DB_query("delete from cstest_estimation_bom_details_asmbly 
		where details_mst_id in('".$masterid_details."')",$db);

		for($j=0;$j<count($_REQUEST['mst_itemid']);$j++)
		{
						$datahard['master_id']=$_REQUEST['SID'];
						$datahard['mst_itemid']=$_REQUEST['mst_itemid'][$j];
						$datahard['item_size']=$_REQUEST['item_size'][$j];
						$datahard['item_prim_qty']=$_REQUEST['item_prim_qty'][$j];
						$datahard['item_sec_qty']=$_REQUEST['item_sec_qty'][$j];
						$datahard['item_rate']=$_REQUEST['item_rate'][$j];
						$datahard['item_amount']=$_REQUEST['item_amount'][$j];
					
						$s->RecordInsert('cstest_estimation_bom_details', $datahard);
						$getlastid2=DB_Last_Insert_ID($db,'cstest_estimation_bom_details', 'id');
						 $u=0;
						 $count1=count($_REQUEST['asmbly_qty']);
						 for($t=0; $t<count($_REQUEST['itemsubcat']); $t++)
							{
									
											if($_REQUEST['basicrateA'][$t]!='')
											{
											$assmbly_array1['details_mst_id']=$getlastid2;
											$assmbly_array1['mst_itemid']=$_REQUEST['mst_itemidasmbly'][$j];
											$assmbly_array1['item_size']=$_REQUEST['item_size'][$j];
											$assmbly_array1['mst_asmbly_cat_id']=$_REQUEST['itemsubcat'][$t];
											$assmbly_array1['mst_asmbly_item_id']=$_REQUEST['itemdata'][$t];
											$assmbly_array1['asmbly_basicrate']=$_REQUEST['basicrateA'][$t];
											$assmbly_array1['asmbly_item_qty']=$_REQUEST['asmbly_qty'][$t][$j];
											$assmbly_array1['asmbly_item_amt']=$_REQUEST['asmbly_amt'][$t][$j];	
											$assmbly_array1['hardware_flag']=0;	
											
											//print_r($assmbly_array1);
											$s->RecordInsert('cstest_estimation_bom_details_asmbly', $assmbly_array1);
											
										   }
					
                  
					
					         }
					
				
					for($n=0; $n<$_REQUEST['counthw']; $n++)
					{
					
						if($_REQUEST['mst_asmbly_item_id'][$n]!=''){
						$assmbly_array['details_mst_id']=$getlastid2;
									$assmbly_array['mst_itemid']=$_REQUEST['mst_itemidasmbly'][$j];
									$assmbly_array['item_size']=$_REQUEST['item_size'][$j];
									$assmbly_array['mst_asmbly_cat_id']=$_REQUEST['mst_asmbly_cat_id'][$n];
                                    $assmbly_array['mst_asmbly_item_id']=$_REQUEST['mst_asmbly_item_id'][$n];
									$assmbly_array['asmbly_basicrate']=$_REQUEST['asmbly_basicrate'][$n];
									$assmbly_array['asmbly_item_qty']=$_REQUEST['asmbly_item_qty'][$n][$j];
									$assmbly_array['asmbly_item_amt']=$_REQUEST['asmbly_item_amt'][$n][$j];
									$assmbly_array['hardware_flag']=1;
									$s->RecordInsert('cstest_estimation_bom_details_asmbly', $assmbly_array);
									}
					}

         }


		if($_REQUEST['against']=='lead')
			$update=DB_query("update crm_leads  set EST_Status=1 where leadid='".$_REQUEST['lead_ref_no']."'",$db);	
			
			
			$s->pageLocation("index.php?route=CostEstimation/estimation_sheet_bom_index".$rurl);


}






$sql=DB_query("select * from  cstest_estimation_bom where id='".$_REQUEST['SID']."'",$db);
$row=DB_fetch_array($sql);
extract($row);



?>

<link href="css/dashboard1.css" rel="stylesheet" type="text/css" />	
<link rel="stylesheet" href="css/new_mxerp.css">
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
<center> <h2>Estimation Sheet Against BOM Edit </h2>
</center>


<form name="esbom" method="post" >
<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #999999;">
  <tr class="row1"><input type="hidden" name="sid" value="<?php echo $_REQUEST['SID']; ?>" id="SID" />
    <td width="13%" class="tabledata"><strong>Branch</strong></td>
    <td width="20%"><select name="site" id="site_id" style="width:100%;">
     <?php 
		$site_sql = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode ='".$branch."'";
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
		?>
         <option value="<?php echo $site_row['loccode']; ?>" ><?php echo $site_row['locationname']; ?></option>
            <?php } ?>
    </select></td>
    <td width="13%" class="tabledata"><strong>Sheet No.</strong></td>
    <td width="20%"><input name="sheet_no" id="sheet_no" type="text" class="" style="width:100%;" value="<?php echo $sheet_no;  ?>" readonly="true" /></td>
    <td class="tabledata"><strong>Date</strong></td>
    <td width="20%"><input name="sheet_date" id="sheet_date" type="text" class="cap datepicker" style="width:100%;" readonly="true"  value="<?php echo ConvertSQLDate($sheet_date);?>"/></td>
  </tr>
  <tr class="row1">
  <td class="tabledata"><strong>Against</strong></td>
    <td><select style="width:100%;" name="against" id="against_id"  onchange="activateDeactivate(this.value)">
      <option value="lead" <?php if($against=='lead') { ?> selected="selected" <?php } ?> >LEAD</option>
	  <option value="none" <?php if($against=='none') { ?> selected="selected" <?php } ?>>NONE</option>
    </select></td>
  	<td class="tabledata"><strong>Client </strong><span class="style1">*</span> </td>
    <td><select name="client" id="client_id" style="width:100%" onchange="getLeads(this.value)">
	<?php if($against=='lead')	
	{
		$sql1=DB_query("Select * from clientmaster where id='".$client."' ",$db);
	}
	else
	{
	?>
	<option value="">Select Client</option>
	<?php 
	$sql1=DB_query("Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
	
	}
	while($row1=DB_fetch_array($sql1))
	{?>
		<option value="<?php echo $row1['id'];?>" <?php if($client==$row1['id']) { ?> selected="selected" <?php } ?>    ><?php echo $row1['c_name'];?></option>
	<?php 
	}
	?>
	
	</select></td>
	
	<td class="tabledata"><strong>Lead/Ref. No.</strong></td>
    <td id="div_lead">
	<?php if($against=='lead')
	{ echo $lead_ref1=$s->getField('crm_leads','leadid',$lead_ref,'leadcode'); } else { echo $lead_ref1=$lead_ref; } ?>
	<input type="hidden" name="lead_ref_no" id="lead_ref_id" value="<?php echo $lead_ref; ?>"  style="width:100%;" /></td>
  </tr>
  <tr class="row1">
  	<td id="test" class="tabledata"><strong>Lead/Ref. Date</strong></td>
    <td><input type="text" name="lead_ref_date" id="lead_ref_date_id" value="<?php echo ConvertSQLDate($lead_ref_date); ?>"  style="width:100%;" class="datepicker" /></td>
    <td class="tabledata"><strong>Type </strong></td>
    <td id="lead_ref_type_id"><select name="lead_ref_type" id="lead_ref_type" style="width:100%">
	    <?php $type1=DB_query("select * from type_master",$db);
	      while($type_row=DB_fetch_array($type1))
		  {
		  ?>
		  <option value="<?php echo $type_row['id']; ?>" <?php if($type==$type_row['id']) { ?> selected="selected" <?php } ?>   ><?php echo $type_row['name']; ?></option>
		  <?php } ?>
		  </select>
	</td>
    <td class="tabledata"><strong>Material Sub Group </strong><span class="style1">* </span></td>
    <td><select style="width:100%;" name="sub_group" id="sub_group">
      <option value="">--Select Product Group--</option>
		<?php $materialSQL=DB_query("SELECT * FROM stockcategory where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
		while($material=DB_fetch_array($materialSQL)){?>
		 <option value="<?php echo $material['id'];?>"<?php if($sub_group==$material['id']) { ?> selected="selected" <?php } ?>     ><?php echo $material['categorydescription'];?></option>
		<?php }?>
    </select></td>
  </tr>
</table>
<br />
<fieldset>
<legend style="font-size:13px;"><strong>Finished Product Details</strong></legend>
<p align="left">
<?php if($against!='lead') { ?>
			
          <input type="button" name="profile_popup" value="Pick Finished Product" id="profile_popup"  onclick="open_items()" class="search" />&nbsp;&nbsp;
		  <input type="button" class="search"  onclick="deleteRow('dataTable')" value="Delete" id="delete" />
			
		  
		  &nbsp;&nbsp;<!--<input type="button" id="expandbom111" value="Expand BOM" onclick="return expandbom()" class="search">&nbsp;&nbsp;<input type="button" id="collapse" value="Collapse BOM" onclick="return collapsebom()" class="search">&nbsp;&nbsp;-->
		  <?php } ?>
		  <input type="button" id="addhardwareid" value="Add Assembly" onclick="displayHardwareBox()" class="search">
			<input type="hidden" id="hiditemcount" name="hiditemcount" value="" />
			
		</p>
		<br/>
<div>
<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" id="div_items" style="border:1px solid #666666; ">
  <tr class="row1">
  	<td width="5%" class="tabledata">&nbsp;</td>
	<td width="1%" class="tabledata">&nbsp;</td>
    <td width="15%" class="tabledata"><strong>Item Code</strong></td>
    <td width="15%" class="tabledata"><strong>Item Name</strong></td>
    <td width="15%" class="tabledata"><strong>Size</strong></td>
    <td width="120" colspan="6" align="center" class="tabledata" style="text-align:center;"><strong>Quantity(Q)</strong></td>
  </tr>
  <tbody id="dataTable">
  <?php 
  $i=0;
   $finish_qty=array();
  $sql_est_data=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['SID']."'",$db);
  while($row_est=DB_fetch_array($sql_est_data))
  {  
  $itemname=$s->getField('item_master','id',$row_est['mst_itemid'],'item_name');
  $itemcode=$s->getField('item_master','id',$row_est['mst_itemid'],'StockID');
  $primary_unit=$s->getField('item_master','id',$row_est['mst_itemid'],'mst_unitid');
  $secondary_unit=$s->getField('item_master','id',$row_est['mst_itemid'],'conv_unit');
            $qty_array[]=$row_est['item_prim_qty'];
			$sec_qty_array[]=$row_est['item_sec_qty'];
			$finish_qty[$i]=$row_est['item_prim_qty'];
			
			
			//$amount_array[]=$amount1;
			
if($i==0)
{			
  ?>
  <tr class="row1">
    <td height="26"><input name="bompro[]" id="bompro" type="checkbox" value=""></td>
	<td><input name="mst_itemid[]" id="itemid" type="hidden" value="<?php echo $row_est['mst_itemid']; ?>"></td>
    <td><input name="itemcode[]" id="itemcode" type="text" value="<?php echo $itemcode; ?>"></td>
	<td><input name="itemname[]" id="itemname" type="text" value="<?php echo $itemname; ?>"></td>
    <td><select name="bomsize[]" id="bomsize"  onchange="showconvac(this.value,this.id)" style="width:120px">
	      <option value="0">Select</option>		
		<?php $sql4=DB_query("SELECT * FROM size_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
while($row4=DB_fetch_array($sql4))
{
if($row_est['item_size']==$row4['id'])$selected="selected"; else $selected="";?>
<option value="<?php echo $row4['id'];?>" <?php echo $selected;?>><?php echo $row4['size'];?></option>
<?php }?>
</select></td>
    <td ><input type="text" name="item_prim_qty[]"  id="stockqty" style="width:100%;" onkeyup="calcValue1(this.value,this.id)" onblur="calcValue1(this.value,this.id)"  value="<?php echo $row_est['item_prim_qty']; ?>"/></td>
<td ><select name="unit_prim[]" id="unitprim" style="width:70px;" >
			<?php
			$sql_unit1=DB_query("select * from unitsofmeasure where unitid='".$primary_unit."'",$db); 
			while($row_sql_unit=DB_fetch_array($sql_unit1))
			{
			if($row_sql_unit['unitid']==$primary_unit)$selected1="selected"; else $selected1="";?>
			<option value="<?php echo $row_sql_unit['unitid']; ?>" <?php echo $selected1;?>   ><?php echo $row_sql_unit['unitname'];  ?></option>
			<?php } ?>
               </select>
</td>
<td ><input type="hidden" name="conv_fac" id="convfac" /></td>
<td ><input type="text" name="item_sec_qty[]"  id="secondaryqty" style="width:100%;" value="<?php echo $row_est['item_sec_qty']; ?>" /></td>
<td ><select name="unit_sec[]" id="unitsec" style="width:70px;" >
            <?php
			$sql_unit1=DB_query("select * from unitsofmeasure where unitid='".$secondary_unit."'",$db); 
			while($row_sql_unit=DB_fetch_array($sql_unit1))
			{
			if($row_sql_unit['unitid']==$secondary_unit)$selected2="selected"; else $selected2="";?>
			<option value="<?php echo $row_sql_unit['unitid']; ?>" <?php echo $selected2;?>><?php echo $row_sql_unit['unitname'];  ?></option>
			<?php } ?>
                </select></td>
<td><input type="hidden" name="conf[]" id="conf" style="width:100%;"  readonly="true" /></td>
				
   
  </tr>
  <?php  } else  { ?>
  
  <tr class="row1">
    <td height="26"><input name="bompro[]" id="bompro_<?php echo $i; ?>" type="checkbox" value=""></td>
	<td><input name="mst_itemid[]" id="itemid_<?php echo $i; ?>" type="hidden" value="<?php echo $row_est['mst_itemid']; ?>"></td>
    <td><input name="itemcode[]" id="itemcode_<?php echo $i; ?>" type="text" value="<?php echo $itemcode; ?>"></td>
	<td><input name="itemname[]" id="itemname_<?php echo $i; ?>" type="text" value="<?php echo $itemname; ?>"></td>
    <td><select name="bomsize[]" id="bomsize_<?php echo $i; ?>"  onchange="showconvac(this.value,this.id)" style="width:120px">
	      <option value="0">Select</option>		
		<?php $sql4=DB_query("SELECT * FROM size_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
while($row4=DB_fetch_array($sql4))
{
if($row_est['item_size']==$row4['id'])$selected="selected"; else $selected="";?>
<option value="<?php echo $row4['id'];?>" <?php echo $selected;?>><?php echo $row4['size'];?></option>
<?php }?>
</select></td>
    <td ><input type="text" name="item_prim_qty[]"  id="stockqty_<?php echo $i; ?>" style="width:100%;" onkeyup="calcValue1(this.value,this.id)" onblur="calcValue1(this.value,this.id)"  value="<?php echo $row_est['item_prim_qty']; ?>"/></td>
<td ><select name="unit_prim[]" id="unitprim_<?php echo $i; ?>" style="width:70px;" >
<option value="0">Select</option>	
			<?php
			$sql_unit1=DB_query("select * from unitsofmeasure where unitid='".$primary_unit."'",$db); 
			while($row_sql_unit=DB_fetch_array($sql_unit1))
			{
			if($row_sql_unit['unitid']==$primary_unit)$selected1="selected"; else $selected1="";?>
			
			<option value="<?php echo $row_sql_unit['unitid']; ?>" <?php echo $selected1;?> ><?php echo $row_sql_unit['unitname'];  ?></option>
			<?php } ?>
               </select>
</td>
<td ><input type="hidden" name="conv_fac_<?php echo $i; ?>" id="convfac_<?php echo $i; ?>" /></td>
<td ><input type="text" name="item_sec_qty[]"  id="secondaryqty_<?php echo $i; ?>" style="width:100%;" value="<?php echo $row_est['item_sec_qty']; ?>" /></td>
<td ><select name="unit_sec[]" id="unitsec_<?php echo $i; ?>" style="width:70px;" >
            <option value="0">Select</option>	
			<?php
			$sql_unit1=DB_query("select * from unitsofmeasure where unitid='".$secondary_unit."'",$db); 
			while($row_sql_unit=DB_fetch_array($sql_unit1))
			{
			if($row_sql_unit['unitid']==$secondary_unit)$selected2="selected"; else $selected2="";?>
			<option value="<?php echo $row_sql_unit['unitid']; ?>" <?php echo $selected2;?> ><?php echo $row_sql_unit['unitname'];  ?></option>
			<?php } ?>
                </select></td>
<td><input type="hidden" name="conf[]" id="conf_<?php echo $i; ?>" style="width:100%;"  readonly="true" /></td>
				
   
  </tr>
  
  <?php } ?>
  
  
  
 <?php $i++;
  
   } 
  
  
   ?>
  </tbody>
  <tr bgcolor='#FFFFFF'>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>	
	<th>Total</th>
	<th style='text-align:right'><?php echo array_sum($qty_array); ?><input type='hidden' name='primary_qty' id='primary_qty' value="<?php echo array_sum($qty_array); ?>" /></th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th style='text-align:right'><?php echo array_sum($sec_qty_array);?><input type='hidden' name='secondary_qty' id='secondary_qty' value="<?php echo array_sum($sec_qty_array); ?>" /></th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	</tr>
  
  </tr>
 
</table>
<?php if($against=='lead') { $againstdata='lead'; } else { $againstdata=$i-1; } ?>
<input type="hidden" name="hiditemcount" id="hiditemcount12" value="<?php echo $againstdata; ?>" />
<input type="hidden" name="" value="<?php echo $i; ?>" id="edit_cnt" />

</div>
</fieldset>
<br />



<div style="display:none;" id="showhardware">
<fieldset>
<legend style="font-size:13px;"><strong>Assembly Details (For single unit)</strong></legend>
<div id="hardware_id">
<?php 
$sql4=DB_query("SELECT * FROM `cstest_estimation_bom_details` where master_id='".$_REQUEST['SID']."'",$db);
$kk=0;
while($row4=DB_fetch_array($sql4))
{
	$item_array[$kk]['id']=$row4['mst_itemid'];
	$item_array[$kk]['size']=$row4['item_size'];
	$item_array[$kk]['qty']=$row4['item_prim_qty'];
	$kk++;
}

$tbodyid="'dataTable1'";
$hardwaretable='<p align="left"><input type="button" onclick="addRow1('.$tbodyid.')" value="Add More" id="addmore" class="search" /></p><table width="100%" cellpadding="2" cellspacing="2">
<tr class="row1">
  <td colspan="3" rowspan="2" class="tabledata"><strong>Assembly Details</strong></td>
<td  align="right" class="tabledata"><strong>Item </strong></td>';

foreach($item_array as $key=>$v)
   {

   $hardwaretable.='<td class="tabledata" colspan="2" style="font-size:11px;">'.$s->GetFieldValue('item_master',"id='".$v['id']."'",'item_name').'<input type="hidden" name="mst_itemidasmbly['.$key.']" value="'.$v['id'].'"/><input type="hidden" name="item_size['.$key.']" value="'.$v['size'].'" id="item_size_'.$key.'"/></td>';
  
   }
  $hardwaretable.='</tr><tr class="row1">
  	<td align="right" class="tabledata"><strong>Size </strong></td>';
	foreach($item_array as $key=>$v)
   {

 $hardwaretable.='<td class="tabledata" colspan="2" style="font-size:11px;">'.$s->GetFieldValue('size_master',"id='".$v['size']."'",'size').'</td>';
 } 	





$hardwaretable.='</tr>';








/////////////////////Assembly Items like frame and shutter///////////////////////////
$hardwaretable.='<tr class="row1">
<td class="tabledata"><strong>Sub Group</strong></td>
<td class="tabledata"><strong>Items</strong></td>
<td class="tabledata"><strong>Basic Rate</strong></td>
<td class="tabledata"><strong>UOM</strong></td>'; 
for($i=0; $i<count($item_array); $i++)
{
$hardwaretable.='<td class="tabledata"><strong>Qty</strong></td>
<td class="tabledata"><strong>Amt</strong></td>';
}
$hardwaretable.='</tr>';


$hardwaretable.='<tbody id="dataTable1">';

$assemamt=array();
  $item2=array(); 
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['SID']."'",$db);
  while($item1=DB_fetch_array($items)) 	  {	  $item2[]=$item1['id'];	  }   $itemsdata=implode("','",$item2);
  
 
  $assem=DB_query("select distinct(mst_asmbly_cat_id) from cstest_estimation_bom_details_asmbly 
			  where details_mst_id in('".$itemsdata."') and 
			  hardware_flag=0",$db);
			  
$t=0;			  
 while($assem_row=DB_fetch_array($assem))
  {
   
	  $itemid=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'mst_asmbly_item_id'); 
  
	   $basic=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'asmbly_basicrate'); 
   
	
		   $sql_4=DB_query("SELECT * FROM `cstest_estimation_bom_details_asmbly` 
			where details_mst_id in('".$itemsdata."') and   hardware_flag=0 
			and mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'",$db);
			
	    $kk1=0;
		while($row_4=DB_fetch_array($sql_4))
		{
			$item_array1[$kk1]['asmbly_item_qty']=$row_4['asmbly_item_qty'];
			$item_array1[$kk1]['asmbly_item_amt']=$row_4['asmbly_item_amt'];
			$kk1++;
		}
		
		
		$unit= $s->getField('item_master','id',$itemid,'mst_unitid');	 

		
		
		
	if($t==0)
	 {	
		
$hardwaretable.='<tr class="row1">
<td>';
$maincatsql=DB_fetch_array(DB_query("Select id from stockmaincategory where stockmaincategoryid='FG' and CompanyId=".$_SESSION['ACGCompanyId'],$db));
$catsql=DB_query("Select * from stockcategory where stockmaincategory=".$maincatsql['id'],$db);
$hardwaretable.='<select name="itemsubcat[]" id="itemsubcat" onchange="getItemList(this.value,this.id)" style="width:120px">';
while($catrow=DB_fetch_array($catsql))
{
$g='';
if($catrow['id']==$assem_row['mst_asmbly_cat_id'])

$g='selected="selected"';

	$hardwaretable.='<option value="'.$catrow['id'].'" '.$g.'  >'.$catrow['categorydescription'].'</option>';	
}
$hardwaretable.='</select></td>
<td><select name="itemdata[]" id="item" onchange="itemBomDetails(this.value,this.id)">
<option value="">Select Item</option>';
$catsql1=DB_query("Select * from item_master where id=".$itemid,$db);
while($catrow1=DB_fetch_array($catsql1))
{

$hardwaretable.='<option value="'.$catrow1['id'].'" selected="selected"   >'.$catrow1['item_name'].'</option>';	
}


$hardwaretable.='</select></td>
<td><input type="text" name="basicrateA[]" id="basicrateA" style="width:50px;" readonly value="'.$basic.'" /></td>
<td><input type="text" name="" id="uomA" style="width:50px;" readonly value="'.$s->getField('unitsofmeasure','unitid',$unit,'unitname').'" /></td>'; 

for($i=0; $i<count($item_array); $i++)
{
if($assemamt[$i]==$i)
$assemamt[$i][]=$assemamt[$i]+$item_array1[$i]['asmbly_item_amt'];
else
$assemamt[$i][]=$item_array1[$i]['asmbly_item_amt'];

$hardwaretable.='<td><input type="text" name="asmbly_qty[0]['.$i.']" id="asmbly_item_qtyA_'.$i.'" 
value="'.$item_array1[$i]['asmbly_item_qty'].'" readonly  /></td>
				 <td><input type="text" name="asmbly_amt[0]['.$i.']" id="asmbly_item_amtA_'.$i.'" 
				 value="'.$item_array1[$i]['asmbly_item_amt'].'" readonly /></td>';
}
$hardwaretable.='<input type="hidden" value="'.$i.'" id="fassmblycnt" /></tr>';



}
else
{
$hardwaretable.='<tr class="row1">
<td>';
$maincatsql=DB_fetch_array(DB_query("Select id from stockmaincategory where stockmaincategoryid='FG' and CompanyId=".$_SESSION['ACGCompanyId'],$db));
$catsql=DB_query("Select * from stockcategory where stockmaincategory=".$maincatsql['id'],$db);
$hardwaretable.='<select name="itemsubcat[]" id="itemsubcat_'.$t.'" onchange="getItemList(this.value,this.id)" style="width:120px">';
while($catrow=DB_fetch_array($catsql))
{
$g='';
if($catrow['id']==$assem_row['mst_asmbly_cat_id'])

$g='selected="selected"';

	$hardwaretable.='<option value="'.$catrow['id'].'" '.$g.'  >'.$catrow['categorydescription'].'</option>';	
	
}
$hardwaretable.='</select></td>
<td><select name="itemdata[]" id="item_'.$t.'" onchange="itemBomDetails(this.value,this.id)">
<option value="">Select Item</option>';
$catsql1=DB_query("Select * from item_master where id=".$itemid,$db);
while($catrow1=DB_fetch_array($catsql1))
{



	$hardwaretable.='<option value="'.$catrow1['id'].'" selected="selected"   >'.$catrow1['item_name'].'</option>';	
}

$hardwaretable.='</select></td>

<td><input type="text" name="basicrateA[]" id="basicrateA_'.$t.'" style="width:50px;" readonly value="'.$basic.'" /></td>
<td><input type="text" name="" id="uomA_'.$t.'" style="width:50px;" readonly  value="'.$s->getField('unitsofmeasure','unitid',$unit,'unitname').'"   /></td>'; 
for($i=0; $i<count($item_array); $i++)
{
if($assemamt[$i]==$i)
$assemamt[$i][]=$assemamt[$i]+$item_array1[$i]['asmbly_item_amt'];
else
$assemamt[$i][]=$item_array1[$i]['asmbly_item_amt'];

$hardwaretable.='<td><input type="text" name="asmbly_qty['.$t.']['.$i.']" id="asmbly_item_qtyA_'.$i.'_'.$t.'" 
value="'.$item_array1[$i]['asmbly_item_qty'].'" readonly  /></td>
<td><input type="text" name="asmbly_amt['.$t.']['.$i.']" id="asmbly_item_amtA_'.$i.'_'.$t.'" 
value="'.$item_array1[$i]['asmbly_item_amt'].'" readonly /></td>';
}
$hardwaretable.='<input type="hidden" value="'.$i.'" id="fassmblycnt" /></tr>';


}

$t++; }
	

$hardwaretable.='</tbody>';
$hardwaretable.='<tr class="row1"><td><b>Sub Total (A)</b></<td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
for($i=0; $i<count($item_array); $i++)
{


$hardwaretable.='<td>&nbsp;</td>
				 <td class="tabledata"><input type="text" name="total[]" id="totalA_'.$i.'" value="'.array_sum($assemamt[$i]).'" readonly /></td>';
}
$hardwaretable.='<tr>';


//////////////////////////////////////////////////////////////


//onfocus="calculateAllAmt(this.id)"






//////////////////Hardware Items/////////////////////////////
$coun= count($item_array);
  $coun1=($coun*2)+4;

$hardwaretable.='<tr class="row1"><td class="tabledata" colspan='.$coun1.'><strong>Sub Assembly Details</strong></td></tr>';


$hardwaretable.='<tr class="row1">
<td class="tabledata"><strong>Sub Group</strong></td>
<td class="tabledata"><strong>Items</strong></td>
<td class="tabledata" colspan=2><strong>Basic Rate</strong></td>'; 
for($i=0; $i<count($item_array); $i++)
{
$hardwaretable.='<td class="tabledata"><strong>Qty</strong></td>
<td class="tabledata"><strong>Amt</strong></td>';
}
$hardwaretable.='</tr>';

 
		  
  


$k=0;
 $item2=array(); 
  $hrdamt=array();
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['SID']."'",$db);
		  while($item1=DB_fetch_array($items))
		  {
		  $item2[]=$item1['id'];
		  }
           $itemsdata=implode("','",$item2);
  
 
		  $assem=DB_query("select distinct(mst_asmbly_cat_id) from cstest_estimation_bom_details_asmbly 
		  where details_mst_id in('".$itemsdata."') and 
		  hardware_flag=1",$db);
  while($assem_row=DB_fetch_array($assem))
  {
   
	   $itemid=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'mst_asmbly_item_id'); 
	  
	   $basic=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'asmbly_basicrate'); 
	   
	   //$unit=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	  // and  details_mst_id in('".$itemsdata."')",'mst_asmbly_unit_id'); 
  
		   $sql_4=DB_query("SELECT * FROM `cstest_estimation_bom_details_asmbly` 
			where details_mst_id in('".$itemsdata."') and   hardware_flag=1 
			and mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'",$db);
	$kk1=0;
			while($row_4=DB_fetch_array($sql_4))
			{
				$item_array1[$kk1]['asmbly_item_qty']=$row_4['asmbly_item_qty'];
				$item_array1[$kk1]['asmbly_item_amt']=$row_4['asmbly_item_amt'];
				$kk1++;
			}


//$ProdManuSQL=DB_query("SELECT * FROM product_manufacture where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
//while($prodManuf=DB_fetch_array($ProdManuSQL)){




  $hardwaretable.='<tr class="row1">
    <td>'.$s->getField('product_manufacture','id',$assem_row['mst_asmbly_cat_id'],'manufacturer').'<input type="hidden" name="mst_asmbly_cat_id['.$k.']" id="manufac" value="'.$assem_row['mst_asmbly_cat_id'].'" /></td>
    <td>';
	$GetItemsSQL=DB_query("select * from item_master where make='".$assem_row['mst_asmbly_cat_id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	$hardwaretable.='<select style="width:150px;" name="mst_asmbly_item_id['.$k.']" id="manufact_'.$k.'" onchange="showbascicamt(this.value,this.id)">
      <option value="">--Select--</option>';
	  while($GetItemsFors=DB_fetch_array($GetItemsSQL))
	  {
	  
		$l='';
		if($GetItemsFors['id']==$itemid)
		
		$l='selected="selected"';

	  $unit= $s->getField('item_master','id',$itemid,'mst_unitid');	
	  
	  
	  $hardwaretable.='<option value="'.$GetItemsFors['id'].'" '.$l.'    >'.$GetItemsFors['item_name'].'</option>';
	  }
	  $hardwaretable.='</select></td>
	<td ><input type="text" name="asmbly_basicrate['.$k.']" id="basicrate_'.$k.'" style="width:50px;" value="'.$basic.'" /></td>
	<td ><input type="text" name="" id="uomaT_'.$k.'" style="width:50px;" readonly  value="'. $s->getField('unitsofmeasure','unitid',$unit,'unitname').'"  /></td>';
	for($i=0; $i<count($item_array); $i++)
		{
		if($hrdamt[$i]==$i)
		{ 	$hrdamt[$i][]=$hrdamt[$i]+$item_array1[$i]['asmbly_item_amt'];}
		else {	$hrdamt[$i][]=$item_array1[$i]['asmbly_item_amt'];	}
   
		
		
		$hardwaretable.='<td><input type="text" name="asmbly_item_qty['.$k.']['.$i.']" id="asmbly_item_qty_'.$k.'_'.$i.'" 
		value="'.$item_array1[$i]['asmbly_item_qty'].'" onkeyup="calcItemAmt1(this.value,'.$k.','.$i.')" onblur="calcItemAmt1(this.value,'.$k.','.$i.')"/></td>
							<td><input type="text" name="asmbly_item_amt['.$k.']['.$i.']" id="asmbly_item_amt_'.$k.'_'.$i.'" value="'.$item_array1[$i]['asmbly_item_amt'].'" readonly /></td>'; 	
		}
  $hardwaretable.='</tr>';
  $k++;
  }
 
  $hardwaretable.='<tr class="row1">
  <td><b>Sub Total (B)</b></td>
  <td></td>
  <td colspan=2></td>';
  for($i=0; $i<count($item_array); $i++)
  {
  		$hardwaretable.='<td></td>
						<td class="tabledata"><input type="text" name="" 
						value="'.array_sum($hrdamt[$i]).'" id="total_hw_amt_id_'.$i.'" readonly /></td>';	
  }
  $hardwaretable.='</tr>';
  $hardwaretable.='<tr class="row1">
  <td colspan="2"><b>Total Amount(C=A+B)</b></td>
  <td colspan=2></td>';
  for($i=0; $i<count($item_array); $i++)
  {
  $sum=array_sum($hrdamt[$i])+array_sum($assemamt[$i]);
  		$hardwaretable.='<td></td>
						<td class="tabledata"><input type="text" name="" 
						value="'. $sum.'" id="totalC_'.$i.'" readonly/></td>';	
  }
  $hardwaretable.='</tr>';
   $hardwaretable.='<tr class="row1">
  <td colspan="2"><b>Grand Total (D=C*Q)</b></td>
  <td colspan=2></td>';
  for($i=0; $i<count($item_array); $i++)
  {
  $totalamt[]=(array_sum($hrdamt[$i])+array_sum($assemamt[$i]))*($finish_qty[$i]);
	 $sum1=(array_sum($hrdamt[$i])+array_sum($assemamt[$i]))*($finish_qty[$i]);
  		$hardwaretable.='<td></td>
						<td class="tabledata"><input type="text" name="" value="'.$sum1.'" id="totalD_'.$i.'" readonly/>
						<input type="hidden" name="totalqtyabc" id="totalqtyabc_'.$i.'" value="'.$item_array[$i][qty].'" ></td>';	
  }
  $hardwaretable.='</tr>';
  
  $hardwaretable.='<input type="hidden" id="count" value="'.$k.'" name="counthw" /></table>';
  echo $hardwaretable;
  ?>


</div>
</fieldset>
</div>
<br />
<table width="100%" border="0">
<tr>
	<td valign="top" width="30%">
		<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #999999;">
		<tr>
    	<td rowspan="2" class="tabledata"><strong>Entire Qty</strong></td>
		<td><input type="text" name="pm_qty" id="pm_qty" value="<?php echo array_sum($qty_array); ?>" /></td>
		</tr>
		<tr>
		<td><input type="text" name="sm_qty" id="sm_qty" value="<?php echo array_sum($sec_qty_array); ?>" /></td>
		<tr>
		<td class="tabledata" nowrap="nowrap"><strong>Total Amount for entire quantity (&#931;D)</strong></td>
		<td><input type="text" name="entire_amt" id="entire_amt" value="<?php echo array_sum($totalamt); ?>" /></td>
		</tr>
  	
		</table>
	</td>
	<td>
		<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #999999;">
<tr>
    <th>Remarks</th>
    <td colspan="5">
	<textarea rows="3" name="remark" style="width:100%"><?php echo $remark; ?></textarea>
	</td>
  </tr>
</table>
	</td>
</tr>
</table>
<p align="center">
	<input type="submit" name="prepare" value="Prepare" onclick="return validate()" /><!-- onclick="return checkBom();"--></p>
</form>
<script language="javascript" type="text/javascript">
function displayHardwareBox()
{
  if(document.getElementById("hiditemcount12"))
  {
  if(document.getElementById("hiditemcount12").value!='lead')
  {
       var sid=document.getElementById("SID").value;
	   
	   
      	  if(document.getElementById("hiditemcount12").value=='')
		  var count_size=0;
		  else
		  var count_size=document.getElementById("hiditemcount12").value;
		   
			var size_array= new Array();
			var item_array=new Array();
			var qty_array=new Array();
			var qty_array1=new Array();
			//var count_size=document.getElementById("fassmblycnt").value;
			if(count_size==0)
			{
			size_array[0]=document.getElementById("itemid").value;
			item_array[0]=document.getElementById("bomsize").value;
			qty_array[0]=document.getElementById("stock_qty").value;
			qty_array1[0]=document.getElementById("secondary_qty").value;

			
			}
			
			else
			{
			
						for(var i=0; i<=count_size; i++)
						{
						   if(i==0)
						   {
						   size_array[i]=document.getElementById("itemid").value;
						   item_array[i]=document.getElementById("bomsize").value;
						   qty_array[i]=document.getElementById("stockqty").value;
						   qty_array1[i]=document.getElementById("secondaryqty").value;
						   //item_array[i]=document.getElementById("stock_qty").value;
						   }
						   else
						   {
							size_array[i]=document.getElementById("itemid_"+i).value;
							item_array[i]=document.getElementById("bomsize_"+i).value;
							qty_array[i]=document.getElementById("stockqty_"+i).value;
							qty_array1[i]=document.getElementById("secondaryqty_"+i).value;
							}
						}
						var primary=0;
						var secondary=0;		
						for(var t=0;t<=count_size;t++)
						{
						var primary=parseFloat(primary)+parseFloat(qty_array[t]);
						var secondary=parseFloat(secondary)+parseFloat(qty_array1[t]);
						}	
						
			}
			
			
			
			
			document.getElementById("pm_qty").value=primary.toFixed(2);
			document.getElementById("sm_qty").value=secondary.toFixed(2);
		
		
				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("hardware_id").innerHTML=xmlhttp.responseText;
						
					}
				}
xmlhttp.open("GET","CRM_GetLeadsRecord_withoutlead_edit.php?size="+size_array+"&itemarray="+item_array+"&qty="+qty_array+"&SID="+sid,true);
				xmlhttp.send(); 
  document.getElementById("showhardware").style.display='block';
  }
  else
   {
  document.getElementById("pm_qty").value=document.getElementById("primary_qty").value;
  document.getElementById("sm_qty").value=document.getElementById("secondary_qty").value;
   
  document.getElementById("showhardware").style.display='block';
  }
  }
}

function activateDeactivate(str)
{
	if(str=='lead')
	{
		//document.getElementById("addmore").disabled=true;
		
		document.getElementById("hiditemcount12").value='lead';
		document.getElementById("delete").disabled=true;
		document.getElementById("profile_popup").disabled=true;
	}
	else if(str=='none')
	{
		//document.getElementById("addmore").disabled=false;
		//document.getElementById("hiditemcount").id='hiditemcount12';
		if(document.getElementById("hiditemcount12").value=='lead')
		document.getElementById("hiditemcount12").value='';
		document.getElementById("delete").disabled=false;
		document.getElementById("profile_popup").disabled=false;
	}
}

function checkBom()
{
	var count = document.getElementById("bomproid").value;
	for(var i=0; i<count; i++)
	{
		var bomrate=document.getElementById("item_rate_id_"+i).value;
		if(bomrate=='')
		{
			alert('Some of the finished items do not have their BOM. Please create BOM of each item before proceeding');
			return false;
		}
	}
}

function getLeads(str)
{
	var against_id=document.getElementById("against_id").value;
	var site_id=document.getElementById("site_id").value;
	if(site_id=='')
	{
		alert('Please choose site'); 
		document.getElementById("client_id").value='';
		document.getElementById("site_id").focus();
		return false;
	}
	if(against_id=='')
	{
		alert('Please choose the option against'); 
		document.getElementById("against_id").focus();
		document.getElementById("client_id").value='';
	}
	
	
	if(against_id=='lead')
	{
		if (str=="")
  		{
  			document.getElementById("div_lead").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("div_lead").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","CRM_GetLeads.php?str="+against_id+"&site="+site_id+"&client_id="+str,true);
		xmlhttp.send();
	}
}

function GetLeadRecord(str)
{
	if (str=="")
	{
		document.getElementById("lead_ref_date_id").value="";
		//document.getElementById("lead_ref_type_id").innerHTML="";
		//document.getElementById("lead_ref_type_id_hid").value='';
		document.getElementById("div_items").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)
		{
		
			var data_brk=xmlhttp.responseText.split('__');
			
			document.getElementById("lead_ref_date_id").value=data_brk[0];
			document.getElementById("lead_ref_type_id").innerHTML=data_brk[1];
			//document.getElementById("lead_ref_type_id_hid").value=data_brk[2];
			document.getElementById("div_items").innerHTML=data_brk[3];
			document.getElementById("hardware_id").innerHTML=data_brk[4];
			
		}
	}
	xmlhttp.open("GET","CRM_GetLeadsRecord.php?lead="+str,true);
	xmlhttp.send();
}

function calculateItemAmount1(i,str)
{
	var item_qty=document.getElementById("item_qty_id_"+i).value;
	var amount=item_qty*str;
	document.getElementById("item_amount_id_"+i).value=amount.toFixed(2);
}
function calculateItemAmount2(i,str)
{
	var rate=document.getElementById("item_rate_id_"+i).value;
	if(rate!='')
	{
		var amount=str*rate;
		document.getElementById("item_amount_id_"+i).value=amount.toFixed(2);
	}
}
function LeadData()
{	
	var t = document.getElementById('bomproid').value;
	if(document.getElementById('bompro').checked==true)
	{	
		if(t==undefined){
			document.getElementById('bompro_'+0).checked=true;		
		}
		for (var i = 0; i < t; i++)
		{
				document.getElementById('bompro_'+i).checked=true;
		}	
	}
	else
	{
		if(t==undefined){
			document.getElementById('bompro_'+0).checked=false;		
		}
	
		for (var i = 0; i < t; i++)
		{
			document.getElementById('bompro_'+i).checked=false;	
		}
	}
}

function expandbom()
{	
	var t = document.getElementById('bomproid').value;
	
	//alert(t);
	var c = 0;
	var b='';
	for (var i = 0; i < t; i++)
	{
		if(document.getElementById('bompro_'+i).checked==true && document.getElementById('bomsize_'+i).value!=''){
			if(i==t-1){
				b += document.getElementById('bompro_'+i).value+"__"+document.getElementById('bomsize_'+i).value+"__"+document.getElementById('item_qty_id_'+i).value;
			}else{
				b += document.getElementById('bompro_'+i).value+"__"+document.getElementById('bomsize_'+i).value+"__"+document.getElementById('item_qty_id_'+i).value+",";
			}
			c = 1;
		}
	}
	
	//alert(b);
	
	if(c==0){
		alert("Please check atleast one item");
		return false;
	}
	
	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("bom_item").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","CRM_GetBomsRecord.php?bomitem="+b,true);
	xmlhttp.send();
	
	document.getElementById("showbomid").style.display='block';
}
function collapsebom()
{
	document.getElementById("showbomid").style.display='none';
}

function addRow1(tableID) 
{
//alert(tableID);
	if(document)
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
           //	newcell.style.background='#e7e7e7';
			  //newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
			   if(newcell.childNodes[0].id=='psize' && newcell.childNodes[0].type=='hidden'){
						newcell.childNodes[0].type='text';
					}
			   	switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
					var nt=newcell.childNodes[0].name.split("[");
					if(nt[1]!=undefined)
					{
					var nt1=nt[1].split("]");
					var name=nt[0]+'['+rowCount+']'+'['+nt[2];
				    }
					else
					{
					var name=nt[0]+'['+rowCount+']';
					} 
					newcell.childNodes[0].name = name;
					
					//var t=newcell.childNodes[0].name.split("[");
					
					newcell.childNodes[0].name =newcell.childNodes[0].name ;
					
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	}
	
	
	function addRow(tableID) 
{
//alert(tableID);
	if(document)
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
           //	newcell.style.background='#e7e7e7';
			  //newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
			   if(newcell.childNodes[0].id=='psize' && newcell.childNodes[0].type=='hidden'){
						newcell.childNodes[0].type='text';
					}
			   	switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
					
					
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	}
	
	
function deleteRow(tableID) 
{
	try 
	{
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
	
		for(var i=0; i<rowCount; i++) 
		{
			var row = table.rows[i];
			var chkbox = row.cells[0].childNodes[0];
			if(null != chkbox && true == chkbox.checked) 
			{
				if(rowCount <= 1) 
				{
					alert("Cannot delete all the rows.");
					break;
				}
				table.deleteRow(i);
				rowCount--;
				i--;
			}
		}
		document.getElementById("hiditemcount12").value=parseInt(rowCount)-1;		
	}
	
	catch(e) 
	{
		alert(e);
	}
}
function open_items()
{

	window.open("open_item_edit.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}


function cal_primary(str)
{
	var str1=document.getElementById("bomproid").value;
	if(str1=="")
	{
		return;
	}
	var psum = 0;
	for(var i=0;i<str1;i++)
	{
		if(document.getElementById("item_qty_id_"+i).value==''){document.getElementById("item_qty_id_"+i).value=0;}
		psum=parseFloat(psum)+parseFloat(document.getElementById("item_qty_id_"+i).value);
	}
	document.getElementById(str).value=psum.toFixed(2);
}	

function cal_secondary(str)
{
	var str1=document.getElementById("bomproid").value;
	if(str1=="")
	{
		return;
	}
	var psum = 0;
	for(var i=0;i<str1;i++)
	{
		if(document.getElementById("secqty_"+i).value==''){document.getElementById("secqty_"+i).value=0;}
		psum=parseFloat(psum)+parseFloat(document.getElementById("secqty_"+i).value);
	}
	document.getElementById(str).value=psum.toFixed(2);
}	
function cal_gross(str)
{
	var str1=document.getElementById("bomproid").value;
	if(str1=="")
	{
		return;
	}
	var psum = 0;
	for(var i=0;i<str1;i++)
	{
		if(document.getElementById("item_amount_id_"+i).value==''){document.getElementById("item_amount_id_"+i).value=0;}
		psum=parseFloat(psum)+parseFloat(document.getElementById("item_amount_id_"+i).value);
	}
	document.getElementById(str).value=psum.toFixed(2);
}

function calcItemAmt1(str,k,i)
{
	if(document.getElementById("manufact_"+k).value=='')
	{
		alert('Please choose an item before entering its details');
		document.getElementById("manufact_"+k).focus();
		document.getElementById("asmbly_item_qty_"+k+"_"+i).value='';
		document.getElementById("asmbly_item_amt_"+k+"_"+i).value='';
		return false;
	}else
	{
	
		var basicrate = document.getElementById("basicrate_"+k).value
		var total = basicrate*str;
		document.getElementById("asmbly_item_amt_"+k+"_"+i).value=total.toFixed(2);
		
		var gtotal=0;
		var cnthw=document.getElementById("count").value;
		var totalamount=0;
		for(var k=0; k<cnthw; k++)
		{
			var amount=document.getElementById("asmbly_item_amt_"+k+"_"+i).value;
			if(amount==''){amount=0;}
			gtotal=parseFloat(gtotal)+parseFloat(amount);
		}
		document.getElementById("total_hw_amt_id_"+i).value=gtotal.toFixed(2);
		var subtotalA=document.getElementById("totalA_"+i).value;
		if(subtotalA==''){subtotalA=0;}
		var totalofAB=parseFloat(gtotal)+parseFloat(subtotalA);
		document.getElementById("totalC_"+i).value=totalofAB.toFixed(2);
		var totalqty=document.getElementById("totalqtyabc_"+i).value;
		if(totalqty==''){totalqty=0;}
		
		var totalqty1=parseFloat(totalofAB)*parseFloat(totalqty);
		document.getElementById("totalD_"+i).value=totalqty1.toFixed(2);
		//alert(document.getElementById("totalD_"+i).value);
		for(var t=0; t<cnthw; t++)
		{
		if(document.getElementById("totalD_"+t))
		 totalamount=parseFloat(totalamount)+parseFloat(document.getElementById("totalD_"+t).value);
		}
		
		
		document.getElementById("entire_amt").value=totalamount.toFixed(2);
		
		
	}
}
function getItemList(str,id)
{
	//alert(str);
	//alert(id);
	var id_brk=id.split('_');
	//alert(id_brk.length);
	if (str=="")
	{
		if(id_brk.length>1){document.getElementById("item_"+id_brk[1]).innerHTML="";}
		else{document.getElementById("item").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)
		{
			var data=xmlhttp.responseText;
			if(id_brk.length>1){document.getElementById("item_"+id_brk[1]).innerHTML=data;}
			else{
			document.getElementById("item").innerHTML=data;}
		}
		
		
	}
	xmlhttp.open("GET","get_item_list.php?i="+str,true);
	xmlhttp.send();
}
function itemBomDetails(str,id)
{
	var id_brk=id.split('_');
	
	
	var size_array= new Array();
	var count_size=document.getElementById("fassmblycnt").value;
	for(var i=0; i<count_size; i++)
	{
		size_array[i]=document.getElementById("item_size_"+i).value;
	}
	
	
	
	
	
	if (str=="")
	{
		if(id_brk.length>1){
		document.getElementById("basicrateA_"+id_brk[1]).value="";
		document.getElementById("uomA_"+id_brk[1]).value='';
		var totalamount=0;
		for(var k=0;k<count_size;k++)
		{
		if(document.getElementById("asmbly_item_amtA_"+k+"_"+id_brk[1]).value!='')
		var amt=parseFloat(document.getElementById("asmbly_item_amtA_"+k+"_"+id_brk[1]).value);
		else
		var amt=0;
		if(document.getElementById("totalA_"+k).value!='')
		var amt1=parseFloat(document.getElementById("totalA_"+k).value);
		else
		var amt1=0;	
			
		var amt2=amt1-amt;
		document.getElementById("asmbly_item_qtyA_"+k+"_"+id_brk[1]).value='';
		document.getElementById("asmbly_item_amtA_"+k+"_"+id_brk[1]).value='';
		document.getElementById("totalA_"+k).value=amt2.toFixed(2);
		
		
		
		   var subtotal_c=document.getElementById("totalC_"+k).value;
			if(subtotal_c==''){subtotal_c=0;}
			var subtotalc_c=parseFloat(subtotal_c)-parseFloat(amt);
			document.getElementById("totalC_"+k).value=subtotalc_c.toFixed(2);
		
			var totalqty=document.getElementById("totalqtyabc_"+k).value;
			if(totalqty==''){totalqty=0;}
			
			var totalABC=parseFloat(totalqty)*parseFloat(subtotalc_c);
			
			document.getElementById("totalD_"+k).value=totalABC.toFixed(2);
					 
		   totalamount=parseFloat(totalamount)+parseFloat(document.getElementById("totalD_"+k).value);
		
		}
		
		document.getElementById("entire_amt").value=totalamount.toFixed(2);
		
		}
		else{
		
		var totalamount=0;
		
		for(var k=0;k<count_size;k++)
		{
		if(document.getElementById("asmbly_item_amtA_"+k).value!='')
		var amt=parseFloat(document.getElementById("asmbly_item_amtA_"+k).value);
		else
		var amt=0;
		if(document.getElementById("totalA_"+k).value!='')
		var amt1=parseFloat(document.getElementById("totalA_"+k).value);
		else
		var amt1=0;
		var amt2=amt1-amt;
		
		document.getElementById("asmbly_item_qtyA_"+k).value='';
		document.getElementById("asmbly_item_amtA_"+k).value='';
		document.getElementById("totalA_"+k).value=amt2.toFixed(2);
		
		   var subtotal_c=document.getElementById("totalC_"+k).value;
			if(subtotal_c==''){subtotal_c=0;}
			var subtotalc_c=parseFloat(subtotal_c)-parseFloat(amt);
			document.getElementById("totalC_"+k).value=subtotalc_c.toFixed(2);
		
			var totalqty=document.getElementById("totalqtyabc_"+k).value;
			if(totalqty==''){totalqty=0;}
			
			var totalABC=parseFloat(totalqty)*parseFloat(subtotalc_c);
			
			document.getElementById("totalD_"+k).value=totalABC.toFixed(2);
		 
		  totalamount=parseFloat(totalamount)+parseFloat(document.getElementById("totalD_"+k).value);
		
		
		}
		
		document.getElementById("entire_amt").value=totalamount.toFixed(2);
		document.getElementById("basicrateA").value='';
		document.getElementById("uomA").value='';
			}
		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)
		{
		               
			var data_brk=xmlhttp.responseText.split('__');
			var data2=data_brk[2].split("--");
			var data3=data_brk[3].split("--");
			if(id_brk.length>1)
			{
			
			var totalamount=0;					
			document.getElementById("basicrateA_"+id_brk[1]).value=data_brk[0];
			document.getElementById("uomA_"+id_brk[1]).value=data_brk[1];
			
			for(var k=0;k<count_size;k++)
			{
			if(document.getElementById("totalA_"+k).value!='')
			var amount=parseFloat(document.getElementById("totalA_"+k).value)+parseFloat(data3[k]);
			else
			var amount=parseFloat(data3[k]);
			
			
					
			document.getElementById("asmbly_item_qtyA_"+k+"_"+id_brk[1]).value=data2[k];
			document.getElementById("asmbly_item_amtA_"+k+"_"+id_brk[1]).value=data3[k];
			document.getElementById("totalA_"+k).value=amount.toFixed(2);
			
			var subtotal_b=document.getElementById("total_hw_amt_id_"+k).value;
			if(subtotal_b==''){subtotal_b=0;}
			var totalAB=parseFloat(amount)+parseFloat(subtotal_b);
			document.getElementById("totalC_"+k).value=totalAB.toFixed(2);
			
			var subtotal_c=document.getElementById("totalC_"+k).value;
			if(subtotal_c==''){subtotal_c=0;}
			
			var totalqty=document.getElementById("totalqtyabc_"+k).value;
			if(totalqty==''){totalqty=0;}
			
			var totalABC=parseFloat(totalqty)*parseFloat(subtotal_c);
			document.getElementById("totalD_"+k).value=totalABC.toFixed(2);
			
			 totalamount=parseFloat(totalamount)+parseFloat(document.getElementById("totalD_"+k).value);
			}	
				
			document.getElementById("entire_amt").value=totalamount.toFixed(2);
			}
			else
			{	
			var totalamount=0;		
			document.getElementById("basicrateA").value=data_brk[0];
			document.getElementById("uomA").value=data_brk[1];
			for(var k=0;k<count_size;k++)
			{
			
			if(document.getElementById("totalA_"+k).value!='')
			{
			var amount=parseFloat(document.getElementById("totalA_"+k).value)+parseFloat(data3[k]);
			}
			else
			{
			var amount=parseFloat(data3[k]);
			}
			
			
			
			
			document.getElementById("asmbly_item_qtyA_"+k).value=data2[k];
			document.getElementById("asmbly_item_amtA_"+k).value=data3[k];
			document.getElementById("totalA_"+k).value=amount.toFixed(2);
			
			var subtotal_b=document.getElementById("total_hw_amt_id_"+k).value;
			if(subtotal_b==''){subtotal_b=0;}
			var totalAB=parseFloat(amount)+parseFloat(subtotal_b);
			document.getElementById("totalC_"+k).value=totalAB.toFixed(2);
			
			var subtotal_c=document.getElementById("totalC_"+k).value;
			if(subtotal_c==''){subtotal_c=0;}
			
			var totalqty=document.getElementById("totalqtyabc_"+k).value;
			if(totalqty==''){totalqty=0;}
			
			var totalABC=parseFloat(totalqty)*parseFloat(subtotal_c);
			document.getElementById("totalD_"+k).value=totalABC.toFixed(2);
			 totalamount=parseFloat(totalamount)+parseFloat(document.getElementById("totalD_"+k).value);
			
			}
			document.getElementById("entire_amt").value=totalamount.toFixed(2);

			}
			
			
		}
	}
	if(id_brk.length>1)
	var frame=document.getElementById("itemsubcat_"+id_brk[1]).value;
	else
	var frame=document.getElementById("itemsubcat").value;
	
	xmlhttp.open("GET","get_item_bomdetails.php?i="+str+"&frame="+frame+"&size="+size_array,true);
	xmlhttp.send();
	}

function calculateAllAmt(id)
{
	var id_brk=id.split('_');
	
	var size_array= new Array();
	var count_size=document.getElementById("fassmblycnt").value;
	
	for(var i=0; i<count_size; i++)
	{
		size_array[i]=document.getElementById("item_size_"+i).value;
	}
	
	
	
	
	if (str=="")
	{
		//if(id_brk.length>1){
		//document.getElementById("basicrateA_"+id_brk[1]).value="";
		//document.getElementById("uomA_"+id_brk[1]).value='';}
		//else{
		//document.getElementById("basicrateA").value='';
		//document.getElementById("uomA").value='';
			//}
		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)
		{
			var data_brk=xmlhttp.responseText.split('__');
			
			
			/*if(id_brk.length>1){
			document.getElementById("basicrateA_"+id_brk[1]).value=data_brk[0];
			document.getElementById("uomA_"+id_brk[1]).value=data_brk[1];
			}
			else{
			document.getElementById("basicrateA").value=data_brk[0];
			document.getElementById("uomA").value=data_brk[1];
			}*/
		}
	}
	xmlhttp.open("GET","get_item_bomdetails.php?i="+str,true);
	xmlhttp.send();
}

function calcValue1(str,id)
{


	var brk=id.split('_');
	
	
	if(brk[1]==undefined)
	{
	  /* if(parseFloat(str)>parseFloat(document.getElementById("total_stock").value))
	   {
	   alert("Qty Should be less or equal then Stock Qty");
	   document.getElementById("stock_qty").value='';
	   return false;
	   }
       else
	   {*/	
	   if(str!='')
	   {
	    if(document.getElementById("convfac").value!='')
	   var price=document.getElementById("convfac").value;
	   else
	   var price=0;
		
		
		var tot=parseFloat(str)*parseFloat(price);
		}
		else
		var tot=0;
		document.getElementById("secondaryqty").value=tot.toFixed(3);
				/*}*/
	}
	else
	{
	  /*if(parseFloat(str)>parseFloat(document.getElementById("total_stock_"+brk[2]).value))
	   {
	   alert("Qty Should be less or equal then Stock Qty");
	   document.getElementById("stock_qty_"+brk[2]).value='';
	   return false;
	   }*/
	     if(str!='')
	   {
	   if(document.getElementById("convfac_"+brk[1]).value!='')
	   var price=document.getElementById("convfac_"+brk[1]).value;
	   else
	   var price=0;
	  
		var tot=parseFloat(str)*parseFloat(price);
		}
		else
		var tot=0;
		document.getElementById("secondaryqty_"+brk[1]).value=tot.toFixed(3);
	   
		
	}
	
}

function showconvac(str,id)
{
var brk=id.split('_');

if(str=='0')
{

if(brk[1]==undefined)
{

var conf=document.getElementById('conf').value;
	document.getElementById("convfac").value=conf;
	}
	else
	{
	var conf=document.getElementById('conf_'+brk[1]).value;
	document.getElementById("convfac_"+brk[1]).value=conf;
	}
}
else
{
var brk=id.split('_');

 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)
	{
	if(brk[1]==undefined)
	document.getElementById("convfac").value=xmlhttp.responseText;
	else
	document.getElementById("convfac_"+brk[1]).value=xmlhttp.responseText;
	//document.getElementById("empdta").innerHTML=data[1];
	
	}
	}
	if(brk[1]==undefined)
	{
	var str2=document.getElementById("itemid").value;
	}
	else
	{
	var str2=document.getElementById("itemid_"+brk[1]).value;
	}
	xmlhttp.open("GET","convactor.php?site="+str+"&itemid="+str2,true);
	xmlhttp.send();

}




}

function showbascicamt(str,id)
{

var id_brk=id.split('_');
	
	
	if (str=="")
	{
		
		document.getElementById("basicrate_"+id_brk[1]).value="";
		document.getElementById("uomA_"+id_brk[1]).value='';
		
		
		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)
		{
		               
			var data_brk=xmlhttp.responseText.split('__');
			
			document.getElementById("basicrate_"+id_brk[1]).value=data_brk[0];
			document.getElementById("uomaT_"+id_brk[1]).value=data_brk[1];
			
			
		}
	}

	
	xmlhttp.open("GET","get_item_wise_basic.php?i="+str,true);
	xmlhttp.send();




}

function validate()
{
if(document.getElementById("sub_group").value=='')
{
alert("Must Select Material Sub Group");
return false;
}
if(document.getElementById("client_id").value=='')
{
alert("Must Select Client First");
return false;
}
return true;


}



</script>