<?php
require_once "./includes/common.inc.php";
$sql = '';
$adtype = array('top', 'main', 'right', 'bottom', 'index_main', 'index_right', 'index_bottom', 'list_main', 'list_right', 'list_bottom', 'view_main', 'view_bottom');

if(in_array($aid,$adtype)) {
	$query = $db -> query("SELECT * FROM {$tablepre}ad WHERE type='$aid' AND startdate<='$time' order by ordermode");

	while($ad = $db -> fetch_array($query)) {	
		$ad['viewcode'] = stripslashes($ad['viewcode']);

		echo 'document.write("'.adtext($ad).'");';
	}
}else{
	$sql = "";
	$ad = $db -> queryarray("SELECT * FROM {$tablepre}ad WHERE aid='$aid' AND startdate<='$time'");
	echo 'document.write("'.adtext($ad).'");';
}

function adtext($ad){
	global $time,$attachurl;
	if(!is_array($ad)) {
		return false;
	}
	$text = '';


	if($ad['endate'] && $ad['endate'] <= $time) {
		switch($ad['adendmode']) {
			case 1:

				$text .=$ad['adendcode'];
			break;
			case 2:
				$text .='<a href="'.$ad['adendtexturl'].'" target="'.$ad['endtarget'].'">'.$ad['adendtext'].'</a>';
			break;
			case 3:
				$text .='<a href="'.$ad['adendpicurl'].'" target="'.$ad['endtarget'].'"><img src="/'.$attachurl.'/'.$ad['adendpic'].'" border="0"></a>';
			break;
			default:
				@include lang('msg');
				$text .='<a href="/reggo.php?aid='.$ad['aid'].'" target="_blank">'.$lang['ad_lost'].'</a><br> '.$ad['width'].'px X '.$ad['height'].'px';
			break;
		}
	}else{
		switch($ad['viewmode']) {
			case 1:
				$text .='<a href="'.$ad['adtexturl'].'" target="'.$ad['target'].'">'.$ad['adtext'].'</a>';
			break;
			case 2:	
				if(fileext($ad['adpic']) == 'swf') {

					$text .='<object id="ad_'.$ad['aid'].'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" '.($ad['width'] ? 'width="'.$ad['width'].'" ' : '').($ad['height'] ? 'height="'.$ad['height'].'"' : '').'><param name="movie" value="/'.$attachurl.'/'.$ad['adpic'].'"><param name="quality" value="high"><param name="wmode" value="transparent"><embed src="/'.$attachurl.'/'.$ad['adpic'].'" id="ad_'.$ad['aid'].'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" '.($ad['width'] ? 'width="'.$ad['width'].'" ' : '').($ad['height'] ? 'height="'.$ad['height'].'"' : '').'></embed></object>';
				}else{
					$text .='<a href="'.$ad['adendpicurl'].'" target="'.$ad['endtarget'].'"><img src="/'.$attachurl.'/'.$ad['adpic'].'" border="0"></a>';
				
				}
				
			break;
			default:
				$text .=$ad['viewcode'];
			break;
		}	
	}
	$text .='';
	$text  =addcslashes($text, '\\\"');
	$text = preg_replace("/\r\n|\n|\r/", '\n',$text);
	return $text;
}

?>