<?php
if(isset($module_catdom_id_adv) && intval($module_catdom_id_adv) > 0) {
	ob_start();
	$remotehost = $_SERVER['REMOTE_ADDR'];
	$date_now = date("Y-m-d", time());
	$after1month  = date("Y-m-d",mktime(0, 0, 0, date("m")+1 ,date("d"), date("Y")));
	$after3months  = date("Y-m-d",mktime(0, 0, 0, date("m")+3 ,date("d"), date("Y")));
	
	$is_admin =  (isset($_SESSION['user_id'])) ? 1 : 0;
	$catdom_id_adv = intval($module_catdom_id_adv);
	$catdom_id = $catdom_id_adv;
	$domain_idx2 = sprintf("%04.0f", $catdom_id_adv);
	
	//echo '<pre>', var_export($_POST, true), '</pre>';
	
	
	$q = "select * from domaincategories where catdom_id='$catdom_id_adv';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		$line_domain = mysql_fetch_assoc($r);
		$domain_id = $line_domain['catdom_id'];
		$parent_id = intval($line_domain['parent_id']);
		$ultra_domainname = $line_domain['domainname'];
		$ultra_addonname = $line_domain['addonname'];
		
		if($parent_id > 0) {
			$q = "select * from domaincategories where catdom_id='$parent_id';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$line_domain = mysql_fetch_assoc($r);
				$domain_id = $line_domain['catdom_id'];
				$ultra_domainname = $line_domain['domainname'];
				$ultra_addonname = $line_domain['addonname'];
			}
		}
		
	}
	
	
	$root_host = $_SERVER['HTTP_HOST'];
	if(str_replace('www.', '', $root_host) != str_replace('www.', '', $ultra_domainname)) {
		$root_host .= "/$ultra_addonname";
		
		require_once(dirname(__FILE__) . "/../../ageneral/functions.php");
		require_once(dirname(__FILE__) . "/../../ageneral/microdata_pool.php");
	}
	else {
		require_once(dirname(__FILE__) . "/../../functions.php");
		require_once(dirname(__FILE__) . "/../../microdata_pool.php");
	}
	
	$short_invoice_types = array(
		"999999"		=> array("short_name"=>"Complim.", "color"=>"#F02FFF"),
		"888888"		=> array("short_name"=>"Filler", "color"=>"#09AF00"),
		"777777"		=> array("short_name"=>"Featured", "color"=>"#FFD100"),
		"555555"		=> array("short_name"=>"Friends", "color"=>"#39424B"),
		"444444"		=> array("short_name"=>"Link Exch.", "color"=>"#800000")
	);
	
	$bind_types =  array(
		//"invoice"		=> "Payable",
		"t_upgrade"			=> "Temp.Upgrade",
		"t_complimentary"	=> "Temp.Complim",
		"t_filler"			=> "Temp.Filler"
	);
	
	$have_featured = "no";
	$microdata_domain = str_replace("www.", "", $ultra_domainname);
	$microdata_host = $_SERVER['HTTP_HOST'];
	if(str_replace('www.', '', $microdata_host) != str_replace('www.', '', $ultra_domainname)) $microdata_host .= "/" . $ultra_addonname;
	$have_microdata = true;
	
	$query_categtop = "
	SELECT * 
	FROM 
		domain_advertisment d, 
		advertisment a 
	WHERE 
		d.catdom_id ='$catdom_id_adv' 
		AND d.category_top='yes' 
		AND a.active='yes' 
		AND d.advertisment_id=a.advertisment_id 
	ORDER BY 
		d.category_top_order_seq ASC
	";
	
	//AND a.catdom_id LIKE '%$domain_idx2%' 
	
	$result_categtop = mysql_query ($query_categtop) or die(mysql_error());
	if (mysql_num_rows($result_categtop) > 0){
		$have_featured = "yes";
		?>
		<div class="vadverts-grid">
		    <div class="vadverts-grid-sizer"></div>
			<?php
			$count_categtop = 1;
			while($line_categtop = mysql_fetch_assoc($result_categtop)){
				
				//Theodosis modification
				$display_name = str_replace('"','',$line_categtop["short_title_en"]);
				$shortNameURL = $line_categtop["title_tr"];
				$shortNameALT = str_replace('"','',$line_categtop["short_title_en"]);
				
				$website = trim($line_categtop['website']);
				$protocol = "http://";
				if(strpos($website, "https://") !== false) $protocol = "https://";
				$website = str_replace("$protocol", "", $website);
				if($website != "") {
					$website = $protocol . $website;
				}
				
				
				$params = $line_categtop['params'];
				if($params != "") {
					$params = unserialize(base64_decode($params));
					if(is_array($params) && count($params)>0) {
						if(isset($params['display_name']) && trim($params['display_name']) != "") {
							$qt = "select * from advertisment_tradenames where tradename_id='{$params['display_name']}';";
							//echo $qt;
							$rt = @mysql_query($qt);
							if($rt && mysql_num_rows($rt) == 1) {
								$rowt = mysql_fetch_assoc($rt);
								$display_name = stripslashes(trim($rowt['tradename']));
								//echo $display_name;
								$shortNameALT = $display_name;
								$shortNameURL = $rowt['friendly_url'];
							}
						}
						
						
					}
				}
				
				?>
				<div class="vadv_unit">
				    <div class="vadverts-unit-content">
                        <?php
                        if (!isset($_SESSION['user_id']) && $is_admin == 0){
                            mysql_query("UPDATE advertisment SET views=views+1 WHERE advertisment_id='{$line_categtop['advertisment_id']}'") or die (mysql_error());
                            count_views('adverts_stats_views', 'advertisment_id', $line_categtop['advertisment_id'], $domain_id, $catdom_id, 'top');

                            $_adverts_views [] = array(
                                "adv_code"				=> $line_categtop['adv_code'],
                                "advertisment_id"		=> $line_categtop['advertisment_id'],
                                "domain_id"				=> $domain_id,
                                "catdom_id"				=> $catdom_id,
                                "domainname"			=> $ultra_domainname,
                                "date_short"			=> date("Y-m-d"),
                                "date_long"				=> date("Y-m-d H:i:s"),
                                "views"					=> 1,
                                "view_from"				=> 'vtop'
                            );

                        }
                        // top = category top
                        $vadv_href = 'click_count.php?link=' . $shortNameURL . '&amp;type=www&amp;id=' . $line_categtop['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] . '&amp;domain_id=' . $domain_id . '&amp;click_from=top&amp;catdom_id=' . $catdom_id_adv;
                        $vadv_img_src = $path . 'images/advertisments/' . $line_categtop["advertisment"];
                        $vadv_target = '_blank';
                        $vadv_rel = 'nofollow';

                        if ($line_categtop['open_window'] == "yes" && $website != "") {
                            $vadv_href = fix_url($website);
                        } elseif ($line_categtop['open_window'] == "relative" && $website != "") {
                            $vadv_href = fix_url($website);
                        }

                        if ($line_categtop['advertisment'] == "") {
                            $vadv_img_src = $path . 'images/navigation/no_pic.gif';
                        }

                        ?>
                        <a href="<?=$vadv_href?>" target="<?=$vadv_target?>" rel="<?=$vadv_rel?>" class="click-count vadvert-link">
                            <img src="<?=$vadv_img_src?>" width="85" height="74" alt="<?=$shortNameALT?>">
                            <span><?=$shortNameALT;?></span>
                        </a>
                        <?php

                        if (((isset($_SESSION['user_id']) || $is_admin == 1) && 1==1)) {
                            ?>
                            <div class="advert_sv">
                                <?php
                                $invoice_no = trim($line_categtop["invoice_no"]);
                                $bind_type = trim($line_categtop["bind_type"]);
                                $t_active_from = (trim($line_categtop["active_from"]) != "" && trim($line_categtop["active_from"]) != "0000-00-00") ? strtotime(trim($line_categtop["active_from"])) : "null";
                                $t_active_to = (trim($line_categtop["active_to"]) != "" && trim($line_categtop["active_to"]) != "0000-00-00") ? strtotime(trim($line_categtop["active_to"])) : "null";
                                $cur_timestamp = time();
                                echo '<a href="../admin/advert_manager.php?advert_action=advert_edit_main&amp;advertisment_id=', $line_categtop['advertisment_id'], '" target="_blank">Edit</a>';
                                echo '<div style="margin: 3px 0px;clear:both;">';
                                if ($invoice_no != "") {
                                    $inv_details = "";
                                    $qinv = "select i.details from advertisment_invoice i where i.adv_code='{$line_categtop["adv_code"]}' and i.in_use='1' and i.invoice='{$line_categtop["invoice_no"]}';";
                                    $rinv = mysql_query($qinv);
                                    if ($rinv && mysql_num_rows($rinv) > 0) {
                                        $rowinv = mysql_fetch_assoc($rinv);
                                        $inv_details = stripslashes($rowinv['details']);
                                    }

                                    if (array_key_exists($invoice_no, $short_invoice_types)) {
                                        echo '<span style="color:' . $short_invoice_types[$invoice_no]['color'] . ';">' . $short_invoice_types[$invoice_no]['short_name'] . '</span>';
                                    } else {
                                        echo '<span style="color: #0000EE; font-weight: bold; text-decoration: none; ">' . $invoice_no . '</span>';
                                    }

                                    if ($inv_details != "") {
                                        $inv_details = "Invoice Note ::: &raquo; " . $inv_details;
                                        ?>
                                        <a href="javascript:;" class="tooltip_views"
                                           title="<?php echo $inv_details; ?>"><img src="/images/class_info_red.gif"
                                                                                    border="0" align="middle"
                                                                                    alt="css-img"/></a>
                                        <?php
                                    }

                                } else {
                                    echo '<span style="color:#ff0000;">[ <strong>NO_INVOICE</strong> ]</span>';
                                }

                                if ($t_active_to != "null") {
                                    $active_from = $t_active_from != "null" ? date("d/m/Y", intval($t_active_from)) : 'Null';
                                    $active_to = $t_active_to != "null" ? date("d/m/Y", intval($t_active_to)) : 'Null';

                                    if ($cur_timestamp > intval($t_active_to)) {
                                        echo '<br style="clear:both;"/><div style="color:#ff0000;" title="EXPIRED!!!">', $active_from, ' - ', $active_to, '</div>';
                                    } elseif (intval($t_active_to) < (strtotime("+2 month", $cur_timestamp))) {
                                        echo '<br style="clear:both;"/><div style="color:#0000EE;" title="EXPIRES IN LESS THAN 2 MONTHS!">', $active_from, ' - ', $active_to, '</div>';
                                    } else {
                                        echo '<br style="clear:both;"/><div style="color:#000000;" title="EXPIRES IN MORE THAN 2 MONTHS!">', $active_from, ' - ', $active_to, '</div>';
                                    }
                                } else {
                                    $active_from = trim($t_active_from) != "null" ? date("d/m/Y", intval($t_active_from)) : 'Null';
                                    $active_to = trim($t_active_to) != "null" ? date("d/m/Y", intval($t_active_to)) : 'Null';
                                    echo '<br style="clear:both;"/><div style="color:#ff0000;" title="No Expiration Date">', $active_from, ' - ', $active_to, '</div>';
                                }
                                if ($bind_type != "") {
                                    echo '<div style="clear: both; height: 16px; color:#ff0000; background-color: #FFE900;">', ($bind_types[$bind_type]), '</div>';
                                }


                                if ($line_categtop["member_id"] > 0) {
                                    $qha = "SELECT a.name, a.surname, a.user_id FROM auth a, members m WHERE m.member_id='{$line_categtop['member_id']}' AND a.user_id=m.salesman_id";
                                    $rha = @mysql_query($qha);
                                    if ($rha && mysql_num_rows($rha) > 0) {
                                        $line_member = mysql_fetch_assoc($rha);
                                        echo '<div style="clear: both; height: 16px;">';
                                        echo '<strong>SP:</strong> ' . $line_member["name"] . ' ' . $line_member["surname"];
                                        echo '</div>';
                                    } else {
                                        echo '<div style="clear: both; height: 16px;">NO_SALESMAN</div>';
                                    }
                                } else {
                                    echo '<div style="clear: both; height: 16px;">NO_CUSTOMER</div>';
                                }
                                ?>
                            </div>
                            </div>
                            <?php
                        }
                        ?>
				    </div>
				</div>
				<?php
				
			}
		?>
		</div>
		<?php 
	}
	?>
	<?php //$query_adverts - loaded from ../general/queries.php
	
	//if (isset($_SESSION['user_id'])){
	//	echo $query_adverts;
	//}
	
	$query_adverts = "
	SELECT * 
	FROM 
		domain_advertisment d,
		advertisment a 
	WHERE 
		d.catdom_id ='$catdom_id_adv' 
		AND a.active='yes' 
		AND d.active='yes' 
		AND d.advertisment_id=a.advertisment_id 
		
	ORDER BY 
		d.order_seq DESC, RAND() 
	";
	
	//echo $query_adverts;
	
	//AND a.catdom_id LIKE '%$domain_idx2%' 
	
	$result_adverts = mysql_query ($query_adverts);
	if (mysql_num_rows($result_adverts) <> 0){
		?>
		<div class="vadverts-grid">
		    <div class="vadverts-grid-sizer"></div>
			<?php 
			$count_adverts=1;
			while($line_adverts = mysql_fetch_array($result_adverts)){
				//Ioan modification
				//echo $line_adverts['order_seq'];
				$display_name = str_replace('"','',$line_adverts["short_title_en"]);
				$shortNameURL = $line_adverts["title_tr"];
				$shortNameALT = str_replace('"','',$line_adverts["short_title_en"]);
				
				
				$website = trim($line_adverts['website']);
				$protocol = "http://";
				if(strpos($website, "https://") !== false) $protocol = "https://";
				$website = str_replace("$protocol", "", $website);
				if($website != "") {
					$website = $protocol . $website;
				}
				
				$params = $line_adverts['params'];
				if($params != "") {
					$params = unserialize(base64_decode($params));
					//print_r($params);
					if(is_array($params) && count($params)>0) {
						if(isset($params['display_name']) && trim($params['display_name']) != "") {
							$qt = "select * from advertisment_tradenames where tradename_id='{$params['display_name']}';";
							$rt = @mysql_query($qt);
							if($rt && mysql_num_rows($rt) == 1) {
								$rowt = mysql_fetch_assoc($rt);
								$display_name = stripslashes(trim($rowt['tradename']));
								$shortNameALT = $display_name;
								$shortNameURL = $rowt['friendly_url'];
							}
						}
					}
						
				}
						
				?>
				<div class="vadv_unit">
                    <div class="vadverts-unit-content">
                        <?php
                        if (!isset($_SESSION['user_id']) || $is_admin == 0) {
                            mysql_query("UPDATE advertisment SET views=views+1 WHERE advertisment_id='{$line_adverts['advertisment_id']}'") or die (mysql_error());
                            count_views('adverts_stats_views', 'advertisment_id', $line_adverts['advertisment_id'], $domain_id, $catdom_id, 'sel');

                            $_adverts_views [] = array(
                                "adv_code" => $line_adverts['adv_code'],
                                "advertisment_id" => $line_adverts['advertisment_id'],
                                "domain_id" => $domain_id,
                                "catdom_id" => $catdom_id,
                                "domainname" => $ultra_domainname,
                                "date_short" => date("Y-m-d"),
                                "date_long" => date("Y-m-d H:i:s"),
                                "views" => 1,
                                "view_from" => 'vsel'
                            );

                        }
                        // sel = selected adverts
                        $vadv_href = 'click_count.php?link=' . $shortNameURL . '&amp;type=www&amp;id=' . $line_adverts['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] . '&amp;domain_id=' . $domain_id . '&amp;click_from=top&amp;catdom_id=' . $catdom_id_adv;
                        $vadv_img_src = $path . 'images/advertisments/' . $line_adverts["advertisment"];
                        $vadv_target = '_blank';
                        $vadv_rel = 'nofollow';

                        if ($line_adverts['open_window'] == "yes" && $website != "") {
                            $vadv_href = fix_url($website);
                        } elseif ($line_adverts['open_window'] == "relative" && $website != "") {
                            $vadv_href = fix_url($website);
                        }

                        if ($line_adverts['advertisment'] == "") {
                            $vadv_img_src = $path . 'images/navigation/no_pic.gif';
                        }

                        ?>
                        <a href="<?=$vadv_href?>" target="<?=$vadv_target?>" rel="<?=$vadv_rel?>" class="click-count vadvert-link">
                            <img src="<?=$vadv_img_src?>" width="85" height="74" alt="<?=$shortNameALT?>">
                            <span><?=$shortNameALT;?></span>
                        </a>
                        <?php
                        if ((isset($_SESSION['user_id']) || $is_admin == 1) && 1 == 1) {
                            ?>
                            <div class="advert_sv">
                                <?php
                                $invoice_no = trim($line_adverts["invoice_no"]);
                                $bind_type = trim($line_adverts["bind_type"]);
                                $t_active_from = (trim($line_adverts["active_from"]) != "" && trim($line_adverts["active_from"]) != "0000-00-00") ? strtotime(trim($line_adverts["active_from"])) : "null";
                                $t_active_to = (trim($line_adverts["active_to"]) != "" && trim($line_adverts["active_to"]) != "0000-00-00") ? strtotime(trim($line_adverts["active_to"])) : "null";
                                $cur_timestamp = time();
                                echo '<a href="../admin/advert_manager.php?advert_action=advert_edit_main&amp;advertisment_id=', $line_adverts['advertisment_id'], '" target="_blank">Edit</a>';
                                echo '<div style="margin: 3px 0px;clear:both;">';
                                if ($invoice_no != "") {
                                    $inv_details = "";
                                    $qinv = "select i.details from advertisment_invoice i where i.adv_code='{$line_adverts["adv_code"]}' and i.in_use='1' and i.invoice='{$line_adverts["invoice_no"]}';";
                                    $rinv = mysql_query($qinv);
                                    if ($rinv && mysql_num_rows($rinv) > 0) {
                                        $rowinv = mysql_fetch_assoc($rinv);
                                        $inv_details = stripslashes($rowinv['details']);
                                    }

                                    if (array_key_exists($invoice_no, $short_invoice_types)) {
                                        echo '<span style="color:' . $short_invoice_types[$invoice_no]['color'] . ';">' . $short_invoice_types[$invoice_no]['short_name'] . '</span>';
                                    } else {
                                        echo '<span style="color: #0000EE; font-weight: bold; text-decoration: none; ">' . $invoice_no . '</span>';
                                    }

                                    if ($inv_details != "") {
                                        $inv_details = "Invoice Note ::: &raquo; " . $inv_details;
                                        ?>
                                        <a href="javascript:;" class="tooltip_views"
                                           title="<?php echo $inv_details; ?>"><img src="/images/class_info_red.gif"
                                                                                    border="0" align="middle"
                                                                                    alt="css-img"/></a>
                                        <?php
                                    }

                                } else {
                                    echo '<span style="color:#ff0000;">[ <strong>NO_INVOICE</strong> ]</span>';
                                }

                                if ($t_active_to != "null") {
                                    $active_from = $t_active_from != "null" ? date("d/m/Y", intval($t_active_from)) : 'Null';
                                    $active_to = $t_active_to != "null" ? date("d/m/Y", intval($t_active_to)) : 'Null';

                                    if (intval($t_active_to) < $cur_timestamp) {
                                        echo '<br style="clear:both;"/><div style="color:#ff0000;" title="EXPIRED!!!">', $active_from, ' - ', $active_to, '</div>';
                                    } elseif (intval($t_active_to) > $cur_timestamp && intval($t_active_to) < (strtotime("+2 month", $cur_timestamp))) {
                                        echo '<br style="clear:both;"/><div style="color:#0000EE;" title="EXPIRES IN LESS THAN 2 MONTHS!">', $active_from, ' - ', $active_to, '</div>';
                                    } else {
                                        echo '<br style="clear:both;"/><div style="color:#000000;" title="EXPIRES IN MORE THAN 2 MONTHS!">', $active_from, ' - ', $active_to, '</div>';
                                    }
                                } else {
                                    $active_from = trim($t_active_from) != "null" ? date("d/m/Y", intval($t_active_from)) : 'Null';
                                    $active_to = trim($t_active_to) != "null" ? date("d/m/Y", intval($t_active_to)) : 'Null';
                                    echo '<br style="clear:both;"/><div style="color:#ff0000;" title="No Expiration Date">', $active_from, ' - ', $active_to, '</div>';
                                }
                                if ($bind_type != "") {
                                    echo '<div style="clear: both; height: 16px; color:#ff0000; background-color: #FFE900;">', ($bind_types[$bind_type]), '</div>';
                                }


                                if ($line_adverts["member_id"] > 0) {
                                    $qha = "SELECT a.name, a.surname, a.user_id FROM auth a, members m WHERE m.member_id='{$line_adverts['member_id']}' AND a.user_id=m.salesman_id";
                                    $rha = @mysql_query($qha);
                                    if ($rha && mysql_num_rows($rha) > 0) {
                                        $line_member = mysql_fetch_assoc($rha);
                                        echo '<div style="clear: both; height: 16px;">';
                                        echo '<strong>SP:</strong> ' . $line_member["name"] . ' ' . $line_member["surname"];
                                        echo '</div>';
                                    } else {
                                        echo '<div style="clear: both; height: 16px;">NO_SALESMAN</div>';
                                    }
                                } else {
                                    echo '<div style="clear: both; height: 16px;">NO_CUSTOMER</div>';
                                }
                                ?>
                            </div>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <?php
			}
			?>
		</div>
		<?php
	}
	
	if(isset($_SESSION['user_id']) || $is_admin == 1) {
		$optres['js'][] = $path . 'amodules/vadverts/js/vadverts_admin.js';
	}
	
	$vadverts_content = ob_get_contents();
	ob_end_clean();
	
	echo $vadverts_content;
}

//echo '', var_export($_adverts_views, true), '';
?>