<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>FatRat web interface</title>
<link rel="icon" href="/favicon.png" type="image/png"/>
<link rel="stylesheet" type="text/css" href="/style.css"/>
<script type="text/javascript" src="/filedialog/api.js"></script>

<script type="text/javascript"><!--
function checkedLimiting()
{
	box = document.getElementById('limitTransfers');
	boxUpAsDown = document.getElementById('upAsDown');
	document.getElementById('downTransferLimit').disabled = !box.checked;
	document.getElementById('upTransferLimit').disabled = !box.checked || boxUpAsDown.checked;
	boxUpAsDown.disabled = !box.checked;
}
//--></script>
</head>

<?qs
	var queue;
	if(GET.queue)
	{
		for(var i=0;i<QUEUES.length;i++)
		{
			if(QUEUES[i].uuid == GET.queue)
			{
				queue = QUEUES[i];
				break;
			}
		}
	}
	
	if(POST.create && !queue)
		queue = addQueue();
	
	if((POST.save || POST.create) && queue)
	{
		queue.name = POST.name;
		queue.setSpeedLimits(POST.downLimit*1024, POST.upLimit*1024);
		
		if(POST.limitTransfers)
		{
			if(!POST.downTransferLimit)
				POST.downTransferLimit = 1;
			if(!POST.upTransferLimit)
				POST.upTransferLimit = 1;
			queue.setTransferLimits(POST.downTransferLimit, POST.upTransferLimit);
		}
		else
			queue.setTransferLimits(-1, -1);
		queue.upAsDown = POST.upAsDown != undefined;
		queue.defaultDirectory = POST.defaultDirectory;
		queue.moveDirectory = POST.moveDirectory;
		
		if(POST.save)
			print('<p style="color: green">Saved.</p>');
		else
			print('<p style="color: green">Created.</p>');
	}
?>

<body onload="javascript:checkedLimiting();">

<p style="text-align: center"><b>Queue details</b></p>

<form method="post" action="?queue=<?qs if(queue) print(queue.uuid);?>">

<table class="transfers">
<tr>
	<td>Name</td>
	<td>
		<input type="text" name="name" value="<?qs if(queue) print(queue.name); ?>"/>
	</td>
</tr>
<tr>
	<td>Destination directory</td>
	<td>
		<input type="text" name="defaultDirectory" id="defaultDirectory" style="width: 80%" value="<?qs if(queue) print(queue.defaultDirectory); ?>"/>
		<input type="button" value="..." onclick="javascript: dirDialog('defaultDirectory')"/>
	</td>
</tr>
<tr>
	<td>Move transfers after completion</td>
	<td>
		<input type="text" name="moveDirectory" id="moveDirectory" style="width: 80%" value="<?qs if(queue) print(queue.moveDirectory); ?>"/>
		<input type="button" value="..." onclick="javascript: dirDialog('moveDirectory')"/>
		(directory, empty = disabled)
	</td>
</tr>
<tr>
	<td>Max. download speed</td>
	<td>
		<?qs
			var speed;
			if(queue)
				speed = queue.speedLimits();
		?>
		<input type="text" size="4" name="downLimit" value="<?qs if(queue) print(speed.down/1024); else print(0); ?>"/> kB/s
	</td>
</tr>
<tr>
	<td>Max. upload speed</td>
	<td>
		<input type="text" size="4" name="upLimit" value="<?qs if(queue) print(speed.up/1024); else print(0); ?>"/> kB/s
	</td>
</tr>

<tr>
	<?qs
		var transfers;
		if(queue)
			transfers = queue.transferLimits();
	?>
	
	<td>&nbsp;</td>
	<td>
		<label><input type="checkbox" name="limitTransfers" id="limitTransfers"
		<?qs
		if( (transfers && (transfers.down != -1 || transfers.up != -1)) || !queue)
			print('checked="checked"');
		?>
		onclick="javascript:checkedLimiting()" />
		Limit active transfer count</label>
	</td>
</tr>

<tr>
	<td>Max. active downloads</td>
	<td>
		<input type="text" size="4" id="downTransferLimit" name="downTransferLimit" value="<?qs if(queue && transfers.down > 0) print(transfers.down); else print(1); ?>"/>
	</td>
</tr>
<tr>
	<td>Max. active uploads</td>
	<td>
		<input type="text" size="4" id="upTransferLimit" name="upTransferLimit" value="<?qs if(queue && transfers.up > 0) print(transfers.up); else print(1); ?>"/>
		<label><input type="checkbox" id="upAsDown" onclick="javascript:checkedLimiting()" name="upAsDown" <?qs if(queue && queue.upAsDown) print('checked="checked"'); ?> />Count as downloads</label>
	</td>
</tr>


</table>

<p style="text-align: center">
	<?qs
	if(queue)
		print('<input type="submit" name="save" value="Apply"/>');
	else
		print('<input type="submit" name="create" value="Create"/>');
	?>
	<input type="button" value="Close" onclick="javascript:window.close()"/>
</p>

</form>

</body>
</html>
