<?php
require_once($application_path . "view/header.tpl.php");
?>
<style>
	th {
		border:solid #999 1px;
		padding:2px;
		background-color:#ccc;
		text-align:center;
		vertical-align:top;
		font-size:11px;
	}
	td {
		border:solid 1px;
		padding:5px;
		font-size:11px;
	}		
	.report_area {
		width: 100%;
		max-height: 750px;
		overflow: auto;
	}
	.trcls:hover { 
		background-color: #CCC;
	}	
	.trow {
		height:15px;
		font-size:11px;
		padding:2px;
	}
	
</style>
        <!-- page content -->
        <div class="right_col" role="main">
		  
          <!-- Body First Row Items -->
          <div class="row">
             <div class="col-md-12 col-sm-12 col-xs-12">
            	
                <!-- Page Panel -->
                            <div class="row x_title">
                              <div class="col-md-6">
                                <h3>Survey: <?php echo $survey_detail['name']; ?></h3><small>(Market: <?php echo $survey_detail['market']; ?>, Population: <?php echo number_format($survey_detail['population'],0,"",","); ?>, Sample: <?php echo $survey_detail['sample']; ?>, Period: <?php echo $survey_detail['starting_date']; ?> to <?php echo $survey_detail['ending_date']; ?>)</small>
                                <h2>Program Analysis - Average Week wise</h2>
                              </div>
                              <div class="col-md-6">
                                <!-- Survey menu -->
                                <div id="survey_menu" class="pull-right" style="cursor: pointer; padding: 5px 10px; border: 0px solid #ccc">
                                  <form action="program_analysis_wk" method="post">
                                  <select class="" name="selected_survey" onchange="this.form.submit()">
                                    <option>Select Survey</option>
                                    <?php
                                    foreach($survey_list as $sl) {
                                        echo "<option value=".$sl['srv_id'];
										if($_SESSION['seas_srv_id'] == $sl['srv_id']) {
											echo " selected ";
										}
										echo ">". $sl['srv_desc'] ."</option>";
                                    }
                                    ?>
                                  </select>
                                  <select class="" name="selected_target" onchange="this.form.submit()">
                                    <option value="0">Total Population</option>
                                    <?php
                                    foreach($list_targets as $tl) {
                                        echo "<option value=".$tl['trg_id'];
										if($_SESSION['seas_target_id'] == $tl['trg_id']) {
											echo " selected ";
										}
										echo ">". $tl['trg_name'] ."</option>";
                                    }									
									?>
                                  </select>
                                  
                                  </form>
                                </div>
                                <!-- End Survey menu -->
                              </div>
                            </div>

							<?php
							if($report_view==0) {
							?>
                                 <form action="" enctype="multipart/form-data" method="post" onsubmit="return valform();">
                                    <button type="submit" name="btn_gen" class="btn btn-success">Generate Analysis</button>
                                    <table width="100%">
                                        <thead>
                                            <th style="width:193px"><a href="program_analysis_wk?sort=chname">Channel Name</a></th>
                                            <th style="width:195px"><a href="program_analysis_wk?sort=network">Network</a></th>
                                            <th style="width:270px"><a href="program_analysis_wk?sort=typology">Typology</a></th>
                                            <th align="right" style="width:78px"><a href="program_analysis_wk?sort=grp">GRP</a></th>
                                            <th align="right" style="width:94px"><a href="program_analysis_wk?sort=reach">Reach</a></th>
                                            <th>Slots: 
                                            <select name="sel_slots" id="sel_slots" style="width: 150px; height:20px; font-size:12px" onChange="this.form.submit()">
                                                <option value="1" <?php if($_SESSION['mslots']==1) echo "selected"; ?>>15 Mins Slots</option>
                                                <option value="2" <?php if($_SESSION['mslots']==2) echo "selected"; ?>>Day Parts</option>
                                            </select>
                                            </th>
                                        </thead>
                                        <tr>
                                            <td colspan="5">
                                                <select name="chnl_list[]" id="chnl_list" size="43" multiple  style="font-family: Courier New; font-size:11px"  >
                                                <?php
                                                foreach($channel_list as $ky=>$lst) {
                                                    if($lst['reach']>0) {
                                                ?>
                                                     <option value="<?php echo $lst['chid']. "_" .$lst['chname'] ; ?>"
                                                     
                                                     <?php
													 if(in_array($lst['chid'],$_SESSION['sel_chnls_list'])) {
														echo " selected " ;
													 }
                                                     ?>
                                                     
                                                     ><?php 
                                                     
                                                     $lst['chname'] = str_replace("  "," ",$lst['chname']);
                                                     $lst['network'] = str_replace("  "," ",$lst['network']);
                                                     $lst['typology'] = str_replace("  "," ",$lst['typology']);
                                                 
                                                     $ln = substr(trim($lst['chname']),0,26) .  str_repeat("&nbsp;",26-strlen(substr(trim($lst['chname']),0,26)));
                                                     $ln .= " &boxv;  ". substr(trim($lst['network']),0,27) .  str_repeat("&nbsp;",27-strlen(substr(trim($lst['network']),0,27)));
                                                     $ln .= " &boxv; ". substr(trim($lst['typology']),0,38) .  str_repeat("&nbsp;",38-strlen(substr(trim($lst['typology']),0,38)));
                                                     $ln .= " &boxv; ". str_repeat("&nbsp;",9-strlen(trim(sprintf("%01.2f", $lst['grp'])))) . trim(sprintf("%01.2f", $lst['grp']));
                                                     $ln .= " &boxv; ". str_repeat("&nbsp;",9-strlen(trim(sprintf("%01.2f", $lst['reach'])))) . trim(sprintf("%01.2f", $lst['reach']));
                                                      
                                                     echo $ln;
                                                     ?></option>
                                                
                
                                                <?php	
                                                    }
                                                }
                                                ?>
                                                </select>
                                          </td>
                                          <td valign="top">
                                            <select name="time_slots[]" id="time_slots"  size="43" multiple style="width: 100%; height:100%; font-family: Courier New; font-size:11px"  >
                                                <?php
                                                foreach($time_segment_list as $tseg) {
                                                
                                                    echo "<option value='" . $tseg[0] . "_" . $tseg[1] . "_" . $tseg[2] . "' ";
													 
													 if(in_array($tseg[0] . "_" . $tseg[1],$_SESSION['sel_time_list'])) {
														echo " selected " ;
													 }
													
													echo ">" . $tseg[2] . "</option>";
                                                }
                                                ?>
                                            </select>
                                          </td>                                  
                                        </tr>
                                    </table>
                            	 </form>
                            <?php
							}
							else {
							?>
                            <button type="button" name="btn_gen" class="btn btn-success" onclick="goback()">Go Back</button>
                            <div class="report_area">
							<table width="100%" class="responsive">
                                <thead>
                                    <th>Channel</th>
									<th>Program Name</th>
                                    <th>No of Episodes</th>
                                    <th>Day of Week</th>
                                    <th>Starting Time</th>
                                    <th>Ending Time</th>
                                    <th>Duration</th>
                                    <th>Avg. Rating %</th>
                                    <th>Reach %</th>
                                    <th>Share %</th>
                                    <th>MIN Avg. Rating %</th>
                                    <th>MIN Reach %</th>
                                    <th>MIN Share %</th>
                                    <th>MAX Avg. Rating %</th>
                                    <th>MAX Reach %</th>
                                    <th>MAX Share %</th>
                                    <th>Total TV %</th>
                                    <th>ATS %</th>
                                    <th>ATS</th>
                                    <th>GRP</th>
                                    <th>Share of GRP on Channel</th>
                                    <th>Share of GRP on Total TV</th>
                                    <th>Typology</th>
                                </thead>
									<?php
                                    foreach($program_data as $pdb) {
									?>
										<tr class="trcls">
                                            <td class="trow" nowrap><?php echo $pdb['chname']; ?></td>
                                            <td class="trow" nowrap><?php echo ucwords(strtolower($pdb['progname'])); ?></td>
                                            <td class="trow" nowrap style="text-align:center"><?php echo $pdb['episode']; ?></td>
                                            <td class="trow" nowrap style="text-align:center"><?php echo $weekdays[$pdb['pday']]; ?></td>
                                            <td class="trow" nowrap style="text-align:center"><?php echo $pdb['StTime']; ?></td>
                                            <td class="trow" nowrap style="text-align:center"><?php echo $pdb['EndTime']; ?></td>
                                            <td class="trow" nowrap style="text-align:center"><?php echo $pdb['mDur']; ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Avg_Rat']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Reach']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Share']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Min_Avg_Rat']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Min_Reach']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Min_Share']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Max_Avg_Rat']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Max_Reach']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Max_Share']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Total_Tv']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Ats_Perc']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo $pdb['Ats'] ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Grp']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Share_Ch']); ?></td>
                                            <td class="trow" nowrap style="text-align:right"><?php echo sprintf("%0.2f",$pdb['Share_T']); ?></td>
                                            <td class="trow" nowrap><?php echo $pdb['typology']; ?></td>
										</tr>									
                                    <?php
                                    }
                                    ?>
                            <?php
							}
							?>
                            </table>
                            </div>            


				<!-- End Page Panel -->
             </div>
          </div>
          <!-- End Body First -->
        </div>  
        <!-- end Page content -->  

<?php
require_once($application_path . "view/footer.tpl.php");
?>


<script>
	function valform() {
		var sel_chnls = getSelectValues(document.getElementById("chnl_list"));
		if(sel_chnls == "") {
			selectAllOptions(document.getElementById("chnl_list"));
		}
		var sel_slots = getSelectValues(document.getElementById("time_slots"));
		if(sel_slots == "") {
			selectAllOptions(document.getElementById("time_slots"));
		}
		return true;
	}


	function getSelectValues(select) {
	  var result = [];
	  var options = select && select.options;
	  var opt;
	
	  for (var i=0, iLen=options.length; i<iLen; i++) {
		opt = options[i];
	
		if (opt.selected) {
		  result.push(opt.value || opt.text);
		}
	  }
	  return result;
	}	
	
	function selectAllOptions(obj) {
		for (var i=0; i<obj.options.length; i++) {
			obj.options[i].selected = true;
		}
	}


	function goback() {
		window.location = "program_analysis_wk";	
	}
</script>
