<?php 
$form_name = "MSM Screening";
$table_name = "custom_msm_screening";
$primary_key = "mem_scrn_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['doi'] = $common->date_format($_REQUEST['doi'], DT_FMT);
		$link = '';
		
		if($_REQUEST['tab_id'] == 1)
		{
			$query = "SELECT * FROM custom_msm_screening WHERE 	mem_scrn_part_id 	= '".$_REQUEST['partcpnt_id']."' AND 
																mem_scrn_survey 	= '".$_REQUEST['sur_id']."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			$cnt = count($rec);
			
			// if($_REQUEST['id'] == "")
			if(count($rec) == 0)
			{
				$_query = sprintf("insert into ".$table_name."
				(mem_scrn_id, mem_scrn_survey, mem_scrn_part_id, mem_scrn_q1, mem_scrn_q1a, mem_scrn_q2, mem_scrn_q3, mem_scrn_q4, mem_scrn_q5, mem_scrn_q6, mem_scrn_q7, mem_scrn_q8, mem_scrn_q9, mem_scrn_q10, mem_scrn_q11, status, create_time, create_user, update_time, update_user)
				values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($_REQUEST['sur_id']),
					mysql_escape_string($_REQUEST['partcpnt_id']),
					mysql_escape_string($_REQUEST['q1']),
					mysql_escape_string($_REQUEST['q1a']),
					mysql_escape_string($_REQUEST['q2']),
					mysql_escape_string($_REQUEST['q3']),
					mysql_escape_string($_REQUEST['q4']),
					mysql_escape_string($_REQUEST['q5']),
					mysql_escape_string($_REQUEST['q6']),
					mysql_escape_string($_REQUEST['q7']),
					mysql_escape_string($_REQUEST['q8']),
					mysql_escape_string($_REQUEST['q9']),
					mysql_escape_string($_REQUEST['q10']),
					mysql_escape_string($_REQUEST['q11']),
					mysql_escape_string(USR)
				);
				$rec = $common->sql->query($_query);
				$last_id = mysql_insert_id();
				$_SESSION['msg'] = 'Saved Successfully';
				$common->UserLog($form_name, $table_name, $last_id, 'Insert');
			}
			else
			{
				$last_id = $_REQUEST['id'];
				$part_id = $_REQUEST['partcpnt_id'];
				$upd_qry = "UPDATE ".$table_name." 	SET 	mem_scrn_q1 		= '".mysql_escape_string($_REQUEST['q1'])."',
															mem_scrn_q1a 		= '".mysql_escape_string($_REQUEST['q1a'])."',
															mem_scrn_q2 		= '".mysql_escape_string($_REQUEST['q2'])."',
															mem_scrn_q3 		= '".mysql_escape_string($_REQUEST['q3'])."',
															mem_scrn_q4 		= '".mysql_escape_string($_REQUEST['q4'])."',
															mem_scrn_q5 		= '".mysql_escape_string($_REQUEST['q5'])."',
															mem_scrn_q6 		= '".mysql_escape_string($_REQUEST['q6'])."',
															mem_scrn_q7 		= '".mysql_escape_string($_REQUEST['q7'])."',
															mem_scrn_q8 		= '".mysql_escape_string($_REQUEST['q8'])."',
															mem_scrn_q9 		= '".mysql_escape_string($_REQUEST['q9'])."',
															mem_scrn_q10		= '".mysql_escape_string($_REQUEST['q10'])."',
															mem_scrn_q11		= '".mysql_escape_string($_REQUEST['q11'])."',
															update_time 		= now(),
															update_user 		= '".USR."'
													WHERE 	mem_scrn_part_id	= '".$part_id."' AND 
															mem_scrn_survey		= '".$_REQUEST['sur_id']."'";
				$common->sql->exeQuery($upd_qry);
				$_SESSION['msg'] = 'Updated Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
				
				$qry = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$part_id."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$chk_rec = $common->sql->record;
				
				if(count($chk_rec) == 0)
					$link = $_SERVER['SCRIPT_NAME'].'?page=recuirtment&pid='.$last_id;
			}
			
			if($_REQUEST['q11'] == 1 && $cnt == 0)
			{
				$loc = substr($_REQUEST['partcpnt_id'], 0, 2);
				$ids = substr($_REQUEST['partcpnt_id'], 2);
				
				$_query = sprintf("insert into prticipant_id_table values(0, '%s', '%s', '%s', '', now())",
					mysql_escape_string($ids),
					mysql_escape_string($loc),
					mysql_escape_string($last_id)
				);
				$rec = $common->sql->query($_query);
				$link = $_SERVER['SCRIPT_NAME'].'?page=recuirtment&pid='.$last_id;
			}
		}
		
		if($link == '')
		{
			$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
			$link = $_SERVER['SCRIPT_NAME'].'?page=msm_introduction';
		}
		
		header("location:".$link);
		exit;
	break;
	
	case "add":
		include($_REQUEST['page']."_add.php");
	break;
	
	case "edit":
		$query = "SELECT * FROM custom_msm_introduction WHERE mem_intr_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
        
		$id = $_REQUEST['id'];
		$id = $rec[0]['mem_intr_part_id'];
		/*$doi = $common->date_format($rec[0]['mem_intr_doi'], DT_FMT);
		$interview_initial = $rec[0]['mem_intr_initial'];
		$location = $rec[0]['mem_intr_location'];
		$participant_type = $rec[0]['mem_intr_part_type'];
		$participant_id = $rec[0]['mem_intr_part_id'];*/
			
		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 mem_intr_id IN ('".$id."')";
		$rec = $common->sql->exeQuery($query);
		
		if($rec)
		{
			$_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:
		$query = "SELECT * FROM custom_msm_introduction WHERE mem_intr_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
        
		$id = $_REQUEST['id'];
		$part_id = $rec[0]['mem_intr_part_id'];
		$loc_id = $rec[0]['mem_intr_location'];
		$sur_id = $rec[0]['mem_intr_survey'];
		
		$query = "SELECT * FROM ".$table_name." WHERE mem_scrn_part_id = '".$part_id."' AND mem_scrn_survey = '".$sur_id."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		if(count($rec) != 0)
		{
			$q1 = $rec[0]['mem_scrn_q1'];
			$q1a = $rec[0]['mem_scrn_q1a'];
			$q2 = $rec[0]['mem_scrn_q2'];
			$q3 = $rec[0]['mem_scrn_q3'];
			$q4 = $rec[0]['mem_scrn_q4'];
			$q5 = $rec[0]['mem_scrn_q5'];
			$q6 = $rec[0]['mem_scrn_q6'];
			$q7 = $rec[0]['mem_scrn_q7'];
			$q8 = $rec[0]['mem_scrn_q8'];
			$q9_ans = $rec[0]['mem_scrn_q9'];
			$q10 = $rec[0]['mem_scrn_q10'];
			$q11 = $rec[0]['mem_scrn_q11'];
		}
		
		echo '<div class="content_bg">';
				include("add.php");
		echo '</div>';
	break;
}
?>