<?php 
$url_cancel= "index.php?route=ToolPlants/Transaction/asset_breakdown_completion".$rurl;
include('includes/Class_Branch.php');
include('includes/AccountGroupMaster_Class.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$rurl="&modid=".$modid."&frmid=".$frmid;

$targetpage="index.php?route=ToolPlants/Transaction/asset_breakdown_completion_index".$rurl;
$msgErr=array();
//print_r($_SESSION);
?>

<?php
//Add Progress
if($_REQUEST['action']=='addProdress'){
   //echo $_REQUEST['rid2'];
  $jobid=$s->getField('tpl_breakdown_mgmt_assig','id',$_REQUEST['rid2'],'job_id');

	$con ="id='".$jobid."'";
	$Get_id = $s->GetRecordById('tpl_breakdown_mgmt',$con);
	$var = DB_fetch_array($Get_id);
	
	$sqlAset = "SELECT * FROM machine_center WHERE ID= ".$_REQUEST['assetID']."";
	$resultDisplay = DB_query($sqlAset,$db);
	$assetRows = DB_fetch_array($resultDisplay);
	// asset group
	$reqDet_sql1 = "SELECT * FROM machine_group_master WHERE id= ".$assetRows['under_group']."";
		$result1 = DB_query($reqDet_sql1,$db);
		$assetRow1 = DB_fetch_array($result1);
		
	// unit of measurement
	$sqlU=DB_query("Select * from unitsofmeasure where unitid='".$assetRows['unit']."' ",$db); 
		 $myrow_unit=DB_fetch_array($sqlU);
		 
	// end meter reading
	 $sqlH=DB_query("SELECT MAX(end_meter_reading) AS HighestPrice FROM tpl_fixed_asset_logbook_details where asset_code=".$assetRows['id']." ",$db); 
		 $myrow_mx=DB_fetch_array($sqlH);
}

	if(isset($_POST['addProdress']))
    {
	
	        $delete = DB_query("delete from tpl_breakdown_mgmt_details where
		       master_id = '".$_REQUEST['rid2']."'",$db);

	for($l=0; $l<count($_REQUEST['p_status']); $l++)
		{				
    
      $data5['break_type'] = $_REQUEST['sub_break_down1'][$l];
		$data5['break_sub_type'] = $_REQUEST['sub_break_down'][$l];
		$data5['break_reason'] = $_REQUEST['break_reason'][$l];
		$data5['technician'] = $_REQUEST['technician'][$l];
		$data5['rec_date'] = FormatDateFORSQL($_REQUEST['rec_date'][$l]);
      $data5['completed_date']=FormatDateFORSQL($_REQUEST['completed_date'][$l]);
		$data5['rec_time'] = $_REQUEST['rec_time'][$l];
		$data5['com_date'] = FormatDateFORSQL($_REQUEST['com_date'][$l]);
     	$data5['remarks'] = $_REQUEST['p_Remarks'][$l];
       $data5['safty'] = $_REQUEST['break_safty'][$l];
    	$data5['master_id'] = $_REQUEST['rid2'];
		$data5['progress_status'] = $_REQUEST['p_status'][$l];
		$data5['remarks'] = $_REQUEST['p_Remarks'][$l];
	
		$s->RecordInsert('tpl_breakdown_mgmt_details',$data5);		 
		}
      
    for($t=0;$t<count($_REQUEST['itemid']);$t++)
    {
      $sql_check_job=DB_query("select id from tbl_asset_assem_item where mst_itemid='".$_REQUEST['itemid'][$t]."' and job_id='".$jobid."'",$db);
      $numrow_job=DB_num_rows($sql_check_job);
      if($numrow_job>0)
      {
          $row_id=DB_fetch_array($sql_check_job);
          $last=$row_id['id'];
      }else{
        $data6['mst_itemid'] = $_REQUEST['itemid'][$t];
    		$data6['job_id'] = $jobid;
        $data6['created_date'] = date("Y-m-d");
        $data6['created_by'] = $_SESSION['UserID'];
        $s->RecordInsert('tbl_asset_assem_item',$data6);

        $last=DB_Last_Insert_ID($db, 'tbl_asset_assem_item', 'id');
      } 

      $data7['parentid'] = $last;
      $data7['qty'] = $_REQUEST['qty'][$t];
      $data7['iqty'] = $_REQUEST['iqty'][$t];
      $data7['pqty'] = $_REQUEST['pqty'][$t]-$_REQUEST['qty'][$t]; 
      $data7['consumption_date'] = date("Y-m-d");
      $data7['consumption_by'] = $_SESSION['UserID'];
      $s->RecordInsert('tbl_asset_assem_item_details',$data7);
    }
      
      $sql_check=DB_query("select * from tpl_breakdown_mgmt_details where master_id='".$_REQUEST['rid2']."'",$db);
      $numrow1=DB_num_rows($sql_check);
      $flag=0;
      if($numrow1>0)
      {
       while($row1=DB_fetch_array($sql_check))
       {
          if($row1['progress_status']=='Done')
           $flag=$flag+1;
       }  
       
       if($numrow1==$flag)
       {
        
        $update=DB_query("update tpl_breakdown_mgmt_assig set completion_status=1
                        where id='".$_REQUEST['rid2']."'",$db);        
       }
       
      }
      echo "<script>location.href='$targetpage'</script>";
      }
?>

<style type="text/css">
 /*.search {
width:auto;
padding-top:3px;
font-size:12px;
font-weight:bold;
background-color:#ddd4d4;
background-repeat: repeat-x;
padding-bottom:3px;
text-align:center;
border:#333333 solid 1px;
-moz-border-radius:3px;
 }
 
        #content { font-size: 1.2em; 
                   font-family: "Lucida Sans Unicode", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif;
                   width: 950px; margin: auto;
        }
        .code { margin: 6px; padding: 9px; background-color: #fdf5ce; border: 1px solid #c77405; }
        fieldset { padding: 0.5em 2em }
        hr { margin: 0.5em 0; clear: both }
        a { cursor: pointer; }
        #requirements li { line-height: 1.6em; }*/
</style>

<link rel="stylesheet" href="js/jquery.ui.timepicker.css?v=0.3.2" type="text/css" />

<script>
function showSubBreak(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("txtHint1").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getsubBreak.php?q="+str,true);
xmlhttp.send();
}
</script>

<div id="PickedItems" style="display:none">
</div>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Add Process</h1>
<form method="post" name="tech_master">
<h3>General Details</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right">Branch <font style="color:#FF0000;">*</font></td>
    <td width="20%"><select disabled id = "site" name="site">
        <option value = "">--Select--</option>
        <?php 
            $site_sql=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                        while($site_row=DB_fetch_array($site_sql))
						{
						$p='';
						if($site_row['loccode']==$var['site'])
						$p="selected";
			?>
        <option value="<?php echo $site_row['loccode']?>"<?php echo $p;?>><?php echo $site_row['locationname'];?></option>
        <?php } ?>
      </select></td>
    <td width="13%" align="right">Asset Received Date: <font style="color:#FF0000;">*</font></strong></td>
    <td width="20%"><input type="text" disabled  name="recieving_date" class="datepicker" value="<?php if($var['asset_rec_date_time']!='') echo date('d/m/y',strtotime($var['asset_rec_date_time'])); else echo "";?>"></td>
    <td width="13%" align="right">Job Card No. <font style="color:#FF0000;">*</font></td>
    <td><input type="text" value = "<?php echo $var['tt_no'];?>" disabled  name="ttno" /></td>
  </tr>
  <tr class="row1">
    <td align="right">Received From <font style="color:#FF0000;">*</font></td>
    <td><input type="text" disabled value = "<?php echo $var['rec_from']; ?>" name="rec_from" /></td>
    <td align="right">Received By <font style="color:#FF0000;">*</font></td>
    <td><input type="text" disabled id ="oo" value = "<?php echo $var['rec_by']; ?>" name="received_by" /></td>
    <td colspan="2"></td>
    </tr>
</table>
<br>
<h3>Asset Details</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th width="10%">Asset Code</th>
    <th>Asset Name </th>
    <th width="16%">Asset Group</th>
    <th width="12%">Unit</th>
    <?php 
   $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'];
    ?>
    <th width="10%"><?php echo $r['title_name'];?></th>
    <?php } ?>
  </tr>

  <tr>  
<input type="hidden" name="item_code" id="astid" value="<?php echo $assetRows['id']; ?>" />
    <td><input type="text" disabled  value= "<?php echo $assetRows['code']; ?>" name="asset_code" id="assetcode"  readonly /></td>
    <td><input type="text" value= "<?php echo $assetRows['name']; ?>" name="Input" id="assetname"   readonly="" /></td>
    <td><input type="text" value = "<?php  echo $assetRow1['machine_gp_location'];?>" name="" id="asset_group"  readonly="" /></td>    
    <td><input type="text" value = "<?php echo $myrow_unit['unitname']; ?>" name="" id="asset_unit"   readonly="" /></td>
    <td><input type="text" value= "<?php echo $assetRows[$title[0]]; ?>" name="" id="asset_model"   readonly="" /></td>
    <td><input type="text" value= "<?php echo $assetRows[$title[1]]; ?>" id = "asset_vechile"  readonly="" ></td>
    <td><input type="text" value= "<?php echo $assetRows[$title[2]]; ?>" id = "asset_meter"  readonly="" ></td>
  </tr>
</table>
<br>
<h3>Add Breakdown Details</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th width="10%">Breakdown Type <font style="color:#FF0000;">*</font></th>
    <th width="10%">Breakdown Sub Type</th>
    <th width="15%">Breakdown Reason <font style="color:#FF0000;">*</font></th>
    <th width="10%">Technician Name</th>
    <th width="10%">Received Date &amp; Time</th>
    <th width="10%">Exp. Com. Date</th>
    <th width="10%">Com. Date</th>
    <th width="10%">Current Status</th>
    <th>Remarks</th>
  </tr>
  <?php 
  if($_REQUEST['action']=='addProdress')
{
  // Breakdown Details
  $h=0;
	$breakSql = "SELECT * FROM tpl_breakdown_mgmt_details WHERE master_id= ".$_REQUEST['rid2']."";
	$resultBd = DB_query($breakSql,$db);
	while($rowBd=DB_fetch_array($resultBd))
	{
	$h++;
	
   ?>
    <tbody id="dataTable">
  <tr>
       <td><select name="sub_break_down1[]" readonly id="ab"  onChange="showSubBreak(this.value)">
         <option value = "" >-- Select--</option>
         <?php 
            $site_sql1=DB_query("Select id,breakdown_reason from break_down_type where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                        while($site_row1=DB_fetch_array($site_sql1)){
						$s='';
						if($site_row1['id']==$rowBd['break_type'])
						$s="selected";
			?>
         <option value="<?php echo $site_row1['id']; ?>"<?php echo $s;?>><?php echo $site_row1['breakdown_reason']; ?></option>
         <?php } ?>
       </select></td>
       <td><div id="txtHint1"><select name="sub_break_down[]" readonly > 
         <option value="">--Select--</option>
         <?php
	$id_query1=DB_query("Select * from tbl_sub_break_type where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
	while($id_row_type=DB_fetch_array($id_query1))
	 {
	 $q='';
	if($id_row_type['id']==$rowBd['break_sub_type'])
	$q="selected";
	 ?>
         <option value="<?php echo $id_row_type['id']; ?>"<?php echo $q;?>><?php echo $id_row_type['sub_breaktype']; ?></option>
         <?php } ?>
       </select></div></td>
       <td><input type="text" value="<?php echo $rowBd['break_reason'];  ?>"  readonly id="res" name="break_reason[]" ></td>
    <td><select name="technician[]" readonly >
	<option value="0">--Select--</option>
	<?php
	$id_query2=DB_query("Select * from technicians", $db);
	while($id_row_technician=DB_fetch_array($id_query2))
	 {
	  $g='';
	if($id_row_technician['id']==$rowBd['technician'])
	$g="selected";
	 ?>
	<option value="<?php echo $id_row_technician['id']; ?>"<?php echo $g;?>><?php echo $id_row_technician['technician']; ?></option>
	<?php } ?>
	</select></td>    
    <td><input type="text" readonly value="<?php if($rowBd['rec_date']!='') echo date('d/m/y',strtotime($rowBd['rec_date'])); else echo "";?>" name = "rec_date[]" style="width:49%;" id= "<?php echo $r; ?>" class="datepicker"/ ><input type="text" value="<?php if($rowBd['rec_time']!='') echo $rowBd['rec_time']; else echo "";?>" id="timepicker.[1]" name = "rec_time[]" style="width:49%;"></td>
    <td><input type="text"  value="<?php if($rowBd['com_date']!='') echo date('d/m/y',strtotime($rowBd['com_date'])); else echo "";?>"  readonly name = "com_date[]" id= "<?php echo $r; ?>" ></td>
	 <td><input type="text"  value="<?php if($rowBd['completed_date']!='') echo date('d/m/y',strtotime($rowBd['completed_date'])); else echo "";?>"  readonly name = "completed_date[]"  class="datepicker"></td>
	<td><select name = "p_status[]" >
      <option value="">--Select--</option>
      <option value="WIP"<?php echo $rowBd['progress_status'] == 'WIP' ? ' selected="selected"' : '';?>>WIP</option>
      <option value="Done"<?php echo $rowBd['progress_status'] == 'Done' ? ' selected="selected"' : '';?>>Done</option>
    </select></td>
	<td><textarea name = "p_Remarks[]" rows="2" cols="15"><?php if($rowBd['remarks']!="") { echo $rowBd['remarks']; } else { echo ''; } ?></textarea></td>
  </tr>
 </tbody>
 <?php }
 } ?>
</table>
<br>
<h3>Safty Details Descriptions</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th>Breakdown Safty Description</th>
    <th colspan="2" style="text-align:center">Action</th>
  </tr>
 
    <?php        
    $j=0;
    $breakSql1 = "SELECT * FROM tpl_break_safty WHERE master_id= ".$_REQUEST['rid2']."";
	$resultBd1 = DB_query($breakSql1,$db);
	while($rowBd1=DB_fetch_array($resultBd1))
   {       
    $sql_bk_type=DB_query("select * from prd_safty where CompanyId='".$_SESSION['ACGCompanyId']."' and id='".$rowBd1['safty']."'",$db);
   $row_bk=DB_fetch_array($sql_bk_type);
    
    ?> 
  <tr>
      <td><?php echo $row_bk['description']; ?> <input type="hidden" name="break_safty[]" value="<?php echo $row_bk['id']; ?>"</td>
      <td width="10%"><input type="radio" id="bkdone_<?php echo $j; ?>" name="bk_done[<?php echo $j; ?>]" value="Y" disabled <?php if($rowBd1['status']=='Y') { ?> checked="checked"; <?php } ?> />Done</td>
      <td width="10%"><input type="radio" name="bk_done[<?php echo $j; ?>]" id="bkdone_<?php echo $j; ?>" value="N" disabled <?php if($rowBd1['status']=='N') { ?> checked="checked"; <?php } ?> />N/R</td> 
  </tr>  
    <?php $j++;} ?>
</table>

 <!--<p align="left">
 <input type="button" name="profile_popup" value="Pick Items" id="profile_popup" class="search" />
 <input type="button" class="search"   onclick="deleteRowOrder('dataTable1')" value="Delete" /> 
  </p>-->
<br>

<h3>Add Breakdown Spare Part Details</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder"> 
    <tbody id="dataTable1">
     <?php $t=0; ?>
     <?php 
     $t=0;
    // echo "SELECT IQ.mst_itemid,SUM(IQ.issueqty) AS iqty, AI.id as main_id FROM material_request_slip AS M LEFT JOIN addissue AS AI ON M.id=AI.mrs_id LEFT JOIN issueqty AS IQ ON AI.id=IQ.issue_id where M.jobcard_id='".$jobid."' and AI.issue_type='MRS' GROUP BY IQ.mst_itemid";

     //echo "SELECT * from addissue where job_id='".$jobid."'";

     /* $sqlrow=DB_query("SELECT IQ.mst_itemid,SUM(IQ.issueqty) AS iqty, AI.id as main_id FROM material_request_slip AS M LEFT JOIN addissue AS AI ON M.id=AI.mrs_id LEFT JOIN issueqty AS IQ ON AI.id=IQ.issue_id where M.jobcard_id='".$jobid."'",$db); */
         
         //echo "SELECT addissue.id as main_id,issueqty.mst_itemid,sum(issueqty) as iqty  from addissue left join issueqty on addissue.id=issueqty.issue_id where job_id='".$jobid."' group by issueqty.mst_itemid ";
      
       //echo $jobid;

       $sqlrow=DB_query("SELECT addissue.id as main_id,issueqty.mst_itemid as mst_itemid,sum(issueqty) as iqty  from addissue left join issueqty on addissue.id=issueqty.issue_id where job_id='".$jobid."' group by issueqty.mst_itemid ",$db);

     $numdata=DB_num_rows($sqlrow);
     if($numdata>0)
     { ?>
    <tr>
      <th width="15%">Item Code</th>
      <th width="1"></th>
      <th>Item Name</th>
      <th width="15%">Issued Qty.</th>
      <th width="15%">Pending Qty.</th>
      <th width="15%">Consumed Qty.</th>
    </tr>
   <?php
     while($rowcol=DB_fetch_array($sqlrow))
     { 
      $sqlrow33=DB_query("SELECT DISTINCT(AI.id) as main_id FROM material_request_slip AS M LEFT JOIN addissue AS AI ON M.id=AI.mrs_id LEFT JOIN issueqty AS IQ ON AI.id=IQ.issue_id where M.jobcard_id='".$jobid."' ",$db);
      $m=array();
      while($rowcol33=DB_fetch_array($sqlrow33))
      {
        $m[]=$rowcol33['main_id'];
      }
      $n=implode(",",$m);

      $sqlrow44=DB_query("SELECT SUM(MRD.receiveqty) AS rqty FROM material_return AS MR LEFT JOIN material_return_details AS MRD ON MR.id=MRD.parentId where MR.slip_return_type='Process' and MRD.itemid='".$rowcol['mst_itemid']."'",$db);
      $rowcol44=DB_fetch_array($sqlrow44);
      if($rowcol44['rqty']==''){$rowcol44['rqty']=0;}


      $sqlrow22=DB_query("SELECT SUM(ID.pqty) AS cqty FROM tbl_asset_assem_item AS AI LEFT JOIN tbl_asset_assem_item_details AS ID ON AI.id=ID.parentid where  AI.mst_itemid='".$rowcol['mst_itemid']."' GROUP BY AI.mst_itemid",$db);
      $rowcol22=DB_fetch_array($sqlrow22);
      //echo $rowcol22['cqty']."<br>";
    // echo $rowcol['iqty']."<br>";


      $item1=DB_query("select * from item_master where id='".$rowcol['mst_itemid']."'",$db);
      $item1row=DB_fetch_array($item1);

      $rowcol['iqty']=$rowcol['iqty']-$rowcol44['rqty'];
     ?>
     
     <tr>
      <td><input type="text" readonly name="" id="itemcode_<?php echo $t; ?>"  value="<?php echo $item1row['StockID']; ?>" ></td>
      <td><input type="hidden" readonly name="itemid[]" id="itemid_<?php echo $t; ?>"  value="<?php echo $item1row['id']; ?>" ></td>
      <td><input type="text" readonly name="" id="itemname_<?php echo $t; ?>"  value="<?php echo $item1row['item_name']; ?>" ></td>
      <td><input type="text" readonly name="iqty[]" id="iqty_<?php echo $t; ?>"  value="<?php echo $rowcol['iqty']; ?>"></td>
      <td><input type="text" readonly name="pqty[]" id="pqty_<?php echo $t; ?>"  value="<?php if($rowcol22['cqty']==''){echo $rowcol['iqty'];}else{echo ($rowcol22['cqty']-$rowcol['iqty']);} ?>"></td>
      <td><input type="text" name="qty[]" id="qty_<?php echo $t; ?>"  value=""></td>
     </tr>
 <?php $t++; } }?>     
     <?php 
     if($numdata==0)
     $colvalue=0;
     else
      $colvalue=$t;
     
     ?>
 </tbody>  
<input type="hidden" name="hiditemcount" id="hiditemcount" value="<?php echo $colvalue; ?>" />
  <input type="hidden" name="" value="<?php echo $colvalue; ?>" id="edit_cnt"   />
    <input type="hidden" name="" value="<?php echo $colvalue; ?>" id="rowcount1" />
</table>
<p align="center">
      <input name="addProdress"  class="search" type="submit" onClick="return validates()" value="Save" /> <a href="<?php echo $url_cancel;?>"><input name="" class="search" value="Cancel" type="button"></a></p>

</form>
 </div>
</div>
<script type="text/javascript">
 
function validates() {

	if(document.tech_master.site.value=='')
	{
		alert("Please Select Branch First");
		(document.tech_master.site).focus();
		return false;
	}
	else if(document.tech_master.recieving_date.value=='')
	{
		alert("Please Enter Asset Receiving Date& Time");
		(document.tech_master.recieving_date).focus();
		return false;
	}
	else if(document.tech_master.ttno.value=='')
	{
		alert("Please Enter TT No");
		(document.tech_master.ttno).focus();
		return false;
	}
	else if(document.tech_master.rec_from.value=='')
	{
		alert("Please Enter Received  From Value");
		(document.tech_master.rec_from).focus();
		return false;
	}
	else if(document.tech_master.received_by.value=='')
	{
			alert("Please Enter Received by Value");
		(document.tech_master.received_by).focus();
		return false;
	}
		else if(document.getElementById("assetcode").value =='')
	{
		alert("Select at least 1 Asset");
		return false;
	}
	else if(document.getElementById("ab").value=='')
	{
		alert("Please Enter Breakdown Type");
		(document.getElementById("ab")).focus();
		return false;
	}
	else if(document.getElementById("res").value=='')
	{
		alert("Please Enter Breakdown Reason");
		(document.getElementById("res")).focus();
		return false;
	}
	
}
</script>



<script type="text/javascript" language="javascript">
$.noConflict();
$(document).ready(function(){	
		
		$('input[name=profile_popup]').click(function(){
		var tt=this.id;
		var tt1=tt.split("_");
		var tt2=tt1[1];
		var str=document.getElementById("astid").value;
		$("#PickedItems").dialog({
				title:'Item List',
				modal: true,
				resizable: false,
				width:900,
				height:400,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: {
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
				
			});
		GetitemListForDirectPO(str);
		});
	});
	
	function GetitemListForDirectPO(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("PickedItems").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","spare_part_kids1.php?id="+str,true);
	xmlhttp.send();
}

function getItemsList(id,code,shade,desc,ctr,current)
{  
	
  var dup='empty';
	var itemcount=parseInt(document.getElementById('rowcount1').value)+1;
	
	var edit_cnt=document.getElementById('edit_cnt').value;
	//alert(edit_cnt);
	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(document.getElementById('itemid') && document.getElementById('itemid').value=='')
		{
			if(edit_cnt=='')
			{
				var counter=0;
			}
		
			for(var j=0; j<counter; j++)
			{
          
           if(document.getElementById('itemid1_'+j))
            {
				if(document.getElementById('itemid1_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
            }
			}
			
			if(dup=='empty')
			{	
				var data=addRow('dataTable1');
				document.getElementById('itemid').value = id;
				document.getElementById('itemcode').value = code;
				document.getElementById('itemname').value = shade;	
					
				
				
			}
		} 
		
		else
		{
		
		
			for(var i=counter; i<=itemcount; i++)
			{	
			
			
				if(edit_cnt=='')
				{
					var counter=0;
				}
				for(var j=0; j<counter; j++)
				{}
				if(document.getElementById('itemid'))
				{
				if(document.getElementById('itemid').value==id)
				{
					alert("Duplicate Value");
					document.getElementById("row_id_"+ctr).checked=false;
					break;
				}
				}
				if(document.getElementById('itemid_'+i))
				{
				if(document.getElementById('itemid_'+i).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					break;
				}
				}
            
				
				if(dup=='empty')
				{
				if(!document.getElementById('itemid_'+i))				{	
					
				var data= addRow('dataTable1');	
				}
					if(document.getElementById('itemid_'+i))
					{	
					
					if(document.getElementById('itemid_'+i).value=='')
					{	
						document.getElementById('itemid_'+i).value = id;
						document.getElementById('itemcode_'+i).value = code;
						document.getElementById('itemname_'+i).value = shade;
						
												
						break;
					}
					}
					else
					{
						continue;
					}
				}
			}
		}
	}
	

	else
	{
		if(document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
			document.getElementById('itemid').value = '';
			document.getElementById('itemcode').value = '';
			document.getElementById('itemname').value = '';
			
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
					document.getElementById('itemid_'+i).value = '';
					document.getElementById('itemcode_'+i).value = '';
					document.getElementById('itemname_'+i).value = '';
					
				}
			}
		}
	}

}

function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var row1=document.getElementById("rowcount1").value;
	var rr=parseInt(row1)+1;
	document.getElementById("rowcount1").value=rr;
	document.getElementById("edit_cnt").value=rr;
	var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
			//newcell.className='tabledata';
           			// newcell.style.background='#ffffff';
			
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
			 

				//alert(newcell.childNodes[0].type);
				switch(newcell.childNodes[0].type) {

				case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;		
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id.split("_");
	
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id = newcell.childNodes[0].id.split("_");
	
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id.split("_");
	
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;	
	}
		
	}
		

	}

	
</script>
