<?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/program.mdl.php");
$SEAS_Program_Model = new program_data();


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


	$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];
		}
	}
	

	/*
	if($_REQUEST['sel_slots'] == 1) { //15 min
		$sel_sttime = str_replace(":","",substr($tsarray[0]['tseg_desc'],10,5));
		$sel_edtime = str_replace(":","",substr($tsarray[count($tsarray)-1]['tseg_desc'],18,5));
	}
	elseif($_REQUEST['sel_slots'] == 2) { //Daypart
		$sel_sttime = substr($tsarray[0]['tseg_desc'],0,4);
		$sel_edtime = substr($tsarray[count($tsarray)-1]['tseg_desc'],7,4);
	}
	
	$sel_sttime =$SEAS_Program_Model->localtogmt($survey_detail['srv_mrkt'],$sel_sttime);	
	$sel_edtime =$SEAS_Program_Model->localtogmt($survey_detail['srv_mrkt'],$sel_edtime);	
	*/
	
	$weekdays = array(0=>"Sunday",1=>"Monday",2=>"Tuesday",3=>"Wednesday",4=>"Thursday",5=>"Friday",6=>"Saturday");
	
	$SEAS_Survey_Calc_Model->filter_resp($survey_detail,$_SESSION['seas_target_id']);


	$program_data = $SEAS_Program_Model->fetchdata($survey_detail['srv_mrkt'],$survey_detail['type'],$_SESSION['sel_chnls_list'],$survey_detail['rw_srv_stdt'],$survey_detail['rw_srv_eddt'],"0000","3000",$SEAS_Survey_Calc_Model->filter_resp_sql,8,1,0,"",1,"Avg_Rat");
	$program_data = $SEAS_Program_Model->Average_PeriodWise($program_data,$survey_detail,$SEAS_Survey_Calc_Model->filter_resp_sql);
	
	$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'],1,array(),1,$_SESSION['msort']);
}
?>