<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');
?>
<link rel="stylesheet" href="css/professional/default.css">
<div class="mainwrapper">
<div class="mwrapper">
<link rel="stylesheet" href="css/new_mxerp.css">

<div class="body_contain" style="padding:0;">
<div class="tab_div">
<form name="form1" action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td width="10%" align="right">Group</td>
	<td width="15%"><select name="under_group" id="under_group">
		<option value="0">Select</option>
		<?php $sql="select * from machine_group_master";
		$query=DB_query($sql,$db);
		while($row1=DB_fetch_array($query))
		{
      $t1='';
      if($row1['id']==$_REQUEST['under_group'])
       $t1="selected";
			?>
			<option value="<?php echo $row1['id']; ?>" <?php echo $t1; ?>><?php echo $row1['machine_gp_location']; ?></option>
		<?php }	?>  
      	</select>
    </td>
    <td width="13%" align="right">Asset Code</td>
	<td width="15%"><input name="asset_code" type="text" id="asset_code" /></td>
	<td width="13%" align="right">Asset Name</td>
	<td><input name="asset_name" type="text" id="asset_name" /></td>
</tr>
<tr class="row1">
	<td align="right">Title</td>
	<td><select name="title" id="title">
		<option value="0">Select</option>
		<?php 
		$title1=DB_query("select * from tbl_title_master order by serial_no ASC",$db);
		while($r=DB_fetch_array($title1))
		{
			$t1='';
      		if($r['id']==$_REQUEST['db_field_name'])
       		$t1="selected";
			?>
			<option value="<?php echo $r['db_field_name']; ?>" <?php echo $t1; ?>><?php echo $r['title_name']; ?></option>
		<?php }	?> 
      	</select>
    </td>
    <td align="right">Title Text</td>
	<td><input name="title_text" type="text" id="title_text" /></td>
	<td></td>
	<td align="center"><input name="site" type="hidden" id="site" value="<?php echo $_REQUEST['site']; ?>"  /><input type="button" onclick="getdataforSearch()" value="Search" class="search" /></td> 
</tr>
</table>
<br />

  <div id="ItemListDiv" class="overpan">

  <table width="120%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
  	<th width="5%">Select</th>
    <th width="12%">Asset Code</th>
	<th>Asset Name</th>
	<th width="12%">Asset Group</th>
	<?php 
   $title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
   while($r=DB_fetch_array($title1))
   { 
    ?>
    <th width="12%"><?php echo $r['title_name'];?></th>
    <?php } ?>
