<?php
$sql=DB_query("select * from cstest_estimation_bom where id='".$_REQUEST['viewid']."'",$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">
<center> <h2>Estimation Sheet Against BOM </h2>
</center>


<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #999999;">
  <tr class="row1">
    <td width="13%" class="tabledata"><strong>Branch</strong></td>
    <td width="20%"><?php echo $s->getField('locations','loccode',$branch,'loccode') ; ?></td>
    <td width="13%" class="tabledata"><strong>Sheet No.</strong></td>
    <td width="20%"><?php echo $sheet_no; ?></td>
    <td class="tabledata"><strong>Date</strong></td>
    <td width="20%"><?php echo  ConvertSQLDate($sheet_date); ?></td>
  </tr>
  <tr class="row1">
  <td class="tabledata"><strong>Against</strong></td>
    <td><?php echo  $against; ?></td>
  	<td class="tabledata"><strong>Client </strong></td>
    <td><?php echo $s->getField('clientmaster','id',$client,'c_name'); ?></td>
	
	<td class="tabledata"><strong>Lead/Ref. No.</strong></td>
    <td id="div_lead"><?php echo $s->getField('crm_leads','leadid',$lead_ref,'leadcode'); ?></td>
  </tr>
  <tr class="row1">
  	<td id="test" class="tabledata"><strong>Lead/Ref. Date</strong></td>
    <td><?php  echo ConvertSQLDate($lead_ref_date); ?></td>
    <td class="tabledata"><strong>Type </strong></td>
    <td><?php echo $s->getField('type_master','id',$type,'name'); ?>	</td>
    <td class="tabledata"><strong>Material Sub Group</strong></td>
    <td><?php echo $s->getField('stockcategory','id',$sub_group,'categorydescription'); ?></td>
  </tr>
</table>
<br />
<fieldset>
<legend style="font-size:13px;"><strong>Finished Product Details</strong></legend>

		<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 class="tabledata"><strong>Item Code</strong></td>
    <td  class="tabledata"><strong>Item Name</strong></td>
    <td  class="tabledata"><strong>Size</strong></td>
     <td  class="tabledata"  colspan="5"  style="text-align:center" ><strong>Quantity (Q)</strong></td>
    
  </tr>
  <?php 
  $i=0;
  $finish_qty=array();
  $finish_primay_qty=array();
  $finish_secondary_qty=array();
  $unit_primary='';
  $unit_secondary='';
  $sql_details=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['viewid']."'",$db);
  while($row1=DB_fetch_array($sql_details))
  {
      if($unit_primary=='')
	  $unit_primary=$s->getField('item_master','id',$row1['mst_itemid'],'mst_unitid');
	  if($unit_secondary=='')
	  $unit_secondary=$s->getField('item_master','id',$row1['mst_itemid'],'conv_unit');
	  $unit=$s->getField('item_master','id',$row1['mst_itemid'],'mst_unitid');
	  $secondunit=$s->getField('item_master','id',$row1['mst_itemid'],'conv_unit');
	  $finish_qty[$i]=$row1['item_prim_qty'];
  
  
  $finish_primay_qty[]=$row1['item_prim_qty'];
  $finish_secondary_qty[]=$row1['item_sec_qty'];
  ?>
  <tr class="row1">
    <td><?php echo $s->getField('item_master','id',$row1['mst_itemid'],'StockID'); ?></td>
	 <td><?php echo $s->getField('item_master','id',$row1['mst_itemid'],'item_name'); ?></td>
	   <td><?php  echo $s->getField('size_master','id',$row1['item_size'],'size'); ?></td>
       	  <td><?php echo $row1['item_prim_qty']; ?></td>
		   <td><?php echo $s->getField('unitsofmeasure','unitid',$unit,'unitname'); ?></td>
			 <td><?php echo $row1['item_sec_qty']; ?></td>
			  <td><?php echo $s->getField('unitsofmeasure','unitid',$secondunit,'unitname'); ?></td>
			   
  </tr>
  <?php $i++; } ?>
</table>
</div>
</fieldset>

