<div id="primary_header">
	<h1><?php print $this->translate('Email Aliases')?></h1>
</div>

<div id="primary_content">
	<div id="primary_content_inner">
		<div id="Flash" class="flash_notice"><?php print $this->translate('The settings have been updated') ?></div>

		<form id="settingsForm" method="post" action="<?php echo $this->moduleBaseUrl ?>/services/aliascreate">
			
			<input type='hidden' name='aliasType' id='aliasType' value='Local'/>
			
			<table class="settings">
				<tr>
					<td class="label"><?php print $this->translate('Filter by domain:')?></td>
					<td class="filterinput">
						<input type="text" name="aliasFilter" id="aliasFilter" class='panelFilter' value=""/> 
						<small><?php print $this->translate('Specify a domain or email to filter results')?></small>
					</td>
					<td class="filterdomain">
						&nbsp;
					</td>
				</tr>               
			</table>

			<div id="alias-table">
			</div>

			<table style="margin-top: 5px;" id="" class="settings">

				<tbody><tr>

					<td>
            <div class="mail_row_paginator_aliases">

				<div id="back_button_delete_selected" class="filterdomain">
					<a href="#">
						<span class="stronglastword"><?php echo $this->translate('Delete Selected') ?></span>
					</a>
				</div>
			</div>
			</td>
			</tr>
			</tbody>
			</table>
			
			<div class="divider"/>

			<table class="settings">
				<tr>
					<td class="label"><?php print $this->translate('Alias Type')?></td>
					<td class="tdstyle">
						<div class="styled-select">
						        <span class="arrow"></span>
						<select name='EmailAlias' id='EmailAlias'>
							<option value="Local" selected=""><?php echo $this->translate('Local Alias') ?></option>
							<option value="Deliver"><?php echo $this->translate('Deliver locally and alias') ?></option>
							<option value="Domain"><?php echo $this->translate('Domain Alias ( Catch-all )') ?></option>
							<option value="Virtual"><?php echo $this->translate('Virtual Domain ( Divert email from one domain to another )') ?></option>
							<?php if($this->userData['UMasterAdmin'] == 1) :?>
							<option value="MailDir"><?php echo $this->translate('Alias to Maildir/File') ?></option>
							<?php endif ?>
						</select>
						</div>
						<small>
						<?php print $this->translate('Specify the alias type. <b>Local aliases</b> forward emails from one account to the other. <b>Deliver locally and alias</b> sends copies to both accounts. <b>Domain aliases</b> "catch" all emails for a domain and forwards copies of the emails to a single account.');
						if($this->userData['UMasterAdmin'] == 1)
						print $this->translate('<b>Alias to Maildir/File</b> creates a copy of the emails and stores them in a single directory.')?>
						</small>
                    
					</td>
				</tr>

			</table>

			<div class="divider"/>

			<table class="settings aliasTable" id='Local'>	
				<tr>
					<td class="label"><?php print $this->translate('Local Email Address')?></td>
					<td>
						<input type="text" class="large" name='AliasNameLocal' id='AliasNameLocal'/>
						<small><?php print $this->translate('Specify a local or virtual email account')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Forward To')?></td>
					<td>
						<input type="text" class="large" name='AliasToLocal' id='AliasToLocal'/>
						<small><?php print $this->translate('Specify the local or remote email address')?></small>
					</td>
				</tr>

			</table>

			<table class="settings aliasTable" id='Deliver' style='display: none;'>	
				<tr>
					<td class="label"><?php print $this->translate('Local Email Address')?></td>
					<td>
						<input type="text" class="large" name='AliasNameDeliver' id='AliasNameDeliver'/>
						<small><?php print $this->translate('Specify a local email account')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Forward To')?></td>
					<td>
						<input type="text" class="large" name='AliasToDeliver' id='AliasToDeliver'/>
						<small><?php print $this->translate('A copy will be stored on the local account, and forwarded to the address specified.')?></small>
					</td>
				</tr>

			</table>

			<table class="settings aliasTable" id='Domain' style='display: none;'>	
				<tr>
					<td class="label"><?php print $this->translate('Domain Name')?></td>
					<td>
						<input type="text" class="large" name='AliasNameDomain' id='AliasNameDomain'/>
						<small><?php print $this->translate('Select the domain to divert all emails from')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Forward To')?></td>
					<td>
						<input type="text" class="large" name='AliasToDomain' id='AliasToDomain'/>
						<small><?php print $this->translate('Specify the local or remote email address')?></small>
					</td>
				</tr>

			</table>

			<table class="settings aliasTable" id='Virtual' style='display: none;'>	

				<tr>
					<td class="label"><?php print $this->translate('Domain Name')?></td>
					<td>
						<input type="text" class="large" name='AliasNameVirtual' id='AliasNameVirtual'/>
						<small><?php print $this->translate('Specify the domain to route')?></small>

					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Forward to Domain')?></td>
					<td>
						<input type="text" class="large" name='AliasToVirtual' id='AliasToVirtual'/>
						<small><?php print $this->translate('Specify the domain to forward messages')?></small>

					</td>
				</tr>

			</table>

			<table class="settings aliasTable" id='MailDir' style='display: none;'>	

				<tr>
					<td class="label"><?php print $this->translate('Local email address')?></td>
					<td>
						<input type="text" class="large" name='AliasNameMailDir' id='AliasNameMailDir' />
						<small><?php print $this->translate('Specify the local email address')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Maildir location on disk')?></td>
					<td>
						<input type="text" class="large" name='AliasMailDir' id='AliasMailDir' />
						<small><?php print $this->translate('Specify the pathname on disk to store the message')?></small>
					</td>
				</tr>
                 
			</table>

			<table class='settings' id='' style="margin-top: 5px;">

			<tr>
				<td class="label"></td>
				<td>
					<div id="back_button_add_alias" class="filterdomain">
						<a href="#">
					<span class="stronglastword"><?php echo $this->translate('Add Alias') ?></span>
				</a>
			</div>	

			 

			  </div>
			  </form>
			</div>

