<?
session_start();

include("../includes/common.php");


$currency = trim($_GET['currency']=="")?trim($_POST['currency']):trim($_GET['currency']);
//echo $currency;

if(!is_numeric($currency)){
	$error = urlencode("Ошибка при передаче данных.");
	header("Location: $SITE_URL/error.php?error=$error");
	exit;
}

$curr_row = mysql_fetch_object(mysql_query("select * from ex_currencies where id = $currency and status = 1"));
if($curr_row->id==""){
	$error = urlencode("Ошибка при передаче данных.");
	header("Location: $SITE_URL/error.php?error=$error");
	exit;
}

$res = mysql_query("select ex_currencies.*, ex_exchanges.course, ex_exchanges.min_amount, ex_exchanges.max_amount, ex_exchanges.init_value_form, ex_exchanges.init_value_in, ex_exchanges.init_value_out from ex_currencies inner join ex_exchanges on ex_currencies.id = ex_exchanges.ex_currency1_id where ex_exchanges.ex_currency2_id=$currency and ex_exchanges.status=1 and ex_currencies.status=1 order by ex_currencies.position asc");

//$info = "Для обмена укажите сумму, которую вы хотите отдать или сумму, которую вы хотите получить. Система автоматически произведет расчет в соответствие с текущим курсом.";
//echo MainTop("ExChanger. Универсальная обменная система. Быстрое пополнение WebMoney, ЯндексДенег, E-Gold, E-Port",$info);

$js = "";
$init = "";
while($row = mysql_fetch_object($res)){
	$course = GetCourseByExID($row->id,$curr_row->id);
	$js .=  "inP['id_$row->id']=".pow(10,$row->precision).";\n";	
	$js .=  "courses['id_$row->id']=".($row->course+0).";\n";	
//	$js .=  "init_value_form['id_$row->id']='".($row->init_value_form)."';\n";	
//	$js .=  "init_value_in['id_$row->id']=".($row->init_value_in+0).";\n";	
//	$js .=  "init_value_out['id_$row->id']=".($row->init_value_out+0).";\n";	
	$js .=  "min_amt['id_$row->id']=".($row->min_amount+0).";\n";	
	$js .=  "max_amt['id_$row->id']=".($row->max_amount+0).";\n";	
	
	if($row->init_value_form=='in'){
		$init .=  "document.forms['id_".$row->id."'].amount_in.value='".($row->init_value_in+0)."';\n";
		$init .=  "in_out('id_".$row->id."', '$row->id', '$row->code',true);\n";
	}
	else{
		$init .=  "document.forms['id_".$row->id."'].out.value='".($row->init_value_out+0)."';\n";
		$init .=  "out_in('id_".$row->id."', '$row->id', '$row->code');\n";
	}	
}





$currs = array();
$res = mysql_query("select ex_currencies.*, ex_exchanges.course from ex_currencies inner join ex_exchanges on ex_currencies.id = ex_exchanges.ex_currency1_id where ex_exchanges.ex_currency2_id=$currency and ex_exchanges.status=1 and ex_currencies.status=1 order by ex_currencies.position asc");
while($row = mysql_fetch_object($res)){
	$bgcolor = $bgcolor=="#F0EFEF"?"#E1E1E1":"#F0EFEF";
	$currs[] = array("bgcolor" => $bgcolor,
									"id" => $row->id,
									"name" => $row->name,
									"code" => $row->code,							
									"ex_course" => number_format($row->course,$SETTINGS['course_precision']),							
									"ex_course_back" => number_format(1/$row->course,$SETTINGS['course_precision'])							
									);

}

$smarty->assign("curr_name", $curr_row->name);
$smarty->assign("curr_id", $curr_row->id);

$smarty->assign("currs", $currs);
$smarty->assign("js", $js);
$smarty->assign("init", $init);

$smarty->assign("curr_amount", $curr_row->metal_id>0?round($curr_row->amount*GetMetalPrice($curr_row->metal_id),2):$curr_row->amount);
$smarty->assign("this_perc_comission",($curr_row->comission_perc+0));
$smarty->assign("this_fix_comission",($curr_row->comission_fix+0));

$smarty->assign("gold_price" ,GetMetalPriceByName("GOLD"));
$smarty->assign("silver_price",GetMetalPriceByName("SILVER"));
$smarty->assign("platinum_price",GetMetalPriceByName("PLATINUM"));
$smarty->assign("palladium_price",GetMetalPriceByName("PALLADIUM"));

$smarty->assign("code_to", $curr_row->code);
$smarty->assign("who_pays_comission_to", $curr_row->who_pay_comission);
$smarty->assign("outP", pow(10,$curr_row->precision));


$smarty->assign("title", $SETTINGS['site_name']." Универсальная обменная система. Быстрое пополнение WebMoney, ЯндексДенег, E-Gold, E-Port");
$smarty->assign("info", "Для обмена укажите сумму, которую вы хотите 
отдать или сумму, которую вы хотите получить. Система автоматически 
произведет расчет в соответствие с текущим курсом. ");
$smarty->assign("ex_currencies", $ex_currencies);
$smarty->assign("main_class", "header1");
$smarty->assign("money_cources", get_currency_cources());
$smarty->assign("metal_cources", get_metal_cources());

$smarty->assign("onLoad", "Init()");


$smarty->display("smartex_currency.tpl");
?>