<?php
// Survey
require_once($application_path . "model/survey.mdl.php");
$SEAS_Survey_Model = new survey_Model();
$survey_list = $SEAS_Survey_Model->list_survey($_SESSION['seas_user_id']);

if($_SESSION['seas_srv_id'] == 0) {
	$_SESSION['seas_srv_id'] = $survey_list[0]['srv_id'];
	$_SESSION['seas_target_id']=0;
}

if(isset($_REQUEST['selected_survey'])) {
	if($_REQUEST['selected_survey']>0) {
		$_SESSION['seas_srv_id'] = $_REQUEST['selected_survey'];
		$_SESSION['seas_target_id']=0;
	}
	else {
		$_SESSION['seas_srv_id'] = $survey_list[0]['srv_id'];
		$_SESSION['seas_target_id']=0;
	}
}

$survey_detail =  $SEAS_Survey_Model->get_survey_details($_SESSION['seas_srv_id']);

// Target
require_once($application_path . "model/target.mdl.php");
$SEAS_Target_Model = new target_Model();
$list_targets = $SEAS_Target_Model->list_target($_SESSION['seas_user_id'],$survey_detail['srv_mrkt']);

if(!isset($_SESSION['seas_target_id'])) {
	$_SESSION['seas_target_id'] =0;
}
if(isset($_REQUEST['selected_target'])) {
	$_SESSION['seas_target_id'] = $_REQUEST['selected_target'];
}


// Calculations
require_once($application_path . "model/survey_calc.mdl.php");
$SEAS_Survey_Calc_Model = new survey_calc_Model();

if(!isset($_SESSION['sel_chnls_list'])) {
	$_SESSION['sel_chnls_list'] = array();
}
if(!isset($_SESSION['sel_time_list'])) {
	$_SESSION['sel_time_list'] = array();
}


$report_view = 0;

if(isset($_REQUEST['btn_gen'])) {
	$chnls_list = array();
	$tsarray  = array();
	$_SESSION['sel_chnls_list'] = array();
	$_SESSION['sel_time_list'] = array();
	
	$chnls_list[0]['grpid'] = 0;
	$chnls_list[0]['netdesc'] = "Total Network";	
	
	if(isset($_REQUEST['chnl_list'])) {
		$cid=1;
		foreach($_REQUEST['chnl_list'] as $chl) {
			$chl_ary = explode("_",$chl);
			$chnls_list[$cid]['grpid'] = $chl_ary[0];
			$chnls_list[$cid]['netdesc'] = $chl_ary[1];
			$cid++;
			
			$_SESSION['sel_chnls_list'][] = $chl_ary[0];
		}		
	}
	


	$tid=0;
	if(isset($_REQUEST['time_slots'])) {
		foreach($_REQUEST['time_slots'] as $tsl) {
			$tsl_ary = explode("_",$tsl);
			$tsarray[$tid]['id']    = $tsl_ary[0] . "_" . $tsl_ary[1];
			$tsarray[$tid]['tseg1'] = $tsl_ary[0];
			$tsarray[$tid]['tseg2'] = $tsl_ary[1];
			$tsarray[$tid]['tseg_desc'] = $tsl_ary[2];
			
			$tid++;
			
			
			$_SESSION['sel_time_list'][] = $tsl_ary[0] . "_" . $tsl_ary[1];
		}
	}

	$tsarray[$tid]['id']    = "0_96";
	$tsarray[$tid]['tseg1'] = 0;
	$tsarray[$tid]['tseg2'] = 96;
	$tsarray[$tid]['tseg_desc'] = "Total";
	
	
		
	$analysis_array = $SEAS_Survey_Calc_Model->extract_ts_grp_reach($survey_detail,$_SESSION['seas_target_id'],$chnls_list,$tsarray,3);
	
	$report_view = 1;
}
else {
	if(!isset($_SESSION['msort'])) {
		$_SESSION['msort'] = "grp";	
	}
	if(isset($_REQUEST['sort'])) {
		$_SESSION['msort'] = $_REQUEST['sort'];
	}
	if(!isset($_SESSION['mslots'])) {
		$_SESSION['mslots'] = 1;	
	}
	if(isset($_REQUEST['sel_slots'])) {
		$_SESSION['mslots'] = $_REQUEST['sel_slots'];
	}
	
	$time_segment_list = $SEAS_Survey_Model->list_time_segment($_SESSION['mslots']);
	$channel_list = $SEAS_Survey_Calc_Model->extract_grp_reach($survey_detail,$_SESSION['seas_target_id'],3,array(),1,$_SESSION['msort']);
	

}
?>