<?php 
if( !isset($this->filter) )
{
	
	$this->filter = array('matchElements' => array( array('field' => '', 'matchType' => '', 'matchString' => '', 'not' => false) ), 'fileInto' => '', );
	$buttonLabel = $this->translate('Add Filter');
	$filtersId = false;
	$modify = false;
}
else
{
	
	$buttonLabel = $this->translate('Save Filter');
	$filtersId = $this->filtersId;
	$modify = true;
}

$fieldNames = array( 'from' => $this->translate('From'), 'to' => $this->translate('To'), 'subject' => $this->translate('Subject') )

?>
<form id="filterForm" method="post" action="<?php echo $this->moduleBaseUrl ?>/settings/filterssave">
	<?php if( $modify ) : ?>
	<input type="hidden" name="modify" value="true" />
	<input type="hidden" id="filtersId" name="filtersId" value="<?php echo $filtersId ?>">
	<?php endif; ?>
	<table class="settings">
		<tbody>
	<?php 
	$elementC = count( $this->filter['matchElements'] );
	foreach( $this->filter['matchElements'] as $elementsK => $element ) : ?>
			<tr id="filterElementRow" rownumber="<?php echo $elementsK ?>">
				<td class="label"><span><?php echo ($elementsK == 1) ? 'AND' : $this->translate('New Rule') ?></span></td>
				<td width="50">
					<select style="width:100px;" name="filter[matchElements][<?php echo $elementsK ?>][field]">
					<?php foreach( $fieldNames as $fieldNamesKey => $fieldNamesValue ) : ?>
						<option value="<?php echo $fieldNamesKey ?>" <?php echo ($element['field']==$fieldNamesKey?' selected':'') ?>><?php echo $fieldNamesValue ?></option>
					<?php endforeach; ?>
					</select>
				</td>
				<td width="50">
					<select style="width:100px;" name="filter[matchElements][<?php echo $elementsK ?>][matchType]">
						<option value='is'<?php echo (($element['matchType']=='is' && !$element['not'])?' selected':'') ?>><?php echo $this->translate('is'); ?></option>
						<option value='isNot'<?php echo (($element['matchType']=='is' && $element['not'])?' selected':'') ?>><?php echo $this->translate('is not'); ?></option>
						<option value='contains'<?php echo (($element['matchType']=='contains' && !$element['not'])?' selected':'') ?>><?php echo $this->translate('contains'); ?></option>
						<option value='doesNotContain'<?php echo (($element['matchType']=='contains' && $element['not'])?' selected':'') ?>><?php echo $this->translate('does not contain'); ?></option>
					</select>
				</td>
				<td width="50">
					<input id="filterElementValue" name="filter[matchElements][<?php echo $elementsK ?>][matchString]" value="<?php echo $element['matchString'] ?>"/>
				</td>
				<td width="1" style="padding-right: 8px; padding-top: 8px;">
					<div id="rowRemoveButton"><img src="<?php echo $this->siteBaseUrl ?>images/icons/less.jpg"/></div>
				</td>
				<td width="1" style="padding-right: 8px; padding-top: 8px;">
					<div id="rowAddButton" style="<?php echo ($elementsK!=$elementC-1?' display:none;':'') ?>"><img src="<?php echo $this->siteBaseUrl ?>images/icons/more.jpg"/></div>
				</td>
				<td>
				</td>
			</tr>  
	<?php endforeach; ?>
		<tbody>
	</table>

	<div class="divider"/>

	<table class="settings">
		<tr>
			<td class="label"><?php print $this->translate('Move To')?></td>
			<td>
				<select id="filterFileInto" style="width:100px;" name="filter[fileInto]">
					<?php 
					foreach( $this->folders as $folderNameLocal => $folderObject ) : 
						
						$globalName = $folderObject->getGlobalName();
						
						if( $globalName == "INBOX" )
						{
							
							$globalFolderNameOnDisk = '.';
							$globalNameReadable = ucfirst(strtolower($globalName));
							
						}   
						else
						{
                            
							$globalFolderNameOnDisk = '.' . ((strpos($globalName, 'INBOX')===0)?ltrim(substr($globalName, 5),'.'):ltrim($globalName,'.'));
							$globalNameReadable = strpos($globalName, 'INBOX')===0?ltrim(substr($globalName, 5),'.'):ltrim($globalName,'.');
							$globalNameReadable = decodeUTF7($globalNameReadable);
							
						}
						
					 ?>
					<option value="<?php echo htmlentities(addslashes($globalFolderNameOnDisk)); ?>" <?php if( $this->filter['fileInto']==$globalFolderNameOnDisk ) echo ' selected' ?>><?php echo htmlentities($globalNameReadable); ?></option>
					<?php endforeach; ?>
				</select>
				<small><?php print $this->translate('Specify which folder the rule will deliver to')?></small>
			</td>
		</tr>
	</table>
	
	<div class="divider"/>
	
	<table class='settings' id='' style="margin-top: 5px;">
	
	<tr>
		<td class="label"></td>
		<td>
			<div id="add_filter_action" class="addFilter" style="width: 120px;">
				<a href="#">
			<span class="stronglastword"><?php echo $buttonLabel ?></span>
		</a>
	</div>
	
	</tr>
		
	</table>

