<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
?>
<link href="css/professional/default.css" rel="stylesheet" type="text/css" />

<style type="text/css">
<!--
.style1 {
	font-size: 12px;
	font-weight: bold;
	font-family: "Comic Sans MS";
}
-->
</style>
<h2 align="center">SEARCH ITEMS</h2>
<table width="100%" style="border: solid 1px #CCCCCC">
  <tr>
    <th width="29%"><strong>Part Name</strong></th>
    <td width="35%"> <input name="part_name" type="text" id="part_name"    onkeyup="getItems2(this.value)"  />    </td>
    <td width="29%"><span class="style1">Main Category </span></td>
	<td   width="18%"><select name="manicat" id="maincat" onchange="getItems3(this.value)" >
	<option value="0">Select </option>
	<?php                             
	$stock_cat=DB_query("select * from  stockmaincategory where CompanyId='".$_SESSION['ACGCompanyId']."'
	 and stockmaincategoryid in('FG','SF')",$db);
	while($stockcat=DB_fetch_array($stock_cat))
	{
	?>
	<option value="<?php echo $stockcat['id']; ?>"><?php echo $stockcat['stockmaincategoryname']; ?></option>
	<?php }	 ?></select>
	
	
	</td>
  </tr>
</table>
  <table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
    <tr>
      <th width="12%"><strong>SELECT</strong></th>
      <th width="23%"><strong>Part Name</strong></th>
      <th width="39%"><strong>ITEM DESCRIPTION </strong></th>
      <th width="26%"><strong>UNIT</strong></th>
    </tr>
  </table>
  <?php
     $categoryid=array();
     $catid=DB_query("select * from stockmaincategory where stockmaincategoryid  in('FG','SF') 
	 and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
     while($cat1=DB_fetch_array($catid))
	 {
	 $categoryid[]=$cat1['id'];
	 }
	 $category=implode("','",$categoryid);
	 
  
 	$sql=DB_query("SELECT * FROM item_master WHERE CategoryID in('". $category."') ",$db); 
	?>
  <div style="overflow: scroll; height: 300px" id="sub_id">
    <table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
      <?php	
	
	$i=0;
	if(DB_num_rows($sql)>0)
	{
		while($myrow=DB_fetch_array($sql))
	  	{ 	
		
		 $parameter=$myrow['id'];
		 $data1='';
		
		   $unit=DB_query("select * from  unitsofmeasure 
		where unitid ='".$myrow['mst_unitid']."'   ",$db);
		$numrow=DB_num_rows($unit);
		if($numrow>0)
		{
						   while($unit_row=DB_fetch_array($unit))
						   {
						   $data1.="<option value=".$unit_row['unitid'].">".$unit_row['unitname']."</option>";
						   }
		 
		 }
		
		 
		   $data2='';
		 $secondaryunit=DB_query("select * from  unitsofmeasure 
		where  unitid='".$myrow['conv_unit']."'  ",$db);
		$numrow1=DB_num_rows($secondaryunit);
		if($numrow1>0)
		{
						   while($unit_row1=DB_fetch_array($secondaryunit))
						   {
						   $data2.="<option value=".$unit_row1['unitid'].">".$unit_row1['unitname']."</option>";
						   }
		 
		 }
         
		 if($myrow['conv_factor']!='')
		 $convac=$myrow['conv_factor'];
		 else
		 $convac=0;	
		
		
		
		
		 
		 $unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname'); 
		 
  ?>
      <tr bgcolor="#CCCCCC">
        <td width="12%"><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>"	<?php echo $c;?> 
		onclick="getItemsList('<?php echo $parameter;?>','<?php echo $myrow['StockID'];?>','<?php echo  $myrow['item_name'];?>','<?php echo $myrow['item_desc'];?>','<?php echo $data1; ?>','<?php echo $data2; ?>',<?php echo $convac; ?>,'<?php echo $i;?>')"/>
        </td>
        <td width="24%"><?php echo  $myrow['item_name'];?></td>
        <td width="40%"><?php echo $myrow['item_desc'];?></td>
        <td width="24%"><?php //echo $unit;?>
          <?php echo $unit?></td>
      </tr>
      <?php $i++;} }?>
    </table>
  </div>
</table>
<script language="javascript" >


function getItems2(str)
{

	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("sub_id").innerHTML=xmlhttp.responseText;
    	
      }
		}
xmlhttp.open("GET","item_category.php?q="+str,true);
xmlhttp.send();

	}
function getItems3(str)
{

	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("sub_id").innerHTML=xmlhttp.responseText;
    	
      }
		}
xmlhttp.open("GET","item_category_fab.php?q="+str,true);
xmlhttp.send();
}