</tr>

	<?php 
	if(isset($_REQUEST['site']) && !empty($_REQUEST['site']))
	{	
		//echo "Select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."' and location='".$_REQUEST['site']."' ";
	 $sql=DB_query("Select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."' and location='".$_REQUEST['site']."' ",$db); 
	}else{
		//echo "Select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."' ";
		$sql=DB_query("Select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db); 
}?>
	<?php	$i=0;
	if(DB_num_rows($sql)>0)
	{
		while($myrow=DB_fetch_array($sql))
	  	{ 
		 $parameter=$myrow['id'];
		 $sqlU=DB_query("Select * from unitsofmeasure where unitid='".$myrow['unit']."' ",$db); 
		 $myrow_unit=DB_fetch_array($sqlU);
      
		 $sqlG=DB_query("Select * from machine_group_master where id='".$myrow['under_group']."' ",$db); 
		 $myrow_group=DB_fetch_array($sqlG);

		 $sqlH=DB_query("SELECT MAX(end_meter_reading) AS HighestPrice FROM tpl_fixed_asset_logbook_details where asset_code=".$myrow['id']." ",$db); 
		 $myrow_mx=DB_fetch_array($sqlH);

		 $title='';
		   $title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
		   while($r=DB_fetch_array($title1))
		   {
		    $title[]=$r['db_field_name'];
		   }  
		 
		 
		 
?>
 <tr>
      <td><input type="radio" name="radAnswer" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>" onClick="getItemsList('<?php echo $parameter;?>','<?php echo $myrow['code'];?>','<?php echo  $myrow['name'];?>','<?php echo $myrow_group['machine_gp_location']; ?>','<?php echo $myrow['asset_type'];?>','<?php echo $myrow_unit['unitname'];?>','<?php echo $myrow['model_no'];?>','<?php echo $myrow['vehicle_no'];?>','<?php echo $myrow_mx['HighestPrice'];?>','<?php echo $i; ?>','<?php echo $myrow[$title[0]];?>','<?php echo $myrow[$title[1]];?>','<?php echo $myrow[$title[2]];?>')"/></td>
      <td><?php echo $myrow['code'];?></td>
	  <td><?php echo  $myrow['name'];?></td>
      <td><?php echo $myrow_group['machine_gp_location']; ?></td>
	  <td style="word-break:break-all;"><?php echo $myrow[$title[0]]; ?></td>
	  <td style="word-break:break-all;"><?php echo $myrow[$title[1]]; ?></td>
	  <td style="word-break:break-all;"><?php echo $myrow[$title[2]]; ?></td>
   </tr>
<?php $i++;} }?> 
</table>
</div>
</form>

</div>
</div>

</div>
</div>

<script language="javascript" type="text/javascript">
function getItemsList(id,code,name,group,type,units,model,vechile,meter,ctr,t1,t2,t3)
{ 
  
  window.opener.document.getElementById('itemid').value = '';

	var dup='empty';
	var itemcount=parseInt(window.opener.document.getElementById('hiditemcount').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=='')
	{	
	window.opener.document.getElementById('itemid').value = id;
	window.opener.document.getElementById('assetcode').value = code;
	window.opener.document.getElementById('assetname').value = name;
	window.opener.document.getElementById('asset_group').value =group;
	window.opener.document.getElementById('asset_unit').value =units;
	window.opener.document.getElementById('title1').value=t1;
	window.opener.document.getElementById('title2').value=t2;
	window.opener.document.getElementById('title3').value=t3;
	}
	}
		}
		}
		
		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=='')
					{
										
			window.opener.document.getElementById('itemid').value = id;
	window.opener.document.getElementById('assetcode').value = code;
	window.opener.document.getElementById('assetname').value = name;
	window.opener.document.getElementById('asset_group').value =group;
	window.opener.document.getElementById('asset_unit').value =units;
	window.opener.document.getElementById('title1').value=t1;
	window.opener.document.getElementById('title2').value=t2;
	window.opener.document.getElementById('title3').value=t3;		
	            break;
					
					
				}
				}
					
					else
					{
						continue;
					}
				}
			}
		}
	}
	else
	{
	
		if(window.opener.document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
				window.opener.document.getElementById('itemid').value = '';
	
	window.opener.document.getElementById('assetcode').value = code;
	window.opener.document.getElementById('assetname').value = name;
	window.opener.document.getElementById('asset_group').value =group;
	window.opener.document.getElementById('asset_unit').value =units;
	window.opener.document.getElementById('title1').value=t1;
	window.opener.document.getElementById('title2').value=t2;
	window.opener.document.getElementById('title3').value=t3;
	
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(window.opener.document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
        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='';
		window.opener.document.getElementById('title1_'+i).value='';
	window.opener.document.getElementById('title2_'+i).value='';
	window.opener.document.getElementById('title3_'+i).value='';
	
				}
			}
		}
	}
}

function addRow(tableID) 
{

	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	window.opener.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='#fff';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	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;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	window.opener.document.getElementById("stock_perunit_"+rowCount).value='1';
}



function getdata1(str)
{  
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	var data=xmlhttp.responseText.split("__");	 
	document.getElementById("ItemListDiv").innerHTML=data[0];
	//document.getElementById("subcategory1").innerHTML=data[0];
	}
	}
	xmlhttp.open("GET","item_list_ajax.php?site="+str,true);
	xmlhttp.send();
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
}

function getdataforSearch()
{  
	var group=document.getElementById('under_group').value;
	var code=document.getElementById('asset_code').value;
	var name=document.getElementById('asset_name').value;
	var title=document.getElementById('title').value;
	var titletext=document.getElementById('title_text').value;

	
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	var data=xmlhttp.responseText;	 
	document.getElementById("ItemListDiv").innerHTML=data;
	}
	}
	xmlhttp.open("GET","asset_search.php?group="+group+"&code="+code+"&name="+name+"&title="+title+"&titletext="+titletext,true);
	xmlhttp.send();
}	


</script>