<fieldset>
<legend style="font-size:13px;"><strong></strong></legend>
<table width="100%" border="0">
  <tr class="row1">
    <td colspan="3" rowspan="2" class="tabledata"><strong>Assembly Details</strong></td>
	<td  align="right" class="tabledata"><strong>Item </strong></td>
	<?php 
	$sql4=DB_query("SELECT * FROM `cstest_estimation_bom_details` where master_id=".$_REQUEST['viewid']."",$db);
	$kk=0;
	while($row4=DB_fetch_array($sql4))
	{
		$item_array[$kk]['id']=$row4['mst_itemid'];
		$item_array[$kk]['size']=$row4['item_size'];
		$kk++;
	}
	
	foreach($item_array as $key=>$v)
   {
?>
    <td class="tabledata" colspan="2" style="font-size:11px;"><?php echo  $s->GetFieldValue('item_master',"id='".$v['id']."'",'item_name'); ?></td>
  <?php } ?>
  </tr>
  <tr class="row1">
  	<td align="right" class="tabledata"><strong>Size </strong></td>
    <?php
	
	foreach($item_array as $key=>$v)
   {
?>
<td class="tabledata" colspan="2" style="font-size:11px;"><?php echo $s->GetFieldValue('size_master',"id='".$v['size']."'",'size'); ?> </td>
<?php } ?>	
  </tr>
  <?php 
  
  
  ?>
  <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>
 <?php 	foreach($item_array as $key=>$v)
   { ?>
    <td class="tabledata"><strong>QTY</strong></td><td class="tabledata"><strong>Amount</strong></td>
	<?php } ?>
  </tr>
  
  <?php
  $assemamt=array();
  $item2=array(); 
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['viewid']."'",$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);
			  
			  
 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=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++;
		}
  
  
  ?>
  <tr class="row1">
    <td ><?php echo $s->getField('stockcategory','id',$assem_row['mst_asmbly_cat_id'],'categorydescription'); ?></td>
    <td ><?php echo $s->getField('item_master','id',$itemid,'item_name'); ?></td>
    <td ><?php echo $basic; ?> </td>
    <td ><?php  $unit= $s->getField('item_master','id',$itemid,'mst_unitid');	echo $s->getField('unitsofmeasure','unitid',$unit,'unitname'); ?></td>
 <?php
 foreach($item_array1 as $key=>$v)
   { 
		if($assemamt[$key]==$key)
		{
		
		$assemamt[$key][]=$assemamt[$key]+$v['asmbly_item_amt'];
		}
		else
		{
		$assemamt[$key][]=$v['asmbly_item_amt'];
		}
   ?>
    <td ><?php echo $v['asmbly_item_qty']; ?></td><td ><?php echo $v['asmbly_item_amt']; ?></td>
	<?php } ?>
  </tr>
  
  <?php } ?>
  
  <tr class="row1">
    <td colspan="4" class="tabledata"><strong>Sub Total (A) </strong> </td>
    <?php 
 
foreach($item_array1 as $key=>$v)
  { 
   
   ?>
    <td class="tabledata" ></td><td class="tabledata" ><?php echo  array_sum($assemamt[$key]); ?></td>
	<?php } ?>
  </tr>
  

<tr class="row1">
 
   
	<?php 
 $coun= count($item_array1);
  $coun1=($coun*2)+4;
 ?>
  <td colspan="<?php echo $coun1; ?>" class="tabledata"  ><strong>Sub Assembly Details</strong></td>
  </tr>
 
 
  <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>
 <?php 
foreach($item_array as $key=>$v)
   {
    ?>
    <td class="tabledata"><strong>QTY</strong></td><td class="tabledata"><strong>Amount</strong></td>
	<?php } ?>
  </tr>
  
  <?php
  $item2=array(); 
  $hrdamt=array();
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['viewid']."'",$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++;
			}
		  
  
  ?>
  <tr class="row1">
    <td ><?php echo $s->getField('product_manufacture','id',$assem_row['mst_asmbly_cat_id'],'manufacturer'); ?></td>
    <td ><?php echo $s->getField('item_master','id',$itemid,'item_name'); ?></td>
    <td ><?php echo $basic; ?> </td>
    <td ><?php $unit= $s->getField('item_master','id',$itemid,'mst_unitid');	echo $s->getField('unitsofmeasure','unitid',$unit,'unitname');?></td>
 <?php 
 foreach($item_array1 as $key=>$v)
   { 
       if($hrdamt[$key]==$key)
		{ 	$hrdamt[$key][]=$hrdamt[$key]+$v['asmbly_item_amt'];}
		else {	$hrdamt[$key][]=$v['asmbly_item_amt'];	}
   
   
   ?>
    <td ><?php echo $v['asmbly_item_qty']; ?></td><td style="font-size:11px;" ><?php echo $v['asmbly_item_amt']; ?></td>
	<?php } ?>
  </tr>
  
  <?php } ?>
  <tr class="row1">
    <td colspan="4" class="tabledata"><strong>Sub Total (B) </strong> </td>
    <?php 
