<?php 
$form_name = "MSM Introduction";
$table_name = "custom_msm_introduction";
$primary_key = "mem_intr_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);
		$error = false;
		$chk_flag = false;
		
		if($_REQUEST['tab_id'] == 1)
		{
			if($_REQUEST['participant_type'] == "W")
			{
				$qry = "SELECT * FROM custom_coupon_master WHERE coupn_participant_id = '".$_REQUEST['participant_id']."' AND coupn_survey_id = '".$_REQUEST['survey_id']."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$chk_rec = $common->sql->record;
				$chk_rec_cnt = count($chk_rec);
				
				if($chk_rec_cnt == 1)
				{
					$part_id = $chk_rec[0]['coupn_reg_id'];
					
					$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$_REQUEST['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'];
					$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'];
					
					//if($db_sur_id == "23634")
					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 = "";
					
					$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 ".$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) == 1)
						$chk_flag = true;
				}
				else
				{
					$error = true;
					$_SESSION['msg'] = strtoupper($_REQUEST['participant_id']).' Coupon ID Not Exsists';
				}
			}
			else
				$chk_flag = true;
			
			if($chk_flag)
			{
				if($_REQUEST['id'] != "")
					$whr = " AND mem_intr_id != '".$_REQUEST['id']."'";
				else
					$whr = "";
				
				$qry = "SELECT * FROM ".$table_name." WHERE mem_intr_survey = '".$_REQUEST['survey_id']."' AND mem_intr_part_id = '".strtoupper($_REQUEST['participant_id'])."'".$whr;
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$rec = $common->sql->record;
				
				if(count($rec) == 0)
				{
					if($_REQUEST['id'] == "")
					{
						$_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', '%s', '%s', 'Y', 'N', now(), '%s', 0, 0)",
							mysql_escape_string($_REQUEST['survey_id']),
							mysql_escape_string($_REQUEST['doi']),
							mysql_escape_string(strtoupper($_REQUEST['interview_initial'])),
							mysql_escape_string($_REQUEST['location']),
							mysql_escape_string($_REQUEST['participant_type']),
							mysql_escape_string(strtoupper($_REQUEST['participant_id'])),
							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'];
						$upd_qry = "UPDATE ".$table_name." 	SET 	mem_intr_survey 	= '".mysql_escape_string($_REQUEST['survey_id'])."',
																	mem_intr_initial 	= '".mysql_escape_string(strtoupper($_REQUEST['interview_initial']))."',
																	mem_intr_location 	= '".mysql_escape_string($_REQUEST['location'])."',
																	mem_intr_part_type 	= '".mysql_escape_string($_REQUEST['participant_type'])."',
																	mem_intr_part_id 	= '".mysql_escape_string(strtoupper($_REQUEST['participant_id']))."',
																	update_time     	= now(),
																	update_user     	= '".USR."'
															WHERE 	mem_intr_id			= '".$last_id."'";
						$common->sql->exeQuery($upd_qry);
						//$_SESSION['msg'] = 'Updated Successfully';
						$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
					}
				}
				else
				{
					$error = true;
					$_SESSION['msg'] = strtoupper($_REQUEST['participant_id']).' Participant ID Already Exsists';
				}
			}
			elseif(!$error)
			{
				$error = true;
				$_SESSION['msg'] = 'The Seed ('.strtoupper($part_id).') Needs to Complete the Survey';
			}
		}
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		
		if($error)
			$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		else
		{
			if($_REQUEST['survey_id'] == 'YRGBS1')
				$link = $_SERVER['SCRIPT_NAME'].'?page=msm_screening&id='.$last_id;
			else
				$link = $_SERVER['SCRIPT_NAME'].'?page=idu_screening&id='.$last_id;
		}
		
		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.".mem_intr_survey)
					WHERE mem_intr_id = '".$_REQUEST['id']."'";
		
		$query = "SELECT * FROM ".$table_name." WHERE mem_intr_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
        
		$id = $_REQUEST['id'];
		$doi = $common->date_format($rec[0]['mem_intr_doi'], DT_FMT);
		$survey_title = $rec[0]['sur_title'];
		$survey_id = $rec[0]['mem_intr_survey'];
		$mem_intr_doi = $rec[0]['mem_intr_doi'];
		$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:
		$location = LCTN;
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>