<?php 
$form_name = "Location Information";
$table_name = "custom_locator_information";
$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":
		/*$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;
		$last_id = $rec[0]['reg_id'];*/
		
		if(isset($_REQUEST['id']) && $_REQUEST['id'] != '')
		{
			$qry = "SELECT * FROM custom_locator_information WHERE loctr_reg_id = '".$_REQUEST['partcpnt_id']."' AND loctr_survey_id = '".$_REQUEST['survey_id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			$cnt = count($rec);
		}
		else
			$cnt = 0;
		
		if($cnt == 0)
		{
			$_query = sprintf("insert into custom_locator_information values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
				mysql_escape_string($_REQUEST['survey_id']),
				mysql_escape_string($_REQUEST['partcpnt_id']),
				mysql_escape_string($_REQUEST['addr_mde']),
				mysql_escape_string($_REQUEST['name']),
				mysql_escape_string($_REQUEST['addrs']),
				mysql_escape_string(strtoupper($_REQUEST['city'])),
				mysql_escape_string($_REQUEST['pin_code']),
				mysql_escape_string($_REQUEST['mble']),
				mysql_escape_string($_REQUEST['phne']),
				mysql_escape_string(USR)
			);
			$rec = $common->sql->query($_query);
			$last_id = mysql_insert_id();
			$common->UserLog($form_name, $table_name, $_REQUEST['partcpnt_id'], 'Insert');
		}
		else
		{
			$_query = "UPDATE custom_locator_information    SET     loctr_name		= '".$_REQUEST['name']."',
																	loctr_address	= '".$_REQUEST['addrs']."',
																	loctr_city		= '".strtoupper($_REQUEST['city'])."',
																	loctr_pincode	= '".$_REQUEST['pin_code']."',
																	loctr_mobile	= '".$_REQUEST['mble']."',
																	loctr_contno	= '".$_REQUEST['phne']."',
																	update_time		= now(),
																	update_user		= '".USR."'
															WHERE   loctr_id		= '".$_REQUEST['id']."'";
			$rec = $common->sql->exeQuery($_query);
			$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
		}
		$_SESSION['msg'] = 'Locator Information has been updated Successfully';
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
		exit;
	break;
	
	case "edit":
        /*$qry = "SELECT reg_participant_id FROM custom_participant_registration WHERE reg_id = '".$_REQUEST['id']."'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		$part_id = $rec[0]['reg_participant_id'];*/
                
		$query = "Select * FROM custom_locator_information WHERE loctr_reg_id = '".$_REQUEST['id']."'";
		$query = "Select * FROM custom_locator_information WHERE loctr_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
                
		$id = $_REQUEST['id'];
		$survey_id = $rec[0]['loctr_survey_id'];
		$part_id = $rec[0]['loctr_reg_id'];
		$addr_mde = $rec[0]['loctr_mode'];
		$name = $rec[0]['loctr_name'];
		$addrs = $rec[0]['loctr_address'];
		$city = $rec[0]['loctr_city'];
		$pin_code = $rec[0]['loctr_pincode'];
		$mble = $rec[0]['loctr_mobile'];
		$phne = $rec[0]['loctr_contno'];
		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 loctr_reg_id IN ('".$id."')";
		$query = "DELETE FROM ".$table_name." WHERE loctr_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;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>