<?php
/**
* @version 1.0.0
* @package RSFiles! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/


defined( '_JEXEC' ) or die( 'Restricted access' ); 
$session = & JFactory::getSession();
$writable = is_writable($this->rsfilesConfig['download_folder'].DS.urldecode($this->currentfolder)) ? JText::_('RSFL_WRITABLE') : JText::_('RSFL_NOT_WRITABLE'); 

echo JText::sprintf('RSFL_UPLOAD_TO',empty($this->currentfolder) ? JText::_('RSFL_HOME') : urldecode($this->currentfolder)).'<br/>';
echo JText::sprintf('RSFL_PATH_IS',$writable);
?>
<br/><br/>
<?php if (!$this->rsfilesConfig['enable_flash_upload']) { ?>	
<script type="text/javascript">
function rsfl_add_new(id)
{
	var newid = Math.random();

	field = document.getElementById("files");
	
	var spanid = 'f'+newid;
	var spanTag = document.createElement("span");   
	spanTag.id = spanid;
	spanTag.innerHTML = '<br/>';
	
	input = document.createElement("input");
	input.type = 'file';
	input.name = 'files[]';
	input.size = '40';
	
	a1 = document.createElement("a");
	a1.setAttribute('onclick', 'rsfl_add_new('+newid+')');
	img1 = document.createElement("img");
	img1.setAttribute('src', '<?php echo JURI::root().'components/com_rsfiles/images/icons/add.png'; ?>');
	a1.appendChild(img1);
	
	a2 = document.createElement("a");
	a2.setAttribute('onclick', 'rsfl_remove('+newid+')');
	img2 = document.createElement("img");
	img2.setAttribute('src', '<?php echo JURI::root().'components/com_rsfiles/images/icons/remove.png'; ?>');
	a2.appendChild(img2);
	
	field.appendChild(spanTag);
	
	span = document.getElementById(spanid);
	span.appendChild(input);
	span.appendChild(a1);
	span.appendChild(a2);
}

function rsfl_remove(id)
{
	if(id != 1) 
	{
		var d = document.getElementById('f'+id);
		d.parentNode.removeChild(d); 
	}
}

function rsfl_back()
{
	document.location = '<?php echo JRoute::_('index.php?option=com_rsfiles&view=files'.(empty($this->currentfolder) ? '' : '&folder='.addslashes($this->currentfolder)).$this->itemid); ?>';
}

</script>
<?php } else { ?>
<script type="text/javascript">
function rsfl_back()
{
	document.location = '<?php echo JRoute::_(JURI::root().'index.php?option=com_rsfiles&view=files'.(empty($this->currentfolder) ? '' : '&folder='.addslashes($this->currentfolder)).$this->itemid); ?>';
}
</script>
<script type="text/javascript">
	var upload;

	window.onload = function() {
		upload = new SWFUpload({
			// Backend Settings
			upload_url: "index.php",
			post_params: 
			{
					"option" : "com_rsfiles",
					"controller" : "files",
					"task" : "uploadflash",
					"folder" : "<?php echo $this->currentfolder; ?>",
					"<?php echo $session->getName(); ?>" : "<?php echo $session->getId(); ?>",
					"format" : "raw"
			},

			// File Upload Settings
			file_size_limit : "<?php echo $this->rsfilesConfig['max_upl_size']*1024; ?> B",	// maximum size in B
			file_types : "<?php echo $this->extensions; ?>",
			file_types_description : "Files",
			file_upload_limit : "10",
			file_queue_limit : "0",
			
			file_queued_handler : fileQueued,
			file_queue_error_handler : fileQueueError,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,


			// Button Settings
			button_image_url : "<?php echo JURI::root(); ?>components/com_rsfiles/assets/uploader/upload.png",
			button_placeholder_id : "rsf_upload",
			button_width: 61,
			button_height: 22,
			
			// Flash Settings
			flash_url : "<?php echo JURI::root(); ?>components/com_rsfiles/assets/uploader/swfupload.swf",
			

			custom_settings : {
				progressTarget : "rsf_upload_process",
				cancelButtonId : "btnCancel"
			},
			
			// Debug Settings
			debug: false
		});
	 }
</script>
<?php } ?>


<div id="rse_loading" class="rse_loading" style="display: none; height: 100%;"></div>
<form action="<?php echo JRoute::_('index.php?option=com_rsfiles&task=upload'.$this->itemid); ?>" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
<?php if (!$this->rsfilesConfig['enable_flash_upload']) { ?>	
	<div style="text-align:right"><button type="submit" onclick="document.getElementById('rse_loading').style.display = 'block';"><?php echo JText::_('RSFL_UPLOAD'); ?></button> <button onclick="rsfl_back();" type="button"><?php echo JText::_('RSFL_CANCEL_BTN'); ?></button></div>
	<table cellspacing="0" cellpadding="0" border="0" width="100%">
		<tr>
			<td valign="top">
				<table  class="adminform">
					<tr>
						<td>
							<label for="upload">
								<?php echo JText::_( 'RSFL_FILE' ).':'; ?>
							</label>
						</td>
						<td>
							<span id="files">
								<span id="f">
									<input type="file" name="files[]" size="40"/> <a onclick="rsfl_add_new(1);"><img src="<?php echo JURI::root().'components/com_rsfiles/images/icons/add.png'; ?>" /></a>
								</span>
							</span>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
<?php } else { ?>	
	<div class="fieldset flash" id="rsf_upload_process">
		<span class="legend"><?php echo JText::_('RSFL_UPLOAD_FILES'); ?></span>
	</div>
	<div style="padding-left: 5px;">
		<span id="rsf_upload"></span>
		<input type="button" value="<?php echo JText::_('RSFL_START_UPLOAD'); ?>" onclick="upload.startUpload();" style="margin-left: 2px; font-size: 8pt; height: 22px;" />
		<input id="btnCancel" type="button" value="<?php echo JText::_('RSFL_CANCEL_UPLOADS'); ?>" onclick="cancelQueue(upload);" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
		<input id="back" type="button" value="<?php echo JText::_('RSFL_CANCEL_BTN'); ?>" onclick="rsfl_back();" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
	</div>
<?php } ?>

	
<?php echo JHTML::_( 'form.token' ); ?>
<input type="hidden" name="option" value="com_rsfiles" />
<input type="hidden" name="folder" value="<?php echo $this->currentfolder; ?>" />
<input type="hidden" name="controller" value="files" />
<input type="hidden" name="task" value="upload" />
</form>

<?php
//keep session alive while editing
JHTML::_('behavior.keepalive');
?>