<table id="filtersList" class="aliases genericList">
	<tbody>
		<tr class="header">
			<td class="td1">
				<input style="margin-left: 8px; vertical-align: middle" id="checkToggle" type="checkbox" name="checker"/>
			</td>
			<td class="td2" style="vertical-align: middle; padding-bottom: 5px;">
				<b><?php echo $this->translate('Filter'); ?></b>
			</td>
			<td class="td3" style="vertical-align: middle; padding-bottom: 5px;">
				<b><?php echo $this->translate('Options'); ?></b>
			</td>
		</tr>
		<?php
		if( count($this->filters) == 0 )
		{
			
			echo "<tr class='divide'><td>&nbsp;</td><td>" . $this->translate('No filters currently defined') . "</td><td>&nbsp;</td></tr>";
			
		}
		foreach( $this->filters as $k => $v ) : ?>
		<tr class="divide">
			<td class="td1">
				<input style="margin-left: 8px; vertical-align: middle" id="filterSelectedCheckbox" type="checkbox" value="" name="filtersId[]"/>
			</td>
			<td class="td2">
				<?php 
				$matchElementsC = count( $v['matchElements'] );
				foreach( $v['matchElements'] as $k2 => $v2 ) : 
					
					$matchTypeString = $v2['matchType'];
					if( $v2['not'] == true )
					{
						
						if( $v2['matchType'] == 'is')
						{
							
							$matchTypeString .= " not";
							
						} 
						else if( $v2['matchType'] == 'contains')
						{
							
							$matchTypeString = 'does not contain';
							
						}
						else
						{
							
							$matchTypeString = 'does not ' . $matchTypeString;
							
						}
						
					}
				?>
					<strong><?php echo $v2['field'] ?></strong> <?php echo $matchTypeString ?> "<?php echo $v2['matchString'] ?>"
					<?php if( $k2 != $matchElementsC-1 ) : ?>
						<?php echo $this->translate('and'); ?> 
					<?php endif; ?> 
				   
				<?php endforeach; ?>
				filters to <strong><?php 
				$found = false;
				foreach( $this->folders as $folderNameLocal => $folderObject ) : 
					
					$globalName = $folderObject->getGlobalName();
					
					if( $globalName == "INBOX" )
					{
						
						$globalFolderNameOnDisk = '.';
						$globalNameReadable = ucfirst(strtolower($globalName));
						
					}   
					else
					{
                        
						$globalFolderNameOnDisk = (strpos($globalName, 'INBOX')===0)?substr($globalName, 5):$globalName;
						$globalNameReadable = strpos($globalName, 'INBOX')===0?ltrim(substr($globalName, 5),'.'):ltrim($globalName,'.');
						$globalNameReadable = ucfirst(strtolower($globalNameReadable));
						
					}
					if( $v['fileInto'] == $globalFolderNameOnDisk)
					{
						
						echo htmlentities($globalNameReadable);
						$found = true;
						
					}
					
				endforeach; 
				if( !$found )
				{
					echo htmlentities(stripslashes($v['fileInto']));	
				}
				?></strong>
			</td>
			<td class="td3">
				<a class="filterDelete" href="#"><?php echo $this->translate('Delete'); ?></a>/<a class="filterModify" href="#"><?php echo $this->translate('Modify'); ?></a>
			</td>
		</tr>
		<?php endforeach; ?>
	</tbody>
</table>

<script>

$( function() 
{

	$("#checkToggle", "#filters").bind("click", function() 
	{

		if( $(this).attr('checked') ) 
		{
	
			$("#filterSelectedCheckbox", "#filters").each( function() { this.checked=true; } );
		}
		else
		{
	
			$("#filterSelectedCheckbox", "#filters").each( function() { this.checked=false; } );
	
		}

	})

	$(".filterDelete", "#filters").unbind("click");
	$(".filterDelete", "#filters").bind("click", function() 
	{                                                                           
	
		var rowId = $(this).parent().parent().parent().children('tr').index( $(this).parent().parent() ); //ignore header row
		$.post('<?php echo $this->moduleBaseUrl ?>/settings/filtersdelete', {'filtersId[]':(rowId-1)}, function(data, textStatus) 
		{

			if( handleResponseErrors(data, textStatus) )
			{
			    
				$('table#filtersList tr:eq(' + rowId + ')', '#filters').fadeOut('slow', function() { $(this).remove() });
			
			}

		}, 'json');
	
	});

	$("#back_button_delete_selected", "#filters").unbind("click");
	$("#back_button_delete_selected", "#filters").bind("click", function() 
	{
        
		//populate checkbox ids with row ids to be in sync before serializing data for delete
		$('table#filtersList tr', '#filters').each( function(index) 
		{
			
			//skip headding row
			if( index != 0 )
			{
				$(this).children('td:first').children('input#filterSelectedCheckbox').attr('value', (index-1) );
				
			}
			
		});
		$.post('<?php echo $this->moduleBaseUrl ?>/settings/filtersdelete', $("#filtersForm", "#filters").serializeArray(), function(data, textStatus) 
		{
			
			if( handleResponseErrors(data, textStatus) )
			{
			    
				//$('#relay-table', "#filters").load('<?php echo $this->moduleBaseUrl ?>/settings/filterslist');
				$('table#filtersList tr', '#filters').each( function(index) 
				{
					if( index!=0 && $(this).children('td:first').children('#filterSelectedCheckbox').attr('checked') )
					{

						$(this).fadeOut('slow', function() { $(this).remove() });

					}

				});
			
			}
			
		}, 'json'); 
		

	});

	$(".filterModify", "#filters").unbind("click");
	$(".filterModify", "#filters").bind("click", function() 
	{                                                                           
	
		var rowId = $(this).parent().parent().parent().children('tr').index( $(this).parent().parent() );
		$('div#filterForm').load('<?php echo $this->moduleBaseUrl ?>/settings/filtersedit/filtersId/' + (rowId-1) );
	
	});

}); 
</script>