<?php
$decimal=1; 
$memberid="";
$PHPSESSID = session_id();
$back_step = $_REQUEST['back_step'];
$domain_id = $_REQUEST['domain_id'];
$type = $_REQUEST['type'];
function getTimestamp($date) {
	if(!empty($date)) {
		$parts = explode("/", $date);
		$timestamp = mktime(12, 0, 0 , intval($parts[1]), intval($parts[0]), intval($parts[2]));
		return $timestamp;
	}
	return date("Y-m-d H:i:s");
}

if(isset($_SESSION["member_id"])){
	$memberid = $_SESSION["member_id"];
	$querymembers = "SELECT * FROM members WHERE member_id='$memberid'";
	$resultmembers = mysql_query ($querymembers);
	$linemembers= mysql_fetch_array($resultmembers);
}
else{
	echo "Please define either a member, an advertisment or a banner";
}
?>
&nbsp;&nbsp;&nbsp;<a class="go_back" href="index.php?module_page=my_stats&stats_step=<?php print $back_step; ?>&domain_id=<?php print $line['domain_id'];?>&selection=<?php print $selection;?>&r1=<?php print $r1;?>&from_date=<?php print date("d/m/Y", strtotime($_REQUEST['from_date']));?>&todate=<?php print date("d/m/Y", strtotime($_REQUEST['to_date']));?>">&lt;&lt; Go back</a> <br/>
<table width="99%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
	<td>
		<?php
// Set the default selection parameters
if(!isset($_REQUEST['r1']) || $_REQUEST['r1']!="FDTD") $r1 = "default";
if(!isset($_REQUEST['selection'])){
	$_REQUEST['selection'] = "ALL";
	$selection = $_REQUEST['selection'];
}
else {
	$selection = $_REQUEST['selection'];
}

if(isset($_REQUEST['r1']) && $_REQUEST['r1']!="") $r1 = $_REQUEST['r1'];
// Get the where clause from selection

if($r1=="FDTD"){
	$fromdate 	= date("Y-m-d 00:00:00", getTimestamp($_REQUEST['from_date']));
	$todate		= date("Y-m-d 23:59:59", getTimestamp($_REQUEST['to_date']));
	$wherequery=" AND added_on >= '$fromdate' AND added_on <= '$todate' ";
}
else{
	$selection = $_REQUEST['selection'];
	if($selection=="CM"){
		$fromdate 	= date("Y-m-d 00:00:00", getTimestamp("01/".date("m/Y")));
		$todate		= date("Y-m-d H:i:s");
		$wherequery=" AND added_on >= '$fromdate' AND added_on <= '$todate' ";
	}
	elseif($selection=="LM"){
		$cm = intval(date("m"));
		$last_month = ($cm > 1) ? ( ($cm > 10) ? ($cm-1): "0".($cm-1) ) : 12;
		$last_month_year = (intval($last_month) == 12) ? (intval(date("Y"))-1):intval(date("Y"));
		$temp_last_month = mktime(12, 0, 0, intval($last_month), 1, intval($last_month_year));
		$last_day_of_last_month = date("t", $temp_last_month);
		
		$fromdate 	= date("Y-m-d 00:00:00", getTimestamp("01/$last_month/$last_month_year"));
		$todate		= date("Y-m-d 00:00:00", getTimestamp("$last_day_of_last_month/$last_month/$last_month_year"));
		$wherequery=" AND added_on >= '$fromdate' AND added_on <= '$todate' ";
	}
	elseif($selection=="Y"){
		$today = intval(date('d'));
		if($today == 1) {
			$cm = intval(date("m"));
			$last_month = ($cm > 1) ? ( ($cm > 10) ? ($cm-1): "0".($cm-1) ) : 12;
			$last_month_year = (intval($last_month) == 12) ? (intval(date("Y"))-1):intval(date("Y"));
			$temp_last_month = mktime(12, 0, 0, intval($last_month), 1, intval($last_month_year));
			$last_day_of_last_month = date("t", $temp_last_month);
		}
		else {
			$last_month = date("m");
			$last_month_year = date("Y");
			$last_day_of_last_month = $today - 1;
			$last_day_of_last_month = ($last_day_of_last_month >= 10) ? $last_day_of_last_month : "0".$last_day_of_last_month;
		}
		
		$fromdate 	= date("Y-m-d 00:00:00", getTimestamp("$last_day_of_last_month/$last_month/$last_month_year"));
		$todate		= date("Y-m-d 00:00:00");
		$wherequery=" AND added_on >= '$fromdate' AND added_on <= '$todate' ";
	}
	else{
		$fromdate 	= date("Y-m-d");
		$todate 	= date("Y-m-d");
		$wherequery = "";
	}
}



