<?php 
$form_name = "Registration";
$table_name = "custom_participant_registration";
$primary_key = "reg_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	
	case "submit":
		$_REQUEST['dor'] = $common->date_format($_REQUEST['dor'], DT_FMT);
		$_REQUEST['dob'] = $common->date_format($_REQUEST['dob'], DT_FMT);
		
		$qry = "SELECT * FROM custom_app_settings WHERE app_survey_id = '".$_REQUEST['survey_id']."'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		$app_cupn_cde_fixed = $rec[0]['app_cupn_cde_fixed'];
		
		$qry = "SELECT Substring(reg_participant_id, 3) AS part_id FROM ".$table_name." WHERE reg_location = '".$_REQUEST['location']."' ORDER BY reg_id DESC LIMIT 0, 1";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		if(count($rec) == 0)
			$part_id = "0001";
		else
		{
			$part_id = $rec[0]['part_id']+1;
			
			if(strlen($part_id) < 4)
			{
				for($i=0; $i<3; $i++)
					$zero .= "0";
				$part_id = $zero.$part_id;
			}
		}
		
		 
		if($_REQUEST['tab_id'] == 1)
		{
			if($_REQUEST['id'] == "")
			{
				$qry = "SELECT * FROM custom_coupon_master WHERE coupn_participant_id = '".$_REQUEST['cupn_cde']."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$chk_rec = $common->sql->record;
				$chk_rec_cnt = count($chk_rec);
				
				$qry = "SELECT * FROM ".$table_name." WHERE reg_coupon_code = '".$_REQUEST['cupn_cde']."' AND reg_survey_id = '".$_REQUEST['survey_id']."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$chk_rec = $common->sql->record;
				
				$qry = "SELECT * FROM custom_coupon_master WHERE coupn_participant_id = '".$_REQUEST['cupn_cde']."' AND coupn_valid_period >= CURDATE()";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$cpn_chk_rec = $common->sql->record;
				
				$chk_flag = false;
				
				if($_REQUEST['location_type'] == "S")
				{
					if($chk_rec_cnt == 0 && count($chk_rec) == 0)
						$chk_flag = true;
				}
				elseif($_REQUEST['location_type'] == "W")
				{
					//echo $chk_rec_cnt." == 1 && ".count($chk_rec)." == 0 && ".count($cpn_chk_rec)." == 1)";
					if($chk_rec_cnt == 1 && count($chk_rec) == 0 && count($cpn_chk_rec) == 1)
						$chk_flag = true;
				}
				
			
				
				if($chk_flag)
				{
					if($app_cupn_cde_fixed != "Y")
						$_REQUEST['participant_id'] = $_REQUEST['location'].$part_id;
					else
						$_REQUEST['participant_id'] = $_REQUEST['cupn_cde'];
					
					$_query = sprintf("insert into ".$table_name."(`reg_id`, `reg_survey_id`, `reg_coupon_code`, `reg_participant_id`, 
							`reg_location`, `reg_participant_type`, `reg_date_of_recurit`, `reg_network_size`, `reg_date_of_birth`, 
							`reg_age`, `reg_sex`, `reg_remarks`, `reg_biometric_reference`, `status`, `create_time`, `create_user`, 
							`update_time`, `update_user`) 
							values (0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '', '%s', 'Y', now(), '%s', 0, 0)",
						mysql_escape_string($_REQUEST['survey_id']),
						mysql_escape_string($_REQUEST['cupn_cde']),
						mysql_escape_string($_REQUEST['participant_id']),
						mysql_escape_string($_REQUEST['location']),
						mysql_escape_string($_REQUEST['location_type']),
						mysql_escape_string($_REQUEST['dor']),
						mysql_escape_string($_REQUEST['ntwrk_sze']),
						mysql_escape_string($_REQUEST['dob']),
						mysql_escape_string($_REQUEST['age']),
						mysql_escape_string($_REQUEST['sex']),
						mysql_escape_string($_REQUEST['biometric_ref']),
						mysql_escape_string(USR)
					);
						
						/* $_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '', '%s', 'Y', now(), '%s', 0, 0,1,'Y','Y',10)",
								mysql_escape_string($_REQUEST['survey_id']),
								mysql_escape_string($_REQUEST['cupn_cde']),
								mysql_escape_string($_REQUEST['participant_id']),
								mysql_escape_string($_REQUEST['location']),
								mysql_escape_string($_REQUEST['location_type']),
								mysql_escape_string($_REQUEST['dor']),
								mysql_escape_string($_REQUEST['ntwrk_sze']),
								mysql_escape_string($_REQUEST['dob']),
								mysql_escape_string($_REQUEST['age']),
								mysql_escape_string($_REQUEST['sex']),
								mysql_escape_string($_REQUEST['biometric_ref']),
								mysql_escape_string(USR)
								); */
							
					
					//$_query = "insert into ".$table_name." values(DEFAULT,$_REQUEST['survey_id'],$_REQUEST['cupn_cde'],$_REQUEST['participant_id'],
							//$_REQUEST['location'],$_REQUEST['location_type'],$_REQUEST['dor'],$_REQUEST['ntwrk_sze'],$_REQUEST['dob'],$_REQUEST['age'],$_REQUEST['sex'],$_REQUEST['biometric_ref'])"; 	
						
					$rec = $common->sql->query($_query);
					$last_id = mysql_insert_id();
					
					$_SESSION['msg'] = 'last_id='.$rec.',lastid='.$last_id.$_REQUEST['participant_id'].','.$_REQUEST['survey_id'].','.$_REQUEST['cupn_cde'].','.$_REQUEST['location'].','.$_REQUEST['location_type'].
					','.$_REQUEST['dor'].','.$_REQUEST['ntwrk_sze'].','.$_REQUEST['dob'].','.$_REQUEST['age'].','.$_REQUEST['sex'].','.$_REQUEST['biometric_ref'];
					
					//Added by Vidhya - 08Dec2017 - to update allocation into custom_participant_registration and random allocation table
					if($_REQUEST['location_type'] == 'W')
					{
					
					$qry = "SELECT allocation_id,allocation FROM custom_random_allocation WHERE used = 0 ORDER BY allocation_id ASC LIMIT 1";
					$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
					$rec = $common->sql->record;
					$alloc = $rec[0]['allocation'];
					$allocid = $rec[0]['allocation_id'];
					
					$upd_qry1 = "UPDATE custom_random_allocation SET used = 1,regid=$last_id WHERE allocation_id = $allocid";
					$common->sql->exeQuery($upd_qry1);
					
					$upd_qry = "UPDATE ".$table_name." SET rand_alloc = ".$alloc." WHERE reg_id		= '".$last_id."'";
					$common->sql->exeQuery($upd_qry);
					}
					
					$_SESSION['msg'] = 'The Participant ID <b>'.$_REQUEST['participant_id'].'</b> has been Registred Successfully';
					$common->UserLog($form_name, $table_name, $last_id, 'Insert');
						
				}
				else
				{
					$_SESSION['msg'] = 'Please Provide a Valid Coupon Code';
					
					if(!isset($_REQUEST['pid']))
						$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
					else
						$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page']."&pid=".$_REQUEST['pid'];
					header("location:".$link);
					return false;
				}
			}
			else
			{
				$last_id = $_REQUEST['id'];
				$upd_qry = "UPDATE ".$table_name." 	SET 	reg_survey_id 			= '".mysql_escape_string($_REQUEST['survey_id'])."',
															reg_coupon_code 		= '".mysql_escape_string($_REQUEST['cupn_cde'])."',
															reg_location 			= '".mysql_escape_string($_REQUEST['location'])."',
															reg_participant_type 	= '".mysql_escape_string($_REQUEST['location_type'])."',
															reg_date_of_recurit 	= '".mysql_escape_string($_REQUEST['dor'])."',
															reg_network_size 		= '".mysql_escape_string($_REQUEST['ntwrk_sze'])."',
															reg_date_of_birth 		= '".mysql_escape_string($_REQUEST['dob'])."',
															reg_age 				= '".mysql_escape_string($_REQUEST['age'])."',
															reg_sex 				= '".mysql_escape_string($_REQUEST['sex'])."'
													WHERE 	reg_participant_id		= '".$last_id."'";
				$common->sql->exeQuery($upd_qry);
				$_SESSION['msg'] = 'The Participant ID <b>'.$_REQUEST['participant_id'].'</b> has been Updated Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
			}
		}
		
		if($_REQUEST['tab_id'] == 2)
		{
			if(BIOREF == "Y")
			{
				$qry = "SELECT * FROM participant_finger_print WHERE participant_id = '".$_REQUEST['partcpnt_id2']."' AND ref_id = '".$_REQUEST['biometric_ref']."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$rec = $common->sql->record;
				$ref_chk = count($rec);
				
				// if($ref_chk != 0)
				// $ref_chk = true;
				if($ref_chk)
				{
					$qry = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$_REQUEST['partcpnt_id2']."'";
					$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
					$rec = $common->sql->record;
					
					$last_id = $rec[0]['reg_id'];
					$upd_qry = "UPDATE custom_participant_registration SET reg_biometric_reference = '".mysql_escape_string($_REQUEST['biometric_ref'])."' WHERE reg_id = '".$last_id."'";
					$common->sql->exeQuery($upd_qry);
			
					if($_REQUEST['id'] == "")
					{
						$_SESSION['msg'] = 'The Biometric Reference for the Participant ID <b>'.$_REQUEST['partcpnt_id2'].'</b> has been Registred Successfully';
						$common->UserLog($form_name, $table_name, $last_id, 'Biometric Reference Insert');
					}
					else
					{
						$_SESSION['msg'] = 'The Biometric Reference for the Participant ID <b>'.$_REQUEST['partcpnt_id2'].'</b> has been Updated Successfully';
						$common->UserLog($form_name, $table_name, $last_id, 'Biometric Reference Update');
					}
				}
				else
				{
					$_SESSION['msg'] = 'Please Provide a Valid Reference Code';
					$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-2';
					header("location:".$link);
					return false;
				}
			}
		}
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		if($_REQUEST['id'] != '')
		{
			/*if($_REQUEST['cupn'] != "")
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
			else
			{*/
				$last_id = $_REQUEST['id'];
				if($_REQUEST['tab_id'] == 1)
					$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
				elseif($_REQUEST['tab_id'] == 2)
					$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
				else
					$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
			// }
		}
		else
		{
			if($_REQUEST['tab_id'] == 1)
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
			elseif($_REQUEST['tab_id'] == 2)
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
			else
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		}
		
		if($_REQUEST['Submit'] == 'Next')
		{
			
			if($_REQUEST['id'] != '')
				$last_id = $_REQUEST['id'];
			if($_REQUEST['tab_id'] == 1)
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
			elseif($_REQUEST['tab_id'] == 2)
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
		}
		else
			$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
                
		if($_REQUEST['tab_id'] == 3)
		{
			$last_id = $_REQUEST['partcpnt_id'];
								
			$qry = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$_REQUEST['partcpnt_id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			$reg_id = $rec[0]['reg_id'];
			$_REQUEST['cupn_dt'] = $common->date_format($_REQUEST['cupn_dt'], DT_FMT);
			$_REQUEST['cupn_vld_dt'] = $common->date_format($_REQUEST['cupn_vld_dt'], DT_FMT);
			$error = false;
			
			/*if($_REQUEST['id'] != "")
			{
				$del_qry = "DELETE FROM custom_coupon_master WHERE coupn_reg_id = '".$last_id."'";
				$common->sql->exeQuery($del_qry);
			}*/
			
			for($i=0; $i<count($_REQUEST['cupn_lst']); $i++)
			{
				/*$qry = "SELECT * FROM custom_coupon_master 
                                                INNER JOIN custom_participant_registration 
                                                    ON (custom_participant_registration.reg_id = custom_coupon_master.coupn_reg_id)
                                            WHERE coupn_participant_id = '".$_REQUEST['cupn_lst'][$i]."' AND reg_location = '".LCTN."'";*/
				
				$qry = "SELECT * FROM custom_coupon_master WHERE coupn_participant_id = '".$_REQUEST['cupn_lst'][$i]."' AND coupn_survey_id = '".$_REQUEST['survey_id']."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$rec = $common->sql->record;
				
				$qry = "SELECT * FROM ".$table_name." WHERE reg_coupon_code = '".$_REQUEST['cupn_lst'][$i]."' AND reg_survey_id = '".$_REQUEST['survey_id']."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$chk_rec = $common->sql->record;
				
				if(count($rec) == 0 && count($chk_rec) == 0)
				{
					$_query = sprintf("insert into custom_coupon_master values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
						mysql_escape_string($_REQUEST['survey_id']),
						mysql_escape_string($last_id),
						mysql_escape_string($_REQUEST['cupn']),
						mysql_escape_string($_REQUEST['cupn_lst'][$i]),
						mysql_escape_string($_REQUEST['cupn_dt']),
						mysql_escape_string($_REQUEST['cupn_prd']),
						mysql_escape_string($_REQUEST['prd_type']),
						mysql_escape_string($_REQUEST['cupn_vld_dt']),
						mysql_escape_string(USR)
					);
					$rec = $common->sql->query($_query);
				}
				elseif($last_id == $rec[0]['coupn_reg_id'])
				{
					$upd_qry = "UPDATE custom_coupon_master	SET 	coupn_survey_id 		= '".mysql_escape_string($_REQUEST['survey_id'])."',
																	coupn_issue_date 		= '".mysql_escape_string($_REQUEST['cupn_dt'])."',
																	coupn_valid_period_no	= '".mysql_escape_string($_REQUEST['cupn_prd'])."',
																	coupn_valid_period_type	= '".mysql_escape_string($_REQUEST['prd_type'])."',
																	coupn_valid_period	 	= '".mysql_escape_string($_REQUEST['cupn_vld_dt'])."'
															WHERE 	coupn_reg_id			= '".$last_id."' 
															AND 	coupn_participant_id 	= '".$_REQUEST['cupn_lst'][$i]."'";
					$common->sql->exeQuery($upd_qry);
				}
				else
				{
					$error = true;
					$cupn_ids .= $_REQUEST['cupn_lst'][$i].",";
				}
			}
			
			if(!$error)
			{
				if($_REQUEST['id'] == "")
				{
					$_SESSION['msg'][0] = 'Coupons are Created Successfully';
					$common->UserLog($form_name, "custom_coupon_master", $last_id, 'Insert');
				}
				else
				{
					$upd_qry = "UPDATE custom_coupon_master	SET 	coupn_count 	= '".mysql_escape_string($_REQUEST['cupn'])."'
															WHERE 	coupn_reg_id	= '".$last_id."' 
															AND 	coupn_survey_id = '".$_REQUEST['survey_id']."'";
					$common->sql->exeQuery($upd_qry);
					
					$_SESSION['msg'][0] = 'Coupons are Updated Successfully';
					$common->UserLog($form_name, "custom_coupon_master", $last_id, 'Update');
				}
				
				$qry = "SELECT * FROM custom_survey_master WHERE sur_code = '".$_REQUEST['survey_id']."' AND sur_url != '' AND sur_url != 'http://'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$sur_rec = $common->sql->record[0];
				
				if(count($sur_rec) != 0)
				{
					$_SESSION['msg'][0] .= "<br/> The Survey Link Will Open in 5 Seconds";
					$_SESSION['msg'][] = $sur_rec["sur_url"];
				}
				else
					$_SESSION['msg'] = $_SESSION['msg'][0];
				
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
			}
			else
			{
				if(count($cupn_ids) != 1)
				{
					$cupn_ids = substr($cupn_ids, 0, -1);
					$_SESSION['msg'] = $cupn_ids.' Coupons are Already Exsists';
				}
				else
				{
					$cupn_ids = substr($cupn_ids, 0, -1);
					$_SESSION['msg'] = $cupn_ids.' Coupon Already Exsists';
				}
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page']."#tabs-3";
				$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$reg_id.'#tabs-3';
			}
		}
		
		header("location:".$link);
		exit;
	break;
	
	case "add":
		include($_REQUEST['page']."_add.php");
	break;
	
	case "edit":
		
		
		
		$query = "SELECT * FROM ".$table_name." 
						INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = ".$table_name.".reg_survey_id)
						INNER JOIN custom_location ON (custom_location.loc_code = ".$table_name.".reg_location)
					WHERE reg_participant_id = '".$_REQUEST['id']."' AND reg_survey_id = '".$_REQUEST['sur_id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
        
		$id = $_REQUEST['id'];
		$survey_id = $rec[0]['reg_survey_id'];
		$sur_title = $rec[0]['sur_title'];
		$location = $rec[0]['reg_location'];
		$loc_title = $rec[0]['loc_name'];
		$location_type = $rec[0]['reg_participant_type'];
		$participant_id = $rec[0]['reg_participant_id'];
		$cupn_cde = $rec[0]['reg_coupon_code'];
		$dor = $rec[0]['reg_date_of_recurit'];
		$dor = $common->date_format($dor, DT_FMT, true);
		$ntwrk_sze = $rec[0]['reg_network_size'];
		$dob = $rec[0]['reg_date_of_birth'];
		$dob = $common->date_format($dob, DT_FMT, true);
		$age = $rec[0]['reg_age'];
		$sex = $rec[0]['reg_sex'];
		$biometric_ref = $rec[0]['reg_biometric_reference'];
		
		//added on Dec082017
		$sur_onlne_id = $rec[0]['sur_onlne_id'];
		$surveytable1 = "lime_survey_".$sur_onlne_id;
		$p_column1 = trim($common->getQuestionCode('ParticipantID'));
		$qry1 = "SELECT submitdate FROM hrmsmsurvey.$surveytable1 WHERE $p_column1 = '$participant_id'";
		
		$common->sql->query($qry1, SQL_ALL, SQL_ASSOC);
		$survalues1 = $common->sql->record;
		$survey_comp_dt = $survalues1[0]['submitdate'];
		
		// Vidhya - added on Dec13,2017 - Give message when lab is not done for any participant
		
		$labquery = "SELECT lab_smple_result_id FROM hrmsmcms.custom_lab_samples as s WHERE
						lab_smple_part_id = '".$participant_id."' and s.lab_smple_tst_id in (1,2) ";
		
		$common->sql->query($labquery, SQL_ALL, SQL_ASSOC);
		$recvalues = $common->sql->record;
		
		if(count($recvalues) == 0)
			$lab_done = 'failed';
		else 
		{
			$flag = false;
			for($i=0;$i<count($recvalues);$i++)
			{
				if(trim($recvalues[$i]['lab_smple_result_id']) != '' && ($recvalues[$i]['lab_smple_result_id'] >= 0 && $recvalues[$i]['lab_smple_result_id'] <= 2)){
					$flag = true;					
				}
				else{
					$flag = false;
					break;
				}
			}
			if($flag)		
				$lab_done = 'success';
			else
				$lab_done = 'failed';
		}	
		
		
		
		$query = "SELECT * FROM custom_app_settings WHERE app_survey_id = '".$survey_id."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$app_rec = $common->sql->record[0];
		
		$coupon = $app_rec['app_coupons'];
		$incentive = $app_rec['app_incentive'];
		
		$cupn_showvar = $app_rec['app_cupn_showvar'];
		$cupn_stdarm = $app_rec['app_std_arm'];
		
		if($coupon == "Y")
		{
			$no_of_coupon = $app_rec['app_no_of_coupon'];
			$cupn_prd = $app_rec['app_cupn_prd'];
			$cupn_prd_type = $app_rec['app_cupn_prd_type'];
			
			$no_of_coupon = $app_rec['app_no_of_coupon'];
			$no_of_coupon_fxd = $app_rec['app_no_of_coupon_fixed'];
			$cupn_prd = $app_rec['app_cupn_prd'];
			$cupn_prd_type = $app_rec['app_cupn_prd_type'];
			$cupn_prd_fxd = $app_rec['app_cupn_prd_fixed'];
			
		}
		else
		{
			$no_of_coupon_fxd = "N";
			$cupn_prd_fxd = "N";
		}
		
		$query = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$participant_id."' AND coupn_survey_id = '".$survey_id."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		if(count($rec) != 0)
		{
			$cupn = $rec[0]['coupn_count'];
						
			$cupn_dt = $rec[0]['coupn_issue_date'];
			$cupn_dt = $common->date_format($cupn_dt, DT_FMT, true);
			$cupn_prd = $rec[0]['coupn_valid_period_no'];
			$prd_type = $rec[0]['coupn_valid_period_type'];
			$cupn_vld_dt = $rec[0]['coupn_valid_period'];
			$cupn_vld_dt = $common->date_format($cupn_vld_dt, DT_FMT, true);
		}
		else
		{
			$cupn = $no_of_coupon;
			
			
			$cupn_prd = $cupn_prd;
			$prd_type = $cupn_prd_type;
		}
			
		include("edit.php");
	break;
	
	case "delete":
		if(isset($_REQUEST[$prefix.'id']))
			$id = $_REQUEST[$prefix.'id'];
		else
			$id = implode("', '", $_REQUEST[$primary_key]);
		
		$query = "DELETE FROM ".$table_name." WHERE ".$primary_key." IN ('".$id."')";
		$query = "DELETE FROM ".$table_name." WHERE reg_participant_id IN ('".$id."')";
		$rec = $common->sql->exeQuery($query);
		
		if($rec)
		{
			$query = "DELETE FROM custom_coupon_master WHERE coupn_reg_id IN ('".$id."')";
			$rec = $common->sql->exeQuery($query);
			
			$query = "DELETE FROM custom_locator_information WHERE loctr_reg_id IN ('".$id."')";
			$rec = $common->sql->exeQuery($query);
			
			$_SESSION['msg'] = 'Selected record has been deleted successfully';
			if(isset($_REQUEST['id']))
				$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
			else
				for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
					$common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
		}
		
		$query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	case "status":
		
		$field = "status";
		
		$query = "SELECT ".$field." FROM ".$table_name." WHERE ".$primary_key." = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec1 = $common->sql->record[0][$field];
		
		if($rec1 == 'Y')
			$upd = $field.' = "N",';
		else
			$upd = $field.' = "Y",';
		
		$query = "UPDATE ".$table_name." SET ".$upd." update_time = now(), update_user = '".USR."' WHERE ".$primary_key." = '".$_REQUEST['id']."'";
		$rec = $common->sql->exeQuery($query);
		
		if($rec)
			$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	default:
		
		//added on Dec082017
		$surveytable1 = "lime_survey_".$survey_id;
		$p_column1 = trim($common->getQuestionCode('ParticipantID'));
		$qry1 = "SELECT submitdate FROM hrmsmsurvey.$surveytable1 WHERE '$p_column1' = '$participant_id'";
		$common->sql->query($qry1, SQL_ALL, SQL_ASSOC);
		$survalues1 = $common->sql->record;
		$survey_comp_dt = $survalues1[0]['submitdate'];
		
		if(isset($_REQUEST['id']))
		{
			$query = "Select * FROM ".$table_name." WHERE reg_participant_id = '".$_REQUEST['id']."'";
			$query = "Select * FROM ".$table_name." WHERE ".$primary_key." = '".$_REQUEST['id']."'";
			
			$query = "SELECT * FROM ".$table_name." 
							INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = ".$table_name.".reg_survey_id)
							INNER JOIN custom_location ON (custom_location.loc_code = ".$table_name.".reg_location)
						WHERE ".$primary_key." = '".$_REQUEST['id']."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;

			$id = $_REQUEST['id'];
			$survey_id = $rec[0]['reg_survey_id'];
			$sur_title = $rec[0]['sur_title'];
			$location = $rec[0]['reg_location'];
			$loc_title = $rec[0]['loc_name'];
			$location_type = $rec[0]['reg_participant_type'];
			$participant_id = $rec[0]['reg_participant_id'];
			$cupn_cde = $rec[0]['reg_coupon_code'];
			$dor = $rec[0]['reg_date_of_recurit'];
			$dor = $common->date_format($dor, DT_FMT, true);
			$ntwrk_sze = $rec[0]['reg_network_size'];
			$dob = $rec[0]['reg_date_of_birth'];
			$dob = $common->date_format($dob, DT_FMT, true);
			$age = $rec[0]['reg_age'];
			$sex = $rec[0]['reg_sex'];
			$biometric_ref = $rec[0]['reg_biometric_reference'];
			if($biometric_ref == "")
				$biometric_ref = "";
			
			$query = "SELECT * FROM custom_app_settings WHERE app_survey_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$app_rec = $common->sql->record[0];
			
			$coupon = $app_rec['app_coupons'];
			$incentive = $app_rec['app_incentive'];
			$location_fixed = $app_rec['app_location_fixed'];
			
			$cupn_showvar = $app_rec['app_cupn_showvar'];
			$cupn_stdarm = $app_rec['app_std_arm'];
			
			if($coupon == "Y")
			{
				$no_of_coupon = $app_rec['app_no_of_coupon'];
				$no_of_coupon_fxd = $app_rec['app_no_of_coupon_fixed'];
				$cupn_prd = $app_rec['app_cupn_prd'];
				$cupn_prd_type = $app_rec['app_cupn_prd_type'];
				$cupn_prd_fxd = $app_rec['app_cupn_prd_fixed'];
			}
			else
			{
				$no_of_coupon = 1;
				$no_of_coupon_fxd = "N";
				$cupn_prd_fxd = "N";
			}
			
			$query = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$participant_id."'";
			$query = "SELECT * FROM ".$table_name." 
						INNER JOIN custom_coupon_master ON (custom_coupon_master.coupn_reg_id = ".$table_name.".reg_participant_id)
					WHERE reg_participant_id = '".$_REQUEST['id']."' AND reg_survey_id = '".$survey_id."'";
			
			$qry = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$participant_id."' AND coupn_survey_id = '".$survey_id."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			if(count($rec) != 0)
			{
				$cupn = $rec[0]['coupn_count'];
				$cupn_dt = $rec[0]['coupn_issue_date'];
				$cupn_dt = $common->date_format($cupn_dt, DT_FMT, true);
				$cupn_prd = $rec[0]['coupn_valid_period_no'];
				$prd_type = $rec[0]['coupn_valid_period_type'];
				$cupn_vld_dt = $rec[0]['coupn_valid_period'];
				$cupn_vld_dt = $common->date_format($cupn_vld_dt, DT_FMT, true);
			}
			else
			{
				/*$cupn = 1;
				$prd_type = "";*/
				$cupn = $no_of_coupon; 
									
				$cupn_prd = $cupn_prd;
				$prd_type = $cupn_prd_type;
			}
			echo '<div class="content_bg">
				<div style="width: 95%; margin: 0 auto; padding: 10px;" id="content">';
				include("edit.php");
			echo '</div></div>';
			
			
		}
		else
		{
			if(isset($_REQUEST['pid']))
			{
				$query = "SELECT * FROM custom_msm_introduction 
								INNER JOIN custom_msm_screening 
									ON (custom_msm_screening.mem_scrn_part_id = custom_msm_introduction.mem_intr_part_id AND 
										custom_msm_screening.mem_scrn_survey = custom_msm_introduction.mem_intr_survey)
								INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_msm_screening.mem_scrn_survey)
								INNER JOIN custom_location ON (custom_location.loc_code = custom_msm_introduction.mem_intr_location)
							WHERE mem_scrn_id = '".$_REQUEST['pid']."'";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$rec = $common->sql->record;
				
				$id = $_REQUEST['id'];
				$survey_id = $rec[0]['mem_intr_survey'];
				$sur_title = $rec[0]['sur_title'];
				$location = $rec[0]['mem_intr_location'];
				$loc_title = $rec[0]['loc_name'];
				$participant_type = $rec[0]['mem_intr_part_type'];
				$participant_id = $rec[0]['mem_intr_part_id'];
				$cupn_cde = $rec[0]['mem_intr_part_id'];
				$location_type_val = $rec[0]['mem_intr_part_type'];
				
				/*if($survey_id == "YRGBS1")
				{
					$age = $rec[0]['mem_scrn_q3'];
					$sex = $rec[0]['mem_scrn_q8'];
				}
				else
				{
					$age = $rec[0]['mem_scrn_q4'];
					$sex = $rec[0]['mem_scrn_q9'];
				}*/
				
				$age = $rec[0]['mem_scrn_q3'];
				$dob = $rec[0]['mem_scrn_dob'];
				$dob = $common->date_format($dob, DT_FMT, true);
				$sex = $rec[0]['mem_scrn_q8'];
			}
			else
				$location = LCTN;
			
			echo '<div class="content_bg">';
					include("add.php");
			echo '</div>';
		}
	break;
}
?>