</form>

<script>

$( function() 
{
	
	$('#rowAddButton', '#filters').bind('click', function() 
	{
		
		if( $(this).parent().parent().children('td:eq(3)').children('input').attr('value') == '') 
		{
		
			$(this).parent().parent().children('td:eq(3)').children('input').effect('highlight', {}, 500);
			$(this).parent().parent().children('td:eq(3)').children('input').focus();
			return false;
		
		}

		newRow = $(this).parent().parent().clone(true);
		$(this).parent().parent().after( newRow );

		newRow = $(this).parent().parent().next();
		//update fieldnames for new row
		var newRowNumber = Number($(newRow).attr('rownumber'))+1;
		$(newRow).attr('rownumber', newRowNumber );
		$(newRow).children('td:eq(0)').children('span').html("<?php echo $this->translate('AND') ?>");
		$(newRow).children('td:eq(1)').children('select').attr('name', 'filter[matchElements][' + newRowNumber + '][field]');
		$(newRow).children('td:eq(2)').children('select').attr('name', 'filter[matchElements][' + newRowNumber + '][matchType]');
		$(newRow).children('td:eq(3)').children('input').attr('name', 'filter[matchElements][' + newRowNumber + '][matchString]');
		$(newRow).children('td').children('input').attr('value', '');
		
		//$(this).parent().parent().parent().children('#rowAddButton').each( function() { $(this).fadeOut('slow') } );
		//$(this).parent().parent().parent().children('#rowAddButton:last').each( function() { $(this).fadeIn('slow') } );
		$(this).fadeOut('slow');
		
	});

	$('#rowRemoveButton', '#filters').bind('click', function()
	{
		
		//if only one row left then just remove the input value else remove row
		if( $(this).parent().parent().parent().children('tr').length == 1 )
		{
			
			if( $(this).parent().parent().children('td:eq(3)').children('input').attr('value') == '' ) 
			{

				$(this).parent().parent().children('td:eq(3)').children('input').effect('highlight', {}, 500);
				$(this).parent().parent().children('td:eq(3)').children('input').focus();
				return false;

			}
			$(this).parent().parent().children('td:eq(3)').children('input').attr('value', '');
			$(this).parent().parent().parent().children('tr:last').children('td:eq(5)').children('#rowAddButton').fadeIn('slow');
		
		}
		else
		{
			
			currentRowIndex = $(this).parent().parent().parent().children('tr').index( $(this).parent().parent() );
			rowC = $(this).parent().parent().parent().children('tr').length;
			if( (currentRowIndex+1) == rowC )
			{
				
				previousRow = $(this).parent().parent().prev();
				previousRow.children('td:eq(5)').children('#rowAddButton').fadeIn('slow');
				
			}
			$(this).parent().parent().fadeOut('slow', function() { $(this).remove() });
			
		}
		
	});
	
	$("#add_filter_action.addFilter", '#filters').bind('click', function() {
		
		if( $('select#filterFileInto', '#filters').attr('value') == '' ) 
		{
		
			$('select#filterFileInto', '#filters').effect('highlight', {}, 500);
			$('select#filterFileInto', '#filters').focus();
			return false;
		
		}
		$.post($('form#filterForm').attr('action'), $('form#filterForm').serialize(), function(data, textStatus) {

			if( handleResponseErrors(data, textStatus) )
			{
				
				var filtersId = $('form#filterForm', '#filters').children('input#filtersId').attr('value');
				$('#relay-table', "#filters").load('<?php echo $this->moduleBaseUrl ?>/settings/filterslist');
				$('div#filterForm').load('<?php echo $this->moduleBaseUrl ?>/settings/filtersedit' );
			}
			

		}, 'json');
		
	});
	
	//prevent legacy submit
	$('#filterForm', '#filters').submit( function() 
	{	                                 
		
		return false; 
	
	});
	
	$('.stronglastword').each( function() { $(this).stronglastword() });

});
</script>
