<?
session_start();
include("../includes/common.php");

//include($_SERVER["DOCUMENT_ROOT"]."/inc/funcs.php");


function Close(){
	$res = "<HTML><HEAD><TITLE>Ошибка при передаче данных</TITLE><BODY OnLoad=\"window.close();\"></BODY></HTML>";
	echo $res;
	exit;
}



$id = trim($_GET['id']=="")?trim($_POST['id']):trim($_GET['id']);
if(!is_numeric($id)){
	Close();	
}

$curr_row = mysql_fetch_object(mysql_query("select * from ex_currencies where id = $id and status = 1"));
if($curr_row->id==""){
	Close();
}
$title = "Курсы обмена $row->name на WebMoney, ЯндексДеньги, E-Port, E-Gold";

$res = mysql_query("select ex_currencies.*, ex_exchanges.sys_comission_perc,ex_exchanges.sys_comission_fix,ex_exchanges.max_amount from ex_currencies inner join ex_exchanges on ex_currencies.id = ex_exchanges.ex_currency1_id where ex_exchanges.ex_currency2_id=$id and ex_exchanges.status=1 and ex_currencies.status=1 order by ex_currencies.position asc");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE><?=$title?></TITLE>
<script>
var sys_perc_arr = new Array();
var sys_fix_arr = new Array();
var max_amt = new Array();
var courses = new Array();
var inP = new Array();

var this_perc_comission = <?=($curr_row->comission_perc+0)?>;
var this_fix_comission = <?=($curr_row->comission_fix+0)?>;

<?
while($row = mysql_fetch_object($res)){
	$course = GetCourseByExID($row->id,$curr_row->id);
	echo "inP[\"$row->id\"]=".pow(10,$row->precision).";\n";	
	echo "courses[\"$row->id\"]=".($course+0).";\n";	
	echo "sys_perc_arr[\"$row->id\"]=".($row->sys_comission_perc+0).";\n";	
	echo "sys_fix_arr[\"$row->id\"]=".($row->sys_comission_fix+0).";\n";	
	echo "max_amt[\"$row->id\"]=".($row->max_amount+0).";\n";	
}
?>
var gold_price = <?=GetMetalPriceByName("GOLD")?>;
var silver_price = <?=GetMetalPriceByName("SILVER")?>;
var platinum_price= <?=GetMetalPriceByName("PLATINUM")?>;
var palladium_price = <?=GetMetalPriceByName("PALLADIUM")?>;
var curr_amount = <?=$curr_row->metal_id>0?round($curr_row->amount*GetMetalPrice($curr_row->metal_id),2):$curr_row->amount?>;

var code_to = '<?=$curr_row->code?>';
var who_pays_comission_to = '<?=$curr_row->who_pay_comission?>';
var outs_per_1in = 1;
//var inP = Math.pow(10,2);
var outP = Math.pow(10,<?=$curr_row->precision?>);
var outAddon = 0;

</script>

<SCRIPT language=javascript src="./inc/funcs_js.php">
</script>

<SCRIPT language=javascript>

function Init(){
<?
$res = mysql_query("select ex_currencies.*, ex_exchanges.sys_comission_perc,ex_exchanges.sys_comission_fix,ex_exchanges.max_amount from ex_currencies inner join ex_exchanges on ex_currencies.id = ex_exchanges.ex_currency1_id where ex_exchanges.ex_currency2_id=$id and ex_exchanges.status=1 and ex_currencies.status=1 order by ex_currencies.position asc");
while($row = mysql_fetch_object($res)){
	if(GetCourseByExID($row->id,$curr_row->id) > 1){
		echo "document.forms('$row->id').all(\"amount_in\").value=1;\n";	
		echo "in_out('$row->id','$row->code',true);\n";	
	}
	else{
		echo "document.forms('$row->id').all(\"out\").value=1;\n";	
		echo "out_in('$row->id','$row->code',true);\n";	
	}
}
?>
}