function getItemsList(id,code,shade,desc,data11,data12,data4,ctr)
{

  
	var dup='empty';
	var itemcount=parseInt(window.opener.document.getElementById('hiditemcount12').value)+1;
	var edit_cnt=window.opener.document.getElementById('edit_cnt').value;
	if(edit_cnt=='')
		{var counter=1;} 
	else if(edit_cnt>itemcount)
		{var counter=edit_cnt;
		var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} else  {var counter=edit_cnt;}
	

	
	if(document.getElementById("row_id_"+ctr).checked==true)
	{
       
	  
		if(window.opener.document.getElementById('itemid').value=='')
		{
			if(edit_cnt==''){var counter=0;}
			if(dup=='empty')
			{		
	if(window.opener.document.getElementById('itemid'))
	{	
	if(window.opener.document.getElementById('itemid').value=='')
	{	
	
	var data= addRow1('dataTable');
	window.opener.document.getElementById('itemid').value = id;
	window.opener.document.getElementById('itemcode').value = code;
	window.opener.document.getElementById('itemname').value = shade;
	window.opener.document.getElementById('unit_prim').innerHTML =data11;
	window.opener.document.getElementById('conv_fac').value =data4;
	window.opener.document.getElementById('unit_sec').innerHTML =data12;
	window.opener.document.getElementById('conf').value=data4;
	
	}
	}
		}
		}
		
		else
		{
		
			for(var i=counter; i<=itemcount; i++)
			{
			
			
				if(edit_cnt==''){var counter=0;}
									
				if(dup=='empty')
				{	
				
				 if(window.opener.document.getElementById('itemid_'+i))
				 {
				 
					if(window.opener.document.getElementById('itemid_'+i).value=='')
					{
					
						var data= addRow1('dataTable');				
			window.opener.document.getElementById('itemid_'+i).value = id;
			window.opener.document.getElementById('itemcode_'+i).value = code;
			window.opener.document.getElementById('itemname_'+i).value = shade;
			window.opener.document.getElementById('unitprim_'+i).innerHTML =data11;
			window.opener.document.getElementById('convfac_'+i).value =data4;
			window.opener.document.getElementById('unitsec_'+i).innerHTML =data12;
			window.opener.document.getElementById('conf_'+i).value=data4;
			
			
	            break;
					
					
				}
				}
				
				
					
					else
					{
						var data= addRow1('dataTable');		
			if(window.opener.document.getElementById('itemid_'+i).value=='')
					{					
			window.opener.document.getElementById('itemid_'+i).value = id;
			window.opener.document.getElementById('itemcode_'+i).value = code;
			window.opener.document.getElementById('itemname_'+i).value = shade;
			window.opener.document.getElementById('unitprim_'+i).innerHTML =data11;
			window.opener.document.getElementById('convfac_'+i).value =data4;
			window.opener.document.getElementById('unitsec_'+i).innerHTML =data12;
			window.opener.document.getElementById('conf_'+i).value=data4;
			}
			else
			{
						continue;
			}			
					}
				}
			}
			
		
			
				
		}
	}
	else
	{
	
	
		if(window.opener.document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
			 //var data= deleteRowOrder1('dataTable');	
	window.opener.document.getElementById('itemid').value = '';
	window.opener.document.getElementById('itemcode').value = '';
	window.opener.document.getElementById('itemname').value = '';
	window.opener.document.getElementById('total_stock').value ='';
	window.opener.document.getElementById('conf').value='';
	
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(window.opener.document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{	
				//alert("manoj");
				// var data= deleteRowOrder1('dataTable');				
        window.opener.document.getElementById('itemid_'+i).value = '';
		window.opener.document.getElementById('itemcode_'+i).value = '';
		window.opener.document.getElementById('itemname_'+i).value = '';
		window.opener.document.getElementById('total_stock_'+i).value ='';
		window.opener.document.getElementById('conf_'+i).value='';
	  
				}
			}
		}
	}
}



function addRow1(tableID) 
{
//alert(tableID);
	if(document)
		var table = window.opener.document.getElementById(tableID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		window.opener.document.getElementById("hiditemcount12").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 deleteRowOrder(tableID) 
    {
	alert("hell");
	
}
	


</script>
