<?php
$page_title='Estimation Sheet BOM';
$prefix=$s->GetFieldValue('prefix_tbl',"prefix_name='".$page_title."'",'prefix_code');
if(isset($_POST['prepare']))
{  
  
	$codeSQL=DB_query("select sheet_no as sheetid from cstest_estimation_bom order by id desc limit 0,1",$db);
	if(DB_num_rows($codeSQL)>0)
	{
	$Code1=DB_fetch_array($codeSQL);
	$exp=explode("/",$Code1['sheetid']);
	$maxval=$exp[1]+1;
	$finalcode=$prefix.'/'.str_pad($maxval,5,"0",STR_PAD_LEFT);
	}
	else
	{
		$finalcode=$prefix.'/'.'00001';
	}
	
	
	$data['branch']=$_REQUEST['site'];
	$data['sheet_no']=$finalcode;
	$data['sheet_date']=FormatDateFORSQL($_REQUEST['sheet_date']);
	$data['against']=$_REQUEST['against'];
	$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['CompanyId']=$_SESSION['ACGCompanyId'];
	$data['created_date']=date('Y-m-d');
	$data['created_by']=$_SESSION['UserID'];
	$s->RecordInsert('cstest_estimation_bom',$data);
	$getlastid1=DB_Last_Insert_ID($db,'cstest_estimation_bom', 'id');
	
		for($j=0;$j<count($_REQUEST['mst_itemid']);$j++)
		{
				$datahard['master_id']=$getlastid1;
				$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['secondary_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;	
						$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);
}?>

<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 </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">
    <td width="13%" class="tabledata"><strong>Branch</strong></td>
    <td width="20%"><select name="site" id="site_id" style="width:100%;">
    <option value="">All Branches</option>
     <?php 
		$site_sql = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
		$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="Auto Generated" 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 date('d/m/Y');?>"/></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="">--Select--</option>
      <option value="lead">LEAD</option>
	  <option value="none">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)">
	<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 echo $row1['c_name'];?></option>
	<?php 
	}
	?>
	
	</select></td>
	
	<td class="tabledata"><strong>Lead/Ref. No.</strong></td>
    <td id="div_lead"><input type="text" name="lead_ref_no" id="lead_ref_id" value=""  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=""  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 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 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">
			
          <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;--><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">
  <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=""></td>
    <td><input name="itemcode[]" id="itemcode" type="text" value=""></td>
	<td><input name="itemname[]" id="itemname" type="text" value=""></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($row3['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="stock_qty" style="width:100%;" onkeyup="calcValue1(this.value,this.id)" onblur="calcValue1(this.value,this.id)" /></td>
<td ><select name="unit_prim[]" id="unit_prim" style="width:70px;" >
            <option value="0">Select</option>
               </select>
</td>
<td ><input type="hidden" name="conv_fac" id="conv_fac" /></td>
<td ><input type="text" name="secondary_qty[]"  id="secondary_qty" style="width:100%;"/></td>
<td ><select name="unit_sec[]" id="unit_sec" style="width:70px;" >
            <option value="0">Select</option>
                </select></td>
<td><input type="hidden" name="conf[]" id="conf" style="width:100%;"  readonly="true" /></td>
				
   
  </tr>
  </tbody>
 
</table>
<input type="hidden" name="hiditemcount" id="hiditemcount12" value="" />
<input type="hidden" name="" value="" 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">

</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="" /></td>
		</tr>
		<tr>
		<td><input type="text" name="sm_qty" id="sm_qty" value="" /></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="" /></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%"></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')
  {
  
      	  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("stock_qty").value;
						   qty_array1[i]=document.getElementById("secondary_qty").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("stock_qty_"+i).value;
							qty_array1[i]=document.getElementById("secondary_qty_"+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.php?size="+size_array+"&itemarray="+item_array+"&qty="+qty_array,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.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++)
		{
		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);
		
		
		
		   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++)
		{
		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);
		
		   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;
	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();
}

function calcValue1(str,id)
{

	var brk=id.split('_');
	if(brk.length==2)
	{
	  /* 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("conv_fac").value!='')
	   var price=document.getElementById("conv_fac").value;
	   else
	   var price=0;
		
		
		var tot=parseFloat(str)*parseFloat(price);
		}
		else
		var tot=0;
		document.getElementById("secondary_qty").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("conv_fac_"+brk[2]).value!='')
	   var price=document.getElementById("conv_fac_"+brk[2]).value;
	   else
	   var price=0;
	  
		var tot=parseFloat(str)*parseFloat(price);
		}
		else
		var tot=0;
		document.getElementById("secondary_qty_"+brk[2]).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("conv_fac").value=conf;
	}
	else
	{
	var conf=document.getElementById('conf_'+brk[1]).value;
	document.getElementById("conv_fac_"+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("conv_fac").value=xmlhttp.responseText;
	else
	document.getElementById("conv_fac_"+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,rr)
{

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>