<script type="text/javascript">
$( function()
{
	var tabId = '#Email_Aliases';
	perform_service_common_binds(tabId);

	// Load the email-alias list
	$.php('<?php echo $this->moduleBaseUrl ?>/services/aliaslist');

	// Add the hook to change the email-alias types
	$("#EmailAlias", tabId).change(function()
	{
		// Show the specified email-alias table
		var type = $(this).val();
		$('.aliasTable', tabId).hide();
		$("#" + type, tabId).show();
		$("#aliasType", tabId).val(type);
	});

	$('#back_button_add_alias a', tabId).bind("click", function()
	{
		$(tabId + " #Flash").hide();
		$('#settingsForm', tabId).submit();
        //$("#AliasNameLocal").text = "";
		return false
	});        

	// Filter results
	$('#back_button_filter a', tabId).bind("click", function()
	{
		$(tabId + " #Flash").hide();
		$.php('<?php echo $this->moduleBaseUrl ?>/services/aliaslist/filter/' + $("#aliasFilter", tabId).val() );
		return false
	}); 

	var search_timeout = 0;

	$('#aliasFilter', tabId).bind('keyup', function()
	{
		if ( search_timeout != undefined ) 
		{
			clearTimeout(search_timeout);
		}

		var thecall = this; // save reference to 'this' so we can use it in timeout function
		search_timeout = setTimeout( function() 
									{
										search_timeout = undefined;
										$.php('<?php echo $this->moduleBaseUrl ?>/services/aliaslist/filter/' + $("#aliasFilter", tabId).val() );
									}, 300);
	});

	// Delete multiple entries
	$("#back_button_delete_selected", tabId).bind("click", function()
	{
		$.php('<?php echo $this->moduleBaseUrl ?>/services/aliasdelete/', $("#checkEntry", tabId).serializeArray());
        $id = $("#checkEntry", tabId).serializeArray();
        for($i = 0; $i < $id.length; $i++) {
            $idVal = $id[$i].value;
            $idStr = "a#".concat($idVal);
            $($idStr, ".aliasRow").parent().parent().fadeOut();
        }

	});

});

</script>