<?php
require_once($application_path . "view/header.tpl.php");
?>
<style>
	th {
		border:solid #999 1px;
		padding:15px;
		background-color:#ccc;
	}
	td {
		border:solid 1px;
		padding:5px;
	}		
	.report_area {
		width: 100%;
		max-height: 750px;
		overflow: auto;
	}
	.trcls:hover { 
		background-color: #CCC;
	}	
	
</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>Analysis of Media Penetration - Reach</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="media_pene_reach" 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="media_pene_reach?sort=chname">Channel Name</a></th>
                                            <th style="width:195px"><a href="media_pene_reach?sort=network">Network</a></th>
                                            <th style="width:270px"><a href="media_pene_reach?sort=typology">Typology</a></th>
                                            <th align="right" style="width:78px"><a href="media_pene_reach?sort=grp">GRP</a></th>
                                            <th align="right" style="width:94px"><a href="media_pene_reach?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>
                                    <?php
									foreach($tsarray as $tary) {
										echo "<th  align='center'>"	. $tary['tseg_desc'] . "</th>";
									}
									?>
                                </thead>
									<?php
                                    foreach($chnls_list as $chl) {
										if($chl['chname'] != "Total") {
											echo "<tr class='trcls'>";
											echo "<td align='left'>"	. $chl['chname'] . "</td>";
											foreach($tsarray as $tary) {
												echo "<td align='right'>" . sprintf('%0.2f',$analysis_array[$chl['chid']]['tseg'][$tary['id']]['reach']) . "</td>";
											}
											echo "</tr>";
										}
                                    }
                                    ?>
                            <?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 = "media_pene_reach";	
	}
</script>