foreach($item_array1 as $key=>$v)
{
  ?>
    <td class="tabledata" ></td><td class="tabledata" ><?php echo  array_sum($hrdamt[$key]); ?></td>
	<?php } ?>
  </tr>
<tr class="row1">
    <td colspan="4" class="tabledata"><strong>Total Amount(C=A+B)</strong> </td>
    <?php 
foreach($item_array1 as $key=>$v)
   { 
   
   ?>
    <td class="tabledata"></td><td class="tabledata"><?php echo  array_sum($hrdamt[$key])+array_sum($assemamt[$key]); ?></td>
	<?php } ?>
  </tr>
  <tr class="row1">
    <td colspan="4" class="tabledata"><strong>Grand Total (D=C*Q)</strong> </td>
    <?php 
 
 	$totalamt=array();
foreach($item_array1 as $key=>$v)
{
 ?>
    <td class="tabledata"></td>
    <td class="tabledata"><strong><?php 
	$totalamt[]=(array_sum($hrdamt[$key])+array_sum($assemamt[$key]))*($finish_qty[$key]);
	echo   (array_sum($hrdamt[$key])+array_sum($assemamt[$key]))*($finish_qty[$key]); ?></strong></td>
<?php } ?>
  </tr>
</table>
</fieldset>


<br />
<table width="100%" border="1" cellspacing="1" cellpadding="3">
  <tr>
    <th width="27%" rowspan="2" align="left" class="tabledata"><strong>Entire Quantity</strong></th>
    <td width="15%" align="left" style="padding:0px;"><table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td width="50%" style="border-right:#666666 1px solid; font-size:13px;"><?php echo array_sum($finish_primay_qty); ?></td>
    <td style="font-size:13px;"><?php  echo $s->getField('unitsofmeasure','unitid',$unit_primary,'unitname'); ?></td>
  </tr>
</table>
</td>
    <th width="15%" rowspan="3" align="left" class="tabledata">Remarks</th>
    <td rowspan="3" valign="top"><textarea rows="3" name="remark" style="width:100%; height:70px;" readonly="readonly"><?php echo $remark; ?></textarea></td>
  </tr>
  <tr>
    <td align="left" style="padding:0px;"><table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td width="50%" style="border-right:#666666 1px solid; font-size:13px;"><?php echo array_sum($finish_secondary_qty); ?></td>
    <td style="font-size:13px;"><?php echo $s->getField('unitsofmeasure','unitid',$unit_secondary,'unitname'); ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <th align="left" class="tabledata">Total Amount for Entire Quantity (&#931;D)</th>
    <td align="left" style="font-size:13px;"><?php echo array_sum($totalamt); ?></td>
  </tr>
</table>

<p align="center">
  <!-- onclick="return checkBom();"-->
</p>

<script language="javascript" type="text/javascript">
function displayHardwareBox()
{
	document.getElementById("showhardware").style.display='block';
}

function activateDeactivate(str)
{
	if(str=='lead')
	{
		document.getElementById("addmore").disabled=true;
		document.getElementById("delete").disabled=true;
		document.getElementById("profile_popup").disabled=true;
	}
	else if(str=='none')
	{
		document.getElementById("addmore").disabled=false;
		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").value="";
		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").value=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("hiditemcount").value=parseInt(rowCount)-1;		
	}
	
	catch(e) 
	{
		alert(e);
	}
}
function open_items()
{

	window.open("open_item.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;
		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);
	}
}
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='';
		for(var k=0;k<count_size;k++)
		{
		var amt=parseFloat(document.getElementById("asmbly_item_amtA_"+k+"_"+id_brk[1]).value);
		var amt1=parseFloat(document.getElementById("totalA_"+k).value);
		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);
		}
		
		}
		else{
		for(var k=0;k<count_size;k++)
		{
		var amt=parseFloat(document.getElementById("asmbly_item_amtA_"+k).value);
		var amt1=parseFloat(document.getElementById("totalA_"+k).value);
		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);
		}
		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)
			{	
					
			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);
			}		
			
			}
			else
			{			
			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);
			}

			}
		}
	}
	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;
	alert(count_size);
	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();
}
</script>