<?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'];
}

// Analysis By
if(!isset($_SESSION['analysis_by'])) {
	$_SESSION['analysis_by'] = 1;
}
if(isset($_REQUEST['analysis_by'])) {
	$_SESSION['analysis_by'] = $_REQUEST['analysis_by'];
}

// 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();
}
if(!isset($_SESSION['sel_banner'])) {
	$_SESSION['sel_banner'] = array();
}

// Socio Composition
require_once($application_path . "model/survey_socio.mdl.php");
$SEAS_Survey_Socio_Model = new seas_socio_Model();

$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]['chid'] = 0;
	$chnls_list[0]['chname'] = "Total TV";	
	
	if(isset($_REQUEST['chnl_list'])) {
		$cid=1;
		foreach($_REQUEST['chnl_list'] as $chl) {
			$chl_ary = explode("_",$chl);
			$chnls_list[$cid]['chid'] = $chl_ary[0];
			$chnls_list[$cid]['chname'] = $chl_ary[1];
			$cid++;
			
			$_SESSION['sel_chnls_list'][] = $chl_ary[0];
		}		
	}

	if(isset($_REQUEST['banner_sel'])) {
		$_SESSION['sel_banner'] = array();
		foreach($_REQUEST['banner_sel'] as $bsl) {
			$bsl_data = explode(";",$bsl); 
			$bsl_id = $bsl_data[0];
			$socio_compo[$bsl_id] = $SEAS_Survey_Socio_Model->socio_composition($survey_detail,$bsl_id);
			$_SESSION['sel_banner'][] = $bsl_id;
		}
	}


	$analysis_array = array();
	
		$tid=0;
		if(isset($_REQUEST['time_slots'])) {
			foreach($_REQUEST['time_slots'] as $tsl) {
				
				$tsl_data = explode(";",$tsl); 
				$tsl_ary = explode("_",$tsl_data[0]);
				$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];

				$adata_array = $SEAS_Survey_Calc_Model->extract_socio_grp_reach($survey_detail,0,0,$chnls_list,$tsarray);
				$analysis_array[$tsl_data[1]]["totaltv"] = $adata_array;  // For individual socio factors
				
			
				$analysis_array[$tsl_data[1]][0] = array();  // For all factors
				foreach($_SESSION['sel_banner'] as $selbaner) {			
					foreach($socio_compo[$selbaner] as $seky=>$sbnr) {
						foreach($sbnr['options'] as $oky=>$opts) {
							$fact_id = $sbnr['factordes'] .  "_"  . $selbaner;
							$opt_id  = $opts['opt_desc']  .  "_"  . $oky;
							$adata_array = $SEAS_Survey_Calc_Model->extract_socio_grp_reach($survey_detail,$selbaner,$opts['opt_id'],$chnls_list,$tsarray);
							$analysis_array[$tsl_data[1]][$fact_id][$opt_id] = $adata_array;  // For individual socio factors
						}
					}
				}
			}
		}
		

	$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_segment2($_SESSION['mslots']);
	$channel_list = $SEAS_Survey_Calc_Model->extract_grp_reach($survey_detail,$_SESSION['seas_target_id'],1,array(),1,$_SESSION['msort']);
}
?>