<?php
    $sur_id = isset($_REQUEST['sur_id']) ? $_REQUEST['sur_id'] : "";
    $prd_frm = isset($_REQUEST['prd_frm']) ? $_REQUEST['prd_frm'] : "";
    $prd_to = isset($_REQUEST['prd_to']) ? $_REQUEST['prd_to'] : "";
	
	$query = "Select * FROM custom_lab_samples 
                            INNER JOIN custom_participant_registration 
                                ON (custom_participant_registration.reg_participant_id = custom_lab_samples.lab_smple_part_id)
							INNER JOIN custom_gender_master ON (custom_gender_master.gen_code = custom_participant_registration.reg_sex)
							INNER JOIN custom_location ON (custom_location.loc_code = custom_participant_registration.reg_location)
							INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
                            INNER JOIN custom_lab_master 
                                ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
                            INNER JOIN custom_lab_spceimen_master 
                                ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_samples.lab_smple_spcmn_id)
                          WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' GROUP BY lab_smple_acc_no";
    $common->sql->query($query, SQL_ALL, SQL_ASSOC);
    $rec = $common->sql->record;
	
        
	$query = "Select * FROM custom_lab_samples 
                            INNER JOIN custom_lab_master 
                                ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
                            LEFT JOIN custom_lab_result_master 
                                ON (custom_lab_result_master.lab_result_code = custom_lab_samples.lab_smple_result_id)
                          WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' GROUP BY lab_smple_tst_id";
     $query = "Select * FROM custom_lab_samples 
                            INNER JOIN custom_lab_master 
                                ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
                          WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' GROUP BY lab_smple_tst_id";
    $common->sql->query($query, SQL_ALL, SQL_ASSOC);
    $lab_rec = $common->sql->record;
	
	$query = "Select * FROM custom_lab_samples 
                            INNER JOIN custom_lab_spceimen_master 
                                ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_samples.lab_smple_spcmn_id)
                          WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' GROUP BY lab_smple_spcmn_id";
    $common->sql->query($query, SQL_ALL, SQL_ASSOC);
    $spe_rec = $common->sql->record;
	
	if($attr == "excel")
	{
		require_once PEAR.'/PHPExcel.php';
		
		$styleThickBrownBorderOutline = array(
			'font'    => array(
				'bold'      => true
			)
		);
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->setActiveSheetIndex(0);
		
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 1, "Lab Master Chart");
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:K1');
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 2, "Protocol");
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 2, $rec[0]['sur_title']);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(4, 2, "Report Printed On");
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(5, 2, $common->date_format(date("Y-m-d"), DT_FMT, ''));
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 3, "Report Period");
		$objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
		// $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(2, 3, $common->date_format($prd_frm, DT_FMT, ''));
		// $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(3, 3, $common->date_format($prd_to, DT_FMT, ''));
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(2, 3, $prd_frm);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(3, 3, $prd_to);
		
		$j = 0;
		$row = 5;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "S. No.");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Participant ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Site");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Visit Date");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Sex");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Age");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "VCT Number");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Req. No.");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Accession No.");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		
		for($i=0; $i<count($spe_rec);$i++)
		{
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $spe_rec[$i]['lab_spcmn_name']);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
			$j++;
		}
		for($i=0; $i<count($lab_rec);$i++)
		{
			//$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $lab_rec[$i]['lab_test_code']);
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $lab_rec[$i]['lab_test_name']);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
			$j++;
		}
		/*$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(9, 1, "Test Name");
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(10, 1, "Sample");*/
		
		$row++;
		for($i=0; $i<count($rec); $i++)
		{
			$j = 0;
			$sex = $rec[$i]['reg_sex'];
			$stxt = $rec[$i]['gen_name'];
			
			$query = "Select * FROM custom_lab_samples 
						INNER JOIN custom_lab_master 
							ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
						LEFT JOIN custom_lab_result_master 
							ON (custom_lab_result_master.lab_result_code = custom_lab_samples.lab_smple_result_id)
					  WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' AND lab_smple_part_id = '".$rec[$i]['reg_participant_id']."' order by lab_smple_tst_id";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$lab_rec = $common->sql->record;
			
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, ($i+1));
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['reg_participant_id']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loc_name']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $common->date_format($rec[$i]['lab_smple_date'], DT_FMT, true));
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $stxt);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['reg_age']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, '');
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, '');
			$j++;
			//$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['lab_smple_acc_no']);
			$j++;
			for($k=0; $k<count($spe_rec);$k++)
			{
				/*if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$k]['lab_smple_acc_no'])
				{*/
					$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "");
					$j++;
				//}
			}
			for($k=0; $k<count($lab_rec);$k++)
			{
				/*if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$k]['lab_smple_acc_no'])
				{*/
					$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $lab_rec[$k]['lab_result_name']);
					$j++;
				//}
			}
			/*$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['lab_test_name']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['lab_spcmn_name']);
			$j++;*/
			$row++;
		}
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle($form_name);
		$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
		
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$form_name.'.xls"');
		header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	else if($attr == "pdf")
	{
		$disp_header = '
			<br/><br/>
			<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;">
				<tr>
					<th align="center"><font size="13"><strong>Lab Master Chart</strong></font></th>
				</tr>
				<tr>
					<td><font size="10"><strong> Survey Title : </strong> '.$rec[0]['sur_title'].'</font></td>
				</tr>
				<tr>
					<td><font size="10"><strong> Report Period From : </strong> '.$prd_frm.'  <strong>To :</strong> '.$prd_to.'</font></td>
				</tr>
			</table>
			<br><br>';
		$disp_table = '<table border="1" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;" align="center">
			<thead>';
		$disp_table .= '<tr>
				<td nowrap><strong>S. No.</strong></td>
				<td nowrap><strong>Participant ID</strong></td>
				<td nowrap><strong>Site</strong></td>
				<td nowrap><strong>Visit Date</strong></td>
				<td nowrap><strong>Sex</strong></td>
				<td nowrap><strong>Age</strong></td>
				<td nowrap><strong>VCT Number</strong></td>
				<td nowrap><strong>Req. No.</strong></td>
				<td nowrap><strong>Accession No.</strong></td>';
				for($i=0; $i<count($spe_rec);$i++)
				/*{*/
					// if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$k]['lab_smple_acc_no'])
						$disp_table .= '<td nowrap><strong>'.$spe_rec[$i]['lab_spcmn_name'].'</strong></td>';
				//}
				for($i=0; $i<count($lab_rec);$i++)
				/*{*/
					// if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$k]['lab_smple_acc_no'])
						//$disp_table .= '<td nowrap><strong>'.$lab_rec[$i]['lab_test_code'].'</strong></td>';
						$disp_table .= '<td nowrap><strong>'.$lab_rec[$i]['lab_test_name'].'</strong></td>';
				//}
				/*echo '<td nowrap><strong>Test Name</strong></td>
				<td nowrap><strong>Sample</strong></td>';*/
		$disp_table .= '</tr>
			</thead>
			<tbody>';
			for($i=0; $i<count($rec); $i++)
			{
				$sex = $rec[$i]['reg_sex'];
				$stxt = $rec[$i]['gen_name'];
				
				$query = "Select * FROM custom_lab_samples 
                            INNER JOIN custom_lab_master 
                                ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
                            LEFT JOIN custom_lab_result_master 
                                ON (custom_lab_result_master.lab_result_code = custom_lab_samples.lab_smple_result_id)
                          WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' AND lab_smple_part_id = '".$rec[$i]['reg_participant_id']."' order by lab_smple_tst_id";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$lab_rec = $common->sql->record;
				
				$disp_table .= '<tr>
						<td nowrap>'.($i+1).'.</td>
						<td nowrap>'.$rec[$i]['reg_participant_id'].'</td>
						<td nowrap>'.$rec[$i]['loc_name'].'</td>
						<td nowrap>'.$common->date_format($rec[$i]['lab_smple_date'], DT_FMT, true).'</td>
						<td nowrap>'.$stxt.'</td>
						<td nowrap>'.$rec[$i]['reg_age'].'</td>
						<td nowrap>&nbsp;</td>
						<td nowrap>&nbsp;</td>
						<td nowrap>&nbsp;</td>
						<!--<td nowrap>'.$rec[$i]['lab_smple_acc_no'].'</td>-->';
						for($j=0; $j<count($spe_rec);$j++)
							//if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$j]['lab_smple_acc_no'])
								$disp_table .= '<td nowrap> </td>';
						for($j=0; $j<count($lab_rec);$j++)
							$disp_table .= '<td nowrap>'.$lab_rec[$j]['lab_result_name'].'</td>';
							//if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$j]['lab_smple_acc_no'])
								// $disp_table .= '<td nowrap> </td>';
				$disp_table .= '</tr>';
			}
		$disp_table .= '
			</tbody>
			</table>
		<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;">
		<tbody>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>Batch #</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>Site Lab Personal Sign and Date</td>
			</tr>
		</tbody>
		</table>';
		
		$query = "SELECT * FROM custom_settings";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$stngs_rec = $common->sql->record[0];
		
		$stngs_org_name = $stngs_rec['stngs_org_name'];
		$stngs_org_addrs = $stngs_rec['stngs_org_addrs'];
		$stngs_org_phone = $stngs_rec['stngs_org_phone'];
		$stngs_org_mail = $stngs_rec['stngs_org_mail'];
		$stngs_org_website = $stngs_rec['stngs_org_website'];
		
		$pdf = '';
		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		
		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('YRG Care');
		$pdf->SetTitle('Expense Report');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		
		// set default header data
		$pdf->SetHeaderData(PDF_HEADER_LOGO, '30', $stngs_org_name, "<font size=\"9\">".$stngs_org_addrs."<br/><b>T : </b>".$stngs_org_phone."<br/><b>E : </b>".$stngs_org_mail."  <b>  Web : </b>".$stngs_org_website."</font>");
		// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
		
		// set header and footer fonts
		/*$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 20, 'L', true));
		$pdf->setHeaderStringFont(Array(PDF_FONT_NAME_MAIN, '', 10, 'L', true));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));*/
		
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 12, '', true));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		
		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		
		//set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		
		//set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		
		//set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
		
		//set some language-dependent strings
		$pdf->setLanguageArray($l);
		
		// ---------------------------------------------------------
		
		// set font
		$pdf->SetFont('dejavusans', '', 8);
		
		// add a page
		$pdf->AddPage('L');
		
		$dt_fmt = str_replace("mm", "m", DT_FMT);
		$dt_fmt = str_replace("dd", "d", $dt_fmt);
		$dt_fmt = str_replace("yyyy", "Y", $dt_fmt);
		$dt = date($dt_fmt);
		
		$title_rght = "<br/><br/><br/><font size=\"9\"><b>Printed from :</b> ".APP_NAME."<br/>
		<b>Printed by : </b>".USRNME."<br/>
		<b>Report Printed on : </b>".$dt."</font>";
		
		// Print text using writeHTMLCell()
		$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $disp_header, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
		$pdf->writeHTML($disp_table, true, false, true, false, '');
		$pdf->MultiCell(0, 10, $title_rght, 0, 'R', 0, 1, '', 5, false, 0, true, true, 0, 'T', false);
		$pdf->lastPage();
		$pdf->Output("Lab Chart Report.pdf", "I");
	}
	else if($attr == "print")
	{
		echo '<br/><table border="1" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" align="center">
			<thead>';
		echo '<tr>
				<td nowrap><strong>S. No.</strong></td>
				<td nowrap><strong>Participant ID</strong></td>
				<td nowrap><strong>Site</strong></td>
				<td nowrap><strong>Visit Date</strong></td>
				<td nowrap><strong>Sex</strong></td>
				<td nowrap><strong>Age</strong></td>
				<td nowrap><strong>VCT Number</strong></td>
				<td nowrap><strong>Req. No.</strong></td>
				<td nowrap><strong>Accession No.</strong></td>';
				for($i=0; $i<count($spe_rec);$i++)
				//{
					// if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$k]['lab_smple_acc_no'])
						echo '<td nowrap><strong>Sample '.$spe_rec[$i]['lab_spcmn_name'].'</strong></td>';
				//}
				for($i=0; $i<count($lab_rec);$i++)
				/*{*/
					// if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$k]['lab_smple_acc_no'])
						//echo '<td nowrap><strong>'.$lab_rec[$i]['lab_test_code'].'</strong></td>';
						echo '<td nowrap><strong>'.$lab_rec[$i]['lab_test_name'].'</strong></td>';
				//}
				/*echo '<td nowrap><strong>Test Name</strong></td>
				<td nowrap><strong>Sample</strong></td>';*/
		echo '</tr>
			</thead>
			<tbody>';
			for($i=0; $i<count($rec); $i++)
			{
				$sex = $rec[$i]['reg_sex'];
				$stxt = $rec[$i]['gen_name'];
				
				$query = "Select * FROM custom_lab_samples 
                            INNER JOIN custom_lab_master 
                                ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
                            LEFT JOIN custom_lab_result_master 
                                ON (custom_lab_result_master.lab_result_code = custom_lab_samples.lab_smple_result_id)
                          WHERE lab_smple_sur_id = '".$sur_id."' AND lab_smple_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' AND lab_smple_part_id = '".$rec[$i]['reg_participant_id']."' order by lab_smple_tst_id";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$lab_rec = $common->sql->record;
				
				echo '<tr>
						<td nowrap>'.($i+1).'.</td>
						<td nowrap>'.$rec[$i]['reg_participant_id'].'</td>
						<td nowrap>'.$rec[$i]['loc_name'].'</td>
						<td nowrap>'.$common->date_format($rec[$i]['lab_smple_date'], DT_FMT, true).'</td>
						<td nowrap>'.$stxt.'</td>
						<td nowrap>'.$rec[$i]['reg_age'].'</td>
						<td nowrap>&nbsp;</td>
						<td nowrap>&nbsp;</td>
						<td nowrap>&nbsp;</td>
						<!--<td nowrap>'.$rec[$i]['lab_smple_acc_no'].'</td>-->';
						for($j=0; $j<count($spe_rec);$j++)
							//if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$j]['lab_smple_acc_no'])
								echo '<td nowrap> </td>';
						for($j=0; $j<count($lab_rec);$j++)
						{
							echo '<td nowrap>'.$lab_rec[$j]['lab_result_name'].'</td>';}
							//if($rec[$i]['lab_smple_acc_no'] == $lab_rec[$j]['lab_smple_acc_no'])
						/*echo '<td nowrap>'.$rec[$i]['lab_test_name'].'</td>
						<td nowrap>'.$rec[$i]['lab_spcmn_name'].'</td>';*/
				echo '</tr>';
			}
		echo '
			</tbody>
			</table>';
	}
?>