<?php

						/*************************************\
						
						    Handshakes Professional (3.0)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////
							
						admin_validate_section("getpaid",2);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									ads_manager();
									break;
									
								case 'create_campaign':
									create_campaign();
									break;
									
								case 'do_create_campaign':
									do_create_campaign();
									break;
									
								case 'edit_campaign':
									edit_campaign();
									break;
									
								case 'edit_campaign_settings':
									edit_campaign_settings();
									break;
									
								case 'save_campaign_settings':
									save_campaign_settings();
									break;
									
								case 'edit_banner':
									edit_banner();
									break;
									
								case 'save_banner':
									save_banner();
									break;
									
								case 'save_banner_areas':
									save_banner_areas();
									break;
									
								case 'save_banner_pages':
									save_banner_pages();
									break;
									
								case 'save_banner_countries':
									save_banner_countries();
									break;
									
								case 'save_banner_categories':
									save_banner_categories();
									break;
									
								case 'edit_banner_subcategories':
									edit_banner_subcategories();
									break;
									
								case 'save_banner_subcategories':
									save_banner_subcategories();
									break;
									
								case 'delete_banner':
									delete_banner();
									break;
									
								case 'pause_campaign':
									pause_campaign();
									break;
									
								case 'resume_campaign':
									resume_campaign();
									break;
									
								case 'delete_campaign':
									delete_campaign();
									break;
									
								case 'edit_format':
									edit_format();
									break;
									
								case 'save_format':
									save_format();
									break;
									
								case 'delete_format':
									delete_format();
									break;
									
								case 'edit_area':
									edit_area();
									break;
									
								case 'save_area':
									save_area();
									break;
									
								case 'delete_area':
									delete_area();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function ads_manager(){
	
	admin_validate();
	
	$data["themes"] = admin_get_themes();
	$theme = $GLOBALS["Get"]->val("theme");
	
	if(!$theme)
		$theme = $GLOBALS["THEME"];
		
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	$data["formats"] = array();
	
	while(list($key,$val) = each($ads["formats"])){
		$format["name"]   = $val["name"]." (".$val["width"]." x ".$val["height"].")";
		$format["edit"]   = "index.php?section=ads&action=edit_format&theme=".$theme."&format=".$key;
		$format["delete"] = "index.php?section=ads&action=delete_format&theme=".$theme."&format=".$key;
		$data["formats"][] = $format;
	}//while
	
	$data["areas"] = array();
	
	while(list($key,$val) = each($ads["areas"])){
		$area = array();
		$area["name"] = $val["name"];
		$formats = handle_empties(explode(",",$val["formats"]));
		foreach($formats as $format){
			$area["formats"].= $ads["formats"][$format]["name"]."<br>";
		}//foreach
		$area["edit"]   = "index.php?section=ads&action=edit_area&theme=".$theme."&area=".$key;
		$area["delete"] = "index.php?section=ads&action=delete_area&theme=".$theme."&area=".$key;
		$area["tag"] = "{banner area=\"".$key."\"}";
		$data["areas"][] = $area;
	}//while
	
	$sql_query = "SELECT * FROM campaigns WHERE theme='".$theme."' ORDER BY name";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$data["campaigns"] = array();
	
	foreach($result as $res){
		$campaign["name"] = $res["name"];
		$sql_query = "SELECT SUM(impressions) AS impressions, SUM(clicks) AS clicks
					  FROM banners WHERE cmp_id=".$res["cmp_id"];
		$performance = $GLOBALS["DB"]->row($sql_query);
		$cost = campaign_cost($res["type"],$res["price"],$performance["impressions"],$performance["clicks"],$res["start"]);
		
		$campaign["type"] = campaign_type($res["type"]);
		$campaign["clicks"] = ($performance["clicks"]) ? $performance["clicks"] : 0;
		$campaign["impressions"] = ($performance["impressions"]) ? $performance["impressions"] : 0;
		$campaign["cost"] = extract_option("currencies",$res["currency"]).$cost;
		$campaign["balance"] = ($campaign["cost"] != "N/A") ? extract_option("currencies",$res["currency"]).($res["credit"] - $cost) : "N/A";
		$campaign["edit"] = "index.php?section=ads&action=edit_campaign&theme=".$theme."&cmp_id=".$res["cmp_id"];
		$campaign["delete"] = "index.php?section=ads&action=delete_campaign&theme=".$theme."&cmp_id=".$res["cmp_id"];
		if($res["status"]){
			$campaign["pause"]  = "index.php?section=ads&action=pause_campaign&theme=".$theme."&cmp_id=".$res["cmp_id"];
			$campaign["resume"] = "";
		}//if
		else{
			$campaign["resume"] = "index.php?section=ads&action=resume_campaign&theme=".$theme."&cmp_id=".$res["cmp_id"];
			$campaign["pause"]  = "";
		}//else
		$data["campaigns"][] = $campaign;
	}//foreach
	
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["title_id"] = "3";	
	$layout[0] = "ads_manager";
	
	admin_output($layout,$data);
	
}//ads_manager

function create_campaign(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["title_id"] = "3";	$data["theme"] = $theme;
	
	$data["link"] = "index.php?section=ads&theme=".$theme;
	
	$vals = array("type","price","currency","credit","start","end","name");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$values["start"] = $GLOBALS["GetFromForm"]->field("start","date");	
	$values["end"]   = $GLOBALS["GetFromForm"]->field("end","date");
	
	if(!$values["start"])
		$values["start"] = time();
	
	if(!$values["type"])
		$values["type"] = "external";
		
	$data["values"] = $values;
	
	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	function HS_UpdateCostType(FormHandler){		
		for(var i=0;i<FormHandler.type.length;i++){
			if(FormHandler.type[i].checked == true){
				var CostType = FormHandler.type[i].value;
				break;
			}//if
		}//for
		switch(CostType){
			case 'external':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "none";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "none";
				break;
			case 'ppc':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "inline";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "inline";
				var CostType = document.getElementById("cost_type");
				CostType.innerHTML = "per click";
				break;
			case 'impressions':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "inline";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "inline";
				var CostType = document.getElementById("cost_type");
				CostType.innerHTML = "per 1,000 impressions";
				break;
			case 'subscription':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "inline";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "inline";
				var CostType = document.getElementById("cost_type");
				CostType.innerHTML = "per month";
				break;
		}//switch
	}//HS_UpdateCostType
//-->
</script>
HTMLTEXT;

	$data["onload"] = "HS_UpdateCostType(document.forms[0])";
	
	$layout[0] = "create_campaign";	
	
	admin_output($layout,$data);
	
}//create_campaign

function edit_campaign_settings(){
	
	admin_validate();
	
	$theme  = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->val("cmp_id");
	
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["title_id"] = "3";	$data["theme"] = $theme;
	$data["cmp_id"] = $cmp_id;
	
	$data["link"] = "index.php?section=ads&theme=".$theme."&action=edit_campaign&cmp_id=".$cmp_id;
	
	$sql_query = "SELECT * FROM campaigns WHERE cmp_id=".$cmp_id;
	$values = $GLOBALS["DB"]->row($sql_query);
	
	$sql_query = "SELECT SUM(impressions) AS impressions, SUM(clicks) AS clicks
				  FROM banners WHERE cmp_id=".$cmp_id;
	$performance = $GLOBALS["DB"]->row($sql_query);
	
	$cost = campaign_cost($values["type"],$values["price"],$performance["impressions"],$performance["clicks"],$values["start"]);
	$values["balance"] = ($cost !== "N/A") ? ($values["credit"] - $cost) : "N/A";
	
	$data["values"] = $values;
	
	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	function HS_UpdateCostType(FormHandler){		
		for(var i=0;i<FormHandler.type.length;i++){
			if(FormHandler.type[i].checked == true){
				var CostType = FormHandler.type[i].value;
				break;
			}//if
		}//for
		switch(CostType){
			case 'external':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "none";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "none";
				break;
			case 'ppc':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "inline";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "inline";
				var CostType = document.getElementById("cost_type");
				CostType.innerHTML = "per click";
				break;
			case 'impressions':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "inline";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "inline";
				var CostType = document.getElementById("cost_type");
				CostType.innerHTML = "per 1,000 impressions";
				break;
			case 'subscription':
				var CostRow = document.getElementById("cost_row");
				CostRow.style.display = "inline";
				var CreditRow = document.getElementById("credit_row");
				CreditRow.style.display = "inline";
				var CostType = document.getElementById("cost_type");
				CostType.innerHTML = "per month";
				break;
		}//switch
	}//HS_UpdateCostType
//-->
</script>
HTMLTEXT;

	$data["onload"] = "HS_UpdateCostType(document.forms[0])";
	
	$layout[0] = "edit_campaign_settings";
	
	admin_output($layout,$data);
	
}//edit_campaign_settings

function do_create_campaign(){
	
	admin_validate();
	
	$vals = array("type","price","currency","credit","start","end","name","theme");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["name"]) || empty($values["type"])){
		$GLOBALS["aerr_id"] = 10;
		create_campaign();
		exit;
	}//if
	
	if($values["type"] != "external" && (empty($values["price"]) || empty($values["credit"]))){
		$GLOBALS["aerr_id"] = 33;
		create_campaign();
		break;
	}//if
	
	$values["start"] = $GLOBALS["GetFromForm"]->field("start","date");
	$values["end"]   = $GLOBALS["GetFromForm"]->field("end","date");
	
	if(!$values["start"])
		$values["start"] = time();
	
	if(!$values["end"])
		$values["end"] = 0;
		
	$sql_query = "INSERT INTO campaigns (name,theme,type,price,currency,credit,`start`,`end`,`status`,created)
				  VALUES (':name:',':theme:',':type:',':price:',':currency:',':credit:',:start:,:end:,'1',:time:)";
	
	$values["time"] = time();
	$GLOBALS["DB"]->values = $values;
	$cmp_id = $GLOBALS["DB"]->insert($sql_query);
	
	load_page("index.php?section=ads&action=edit_campaign&theme=".$values["theme"]."&cmp_id=".$cmp_id);
	
}//do_create_campaign

function save_campaign_settings(){
	
	admin_validate();
	
	$vals = array("cmp_id","type","price","currency","balance","start","end","name","theme");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["name"]) || empty($values["type"])){
		$GLOBALS["aerr_id"] = 10;
		edit_campaign_settings();
		exit;
	}//if
	
	if($values["type"] != "external" && (empty($values["price"]))){
		$GLOBALS["aerr_id"] = 33;
		edit_campaign_settings();
		break;
	}//if
	
	$values["start"] = $GLOBALS["GetFromForm"]->field("start","date");
	$values["end"]   = $GLOBALS["GetFromForm"]->field("end","date");
	
	if(!$values["start"])
		$values["start"] = time();
	
	if(!$values["end"])
		$values["end"] = 0;
		
	$sql_query = "SELECT * FROM campaigns WHERE cmp_id=".$values["cmp_id"];
	$campaign = $GLOBALS["DB"]->row($sql_query);
	
	$sql_query = "SELECT SUM(impressions) AS impressions, SUM(clicks) AS clicks
				  FROM banners WHERE cmp_id=".$values["cmp_id"];
	$performance = $GLOBALS["DB"]->row($sql_query);
	
	$cost = campaign_cost($campaign["type"],$campaign["price"],$performance["impressions"],$performance["clicks"],$campaign["start"]);
	
	$values["credit"] = $values["balance"]+$cost;
		
	$sql_query = "REPLACE INTO campaigns (cmp_id,name,theme,type,price,currency,credit,`start`,`end`,`status`,created)
				  VALUES (:cmp_id:,':name:',':theme:',':type:',':price:',':currency:',':credit:',:start:,:end:,'1',:time:)";
	
	$values["time"] = time();
	$GLOBALS["DB"]->values = $values;
	$cmp_id = $GLOBALS["DB"]->insert($sql_query);
	
	load_page("index.php?section=ads&action=edit_campaign&theme=".$values["theme"]."&cmp_id=".$values["cmp_id"]);
	
}//save_campaign_settings

function edit_campaign(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->type_val("cmp_id","integer");
	
	$data["theme"] = $theme;
	$data["cmp_id"] = $cmp_id;
	$data["link"] = "index.php?section=ads&action=edit_campaign&theme=".$theme."&cmp_id=".$cmp_id;
	
	$sql_query = "SELECT * FROM campaigns WHERE cmp_id=".$cmp_id;
	$res = $GLOBALS["DB"]->row($sql_query);
	
	$sql_query = "SELECT SUM(impressions) AS impressions, SUM(clicks) AS clicks
				  FROM banners WHERE cmp_id=".$res["cmp_id"];
	$performance = $GLOBALS["DB"]->row($sql_query);
	
	$campaign["name"]  = $res["name"];
	$campaign["type"]  = campaign_type($res["type"]);
	$campaign["price"] = extract_option("currencies",$res["currency"]).campaign_price($res["type"],$res["price"]);
	$campaign["cost"] = extract_option("currencies",$res["currency"]).campaign_cost($res["type"],$res["price"],$performance["impressions"],$performance["clicks"],$res["start"]);
	$campaign["balance"] = ($campaign["cost"] != "N/A") ? extract_option("currencies",$res["currency"]).($res["credit"] - $campaign["cost"]) : "N/A";
	$campaign["start"] = extract_date($res["start"]);
	$campaign["end"]   = ($res["end"]) ? extract_date($res["end"]) : "Unlimited";
	
	$data["campaign"] = $campaign;
	
	$sql_query = "SELECT * FROM banners WHERE cmp_id=".$cmp_id." ORDER BY name";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$data["banners"] = array();
	
	foreach($result as $res){
		if($res["type"] == "static")
			$banner["type"] = "GIF/JPEG";
		elseif($res["type"] == "rich")
			$banner["type"] = "Flash";
		else
			$banner["type"] = "External";
			
		$banner["name"]        = $res["name"];		
		$banner["format"]      = banner_format($theme,$res["format"]);
		$banner["impressions"] = ($res["impressions"]) ? $res["impressions"] : 0;
		$banner["clicks"]      = ($res["clicks"]) ? $res["clicks"] : 0;
		$banner["edit"]        = "index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$res["ban_id"]."&ret_link=".urlencode($data["link"]);
		$banner["delete"]      = "index.php?section=ads&action=delete_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$res["ban_id"]."&ret_link=".urlencode($data["link"]);
		$data["banners"][]     = $banner;
	}//foreach
	
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["title_id"] = "3";	
	$layout[0] = "edit_campaign";
	
	admin_output($layout,$data);
	
}//edit_campaign

function edit_banner(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->type_val("cmp_id","integer");
	$ban_id = $GLOBALS["Get"]->type_val("ban_id","integer");
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	$data["theme"] = $theme;
	$data["cmp_id"] = $cmp_id;
	$data["ban_id"] = $ban_id;
	
	$sql_query = "SELECT * FROM campaigns WHERE cmp_id=".$cmp_id;
	$res = $GLOBALS["DB"]->row($sql_query);
	
	$data["campaign"]["name"] = $res["name"];
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	$data["link"] = $ret_link;
	
	if($ban_id){
	
		$sql_query = "SELECT * FROM banners WHERE ban_id=".$ban_id;
		$banner = $GLOBALS["DB"]->row($sql_query);		
		
		$data["output"] = extract_banner_output($ban_id,0);
		$data["values"] = $banner;		
		
		//areas
		$areas = handle_empties(explode(",",$data["values"]["areas"]));
		$data["values"]["areas"] = array();
		foreach($areas as $area){
			$data["values"]["areas"][$area] = 1;
		}//foreach
		
		$data["areas"] = array();
		$possible_areas = possible_banner_areas($theme,$data["values"]["format"]);
	
		foreach($possible_areas as $area){
			$data["areas"][$area] = $ads["areas"][$area]["name"];
		}//foreach
		
		//pages

		$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	function HS_LoadOptions(FieldHandler, OptionsLine){
		if(OptionsLine != ''){
			ListItems = OptionsLine.split("|");
			OptionsHandler = FieldHandler.options;
			for(i=0;i<(ListItems.length/2);i++){
				j = i*2;
				OptionsHandler[i] = new Option(ListItems[j+1],ListItems[j]);
			}//for
		}//if
	}//HS_LoadOptions
	function HS_MoveOptions(FieldHandlerFrom, FieldHandlerTo){
		var TransferOptions = new Array();
		var j=0;
		var k=0;		
		OptionsHandlerFrom = FieldHandlerFrom.options;
		OptionsHandlerTo = FieldHandlerTo.options;		
		for(i=0;i<OptionsHandlerFrom.length;i++){
			if(OptionsHandlerFrom[i].selected == true){
				OptionsHandlerTo[OptionsHandlerTo.length] = 
				new Option(OptionsHandlerFrom[i].text,OptionsHandlerFrom[i].value);
				OptionsHandlerFrom[i] = null;
				TransferOptions[j] = i;
				j++;
				i--;				
			}//if
		}//for
	}//HS_MoveOptions
	function HS_PrepareData(FieldHandlerSrc, FieldHandlerResult){
		var ResultLine = "";
		OptionsHandlerSrc = FieldHandlerSrc.options;
		for(i=0;i<OptionsHandlerSrc.length;i++){
			ResultLine+= OptionsHandlerSrc[i].value + ",";
		}//for
		FieldHandlerResult.value = ResultLine;		
	}//HS_PrepareData	
//-->
</script>
JAVASCRIPTCODE;

		$site_pages = admin_get_pages($theme);
		if($banner["pages"] == "all")
			$banner_pages = array_keys($site_pages);
		else
			$banner_pages = handle_empties(explode(",",$banner["pages"]));
	
		$data["pages"] = array();
		$data["npages"] = array();
	
		foreach($banner_pages as $banner_page){
			$data["pages"][$banner_page] = $site_pages[$banner_page];
		}//foreach
	
		$nonbanner_pages = array_diff(array_keys($site_pages),array_keys($data["pages"]));
		foreach($nonbanner_pages as $nonbanner_page){
			$data["npages"][$nonbanner_page] = $site_pages[$nonbanner_page];
		}//foreach

		$banner_pages = "";
		while(list($key,$val) = each($data["pages"])){
			$banner_pages.= $key."|".$val."|";
		}//while
		$banner_pages = rtrim($banner_pages,"|");
		while(list($key,$val) = each($data["npages"])){
			$banner_npages.= $key."|".$val."|";
		}//while
		$banner_npages = rtrim($banner_npages,"|");	
	
		$data["onload"].= "HS_LoadOptions(document.banner_pages.b_pages,'".htmlspecialchars(ereg_replace("'","\'",$banner_pages))."');";
		$data["onload"].= "HS_LoadOptions(document.banner_pages.b_npages,'".htmlspecialchars(ereg_replace("'","\'",$banner_npages))."');";
		
		//countries
		
		$data["countries"] = array();
		
		$sql_query = "SELECT con_id,name FROM geo_countries ORDER BY name";
		$result = $GLOBALS["DB"]->result($sql_query);
		
		foreach($result as $res){
			$data["countries"][$res["con_id"]] = $res["name"];
		}//foreach
		
		if($banner["countries"] == "all"){
			$data["values"]["countries"] = array();
			while(list($key,) = each($data["countries"])){
				$data["values"]["countries"][$key] = 1;
			}//while
		}//if
		else{
			$data["values"]["countries"] = array();
			$countries = handle_empties(explode(",",$banner["countries"]));
			foreach($countries as $con_id){
				$data["values"]["countries"][$con_id] = 1;
			}//foreach
		}//else
		
		//classifieds categories
		$categories = array();
		include(DOC_ROOT."/themes/".$theme."/system/cls_categories.inc.php");
		$data["cls_categories"] = $categories;
		
		$data["values"]["cls_categories"] = array();
		
		while(list($key,) = each($categories)){
			if($banner["cls_categories"] == "all" || eregi(",".$key."_all,",$banner["cls_categories"]))
				$data["values"]["cls_categories"][$key] = 1;
			if(eregi(",".$key."_[0-9]+,",$banner["cls_categories"]))
				$data["cls_subcategories"][$key] = 1;
		}//while
		
		//events categories
		$categories = array();
		include(DOC_ROOT."/themes/".$theme."/system/evn_categories.inc.php");
		$data["evn_categories"] = $categories;
		
		$data["values"]["evn_categories"] = array();
		
		while(list($key,) = each($categories)){
			if($banner["evn_categories"] == "all" || eregi(",".$key."_all,",$banner["evn_categories"]))
				$data["values"]["evn_categories"][$key] = 1;
			if(eregi(",".$key."_[^all],",$banner["evn_categories"]))
				$data["evn_subcategories"][$key] = 1;
		}//while
		
		//clubs categories
		$categories = array();
		include(DOC_ROOT."/themes/".$theme."/system/clb_categories.inc.php");
		$data["clb_categories"] = $categories;
		
		$data["values"]["clb_categories"] = array();
		
		while(list($key,) = each($categories)){
			if($banner["clb_categories"] == "all" || eregi(",".$key."_all,",$banner["clb_categories"]))
				$data["values"]["clb_categories"][$key] = 1;
			if(eregi(",".$key."_[^all],",$banner["clb_categories"]))
				$data["clb_subcategories"][$key] = 1;
		}//while
		
	}//if
	else{
		
		$vals = array("name","format","type","tag","url");
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
		
		if(!$data["values"]["type"])
			$data["values"]["type"] = "external";
		
	}//else
	
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["hide_submenu"] = 1;
	$data["title_id"] = "3";	
	$data["formats"] = array();
	
	while(list($key,$val) = each($ads["formats"])){
		$data["formats"][$key] = $val["name"]." (".$val["width"]." x ".$val["height"].")";
	}//while
	
	$data["javascript"].= <<<HTMLTEXT
<script language="JavaScript">
<!--
	function HS_UpdateBannerFields(FormHandler){
		for(var i=0;i<FormHandler.type.length;i++){
			if(FormHandler.type[i].checked == true){
				var BannerType = FormHandler.type[i].value;
			}//if
		}//for
		
		switch(BannerType){
			case 'external':
				var UploadRow1 = document.getElementById("upload1");
				UploadRow1.style.display = "none";
				var UploadRow2 = document.getElementById("upload2");
				UploadRow2.style.display = "none";
				var TagRow = document.getElementById("special_tag");
				TagRow.style.display = "inline";
				var URLRow = document.getElementById("url_row");
				URLRow.style.display = "none";
				var NoticeHandler = document.getElementById("notice");
				NoticeHandler.style.display = "none";
				break;
			case 'static':
				var UploadRow1 = document.getElementById("upload1");
				UploadRow1.style.display = "inline";
				var UploadRow2 = document.getElementById("upload2");
				UploadRow2.style.display = "none";
				var TagRow = document.getElementById("special_tag");
				TagRow.style.display = "none";
				var URLRow = document.getElementById("url_row");
				URLRow.style.display = "inline";
				var NoticeHandler = document.getElementById("notice");
				NoticeHandler.style.display = "none";
				break;
			case 'rich':
				var UploadRow1 = document.getElementById("upload1");
				UploadRow1.style.display = "inline";
				var UploadRow2 = document.getElementById("upload2");
				UploadRow2.style.display = "inline";
				var TagRow = document.getElementById("special_tag");
				TagRow.style.display = "inline";
				var URLRow = document.getElementById("url_row");
				URLRow.style.display = "inline";
				var NoticeHandler = document.getElementById("notice");
				NoticeHandler.style.display = "inline";
				break;
		}//switch
	}//HS_UpdateBannerFields
//-->
</script>
HTMLTEXT;

	$data["onload"].= "HS_UpdateBannerFields(document.forms[0]);";
	
	$layout[0] = "edit_banner";
	
	admin_output($layout,$data);	
	
}//edit_banner

function edit_banner_subcategories(){
	
	$theme    = $GLOBALS["Get"]->val("theme");
	$cmp_id   = $GLOBALS["Get"]->val("cmp_id");
	$ban_id   = $GLOBALS["Get"]->val("ban_id");
	$pro      = $GLOBALS["Get"]->val("pro");
	$category = $GLOBALS["Get"]->val("category");
	
	$data["theme"] = $theme;
	$data["cmp_id"] = $cmp_id;
	$data["ban_id"] = $ban_id;
	$data["category"] = $category;
	
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["hide_submenu"] = 1;
	$data["title_id"] = "3";	
	$sql_query = "SELECT * FROM campaigns WHERE cmp_id=".$cmp_id;
	$res = $GLOBALS["DB"]->row($sql_query);
	
	$data["campaign"]["name"] = $res["name"];
	
	switch($pro){
		case 'classifieds':
			$type = "cls_categories";
			break;
		case 'events':
			$type = "evn_categories";
			break;
		case 'clubs':
			$type = "clb_categories";
			break;
	}//switch
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	$data["link"] = $ret_link;
	$data["link2"] = "index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$ban_id."&ret_link=".urlencode($data["link"])."#".$type;
	
	$sql_query = "SELECT ".$type." FROM banners WHERE ban_id=".$ban_id;
	$banner_categories = $GLOBALS["DB"]->single($sql_query);
	
	include(DOC_ROOT."/themes/".$theme."/system/".$type.".inc.php");
	
	$data["subcategories"] = $subcategories[$category];

	while(list($k,$v) = each($subcategories[$category])){
		if($banner_categories == "all" || eregi(",".$category."_all,",$banner_categories) || eregi(",".$category."_".$k.",",$banner_categories)){
			$data["values"]["subcategories"][$k] = 1;
		}//if
	}//while
	
	$data["type"] = $pro;
	
	$layout[0] = "edit_banner_subcategories";
	
	admin_output($layout,$data);
	
}//edit_banner_subcategories

function save_banner(){
	
	admin_validate();
	
	$vals = array("theme","cmp_id","ban_id","name","format","type","tag","url");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$sql_query = "SELECT * FROM banners WHERE ban_id=".$values["ban_id"];
	$banner = $GLOBALS["DB"]->row($sql_query);
	
	$primary = $GLOBALS["GetFromForm"]->file("primary");
	$secondary = $GLOBALS["GetFromForm"]->file("secondary");	
	
	if(!$values["ban_id"])
		$require_files = 1;
	elseif($values["type"] != $banner["type"])
		$require_files = 1;
	
	if(empty($values["name"]) || empty($values["format"]) || empty($values["type"]) || ($values["type"] != "external" && empty($values["url"]))){
		$GLOBALS["aerr_id"] = 10;
		edit_banner();
		exit;
	}//if
	
	if($values["type"] == "external" && empty($values["tag"])){
		$GLOBALS["aerr_id"] = 34;
		edit_banner();
		exit;
	}//if
	
	if(($values["type"] == "static" || $values["type"] == "rich") && !$primary["tmp"] && $require_files){
		$GLOBALS["aerr_id"] = 35;
		edit_banner();
		exit;
	}//if
	
	if($values["type"] == "rich" && !$secondary["tmp"] && $require_files){
		$GLOBALS["aerr_id"] = 36;
		edit_banner();
		exit;
	}//if
	
	if($values["type"] == "external"){
		$primary["tmp"] = 0;
		$secondary["tmp"] = 0;
		$values["url"] = "";
	}//if
	elseif($values["type"] == "static"){
		$secondary["tmp"] = 0;
		$values["tag"] = "";
	}//elseif
	
	mt_srand((double) microtime() * 100000);
	
	if($primary["tmp"]){		
		
		$file_name = hs_substr(md5(time().mt_rand(0,100)),0,15);
		
		if(eregi("gif",$primary["type"]))
			$extension = ".gif";
		elseif(eregi("jpeg|jpg",$primary["type"]))
			$extension = ".jpeg";
		elseif(eregi("flash",$primary["type"]))
			$extension = ".swf";
			
		move_uploaded_file($primary["tmp"],DOC_ROOT."/ads/".$file_name.$extension);
		chmod(DOC_ROOT."/ads/".$file_name.$extension,0644);
		
		$values["primary_file"] = $file_name.$extension;
		
	}//if
	else{
		$values["primary_file"] = $banner["primary_file"];
	}//else
	
	if($secondary["tmp"]){	
		
		$file_name = hs_substr(md5(time().mt_rand(0,100)),0,15);
		
		if(eregi("gif",$secondary["type"]))
			$extension = ".gif";
		elseif(eregi("jpeg|jpg",$secondary["type"]))
			$extension = ".jpeg";
			
		move_uploaded_file($secondary["tmp"],DOC_ROOT."/ads/".$file_name.$extension);
		chmod(DOC_ROOT."/ads/".$file_name.$extension,0644);
		
		$values["secondary_file"] = $file_name.$extension;
		
	}//if
	else{
		$values["secondary_file"] = $banner["secondary_file"];
	}//else
	
	if($values["type"] == "external"){
		$values["primary_file"] = "";
		$values["secondary_file"] = "";
	}//if
	elseif($values["type"] == "static"){
		$values["secondary_file"] = "";
	}//elseif
	
	if(empty($values["ban_id"])){
		
		$values["areas"]          = ",".implode(",",possible_banner_areas($values["theme"],$values["format"])).",";
		$values["pages"]          = "all";
		$values["countries"]      = "all";
		$values["cls_categories"] = "all";
		$values["evn_categories"] = "all";
		$values["clb_categories"] = "all";
		
		
		$sql_query = "INSERT INTO banners (
						cmp_id,
						name,
						format,
						type,
						url,
						primary_file,
						secondary_file,
						tag,
						areas,
						pages,
						countries,
						cls_categories,
						evn_categories,
						clb_categories
					  ) VALUES (
					  	:cmp_id:,
					  	':name:',
					  	':format:',
					  	':type:',
					  	':url:',
					  	':primary_file:',
					  	':secondary_file:',
					  	':tag:',
					  	':areas:',
					  	':pages:',
					  	':countries:',
					  	':cls_categories:',
					  	':evn_categories:',
					  	':clb_categories:'
					  )";
		
		$GLOBALS["DB"]->values = $values;
		$values["ban_id"] = $GLOBALS["DB"]->insert($sql_query);
		
	}//if
	else{
		
		if($banner["format"] != $values["format"])
			$values["areas"] = ",".implode(",",possible_banner_areas($values["theme"],$values["format"])).",";
		else
			$values["areas"] = $banner["areas"];
			
		if($banner["primary_file"] && $banner["primary_file"] != $values["primary_file"])
			@unlink(DOC_ROOT."/ads/".$banner["primary_file"]);
		
		if($banner["secondary_file"] && $banner["secondary_file"] != $values["secondary_file"])
			@unlink(DOC_ROOT."/ads/".$banner["secondary_file"]);
		
		$sql_query = "UPDATE banners SET
					  	name=':name:',
					  	format=':format:',
					  	type=':type:',
					  	url=':url:',
					  	primary_file=':primary_file:',
					  	secondary_file=':secondary_file:',
					  	tag=':tag:',
					  	areas=':areas:'
					  WHERE ban_id=:ban_id:";
		
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
		
	}//else
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	
	load_page("index.php?section=ads&action=edit_banner&theme=".$values["theme"]."&cmp_id=".$values["cmp_id"]."&ban_id=".$values["ban_id"]."&ret_link=".urlencode($ret_link));
	
}//save_banner

function save_banner_areas(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->val("cmp_id");
	$ban_id = $GLOBALS["Get"]->val("ban_id");
	
	$areas = handle_empties(@array_keys($GLOBALS["Get"]->val("areas")));
	
	$sql_query = "UPDATE banners SET areas=',".implode(",",$areas).",'
				  WHERE ban_id=".$ban_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));	
	load_page("index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$ban_id."&ret_link=".urlencode($ret_link)."#areas");
	
}//save_abnner_areas

function save_banner_pages(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->val("cmp_id");
	$ban_id = $GLOBALS["Get"]->val("ban_id");
	
	$site_pages = array_keys(admin_get_pages($theme));	
	$banner_pages = handle_empties(explode(",",$GLOBALS["Get"]->val("banner_pages")));
	
	$all = 1;
	foreach($site_pages as $page){
		if(!in_array($page,$banner_pages)){
			$all = 0;
			break;
		}//if
	}//foreach
	
	if($all)
		$bpages = "all";
	else
		$bpages = ",".implode(",",$banner_pages).",";
		
	$sql_query = "UPDATE banners SET pages='".$bpages."'
				  WHERE ban_id=".$ban_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));	
	load_page("index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$ban_id."&ret_link=".urlencode($ret_link)."#pages");
	
}//save_banner_pages

function save_banner_countries(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->val("cmp_id");
	$ban_id = $GLOBALS["Get"]->val("ban_id");
	
	$countries = array();
	
	$sql_query = "SELECT con_id FROM geo_countries";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$countries[] = $res["con_id"];
	}//foreach
	
	$banner_countries = handle_empties(@array_keys($GLOBALS["Get"]->val("countries")));
	
	$all = 1;
	foreach($countries as $country){
		if(!in_array($country,$banner_countries)){
			$all = 0;
			break;
		}//if
	}//foreach
	
	if($all)
		$bcountries = "all";
	else
		$bcountries = ",".implode(",",$banner_countries).",";
		
	$sql_query = "UPDATE banners SET countries='".$bcountries."'
				  WHERE ban_id=".$ban_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page("index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$ban_id."&ret_link=".urlencode($ret_link)."#countries");
	
}//save_banner_countries

function save_banner_categories(){
	
	admin_validate();
	
	$theme  = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->val("cmp_id");
	$ban_id = $GLOBALS["Get"]->val("ban_id");
	$pro    = $GLOBALS["Get"]->val("pro");
	
	switch($pro){
		case 'classifieds':
			$type = "cls_categories";
			break;
		case 'events':
			$type = "evn_categories";
			break;
		case 'clubs':
			$type = "clb_categories";
			break;
	}//switch
	
	$sql_query = "SELECT ".$type." FROM banners WHERE ban_id=".$ban_id;
	$original_categories = $GLOBALS["DB"]->single($sql_query);
	
	$banner_categories = handle_empties(@array_keys($GLOBALS["Get"]->val("categories")));
	
	include(DOC_ROOT."/themes/".$theme."/system/".$type.".inc.php");
	
	$all = 1;
	while(list($key,) = each($categories)){
		if(!in_array($key,$banner_categories)){
			$all = 0;
			break;
		}//if
	}//while
	
	if($all){
		$bcategories = "all";
	}//if
	else{
		reset($categories);
		$bcategories = $original_categories;
		if($bcategories == "all")
			$bcategories = "";
		while(list($key,) = each($categories)){
			if(in_array($key,$banner_categories)){
				$bcategories = eregi_replace(",".$key."_[^,]+","",$bcategories).",".$key."_all,";
			}//if
			else{
				$bcategories = eregi_replace(",".$key."_all,",",",$bcategories);
			}//else
		}//while
		$bcategories = eregi_replace("[,]+",",",$bcategories);
	}//else
	
	$sql_query = "UPDATE banners SET ".$type."='".$bcategories."'
				  WHERE ban_id=".$ban_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page("index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$ban_id."&ret_link=".urlencode($ret_link)."#".$type);
	
}//save_banner_categories

function save_banner_subcategories(){
	
	admin_validate();
	
	$theme    = $GLOBALS["Get"]->val("theme");
	$cmp_id   = $GLOBALS["Get"]->val("cmp_id");
	$ban_id   = $GLOBALS["Get"]->val("ban_id");
	$pro      = $GLOBALS["Get"]->val("pro");
	$category = $GLOBALS["Get"]->val("category");
	
	switch($pro){
		case 'classifieds':
			$type = "cls_categories";
			break;
		case 'events':
			$type = "evn_categories";
			break;
		case 'clubs':
			$type = "clb_categories";
			break;
	}//switch
	
	$sql_query = "SELECT ".$type." FROM banners WHERE ban_id=".$ban_id;
	$original_categories = $GLOBALS["DB"]->single($sql_query);
	
	$banner_categories = handle_empties(@array_keys($GLOBALS["Get"]->val("subcategories")));
	
	include(DOC_ROOT."/themes/".$theme."/system/".$type.".inc.php");
	
	$all = 1;
	while(list($key,) = each($subcategories[$category])){
		if(!in_array($key,$banner_categories)){
			$all = 0;
			break;
		}//if
	}//while
	
	$bcategories = $original_categories;
	
	if($all){
		if($bcategories != "all"){
			$bcategories = eregi_replace(",".$category."_[^,]+",",",$bcategories).",".$category."_all,";
			$bcategories = eregi_replace("[,]+",",",$bcategories);
		}//if
	}//if
	else{
		if($bcategories == "all"){
			$bcategories = ",";
			while(list($key,) = each($categories)){
				$bcategories.= $key."_all,";
			}//while
		}//if
		reset($subcategories[$category]);
		$bcategories = eregi_replace(",".$category."_all,",",",$bcategories);
		while(list($key,) = each($subcategories[$category])){
			if(in_array($key,$banner_categories)){
				if(!eregi(",".$category."_".$key.",",$bcategories))
					$bcategories.= ",".$category."_".$key.",";				
			}//if
			else{
				$bcategories = eregi_replace(",".$category."_".$key.",",",",$bcategories);
			}//else
		}//while
		$bcategories = eregi_replace("[,]+",",",$bcategories);
	}//else
	
	$sql_query = "UPDATE banners SET ".$type."='".$bcategories."'
				  WHERE ban_id=".$ban_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page("index.php?section=ads&action=edit_banner&theme=".$theme."&cmp_id=".$cmp_id."&ban_id=".$ban_id."&ret_link=".urlencode($ret_link)."#".$type);
	
}//save_banner_subcategories

function delete_banner(){
	
	admin_validate();
	
	$ban_id = $GLOBALS["Get"]->val("ban_id");
	
	$sql_query = "SELECT * FROM banners WHERE ban_id=".$ban_id;
	$banner = $GLOBALS["DB"]->row($sql_query);
	
	if($banner["primary_file"])
		@unlink(DOC_ROOT."/ads/".$banner["primary_file"]);
		
	if($banner["secondary_file"])
		@unlink(DOC_ROOT."/ads/".$banner["secondary_file"]);
		
	$sql_query = "DELETE FROM banners WHERE ban_id=".$ban_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//delete_banner

function pause_campaign(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->type_val("cmp_id","integer");
	
	$sql_query = "UPDATE campaigns SET `status`='0' WHERE cmp_id=".$cmp_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//pause_campaign

function resume_campaign(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->type_val("cmp_id","integer");
	
	$sql_query = "UPDATE campaigns SET `status`='1' WHERE cmp_id=".$cmp_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//resume_campaign

function delete_campaign(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$cmp_id = $GLOBALS["Get"]->type_val("cmp_id","integer");
	
	$sql_query = "SELECT * FROM banners WHERE cmp_id=".$cmp_id;
	$result = $GLOBALS["DB"]->result($sql_query);
	
	foreach($result as $res){
		if($res["primary_file"])
			@unlink(DOC_ROOT."/ads/".$res["primary_file"]);
		if($res["secondary_file"])
			@unlink(DOC_ROOT."/ads/".$res["secondary_file"]);
	}//foreach
	
	$sql_query = "DELETE FROM banners WHERE cmp_id=".$cmp_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query = "DELETE FROM campaigns WHERE cmp_id=".$cmp_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//delete_campaign

function edit_format(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$format = $GLOBALS["Get"]->type_val("format","integer");
	
	if($format){
		include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
		$data["values"] = $ads["formats"][$format];
	}//if
	else{
		$vals = array("name","width","height");
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
	}//else
	
	$data["theme"] = $theme;
	$data["format"] = $format;
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["title_id"] = "3";	$data["link"] = "index.php?section=ads&theme=".$theme;
	
	$layout[0] = "edit_format";
	
	admin_output($layout,$data);
	
}//edit_format

function save_format(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$format = $GLOBALS["Get"]->type_val("format","integer");
	
	$vals = array("name","width","height");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["name"]) || empty($values["width"]) || empty($values["height"])){
		$GLOBALS["aerr_id"] = 10;
		edit_format();
		exit;
	}//if
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	if(empty($format))
		$format = @max(array_keys($ads["formats"]))+1;
		
	$ads["formats"][$format]["name"]   = $values["name"];
	$ads["formats"][$format]["width"]  = $values["width"];
	$ads["formats"][$format]["height"] = $values["height"];
	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$ads",$ads);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//save_format

function delete_format(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$format = $GLOBALS["Get"]->type_val("format","integer");
	
	if($format){
		include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
		unset($ads["formats"][$format]);
	}//if
	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$ads",$ads);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//delete_format

function edit_area(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$area = $GLOBALS["Get"]->type_val("area","integer");
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	if($area){		
		$data["values"]["name"] = $ads["areas"][$area]["name"];
		$formats = handle_empties(explode(",",$ads["areas"][$area]["formats"]));
		foreach($formats as $format){
			$data["values"]["formats"][$format] = 1;
		}//foreach
	}//if
	else{
		$vals = array("name","formats");
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
	}//else
	
	$data["formats"] = array();
	while(list($key,$val) = each($ads["formats"])){
		$data["formats"][$key] = $val["name"];
	}//while
	
	$data["theme"] = $theme;
	$data["area"] = $area;
	$data["section"] = "getpaid";
	$data["subsection"] = 2;
	$data["title_id"] = "3";	$data["link"] = "index.php?section=ads&theme=".$theme;
	
	$layout[0] = "edit_area";
	
	admin_output($layout,$data);
	
}//edit_area

function save_area(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$area  = $GLOBALS["Get"]->type_val("area","integer");
	
	$values["name"] = $GLOBALS["Get"]->val("name");
	$values["formats"] = handle_empties(@array_keys($GLOBALS["Get"]->val("formats")));
	
	if(empty($values["name"]) || !count($values["formats"])){
		$GLOBALS["aerr_id"] = 10;
		edit_area();
		exit;
	}//if
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	if(empty($area))
		$area = @max(array_keys($ads["areas"]))+1;
		
	$ads["areas"][$area]["name"]     = $values["name"];
	$ads["areas"][$area]["formats"]  = implode(",",$values["formats"]);
	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$ads",$ads);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//save_area

function delete_area(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$area = $GLOBALS["Get"]->type_val("area","integer");
	
	if($area){
		include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
		unset($ads["areas"][$area]);
	}//if
	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$ads",$ads);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=ads&theme=".$theme);
	
}//delete_area

function campaign_type($type){
	
	switch($type){
		case 'external':
			return "External";
		case 'ppc':
			return 'PPC';
		case 'impressions':
			return "Impressions";
		case 'subscription':
			return "Subscription";
	}//switch
	
}//campaign_type

function campaign_cost($type,$price,$impressions,$clicks,$start){
	
	if($type == "ppc"){
		$cost = $price*$clicks;
	}//if
	elseif($type == "impressions"){
		$cost = $price*($impressions/1000);
	}//elseif
	elseif($type == "subscription"){
		$months = date("n")-date("n",$start)+((date("Y")-date("Y",$start)) * 12);
		if(date("j") < date("j",$start))
			$months++;
		$cost = $price*$months;
	}//elseif
	else{
		$cost = "N/A";
	}//else
	
	return $cost;
	
}//campaign_cost

function campaign_price($type,$price){
	
	switch($type){
		case 'external':
			return "N/A";
		case 'ppc':
			return $price." per click";
		case 'impressions':
			return $price." per 1,000 impressions";
		case 'subscription':
			return $price." per month";
	}//switch
	
}//campaign_price

function banner_format($theme,$format){
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	return $ads["formats"][$format]["name"]." (".$ads["formats"][$format]["width"]." x ".$ads["formats"][$format]["height"].")";
	
}//banner_format

function possible_banner_areas($theme,$format){
	
	include(DOC_ROOT."/themes/".$theme."/modules/ads.inc.php");
	
	$areas = array();
	
	while(list($key,$val) = each($ads["areas"])){
		$formats = handle_empties(explode(",",$val["formats"]));
		if(in_array($format,$formats))
			$areas[] = $key;
	}//while
	
	return $areas;
	
}//possible_banner_areas
						
?>