$today=date("Y-m-d H:i:s");
$wherequery .=" AND added_on < '$today' AND added_on > '2005-10-16 00:00:00'";
?> 
<br />
	<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="stats_period" align="center">
		<tr>
			<th width="50%" align="left" scope="row" valign="top">
				<strong class="tdheadername"><?=$linemembers["companyname"]; ?></strong>
				<br/>
				<span class="small">
					<b>
					<?php 
						$linedomain = mysql_fetch_array(mysql_query("SELECT domainname FROM domaincategories WHERE catdom_id='$domain_id'"));
						echo $linedomain["domainname"];
					?>
					</b> Statistics 
				</span>
			</th>
			<td width="50%" rowspan="2" align="right">
				<form action="" method="POST" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
				<table border="0" cellspacing="0" cellpadding="2">
					<tr>
						<td class="left">
							<input name="r1" type="radio" value="default" <?php print ($r1 == "default")? ' checked="checked" ':''; ?>>
						</td>
						<td align="left" class="left">
							<select name="selection" id="selection">
								<option value="All" <?php if(isset($_REQUEST['selection']) && $_REQUEST['selection'] == "ALL")  echo ' selected="selected" ';?>>ALL Time</option>
								<option value="Y"  <?php if(isset($_REQUEST['selection']) && $_REQUEST['selection'] == "Y")  echo ' selected="selected" ';?>>Yesterday</option>
								<option value="CM" <?php if(isset($_REQUEST['selection']) && $_REQUEST['selection'] == "CM") echo ' selected="selected" ';?>>Current Month</option>
								<option value="LM" <?php if(isset($_REQUEST['selection']) && $_REQUEST['selection'] == "LM") echo ' selected="selected" ';?>>Last Month</option>
							</select>
							<input name="memberid" type="hidden" id="memberid" value="<?php print $memberid;?>">
						</td>
						<td class="left">&nbsp;</td>
						<td class="left">&nbsp;</td>
					</tr>
	                <tr>
	                	<td nowrap="nowrap" class="left">
	                		<input name="r1" type="radio" value="FDTD"  <?php print ($r1 == "FDTD")? ' checked="checked" ':''; ?>>
	                    </td>
	                    <td nowrap="nowrap" class="left">
							From date: <input type="text" name="from_date" id="from_date" size="8" value="<?php print date("d/m/Y", strtotime($fromdate)); ?>" maxlength="10"/>
							To date: <input type="text" name="to_date" id="to_date" size="8" value="<?php print date("d/m/Y", strtotime($todate)); ?>" maxlength="10"/>
	                    </td>
	                    <td nowrap="nowrap" class="left">&nbsp;</td>
						
						<td nowrap="nowrap" class="left">
							<input type="submit" name="Submit" value="Go" class="form_submit" />
							<input type="hidden" name="module_page" value="my_stats"/>
							<input type="hidden" name="stats_step" value="my_stats_analitics"/>
							<input type="hidden" name="back_step" value="<?php print $_REQUEST['back_step'];?>"/>
						</td>
					</tr>
				</table>
				</form>
			</td>
		</tr>
	</table>
	</td>