function Exchange(id,name){
	if(document.forms(id).all("amount_in").value > max_amt[id]){
		alert("Максимально допустимая сумма "+name+" равна "+max_amt[id]);
		return false;
	}
	
	if(document.forms(id).all("amount_in").value==0 || document.forms(id).all("out").value==0){
		alert("Укажите суммы обмена, пожалуйста!");
		return false;
	}
	if(document.forms(id).all("out").value > curr_amount){
		alert("На данный момент мы Вам можем предложить только "+curr_amount+" <?=$curr_row->name?>");
		return false;
	}

	if(!confirm("Вы отдаете: "+document.forms(id).all("amount_in").value+" "+name+"\nВы получаете: "+document.forms(id).all("out").value+" <?=$curr_row->name?>\n\nСуммы указаны с учетом всех комиссий, в т.ч. и за перевод.\n\nПРОДОЛЖИТЬ?")){
		return false;
	}

	
	var val = document.forms(id).all("amount_in").value;
	window.opener.location.href="./exchange.php?from="+id+"&to=<?=$curr_row->id?>&amount_in="+val;
	self.close();
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<link href="/style.css" rel="stylesheet" type="text/css">
</HEAD>

<BODY bgcolor="#FFFFFF" leftmargin="0" topmargin="0" onLoad="Init();">
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr> 
    <td width="39%" height="35" align="left" bgcolor="#f4f0e1" class="tableheadpad">Отдадите</td>
    <td width="39%" align="left" bgcolor="#f4f0e1" class="tableheadpad">Получите</td>
    <td width="22%" align="left" bgcolor="#f4f0e1" >&nbsp;</td>
  </tr>
<?
$res = mysql_query("select ex_currencies.* from ex_currencies inner join ex_exchanges on ex_currencies.id = ex_exchanges.ex_currency1_id where ex_exchanges.ex_currency2_id=$id and ex_exchanges.status=1 and ex_currencies.status=1 order by ex_currencies.position asc");
while($row = mysql_fetch_object($res)){
	$bgcolor = $bgcolor=="#f4f0e1"?"#fefbf0":"#f4f0e1";
	

?>
<form name="<?=$row->id?>" action="./exchange.php" method="POST" onSubmit>  
<tr class="celpad1" bgcolor="<?=$bgcolor?>"> 
    <td height="30" align="left" > 
      <input name="amount_in" type="text" class="cells2" size="5"  onchange="in_out('<?=$row->id?>','<?=$row->code?>')" onKeyUp="in_out('<?=$row->id?>','<?=$row->code?>')" onfocusout="in_out('<?=$row->id?>','<?=$row->code?>',true)" onactivate="in_out('<?=$row->id?>','<?=$row->code?>',true)" ondeactivate="in_out('<?=$row->id?>','<?=$row->code?>',true)">
      <?=$row->name?> </td>
    <td align="left" > <input name="out" type="text" class="cells2" size="5" onChange="out_in('<?=$row->id?>','<?=$row->code?>')" onKeyUp="out_in('<?=$row->id?>','<?=$row->code?>')" onfocusout="out_in('<?=$row->id?>','<?=$row->code?>',true)" onactivate="out_in('<?=$row->id?>','<?=$row->code?>',true)" ondeactivate="out_in('<?=$row->id?>','<?=$row->code?>',true)">
      <?=$curr_row->name?></td>
    <td ><a href="#" onClick="Exchange('<?=$row->id?>','<?=$row->name?>');return false;" title="Обменять <?=$row->name?> на <?=$curr_row->name?>"><input type="image" src="images/changebut.gif" width="55" height="25"></a></td>
  </tr>
</form>
<?
}
?>
</table>
<center><a href="#" onClick="window.close();" class="celpad1" style="color:#c4c0a1" title="Закрыть окно курсов">Закрыть окно</a></center>
</BODY>
</HTML>
