<?php
	session_start();
	ini_set('display_errors', 0);
	include_once("includes/config.php");
	include_once("includes/common_functions.php");
	
	$form = $_REQUEST['frm_name'];
	$page = $_REQUEST['page'];
	$common = new General_Functions();
	
	switch($form)
	{
		case 'speciman':
			$formvars = $_REQUEST;
			
			$query = "SELECT * FROM custom_lab_mapping 
							INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
						WHERE lab_mapng_mstr_id = '".$formvars['labid']."' AND custom_lab_spceimen_master.status = 'Y'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			$disp = '<tr>
                        <td class="question_title" style="width: 40%;">Specimen Name</td>
                    	<td class="question_title" style="width: 20%;">Is Collected?</td>
            		</tr>';
			
			for($i=0; $i<count($rec); $i++)
			{
				$disp .= '<tr>
							<td class="question" style="width: 40%;">
							'.$rec[$i]['lab_spcmn_name'].'
							<input type="hidden" name="spcmn_mstr_id[]" id="spcmn_mstr_id_'.$i.'" value="'.$rec[$i]['lab_spcmn_id'].'" />
							</td>
							<td class="question" style="width: 20%;">
								<input type="checkbox" name="spcmn_type[]" id="spcmn_type_'.$i.'" value="1" />
							</td>
						</tr>';
			}
			
			if(count($rec) == 0)
			{
				$disp .= '<tr>
							<td class="error" colspan="2" align="center">Specimens are Not Avaliable</td>
						</tr>';
			}
			echo $disp;
		break;
		
		case 'lab_mapping':
			$formvars = $_REQUEST;
			$survey_id = $formvars['surid'];
			$partid = $formvars['partid'];
			$mode = $formvars['mode'];
			
			$query = "SELECT * FROM custom_lab_samples WHERE lab_smple_sur_id = '".$survey_id."' AND lab_smple_part_id = '".$partid."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$sample_rec = $common->sql->record;
			
			$qry = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$partid."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			$loc = $rec[0]['reg_location'];
			// $partid = $rec[0]['reg_id'];
			
			$query = "SELECT * FROM custom_lab_mapping 
							INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
							INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
							LEFT JOIN custom_lab_samples 
								ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
									AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
									AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
									AND lab_smple_part_id = '".$partid."')
							LEFT JOIN custom_lab_result_master ON (custom_lab_result_master.lab_result_code = custom_lab_samples.lab_smple_result_id)
						WHERE lab_mapng_sur_id = '".$formvars['surid']."' ORDER BY lab_id";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			if($mode == 'add')
			{
				$st = ' display: none;';
				$col = '<th class="question_title" style="width: 15%;'.$st.'" id="div_result_hdr">Test Result</th>';
			}
			else
			{
				$col = '<th class="question_title" style="width: 15%;'.$st.'" id="div_acsn_hdr">Accession Number</th>
                    	<th class="question_title" style="width: 25%;'.$st.'" id="div_result_hdr">Test Result</th>';
			}
			
			$disp = '<tr>
                        <th class="question_title" style="width: 20%;">Test</th>
                        <th class="question_title" style="width: 25%;">Specimen</th>
                    	<th class="question_title" style="width: 15%;">Collected?</th>
                    	<th class="question_title" style="width: 15%;'.$st.'" id="div_date_hdr">Date</th>'.$col.'
                    	<!--<th class="question" style="width: 10%;" align="center">&nbsp;</th>-->
            		</tr>';
			
			for($i=0; $i<count($rec); $i++)
			{
				if($rec[$i]['lab_smple_date'] == '')
					$dt = str_replace("%", "", date(DT_FRMT));
				else
					$dt = $rec[$i]['lab_smple_date'];
				
				$align = 'align="center"';
				if($mode == 'add')
				{
					if($rec[$i]['lab_smple_spcmn_clctd'] == 1)
						$chk = '<input type="checkbox" name="spcmn_type['.$rec[$i]['lab_id'].']['.$rec[$i]['lab_spcmn_id'].']" id="spcmn_type_'.$i.'" value="1" checked onclick="return enb_cols('.$i.', \''.$mode.'\');" />';
					else
						$chk = '<input type="checkbox" name="spcmn_type['.$rec[$i]['lab_id'].']['.$rec[$i]['lab_spcmn_id'].']" id="spcmn_type_'.$i.'" value="1" onclick="return enb_cols('.$i.', \''.$mode.'\');" />';
				}
				else
				{
					if($rec[$i]['lab_smple_spcmn_clctd'] == 1)
					{
						$align = 'align="left"';
						$chk = "&nbsp;Collected";
						// $chk .= '<input type="hidden" name="spcmn_type[]" id="spcmn_type_'.$i.'" value="'.$rec[$i]['lab_smple_spcmn_clctd'].'" />';
						$chk .= '<input type="hidden" name="spcmn_type_hdn[]" id="spcmn_type_hdn_'.$i.'" value="'.$rec[$i]['lab_smple_spcmn_clctd'].'" />';
						$st = '';
						$dt_new = $rec[$i]['lab_smple_date'];
						$dt_new = $common->date_format($rec[$i]['lab_smple_date'], DT_FMT, true);
					}
					else
					{
						$chk = '<input type="hidden" name="spcmn_type_hdn[]" id="spcmn_type_hdn_'.$i.'" value="0" />';
						$chk .= '<input type="checkbox" name="spcmn_type['.$rec[$i]['lab_id'].']['.$rec[$i]['lab_spcmn_id'].']" id="spcmn_type_'.$i.'" value="1" onclick="return enb_cols('.$i.', \''.$mode.'\');" />';
						$st = ' display: none;';
						$dt_new = '';
					}
				}
				if($mode == 'edit')
				{
					$st = '';
					$st_span = '';
					$col = '<td class="question" style="width: 15%;'.$st.'" id="acsn_col_'.$i.'">
								<input type="hidden" name="ascn_no[]" id="ascn_no_'.$i.'" value="'.$rec[$i]['lab_smple_acc_no'].'" /> &nbsp;
								<span id="spn_ascn_no_'.$i.'" style="'.$st_span.'">'.$rec[$i]['lab_smple_acc_no'].'</span>
							</td>';
					if($rec[$i]['lab_smple_spcmn_clctd'] == 0)
					{
						$col .= '<td class="question" style="width: 15%;'.$st.'" id="result_col_'.$i.'">&nbsp;';
						$col .= $common->dropdown('lab_result_code', 'lab_result_name', 'custom_lab_result_master', '', 'lab_result_name', 'result_id[]', $result_id, 'class="required" style="display: none;"', "asc", false);
						$col .= '</td>';
						$col .= '<script language="javascript">arr_id_change_zero("result_id[]");</script>';
					}
					//elseif($rec[$i]['lab_result_code'] == "RES0")
					elseif($rec[$i]['lab_result_code'] == 9) // changed by Vidhya - Oct 20,2017
					{
						$col .= '<td class="question" style="width: 15%;'.$st.'" id="result_col_'.$i.'">&nbsp;';
						//$col .= $common->dropdown('lab_result_code', 'lab_result_name', 'custom_lab_result_master', '', 'lab_result_name', 'result_id[]', "RES0", 'class="required"', "asc", false);
						// changed by Vidhya - Oct 20,2017
						$col .= $common->dropdown('lab_result_code', 'lab_result_name', 'custom_lab_result_master', '', 'lab_result_name', 'result_id[]', 9 , 'class="required"', "asc", false);
						$col .= '</td>';
						// $col .= '<script language="javascript">arr_id_change_zero("result_id[]");</script>';
					}
					else
					{
						$col .= '<td class="question" style="width: 15%;'.$st.'" id="acsn_col_'.$i.'" nowrap>
									<input type="hidden" name="result_id[]" id="result_id" value="'.$rec[$i]['lab_result_name'].'" /> &nbsp;
									<span id="result_id'.$i.'" style="'.$st_span.'">'.$rec[$i]['lab_result_name'].'</span> &nbsp;
								</td>';
					}
				}
				else
				{
					$st_span = $st;
					$col = '<td class="question" style="width: 15%;'.$st.'" id="result_col_'.$i.'">&nbsp;';
					$col .= $common->dropdown('lab_result_code', 'lab_result_name', 'custom_lab_result_master', '', 'lab_result_name', 'result_id[]', $result_id, 'class="required" style="display: none;"', "asc", false);
					$col .= '</td>';
					$col .= '<script language="javascript">arr_id_change_zero("result_id[]");</script>';
				}
				
				$disp .= '<tr>
							<td class="question" style="width: 20%;">
							&nbsp;'.$rec[$i]['lab_test_name'].'
							<input type="hidden" name="lab_mstr[]" id="lab_id_'.$i.'" value="'.$rec[$i]['lab_id'].'" />
							<input type="hidden" name="location[]" id="location_'.$i.'" value="'.$loc.'" />
							<input type="hidden" name="lab_spot_test[]" id="lab_spot_test_'.$i.'" value="'.$rec[$i]['lab_spot_test'].'" />
							</td>
							<td class="question" style="width: 25%;">
							&nbsp;'.$rec[$i]['lab_spcmn_name'].'
							<input type="hidden" name="spcmn_id[]" id="spcmn_mstr_id_'.$i.'" value="'.$rec[$i]['lab_spcmn_id'].'" />
							</td>
							<td class="question" style="width: 15%;" '.$align.'>
								'.$chk.'
							</td>
							<td class="question" style="width: 15%;'.$st.'" id="date_col_'.$i.'">
								<input type="hidden" name="date[]" id="date_'.$i.'" value="'.$dt.'" /> &nbsp;
								<span id="spn_date_'.$i.'" style="'.$st_span.'">'.$dt_new.'</span>
							</td>
							'.$col.'
							<!--<td class="question" style="width: 10%;" align="center">
								<a style="cursor: pointer;" onclick="whichRow(\'dataTable\', this);">
									<img src="includes/images/delete.png" />
								</a>
							</td>-->
						</tr>';
			}
			
			if(count($rec) == 0)
			{
				$disp .= '<tr>
							<td class="error" colspan="2" align="center">Specimens are Not Avaliable</td>
						</tr>';
			}
			echo $disp;
		break;
		
		case 'reibmusement':
			$formvars = $_REQUEST;
			$survey_id = $formvars['surid'];
			$part_id = $formvars['partid'];
			$partid = $formvars['partid'];
			$mode = $formvars['mode'];
			$rec_id = $formvars['rec_id'];
			
			$query = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$part_id."' AND reg_biometric_reference = '1'";
			$query = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$part_id."' AND reg_biometric_reference != ''";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$reg = $common->sql->record;
			
			$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$sur_mstr_rec = $common->sql->record;
			$db_sur_code = $sur_mstr_rec[0]['sur_code'];
			$db_sur_id = $sur_mstr_rec[0]['sur_onlne_id'];
			$p_id = $sur_mstr_rec[0]['sur_pid'];
			
			$db_server = $sur_mstr_rec[0]['sur_pri_db_server'];
			$db_name = $sur_mstr_rec[0]['sur_pri_db_name'];
			$db_usrnme = $sur_mstr_rec[0]['sur_pri_db_usrnme'];
			$db_paswrd = $sur_mstr_rec[0]['sur_pri_db_paswrd'];
			
			$sur_conn = array('host' => $db_server, 'dbname' => $db_name, 'dbuser' => $db_usrnme, 'dbpass' => $db_paswrd);
			$survey = new Survey_Functions($sur_conn);
			
			$sur_chk = array();
			$surveys_chk = explode(",", $db_sur_id);
			$surveys_code_chk = explode(",", $db_sur_code);
			$surveys_pid = explode(",", $p_id);
			
			for($i=0; $i<count($surveys_chk); $i++)
			{
				$db_sur_id = $surveys_chk[$i];
				$db_sur_code = $surveys_code_chk[$i];
				$p_id = $surveys_pid[$i];
				
				$query = "SELECT submitdate FROM lime_survey_".$db_sur_id." WHERE ".$p_id." = '".$part_id."' AND submitdate IS NOT NULL";
				$survey->sql->query($query, SQL_ALL, SQL_ASSOC);
				$sur_mstr = $survey->sql->record;
				
				if(count($sur_mstr) == 0)
					$sur_chk[] = FALSE;
			}
			
			if(in_array(FALSE, $sur_chk))
				$sur_mstr = FALSE;
			else
				$sur_mstr = TRUE;
			
			$query = "SELECT * FROM custom_lab_mapping WHERE lab_mapng_sur_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$lab_mpng = $common->sql->record;
			
			$query = "SELECT * FROM custom_lab_samples WHERE lab_smple_part_id = '".$part_id."' AND lab_smple_sur_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$lab_smples_rec = $common->sql->record;
			
			$query = "SELECT * FROM custom_lab_samples 
							INNER JOIN custom_lab_mapping ON (custom_lab_mapping.lab_mapng_sur_id = custom_lab_samples.lab_smple_sur_id)
						WHERE lab_smple_part_id = '".$part_id."' AND lab_smple_sur_id = '".$survey_id."'";
			
			$query = "SELECT * FROM custom_lab_mapping 
							INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
							INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
							INNER JOIN custom_lab_samples 
								ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
									AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
									AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
									AND lab_smple_part_id = '".$partid."' AND lab_smple_spcmn_clctd = 0)
						WHERE lab_mapng_sur_id = '".$survey_id."' ORDER BY lab_id";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$lab_smples = $common->sql->record;
			
			$query = "SELECT * FROM custom_reimbursement_heads_master 
							INNER JOIN custom_reimbursement_mapping ON (custom_reimbursement_mapping.reimbrsmnt_head_mstr_id = custom_reimbursement_heads_master.reimbrsmnt_head_id)
							LEFT JOIN custom_reimbursement ON (custom_reimbursement.reimbrsmnt_head_id = custom_reimbursement_heads_master.reimbrsmnt_head_id
								AND custom_reimbursement.reimbrsmnt_survy_id = custom_reimbursement_mapping.reimbrsmnt_survey_id AND reimbrsmnt_particpnt_id = '".$partid."')
						WHERE reimbrsmnt_survey_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			for($i=0; $i<count($rec); $i++)
			{
				if($rec_id == $rec[$i]['reimbrsmnt_id'])
					$rec_vchr_no = $rec[$i]['reimbrsmnt_vchr_no'];
			}
			
			if(count($reg) != 0 || BIOREF == 'N')
			{
				if($sur_mstr || SURCMP == 'N')
				{
					if((count($lab_smples_rec) != 0 && count($lab_smples) == 0) || LABREQ == 'N')
					{
						$qry = "SELECT reimbrsmnt_amt FROM custom_reimbursement WHERE reimbrsmnt_head_id = '0' AND reimbrsmnt_particpnt_id = '".$partid."' AND reimbrsmnt_survy_id = '".$survey_id."'";
						$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
						// $rec = $common->sql->record;
						$oth_val = $common->sql->record[0]['reimbrsmnt_amt'];
						
						if($mode == "edit" || $mode == "edit_print")
						{
							$col = '<td class="question_title" style="width: 20%;" nowrap>Voucher Number</td>
									<td class="question_title" style="width: 20%;" nowrap>Voucher Date</td>';
						}
						else
							$col = '';
						
						$disp = '<tr>
									<td class="question_title" style="width: 40%;">Reimbursement Head</td>
									<td class="question_title" style="width: 20%;">Amount</td>
									<td class="question_title" style="width: 20%;">Pay</td>'.$col.'
								</tr>';
						
						$j = 0;
						$chk_enable = false;
						for($i=0; $i<count($rec); $i++)
						{
							if($mode == "edit" || $mode == "add" || ($mode == "edit_print" && $rec_vchr_no == $rec[$i]['reimbrsmnt_vchr_no']))
							{
								if($rec[$i]['reimbrsmnt_amt'] == "")
								{
									if($rec[$i]['reimbrsmnt_type'] == 1)
										$amt = '<input type="hidden" name="reimburs_amt[]" id="reimburs_amt_'.$j.'" value="'.$rec[$i]['reimbrsmnt_amount'].'" />'.$rec[$i]['reimbrsmnt_amount'];
									else
										$amt = '<input type="text" style="text-align: right;" name="reimburs_amt[]" id="reimburs_amt_'.$j.'" value="" />';
								}
								else
								{
									if($rec[$i]['reimbrsmnt_amt'] != "0.00")
										$amt = '<input type="hidden" name="reimburs_amt[]" id="reimburs_amt_'.$j.'" value="'.$rec[$i]['reimbrsmnt_amt'].'" />'.$rec[$i]['reimbrsmnt_amt'];
									else
										$amt = '<input type="text" style="text-align: right;" name="reimburs_amt[]" id="reimburs_amt_'.$j.'" value="'.$rec[$i]['reimbrsmnt_amt'].'" />';
								}
								
								$iss = $rec[$i]['reimbrsmnt_pay_status'];
								$incen_amt = $rec[$i]['reimbrsmnt_amt'];
								$vchr_no = $rec[$i]['reimbrsmnt_vchr_no'];
								$vchr_dt = $rec[$i]['reimbrsmnt_vchr_dt'];
								$vchr_dt = $common->date_format($vchr_dt, DT_FMT, true);
								
								if($iss == "Y")
								{
									$ins_incen_amt = $rec[$i]['reimbrsmnt_amt'];
									$issue = "";
									$iss_chk = "checked";
								}
								else
								{
									$ins_incen_amt = $incen_amt;
									$issue = 0;
									$iss_chk = "";
									$vchr_no = "";
									$vchr_dt = "";
									$chk_enable = true;
								}
								
								$amt .= '<input type="hidden" name="chk_enable[]" id="chk_enable" value="'.(int)$chk_enable.'" />';
								if($iss_chk == "")
								{
									$issued = '<input type="checkbox" name="issue[]" id="issue_'.$j.'" value="'.$rec[$i]['coupn_id'].'" onclick="return val_chk_box(\'issue_val_\', this.checked, '.$j.');" '.$iss_chk.' />
									<input type="hidden" name="issue_val[]" id="issue_val_'.$j.'" value="'.$issue.'" />';
								}
								else
								{
									$issued = 'Y
									<input type="hidden" name="issue[]" id="issue_'.$j.'" value="'.$issue.'" />
									<input type="hidden" name="issue_val[]" id="issue_val_'.$j.'" value="'.$issue.'" />';
								}
								if($mode == "edit" || $mode == "edit_print")
								{
									$col = '<td class="question" style="width: 20%;">&nbsp;'.$vchr_no.'&nbsp;</td>
											<td class="question" style="width: 20%;">&nbsp;'.$vchr_dt.'&nbsp;</td>';
								}
								else
								{
									$col = "";
								}
								$disp .= '<tr>
											<td class="question" style="width: 40%;">
											&nbsp;'.$rec[$i]['reimbrsmnt_head_name'].'
											<input type="hidden" name="reimburs_id[]" id="reimburs_id_'.$j.'" value="'.$rec[$i]['reimbrsmnt_head_mstr_id'].'" />
											</td>
											<td class="question" style="width: 20%; text-align: right;">&nbsp;'.$amt.'&nbsp;</td>
											<td class="question" style="width: 20%;">&nbsp;'.$issued.'&nbsp;</td>'.$col.'
										</tr>';
								$j++;
							}
						}
						
						if($oth_val != "0.00")
						{
							if($mode == "add")
							{
								$oth_amt = '<input type="text" style="text-align: right;" name="reimburs_amt[]" id="reimburs_amt_'.($i+1).'" value="'.$oth_val.'" />';
								$col = "";
							}
							else
							{
								$oth_amt = '<input type="hidden" style="text-align: right;" name="reimburs_amt[]" id="reimburs_amt_'.($i+1).'" value="'.$oth_val.'" />'.$oth_val;
								$col = '<td class="question" style="width: 20%;">&nbsp;&nbsp;</td>';
							}
							
							$disp .= '<tr>
										<td class="question" style="width: 40%;">&nbsp;Others
											<input type="hidden" name="reimburs_id[]" id="reimburs_id_'.($i+1).'" value="0" />
										</td>
										<td class="question" style="width: 20%; text-align: right;">&nbsp;'.$oth_amt.'&nbsp;</td>
										<td class="question" style="width: 20%;">&nbsp;&nbsp;</td>
									</tr>';
						}
						
						if(count($rec) == 0)
						{
							$disp .= '<tr>
										<td class="error" colspan="2" align="center">Participant Not Eligible - Reimbursement Heads are Not Avaliable</td>
									</tr>';
						}
					}
					else
					{
						$disp .= '<tr>
									<td class="error" colspan="2" align="center">Participant Not Eligible - Samples are Not Submitted</td>
								</tr>';
					}
				}
				else
				{
					$disp .= '<tr>
								<td class="error" colspan="2" align="center">Participant Not Eligible - Survey Not Completed</td>
							</tr>';
				}
			}
			else
			{
				$disp .= '<tr>
							<td class="error" colspan="2" align="center">Participant Not Eligible - Registration Not Completed</td>
						</tr>';
			}
			echo $disp;
		break;
		
		case 'incentive':
			$formvars = $_REQUEST;
			$survey_id = $formvars['surid'];
			$part_id = $formvars['partid'];
			$mode = $formvars['mode'];
			$incentive_id = $formvars['incentive_id'];
			
			$qry = "SELECT * FROM custom_incentive WHERE incentive_id = '".$incentive_id."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$inc_rec = $common->sql->record;
			$incentive_id = $inc_rec[0]['incentive_vchr_no'];
			
			$query = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$part_id."' AND reg_biometric_reference = '1'";
			$query = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$part_id."' AND reg_biometric_reference != ''";
			$query = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$part_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$reg = $common->sql->record;
			$partid = $reg[0]['reg_id'];
			$ini_partid = $reg[0]['reg_id'];
			$partid = $reg[0]['reg_participant_id'];
			$ini_partid = $reg[0]['reg_participant_id'];
			
			$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$sur_mstr_rec = $common->sql->record;
			$db_sur_code = $sur_mstr_rec[0]['sur_code'];
			$db_sur_id = $sur_mstr_rec[0]['sur_onlne_id'];
			$p_id = $sur_mstr_rec[0]['sur_pid'];
			
			$db_server = $sur_mstr_rec[0]['sur_pri_db_server'];
			$db_name = $sur_mstr_rec[0]['sur_pri_db_name'];
			$db_usrnme = $sur_mstr_rec[0]['sur_pri_db_usrnme'];
			$db_paswrd = $sur_mstr_rec[0]['sur_pri_db_paswrd'];
			
			$sur_conn = array('host' => $db_server, 'dbname' => $db_name, 'dbuser' => $db_usrnme, 'dbpass' => $db_paswrd);
			$survey = new Survey_Functions($sur_conn);
			
			$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE 91878X93X4214 = '".$part_id."' AND submitdate IS NOT NULL";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$sur_mstr = $common->sql->record;
			
			$sur_chk = array();
			$surveys_chk = explode(",", $db_sur_id);
			$surveys_code_chk = explode(",", $db_sur_code);
			$surveys_pid = explode(",", $p_id);
			// print_r($surveys_chk);
			
			for($i=0; $i<count($surveys_chk); $i++)
			{
				if(count($sur_mstr) == 0)
					$sur_chk[] = FALSE;
				
				$db_sur_id = $surveys_chk[$i];
				$db_sur_code = $surveys_code_chk[$i];
				$p_id = $surveys_pid[$i];
				
				/*
				if($db_sur_code == "YRGBS1")
					$p_id = "23634X14X898";
				elseif($db_sur_code == "YRGBS2")
					$p_id = "23634X16X486";
				elseif($db_sur_code == "IDURDSII")
					$p_id = "166474X19X1162";
				else
					$p_id = "";
				*/
					
				$query = "SELECT submitdate FROM lime_survey_".$db_sur_id." WHERE ".$p_id." = '".$part_id."' AND submitdate IS NOT NULL";
				$survey->sql->query($query, SQL_ALL, SQL_ASSOC);
				$sur_mstr = $survey->sql->record;
				
				/*$db_sur_id = $surveys_chk[$i];
				$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE participant_id = '".$part_id."' AND submitdate IS NOT NULL";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$sur_mstr = $common->sql->record;*/
				
				if(count($sur_mstr) == 0)
					$sur_chk[] = FALSE;
			}
			
			if(in_array(false, $sur_chk))
				$sur_mstr = FALSE;
			else
				$sur_mstr = TRUE;
			
			//$sur_mstr = TRUE;
			
			$query = "SELECT * FROM custom_lab_mapping WHERE lab_mapng_sur_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$lab_mpng = $common->sql->record;
			
			$query = "SELECT * FROM custom_lab_samples WHERE lab_smple_part_id = '".$part_id."' AND lab_smple_sur_id = '".$survey_id."'";
			
			$query = "SELECT * FROM custom_lab_samples 
							INNER JOIN custom_lab_mapping ON (custom_lab_mapping.lab_mapng_sur_id = custom_lab_samples.lab_smple_sur_id)
						WHERE lab_smple_part_id = '".$part_id."' AND lab_smple_sur_id = '".$survey_id."'";
			
			$query = "SELECT * FROM custom_lab_mapping 
							INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
							INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
							INNER JOIN custom_lab_samples 
								ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
									AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
									AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
									AND lab_smple_part_id = '".$partid."' AND lab_smple_spcmn_clctd = 0)
						WHERE lab_mapng_sur_id = '".$survey_id."' ORDER BY lab_id";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$lab_smples = $common->sql->record;
			
			$query = "SELECT * FROM custom_reimbursement_heads_master 
							INNER JOIN custom_reimbursement_mapping ON (custom_reimbursement_mapping.reimbrsmnt_head_mstr_id = custom_reimbursement_heads_master.reimbrsmnt_head_id)
							INNER JOIN custom_reimbursement ON (custom_reimbursement.reimbrsmnt_head_id = custom_reimbursement_heads_master.reimbrsmnt_head_id
								AND custom_reimbursement.reimbrsmnt_survy_id = custom_reimbursement_mapping.reimbrsmnt_survey_id AND reimbrsmnt_particpnt_id = '".$partid."')
						WHERE reimbrsmnt_survey_id = '".$survey_id."'";
			
			$query = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$partid."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			$query = "SELECT * FROM custom_settings";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$stngs = $common->sql->record[0];
			$incen_amt = $stngs['stngs_incendv_amt'];
			
			$query = "SELECT * FROM custom_app_settings WHERE app_survey_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$stngs = $common->sql->record[0];
			
			if($stngs['app_incentive'] == "Y"){
				$incen_amt = $stngs['app_incentive_amt'];
			    $incen_amt_fixed = $stngs['app_incentive_amt_fixed'];
			}
			else {
				$incen_amt = "N/A";
				$incen_amt_fixed ="Y";
			 }
			
			if(count($reg) != 0)
			{
				// if($sur_mstr)
				if(count($sur_mstr) != 0)
				{
					if(count($lab_smples) == 0)
					{
						if($mode == "edit" || $mode == "edit_print")
						{
							//$first_col = '<td class="question_title" style="width: 25%;">&nbsp;</td>';
							$col = '<td class="question_title" style="width: 20%;" nowrap>Voucher Number</td>
									<td class="question_title" style="width: 20%;" nowrap>Voucher Date</td>';
						}
						else
						{
							//$first_col = '';
							$col = '';
						}
						
						$disp = '<tr>
									<td class="question_title" style="width: 25%;">Coupon Code</td>
									<td class="question_title" style="width: 15%;">Amount</td>
									<td class="question_title" style="width: 25%;">Status</td>
									<td class="question_title" style="width: 15%;">Pay</td>'.$col.'
								</tr>';
						
						for($i=0; $i<count($rec); $i++)
						{
							$query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$rec[$i]['coupn_participant_id']."' AND reg_biometric_reference = '1'";
							$query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$rec[$i]['coupn_participant_id']."' AND reg_biometric_reference != ''";
							$query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$rec[$i]['coupn_participant_id']."'";
							$common->sql->query($query, SQL_ALL, SQL_ASSOC);
							$reg = $common->sql->record;
							$partid = $reg[0]['reg_id'];
							$partid = $reg[0]['reg_participant_id'];
							$part_id = $reg[0]['reg_participant_id'];
							
							if(count($reg) == 0)
							{
								$sts = "Registration Pending";
								$sts_val = 1;
							}
							else
							{
								$query = "SELECT * FROM custom_survey_master WHERE sur_id = '".$survey_id."'";
								$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$survey_id."'";
								$common->sql->query($query, SQL_ALL, SQL_ASSOC);
								$sur_mstr_rec = $common->sql->record;
								$db_sur_code = $sur_mstr_rec[0]['sur_code'];
								$db_sur_id = $sur_mstr_rec[0]['sur_onlne_id'];
								$p_id = $sur_mstr_rec[0]['sur_pid'];
								
								$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE 91878X93X4214 = '".$part_id."' AND submitdate IS NOT NULL";
								$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE participant_id = '".$part_id."' AND submitdate IS NOT NULL";
								$common->sql->query($query, SQL_ALL, SQL_ASSOC);
								$sur_mstr = $common->sql->record;
								// $sur_mstr = TRUE;
								
								/*
								if($db_sur_code == "YRGBS1")
									$p_id = "23634X14X898";
								elseif($db_sur_code == "YRGBS2")
									$p_id = "23634X16X486";
								elseif($db_sur_code == "IDURDSII")
									$p_id = "166474X19X1162";
								else
									$p_id = "";
								*/
								
								$query = "SELECT submitdate FROM lime_survey_".$db_sur_id." WHERE ".$p_id." = '".$part_id."' AND submitdate IS NOT NULL";
								$survey->sql->query($query, SQL_ALL, SQL_ASSOC);
								$sur_mstr = $survey->sql->record;
								
								// if(!$sur_mstr)
								if(count($sur_mstr) == 0)
								{
									$sts = "Survey Pending";
									$sts_val = 2;
								}
								else
								{
									$query = "SELECT * FROM custom_lab_mapping WHERE lab_mapng_sur_id = '".$survey_id."'";
									$common->sql->query($query, SQL_ALL, SQL_ASSOC);
									$lab_mpng = $common->sql->record;
									
									$query = "SELECT * FROM custom_lab_mapping 
													INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
													INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
													INNER JOIN custom_lab_samples 
														ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
															AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
															AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
															AND lab_smple_part_id = '".$partid."' AND lab_smple_spcmn_clctd = 1)
												WHERE lab_mapng_sur_id = '".$survey_id."' ORDER BY lab_id";
									$common->sql->query($query, SQL_ALL, SQL_ASSOC);
									$lab_smples = $common->sql->record;
									
									if(count($lab_smples) != count($lab_mpng))
									{
										$sts = "Lab Samples Pending";
										$sts_val = 3;
									}
									else
									{
										$sts = "Qualified";
										$sts_val = 4;
									}
								}
							}
							
							/*$sts = "Qualified";
							$sts_val = 4;*/
							
							if($sts == "Qualified")
							{
								$qry = "SELECT * FROM custom_incentive WHERE incentive_particpnt_id = '".$ini_partid."' AND incentive_survy_id = '".$survey_id."' AND incentive_cupn_id = '".$rec[$i]['coupn_participant_id']."'";
								$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
								$inc_rec = $common->sql->record;
								
								$iss = $inc_rec[0]['incentive_issue'];
								
								if($iss == 1)
								{
									$ins_incen_amt = $inc_rec[0]['incentive_amt'];
									$issue = 1;
									$iss_chk = "checked";
								}
								else
								{
									$ins_incen_amt = $incen_amt;
									$issue = 0;
									$iss_chk = "";
								}
								
								if($iss_chk == "")
								{
									$issued = '<input type="checkbox" name="issue[]" id="issue_'.$i.'" value="'.$rec[$i]['coupn_id'].'" onclick="return val_chk_box(\'issue_val_\', this.checked, '.$i.');" '.$iss_chk.' />
									<input type="hidden" name="issue_val[]" id="issue_val_'.$i.'" value="'.$issue.'" />';
								}
								else
								{
									$issued = 'Y
									<input type="hidden" name="issue[]" id="issue_'.$i.'" value="'.$issue.'" />
									<input type="hidden" name="issue_val[]" id="issue_val_'.$i.'" value="'.$issue.'" />';
								}
								$pd = 'N<input type="hidden" name="paid_val[]" id="paid_val_'.$i.'" value="0" />';
								
								if ($incen_amt_fixed == "Y")
								 $incen_amt_disp = '<input type="hidden" name="reimburs_amt[]" id="reimburs_amt_'.$i.'" value="'.$ins_incen_amt.'" />'.$ins_incen_amt;
								else {								
								 if($mode == "add" || $mode == "edit") {	  
                                   if($inc_rec[0]['incentive_issue'] == 1)
                                     $incen_amt_disp = '<input type="text" name="reimburs_amt[]" id="reimburs_amt_'.$i.'" readonly=true value=" '.$inc_rec[0]['incentive_amt'].'" />';
                                   else							  
								    $incen_amt_disp = '<input type="text" name="reimburs_amt[]" id="reimburs_amt_'.$i.'" maxlength=3 value="" />';
							     }							
								else		
                                  $incen_amt_disp = $inc_rec[0]['incentive_amt'];		
							    }
								
								if($inc_rec[0]['incentive_issue'] == 1)
								{
									$vchr_no = $inc_rec[0]['incentive_vchr_no'];
									$vchr_dt = $inc_rec[0]['incentive_vchr_dt'];
									$vchr_dt = $common->date_format($vchr_dt, DT_FMT, true);
								}
								else
								{
									$vchr_no = "";
									$vchr_dt = "";
								}
								$col_visible = true;
							}
							else
							{
								$issued = 'N/A<input type="checkbox" name="issue[]" id="issue_'.$i.'" value="" style="display: none;" /><input type="hidden" name="issue_val[]" id="issue_val_'.$i.'" value="NA" />';
								$pd = 'N/A<input type="hidden" name="paid_val[]" id="paid_val_'.$i.'" value="NA" />';
									
								if ($incen_amt_fixed == "Y")
								  $incen_amt_disp = '<input type="hidden" name="reimburs_amt[]" id="reimburs_amt_'.$i.'" value="'.$incen_amt.'" />'.$incen_amt;	
							    else
                              	  $incen_amt_disp = '<input type="hidden" name="reimburs_amt[]" id="reimburs_amt_'.$i.'" value="" />';							
								$col_visible = false;							
							}
							
							if($mode == "edit" || $mode == "edit_print")
							{
								if($inc_rec[0]['incentive_issue'] == 1)
								{
									$first_col = '<td class="question" style="width: 20%;">&nbsp;<input type="radio" name="print_enb[]" id="print_enb_'.$i.'" value="'.$inc_rec[0]['incentive_id'].'" /></td>';
								}
								else
									$first_col = '<td class="question" style="width: 20%;">&nbsp;</td>';
								
								$col = '<td class="question" style="width: 20%;">&nbsp;'.$vchr_no.'&nbsp;</td>
										<td class="question" style="width: 20%;">&nbsp;'.$vchr_dt.'&nbsp;</td>';
							}
							else
							{
								$first_col = '';
								$col = "";
							}
							
							if(!$col_visible && $col != "")
							{
								$col = '<td class="question" style="width: 20%;">&nbsp;&nbsp;</td>
										<td class="question" style="width: 20%;">&nbsp;&nbsp;</td>';
							}
							
							if(($incentive_id == $inc_rec[0]['incentive_vchr_no'] && $col_visible && $mode == "edit_print") || $mode == "add" || $mode == "edit")
							{								
								$disp .= '<tr>
											<td class="question" style="width: 25%;">
												&nbsp;'.$rec[$i]['coupn_participant_id'].'
												<input type="hidden" name="reimburs_id[]" id="reimburs_id_'.$i.'" value="'.$rec[$i]['coupn_participant_id'].'" />
											</td>
											<td class="question" style="width: 40%;">
												&nbsp;'.$incen_amt_disp.'												
											</td>
											<td class="question" style="width: 25%;">&nbsp;'.$sts.'
												&nbsp;<input type="hidden" name="status[]" id="status_'.$i.'" value="'.$sts_val.'" />
											</td>
											<td class="question" style="width: 15%;">&nbsp;'.$issued.'</td>'.$col.'
										</tr>';
							}
						
						}
						
						if(count($rec) == 0)
						{
							$disp = '<tr>
										<td class="error" colspan="2" align="center">Participant Not Eligible - Coupon Codes are Not Avaliable</td>
									</tr>';
						}
					}
					else
					{
						$disp = '<tr>
									<td class="error" colspan="2" align="center">Participant Not Eligible - Samples are Not Submitted</td>
								</tr>';
					}
				}
				else
				{
					$disp = '<tr>
								<td class="error" colspan="2" align="center">Participant Not Eligible - Survey Not Completed</td>
							</tr>';
				}
			}
			else
			{
				$disp = '<tr>
							<td class="error" colspan="2" align="center">Participant Not Eligible - Recurt(s) Registration Not Completed</td>
						</tr>';
			}
			echo $disp;
		break;
	}
?>