<?php
include('../includes/common.php');

$id = intval($_GET['id'])<0 ? -1 : intval($_GET['id']);
$pid = intval($_GET['pid'])<0 ? -1 : intval($_GET['pid']);

if($id <0){
	header("Location: http://".$SETTINGS['site_url']);
	exit;
}
if($pid <0){
	header("Location: http://".$SETTINGS['site_url']);
	exit;
}

$SITE_URL = ltrim($SITE_URL, "http://");

$table = "banners";

if($id == 1){

$query_select = "
select
  ex_currencies.*, currencies.name as curr, currencies.code as curr_code
from
(ex_currencies left outer join currencies on currencies.id = ex_currencies.currency_id)
WHERE ex_currencies.status=1 AND ex_currencies.metal_id<>0
GROUP BY ex_currencies.id ORDER BY id";

$res_select = mysql_query($query_select);


$select_out = "";
$select_in = "";
$itter = 0;
while ($row = mysql_fetch_object($res_select)){
	if($itter == 0){
		$select_out .= "<option value='".$row->id."' selected>".$row->name."</option>";
		$select_in .= "<option value='".$row->id."'>".$row->name."</option>";
	}elseif($itter == 1){
		$select_out .= "<option value='".$row->id."'>".$row->name."</option>";
		$select_in .= "<option value='".$row->id."' selected>".$row->name."</option>";
	}else{
		$select_out .= "<option value='".$row->id."'>".$row->name."</option>";
		$select_in .= "<option value='".$row->id."'>".$row->name."</option>";
	}
	$itter++;
}

$query = "SELECT * FROM $table where id = ".$id." LIMIT 1";
$row = mysql_fetch_object(mysql_query($query));
$html = stripslashes($row->html_code);
$html = str_replace("[[SITE_URL]]", $SETTINGS['site_url'], $html);
$html = str_replace("[[SITE_NAME]]", $SETTINGS['site_name'], $html);
$html = str_replace("[[SELECT_OUT]]", $select_out, $html);
$html = str_replace("[[SELECT_IN]]", $select_in, $html);
$html = str_replace("[[PARTNER_ID]]", $pid, $html);
$html = addslashes($html);
$html = explode("\n", $html);

header("Content-type: text/javascript");
foreach($html as $s)
{
	echo "document.write('".preg_replace("(\r\n|\n|\r)", "", $s)."');\n";
}

}// END banner IF id == 1
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
elseif($id == 2){

$codes_arr = Array(
		"WMR"=>1,
		"WMZ"=>2,
		"WME"=>3,
		"WMU"=>4,
		"YMRMAN"=>5,
		"EGOLD"=>7,
		"ESILVER"=>8,
		"EPLATINUM"=>9,
		"EPALLADIUM"=>10,
		"MEGABIZ"=>38,
		"SMPIN"=>39,
		"MM"=>40
		);


$codes = $_GET['codes'];
$codes_list = explode(",",$codes);

$tmp_html = "<tr height=20><td align=\"middle\" width=50><b>[[PURSE_FROM_DECIMAL]]</b></td><td width=100>[[PURSE_FROM]]</td><td><form name=\"smartex\" method=\"post\" action=\"http://[[SITE_URL]]/get_currency.php\"><input type=\"hidden\" name=\"partner\" value=\"[[PARTNER_ID]]\"/><input type=\"hidden\" name=\"curr_out\" value=\"[[CURR_OUT]]\" /><input type=\"hidden\" name=\"curr_in\" value=\"[[CURR_IN]]\" /><input type=image src=http://[[SITE_URL]]/imgs/arrow_green.gif alt=\"Поменять [[BUTTON_ALT]]\" /></form></td><td align=\"middle\" width=50><b>[[PURSE_TO_DECIMAL]]</b></td><td width=150>[[PURSE_TO]]</td><td align=center width=170>[[PURSE_TO_AMOUNT]]&nbsp;[[PURSE_TO]]</td></tr><tr><td colspan=6 bgColor=#dcdcdc></td></tr>";

foreach($codes_list as $k => $v){

	list($curr2, $curr1) = explode("-",$v);

$sql = "select 
	  ex_exchanges.*, 
	  ex_currencies.name as curr2,
	  curr.name as curr1
	from
	  (ex_exchanges left outer join ex_currencies curr on curr.id = ex_exchanges.ex_currency1_id)
	  left outer join ex_currencies on ex_currencies.id = ex_exchanges.ex_currency2_id
	where 
	  ex_currency1_id=$codes_arr[$curr1] and ex_currency2_id=$codes_arr[$curr2]";
	$row = mysql_fetch_object(mysql_query($sql));

	$content = $tmp_html;
	$content = str_replace("[[SITE_URL]]", $SETTINGS['site_url'], $content);
	$content = str_replace("[[PURSE_FROM]]", $row->curr1, $content);
	$content = str_replace("[[CURR_OUT]]", $row->ex_currency1_id, $content);
	$content = str_replace("[[PURSE_TO]]", $row->curr2, $content);
	$content = str_replace("[[CURR_IN]]", $row->ex_currency2_id, $content);
	$content = str_replace("[[BUTTON_ALT]]", $row->curr1." на ".$row->curr2, $content);

	$cource = CountOut(1, $row->ex_currency1_id, $row->ex_currency2_id);
	if($cource['out_amount'] > 1){
		$content = str_replace("[[PURSE_FROM_DECIMAL]]", '1', $content);
		$content = str_replace("[[PURSE_TO_DECIMAL]]", $cource['out_amount'], $content);
	}else{
		$cource = CountIn($cource['out_amount'], $row->ex_currency2_id, $row->ex_currency1_id);
		$content = str_replace("[[PURSE_FROM_DECIMAL]]", $cource['out_amount'], $content);
		$content = str_replace("[[PURSE_TO_DECIMAL]]", '1', $content);
	}
	$sql_amount = "SELECT amount FROM ex_currencies WHERE id=".$row->ex_currency2_id;
	$row_amount = mysql_fetch_object(mysql_query($sql_amount));
	$content = str_replace("[[PURSE_TO_AMOUNT]]", round($row_amount->amount, 2), $content);
	$content = str_replace("[[PARTNER_ID]]", $pid, $content);
	$dump .= $content;
}
$query = "SELECT * FROM $table where id = ".$id." LIMIT 1";
$row = mysql_fetch_object(mysql_query($query));
$html = stripslashes($row->html_code);
$html = str_replace("_CONTENT_", $dump, $html);
$html = addslashes($html);
//echo $html;


$html = explode("\n", $html);

header("Content-type: text/javascript");
foreach($html as $s)
{
	echo "document.write('".preg_replace("(\r\n|\n|\r)", "", $s)."');\n";
}


}// END banner IF id == 2
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
elseif($id == 3){

$query_select = "
select
  ex_currencies.*, currencies.name as curr, currencies.code as curr_code
from
(ex_currencies left outer join currencies on currencies.id = ex_currencies.currency_id)
WHERE ex_currencies.status=1 AND ex_currencies.metal_id<>0
GROUP BY ex_currencies.id ORDER BY id";

$res_select = mysql_query($query_select);


$select_out = "";
$select_in = "";
$itter = 0;
while ($row = mysql_fetch_object($res_select)){
	if($itter == 0){
		$select_out .= "<option value='".$row->id."' selected>".$row->name."</option>";
		$select_in .= "<option value='".$row->id."'>".$row->name."</option>";
	}elseif($itter == 1){
		$select_out .= "<option value='".$row->id."'>".$row->name."</option>";
		$select_in .= "<option value='".$row->id."' selected>".$row->name."</option>";
	}else{
		$select_out .= "<option value='".$row->id."'>".$row->name."</option>";
		$select_in .= "<option value='".$row->id."'>".$row->name."</option>";
	}
	$itter++;
}


$query = "SELECT * FROM $table where id = ".$id." LIMIT 1";
$row = mysql_fetch_object(mysql_query($query));
$html = stripslashes($row->html_code);
$html = str_replace("[[SITE_URL]]", $SETTINGS['site_url'], $html);
$html = str_replace("[[SITE_NAME]]", $SETTINGS['site_name'], $html);
$html = str_replace("[[SELECT_OUT]]", $select_out, $html);
$html = str_replace("[[SELECT_IN]]", $select_in, $html);
$html = str_replace("[[PARTNER_ID]]", $pid, $html);

$sql_amount = "SELECT amount FROM ex_currencies WHERE id=1";
$row_amount = mysql_fetch_object(mysql_query($sql_amount));
$html = str_replace("[[CURR_OUT_AMOUNT]]", round($row_amount->amount, 2), $html);
$sql_amount = "SELECT amount FROM ex_currencies WHERE id=2";
$row_amount = mysql_fetch_object(mysql_query($sql_amount));
$html = str_replace("[[CURR_IN_AMOUNT]]", round($row_amount->amount, 2), $html);


$html = addslashes($html);
$html = explode("\n", $html);

header("Content-type: text/javascript");
foreach($html as $s)
{
	echo "document.write('".preg_replace("(\r\n|\n|\r)", "", $s)."');\n";
}

}// END banner IF id == 3


?>