</tr>
</table>
<br />
<?php 
	$showipaddresses="no";
	if($type<>"ban"){
		$queryadvertisment = "SELECT * FROM advertisment WHERE member_id='$memberid'";
		$resultadvertisment = mysql_query ($queryadvertisment);

		if(mysql_num_rows($resultadvertisment)>0){
			while($lineadv = mysql_fetch_array($resultadvertisment)){
				$query_report1="SELECT SUM(views), MIN(added_on) FROM adverts_stats_views_maintained WHERE advertisment_id='".$lineadv['advertisment_id']."' AND domain_id='$domain_id'";
				$query_report2=" GROUP BY view_from, ipfrom";
				$show_query=$query_report1 . " AND view_from!=''" . $wherequery . $query_report2;

				$show_result=mysql_query($show_query) or die(mysql_error());
				if(mysql_num_rows($show_result)>0){
				
				?>            
				<table width="99%" border="0" cellpadding="3" cellspacing="0" class="tableresults" align="center">
					<tr>
						<th colspan="6" align="left" scope="row"class="tdshowall" >
							&nbsp;Logo:
							<?php 
							print $lineadv["short_title_en"]; 
							$imageadvert="../images/advertisments/".$lineadv['advertisment'];
							if(file_exists($imageadvert) && is_file($imageadvert)){
								?> 
								(<a href="<?=$path;?>images/advertisments/<?php print $lineadv["advertisment"]; ?>" target="_blank">view logo</a>)
								<?php 
							}
							?>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<?php 
							if($lineadv["website"]<>""){
								?>
								Links to: <strong><a href="http://<?=$lineadv["website"];?>" target="_blank">
								<?php print $lineadv["website"];?>
								</a></strong>
								<? 
							}
							?>
						</th>
					</tr>
					<tr>
						<th align="left" nowrap="nowrap" class="tdsort" scope="row"><? $count_loc=0;?>Portal Section </th>
						<td nowrap="nowrap" class="tdsort">First Impres.</td>
						<td nowrap="nowrap" class="tdsort"> <?php print $total_impr=0; ?> Impressions</td>
						<td nowrap="nowrap" class="tdsort"> <?php print $totalnoofclicks=0;?> Clicks</td>
						<td nowrap="nowrap" class="tdsort"> <?php print $totalctr=0;$CountTotalctr=0;?> CTR</td>
						<td nowrap="nowrap" class="tdsort"> 
							<?php  
							$queryactive1 = "SELECT * FROM advertisment WHERE advertisment_id='".$lineadv['advertisment_id']."'";
							$queryactive=$queryactive1 . " AND active='yes' AND ";
							$domain_idx2=sprintf("%04.0f", $domain_id);
							$queryactive .=" (catdom_id LIKE '%$domain_idx2%'";
							$result_sub = mysql_query ("SELECT * FROM  domaincategories WHERE parent_id='$domain_id'");
							if (mysql_num_rows($result_sub) <> 0){			  
								while($line_sub = mysql_fetch_array($result_sub)){ 
									$domain_idx2=sprintf("%04.0f", $line_sub["catdom_id"]);
									$queryactive .=" OR catdom_id LIKE '%$domain_idx2%'";
								}
							}
							$queryactive .=")";
							//  echo $queryactive;
							$resultactive=mysql_query($queryactive);
							?>
							Current status 
						</td>
					</tr>
            
					<?php 
					$query_report_home=$query_report1 . " AND (view_from='top' OR view_from='sel') AND (ipfrom='' OR ipfrom=0) ";
					$query_report_home .=$wherequery;
					$report_home=mysql_query($query_report_home);
			 
					$line_report_home = mysql_fetch_assoc($report_home); 
					$query_categ = "SELECT * FROM domain_advertisment,advertisment WHERE ((domain_advertisment.active='yes' OR (domain_advertisment.active_from <= NOW() AND domain_advertisment.active_to >= NOW())) OR (domain_advertisment.category_top='yes' OR (domain_advertisment.category_top_from <= NOW() AND domain_advertisment.category_top_to >= NOW()))) AND advertisment.active='yes' AND domain_advertisment.advertisment_id=advertisment.advertisment_id";
					if($line_report_home["SUM(views)"]>0){
						?>
						<tr onmouseover="this.className='stats_hover'" onmouseout="this.className='stats_normal'">
							<th class="tdsort1stPortal" scope="row"><? $count_loc++;?>Home page</th>
							<td class="tdsortstats">
							<?php 
							if($line_report_home["MIN(added_on)"]<>""){
								echo date("d-M-Y", strtotime($line_report_home["MIN(added_on)"]));
							}
							else{
								echo "N/A";
							}
							?>
							</td>
							<td class="tdsortstats">
								<?php				
								$total_impr = $line_report_home["SUM(views)"];
								if($line_report_home["SUM(views)"]>0){
									$impr = $line_report_home["SUM(views)"];
									print number_format($impr, 0, '', ',');
								}
								else{
									echo $impr=0;
								}
								?>
							</td>
							<td class="tdsortstats" align="right">
								<?php 
									$queryclicks="SELECT advert_stats_id FROM adverts_stats_clicks WHERE advertisment_id = '$lineadv[advertisment_id]' AND domain_id='$domain_id' AND click_on LIKE 'www%' AND catdom_id=0 AND (click_from='sel' OR click_from='top') ";
									$queryclicks .=$wherequery;
									$resultclicks = mysql_query($queryclicks) or die (mysql_error());
									$noofclicks=mysql_num_rows($resultclicks);
									
									print number_format($noofclicks, 0, '', ',');
									
									$totalnoofclicks=$totalnoofclicks+$noofclicks;
								?>
							</td>
							<td class="tdsortstats" align="right">
								<?php 
								if($impr>0){ 
									echo $ctr = round($noofclicks/$impr*100,$decimal);
									$totalctr = $totalctr+$ctr;
									$CountTotalctr++;
								}
								else{echo 0;}?> %
							</td>
							<td class="tdsortactive">
								<?php
								$query_categhome = $query_categ ." AND advertisment.advertisment_id='$lineadv[advertisment_id]' AND domain_advertisment.catdom_id ='$domain_id' ";
								$result_categhome=mysql_query($query_categhome);
								if(mysql_num_rows($resultactive)>0 && mysql_num_rows($result_categhome)>0){ 
									?>
									Active
									<?php 
								}
								else{ 
									?>
									<span class="tdsortinactive">Inactive</span>
									<? 
								}
								?>
							</td>
	            		</tr>
            		<?php 
					}
				
				$querydomaincategories = "SELECT * FROM  domaincategories WHERE parent_id='$domain_id' ORDER BY order_seq ASC";
				$resultdomaincategories = mysql_query ($querydomaincategories);
				if (mysql_num_rows($resultdomaincategories) > 0){
					while($linedomaincategories = mysql_fetch_array($resultdomaincategories)){ 
						$query_duration="SELECT SUM(days) FROM adver_prom_history WHERE advertisment_id='".$lineadv['advertisment_id']."' AND domain_id='".$linedomaincategories['catdom_id']."'";
						$result_duration = mysql_query($query_duration) or die(mysql_error());
						$line_duration = mysql_fetch_assoc($result_duration);
						if($line_duration["SUM(days)"]>0){
							?>              
							<tr onmouseover="this.className='stats_hover'" onmouseout="this.className='stats_normal'">
                				<th class="tdsort1stPortal" scope="row">
                					<?php 
	                					$count_loc++;
	                					print $linedomaincategories["title_el"]; 
                					?>
                				</th>
                				<td class="tdsortstats">
                					<?php 
                						$query_report_home = $query_report1 . " AND (view_from='top' OR view_from='sel') AND ipfrom='".$linedomaincategories['catdom_id']."'";
										$query_report_home .= $wherequery;
										$line_report_home = mysql_fetch_assoc(mysql_query($query_report_home));
									?>
									<?php 
										if($line_report_home["MIN(added_on)"]<>""){
											echo date("d-M-Y",strtotime($line_report_home["MIN(added_on)"]));
										}
										else{
											echo "N/A";
										}
									?>
								</td>
								<td class="tdsortstats">
									<?php
										$total_impr = $total_impr + $line_report_home["SUM(views)"];
										if($line_report_home["SUM(views)"]>0){
											echo $impr = $line_report_home["SUM(views)"];
										}
										else{
											echo $impr=0;
										}
									?>
								</td>

								<td class="tdsortstats">
									<?php 
										$queryclicks = "SELECT advert_stats_id FROM adverts_stats_clicks WHERE advertisment_id = '$lineadv[advertisment_id]' AND domain_id=$domain_id AND click_on LIKE 'www%' AND (click_from='sel' OR click_from='top') AND catdom_id='".$linedomaincategories['catdom_id']."' ";
			  							$queryclicks .=$wherequery;
										$resultclicks = mysql_query($queryclicks) or die (mysql_error());
										$noofclicks = mysql_num_rows($resultclicks);
										print number_format($noofclicks, 0, '', ',');
										$totalnoofclicks = $totalnoofclicks + $noofclicks;
									?>
								</td>
								<td class="tdsortstats">
									<?php 
										if($impr>0){
											print $ctr = round($noofclicks/$impr*100,$decimal);
											$totalctr = $totalctr + $ctr;
											$CountTotalctr++;
										}
										else{
											print 0;
										}
									?>
									%
								</td>
								<td class="tdsortactive">
									<?php 
									$query_categtown = $query_categ ." AND advertisment.advertisment_id='".$lineadv['advertisment_id']."' AND domain_advertisment.catdom_id ='".$linedomaincategories['catdom_id']."' ";
									$result_categhome = mysql_query($query_categtown);
									if(mysql_num_rows($resultactive)>0 && mysql_num_rows($result_categhome)>0){
										print "Active"; 
									}
									else{
										?>
										<span class="tdsortinactive">Inactive</span>
										<?php 
									}
									?>
								</td>
								</tr>
								<? 
						}
					}
					
				}
				?>
				<?php 
				$query_report_home=$query_report1 . " AND view_from='s'";
				$query_report_home .=$wherequery;
				$line_report_home = mysql_fetch_assoc(mysql_query($query_report_home));
				if($line_report_home["SUM(views)"]>0){
					?>
					<tr onmouseover="this.className='stats_hover'" onmouseout="this.className='stats_normal'">
						<th class="tdsort1stPortal" scope="row">
							<?php 
								$count_loc++;
								print "Internal Search Result";
							?>
						</th>
						<td class="tdsortstats">
							<?php 
							if($line_report_home["MIN(added_on)"]<>""){
								echo date("d-M-Y", strtotime($line_report_home["MIN(added_on)"]));
							}
							else{
								echo "N/A";
							}
							?>
						</td>

						<td class="tdsortstats">
							<?php				
								$total_impr = $total_impr + $line_report_home["SUM(views)"];
								if($line_report_home["SUM(views)"]>0){
									echo $impr=$line_report_home["SUM(views)"];
								}
								else{
									echo $impr=0;
								}
							?>
						</td>
						<td class="tdsortstats">
							<?php 
								$queryclicks = "SELECT advert_stats_id FROM adverts_stats_clicks WHERE advertisment_id = '".$lineadv['advertisment_id']."' AND domain_id='$domain_id' AND click_on LIKE 'www%' AND catdom_id=0 AND click_from='s' ";
					 			$queryclicks .= $wherequery;
								$resultclicks=mysql_query($queryclicks) or die (mysql_error());
								$noofclicks = mysql_num_rows($resultclicks);
								print number_format($noofclicks, 0, '', ',');
								$totalnoofclicks = $totalnoofclicks + $noofclicks;
							?>
						</td>
                		<td class="tdsortstats">
                			<?php 
	                			if($impr>0){
	                				$ctr = round($noofclicks/$impr*100,$decimal);
	                				print $ctr;
	                				$totalctr = $totalctr + $ctr;
	                				$CountTotalctr++;
	                			}
	                			else{
	                				echo 0;
	                			}
                			?>
							%
						</td>
						<td class="tdsortactive">
							<?php 
							$lineactive1 = mysql_fetch_assoc(mysql_query($queryactive1));
							if($domain_id==611 && $lineactive1["active"]=='yes'){
								print "Active";
							}
							elseif(mysql_num_rows($resultactive)>0){
								print "Active"; 
							}
							else{
								?>
								<span class="tdsortinactive">Inactive</span>
								<? 
							}
							?>
						</td>
              		</tr>
              		<?php 
				}
				?>
				<tr onmouseover="this.className='stats_hover'" onmouseout="this.className='stats_normal'">
					<th class="tdsort1stPortal" scope="row">
						<?php 
							$count_loc++;
						?>
						More Info Pop-Up / More Info Page
					</th>
					<td class="tdsortstats">
						<?php 
						$query_report_home=$query_report1 . " AND (view_from='adv' OR  view_from='e')";
						$query_report_home .=$wherequery;
						$line_report_home = mysql_fetch_assoc(mysql_query($query_report_home));
						
						if($line_report_home["MIN(added_on)"]<>""){
							echo date("d-M-Y", strtotime($line_report_home["MIN(added_on)"]));
						}
						else{
							echo "N/A";
						}
						?>
					</td>
					<td class="tdsortstats">
						<?php				
							$total_impr = $total_impr + $line_report_home["SUM(views)"];
							if($line_report_home["SUM(views)"]>0){
								echo $impr = $line_report_home["SUM(views)"];
							}
							else{
								echo $impr=0;
							}
						?>
					</td>
					<td class="tdsortstats">
						<?php 
							$queryclicks = "SELECT advert_stats_id FROM adverts_stats_clicks WHERE advertisment_id = '".$lineadv['advertisment_id']."' AND domain_id='$domain_id' AND click_on LIKE 'www%' AND (click_from='adv' OR click_from='e')";
				 			$queryclicks .= $wherequery;
							$resultclicks=mysql_query($queryclicks) or die (mysql_error());
							$noofclicks=mysql_num_rows($resultclicks);
							print number_format($noofclicks, 0, '', ',');
							$totalnoofclicks = $totalnoofclicks + $noofclicks;
						?>
					</td>
					<td class="tdsortstats">
						<?php 
							if($impr>0){
								$ctr=round($noofclicks/$impr*100,$decimal);
								print $ctr;
								$totalctr = $totalctr + $ctr;
								$CountTotalctr++;
							}
							else{
								echo 0;
							}
						?>
						%
					</td>
					<td class="tdsortactive">
						<?php 
							if(mysql_num_rows($resultactive)>0){
								print "Active";
							}
							else{
								?>
								<span class="tdsortinactive">Inactive</span>
								<?php 
							}
						?>
					</td>
				</tr>
				<tr>
					<th class="tdsort1stfooter" scope="row">Total - all <?php print $count_loc; ?> Section(s) </th>
					<td class="tdsortfooter">&nbsp;</td>
					<td class="tdsortfooter"><?php print number_format($total_impr, 0, '',',');?></td>
					<td class="tdsortfooter"><?php print number_format($totalnoofclicks, 0, '',','); if($totalnoofclicks>0) $showipaddresses="yes";?></td>
					<td class="tdsortfooter">
						<?php if($total_impr>0 && $totalnoofclicks>0){
							echo round(($totalnoofclicks/$total_impr)*100,$decimal);
						}
						else{
							echo 0;
						}
						?>
						%
					</td>
					<td class="tdsortfooter">&nbsp;</td>
				</tr>
				</table>
				<br />
				<?php 
				}
			}
			
		}
		else{
			?>
			No Advertisment found
			<?php 
		}
	}
	?>

	<?php 
	if($type<>"adv"){
		?>
		<br />
		<?php
		$querybanner = "SELECT * FROM banner WHERE member_id='$memberid'";
		$resultbanner = mysql_query ($querybanner);
		if(mysql_num_rows($resultbanner)>0){
			while($lineadv=mysql_fetch_array($resultbanner)){
				$query_report1 = "SELECT *,SUM(views), MIN(added_on) FROM banner_stats_views_maintained WHERE banner_id='".$lineadv['module_id']."' AND domain_id='$domain_id'";
				$query_report2 = " GROUP BY ipfrom";
				$query_report = $query_report1 .$where_query .$wherequery .$query_report2;
				$result_banner = mysql_query($query_report) or die(mysql_error());
				$show_query = $query_report1 . $wherequery . $query_report2;
				$show_result = mysql_query($show_query) or die(mysql_error());
				if(mysql_num_rows($show_result)>0){
				?>
				<table width="100%" border="0" cellpadding="3" cellspacing="0" bordercolor="#BEBEBE" class="tableresults">
					<tr>
						<th colspan="6" align="left" scope="row"class="tdshowall" > &nbsp;
							<strong>
								<?php print $lineadv["title_en"]; ?>
							</strong>
							<?php 
							$imageadvert="../images/banners/".$lineadv['banner'];
							if(file_exists($imageadvert) && is_file($imageadvert)){
								?>
								(<a href="<?php print $path;?>images/banners/<?php print $lineadv["banner"];?>" target="_blank">view banner</a>)
								<?php 
							}
							?>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Links to: <a href="http://<?php print $lineadv["url"]; ?>" target="_blank">
								<?php print $lineadv["url"];?>
							</a>
						</th>
					</tr>
					<tr>
						<th align="left" nowrap="nowrap" class="tdsort" scope="row">Portal Section </th>
						<td nowrap="nowrap" class="tdsort">First Impr. </td>
						<td nowrap="nowrap" class="tdsort"><?php $total_impr=0; ?>Impressions</td>
						<td nowrap="nowrap" class="tdsort"><?php $totalnoofclicks=0;?>Clicks</td>
						<td nowrap="nowrap" class="tdsort"><?php $totalctr=0; $CountTotalctr=0;?>CTR</td>
						<td nowrap="nowrap" class="tdsort">
							<?php  
							$queryactive = "SELECT * FROM left_content WHERE catdom_id='$domain_id' AND (module_active='yes' OR (active_from <= NOW() AND active_to >= NOW())) AND module_id='".$lineadv['module_id']."' AND type='banner'";
							$resultactive=mysql_query($queryactive);
							?>
							Current status 
						</td>
					</tr>
					<?php 
					$count_loc=0;
					while($line_banner=mysql_fetch_assoc($result_banner)){
						?>
						<tr onmouseover="this.className='stats_hover'" onmouseout="this.className='stats_normal'">
							<th class="tdsort1stPortal" scope="row">All Pages -
								<? 
								switch ($line_banner["ipfrom"]){
									case "left":
										echo "Left Column";
										break;
									case "right":
										echo "Right Column";
										break;
									case "middle":
										echo "Middle Column";
										break;
								}
								?>
							</th>
							<td class="tdsortstats">
								<?php 
								if($line_banner["MIN(added_on)"]<>""){
									echo date("d-M-Y", strtotime($line_banner["MIN(added_on)"]));
								}
								else{
									echo "N/A";
								}
								?>
							</td>
							<td class="tdsortstats">
								<?php 
								if($line_banner["SUM(views)"]>0){
									$impr = $line_banner["SUM(views)"];
									print number_format($impr, 0, '', ',');
									$total_impr = $total_impr + $impr;
								}
								else{
									echo $impr=0;
								}
								?>
							</td>
							<td class="tdsortstats">
								<?php 
								$queryclicks = "SELECT banner_stats_id FROM banner_stats_clicks WHERE banner_id='".$line_banner['banner_id']."' AND domain_id='$domain_id' AND location='".$line_banner['ipfrom']."'";
								$queryclicks .= $wherequery;
								$resultclicks=mysql_query($queryclicks) or die (mysql_error());
								$noofclicks = mysql_num_rows($resultclicks);
								print number_format($noofclicks, 0, '', ',');
								$totalnoofclicks = $totalnoofclicks + $noofclicks;
								?>
							</td>
							<td class="tdsortstats">
								<?php 
								if($impr>0){
									echo $ctr = round($noofclicks/$impr*100,$decimal);
									$totalctr = $totalctr + $ctr;
									$CountTotalctr++;
								}
								else{
									echo 0;
								}
								?>
								%
							</td>
							<td class="tdsortactive">
								<?php  
								$queryactive = "SELECT * FROM left_content WHERE catdom_id='$domain_id' AND (module_active='yes' OR (active_from <= NOW() AND active_to >= NOW())) AND module_id='".$lineadv['module_id']."' AND type='banner' AND location='".$line_banner['ipfrom']."'";
								$resultactive=mysql_query($queryactive);
								if(mysql_num_rows($resultactive)>0){
									print "Active";
								}
								else{
									?>
									<span class="tdsortinactive">Inactive</span>
									<?php 
								}
								?>
							</td>
						</tr>
						<?php 
						$count_loc++;
					}
				?>
				<tr>
					<th class="tdsort1stfooter" scope="row">Total - all <?php print $count_loc;?> Section(s) </th>
					<td class="tdsortfooter">&nbsp;</td>
					<td class="tdsortfooter"><?php print number_format($total_impr, 0, '', ',');?></td>
					<td class="tdsortfooter">
						<?php 
						print number_format($totalnoofclicks, 0, '', ',');
						if($totalnoofclicks>0)$showipaddresses="yes";
						?></td>
					<td class="tdsortfooter">
						<?php 
						if($total_impr>0 && $totalnoofclicks>0){
							echo round(($totalnoofclicks/$total_impr)*100,$decimal);
						}
						else{
							echo 0;
						}
						?>
						%
					</td>
					<td class="tdsortfooter">&nbsp;</td>
				</tr>
			</table>            
			<br />
			<?php 
				}
			}
		}
		else{
			?>
			No Banner found
			<?php 
		}
		?>
		<br />
		<?php 
	}
	?>
	
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<th width="67%" align="left" class="tdfooter" scope="row">Reporting is not real-time. Clicks and impressions received in the last 24 hours may not be included</th>
			<td width="33%" align="right" class="tdfooter"><img src="imgs/print.gif" width="18" height="17" /> <a href="javascript:window.print();" class="tdfooter">Print this report</a> </td>
		</tr>
	</table>
	<br />
	<?php 
		if($showipaddresses=="yes"){
			?>
			<table width="500" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="216"><a href="index.php?module_page=my_stats&stats_step=my_stats_country1&domain_id=<?php print $domain_id;?>&selection=<?php print $selection;?>&r1=<?php print $r1;?>&from_date=<?php print $fromdate;?>&to_date=<?php print $todate;?>&type=<?php print $type;?>">Country Statistics</a></td>
					<td width="284"><a href="http://www.cypruswebsites.com/modules/stats/pop-ip.php?memberid=<?php print $memberid;?>&domain_id=<?php print $domain_id;?>&selection=<?php print $selection;?>&r1=<?php print $r1;?>&from_date=<?php print $fromdate;?>&to_date=<?php print $todate;?>&type=<?php print $type;?>" onclick="NewWindow(this.href,'ipstats','500','400','yes');return false">IP Statistics</a></td>
				</tr>
			</table>
			<?php 
		}
	?>

<script type="text/javascript">
	function NewWindow(mypage,myname,w,h,scroll){
		LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
		settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
		win = window.open(mypage,myname,settings);
	}
	
	function MM_openBrWindow(theURL,winName,features) { //v2.0
		window.open(theURL,winName,features);
	}
</script>