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

</div>

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

		<form id="settingsForm" method="post" action="<?php echo $this->moduleBaseUrl ?>/settings/webmailsave">

			<input type="hidden" name="save" value="1" />
			<table class="settings">
<?php 	if( $this->global['install_type'] == 'server') :?>
				<tr>
					<td class="label"><?php print $this->translate('Sign-up page enabled')?></td>
					<td>
						<input name="fields[allow_Signup]" type='hidden' value='0'/>
						<input name="fields[allow_Signup]" type="checkbox" value="1" class="iphoneStyle" <?php echo ($this->global['allow_Signup']==1)?'checked' : '' ?> />
						<small><?php print $this->translate('Sets if Webmail sign-up page is available and shows a link on the login page.')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><?php print $this->translate('Show admin link')?></td>
					<td>
						<input name="fields[adminlink]" type='hidden' value='0'/>
						<input name="fields[adminlink]" type="checkbox" value="1" class="iphoneStyle" <?php echo ($this->global['adminlink']==1)?'checked' : '' ?> />
						<small><?php print $this->translate('Sets if Webmail login page shows a link to the admin page.')?></small>
					</td>
				</tr>
<?php 	endif ?>

				<tr>
					<td class="label"><?php print $this->translate('Webmail Protocols')?></td>
					<td>
						<div class="styled-select">
						        <span class="arrow"></span>
						<select name="fields[mail_type]" id="mail_type">
<?php 	if(extension_loaded('openssl')) : ?>
							<option value="imapssl">IMAP + IMAP SSL</option>
							<option value="ssl">IMAP SSL</option>
<?php 	endif ?>
							<option value="imap">IMAP</option>
						</select>
						</div>
						<small><?php print $this->translate('Specify which protocols can access the Webmail interface')?></small>
					</td>
				</tr>
			</table>				
			<div class="divider"/>

			<table class="settings">
				<tr>
					<td class="label"><?php print $this->translate('Enable Spell Check')?></td>
					<td>
						<?php 
							$pspell_disabled = false;
							$pspell_missing_dict = false;
							if (!extension_loaded('pspell'))
							{
								$pspell_disabled = true;
							}
							else
							{
								require_once 'Atmail/SpellCheck.php';
								$spelling = new Spelling('pspell', realpath(users::getTmpFolder()));
								if (extension_loaded('pspell') && $spelling->testpspell() === false)
								{
									$pspell_disabled = true;
									$pspell_missing_dict = true;
								}
							}
						?>
						<input id="enableSpellCheckHidden" name="fields[enableSpellCheck]" type='hidden' value='0' <?php if ($pspell_disabled) echo 'disabled'; ?>/>
						<input id="enableSpellCheck" name="fields[enableSpellCheck]" type="checkbox" class="iphoneStyle" value="1" <?php echo $this->global['enableSpellCheck'] ? 'checked' : '' ?> <?php if ($pspell_disabled) echo 'disabled'; ?>>
						<small><?php print $this->translate('Enable the Spell Check option for users. If this is enabled users will be able to submit their composed email for server side spell checks.')?></small>
						<?php if (!extension_loaded('pspell')) echo '<small style="font-weight:bold">' . $this->translate('The pspell php extension is required for this feature and is currently NOT loaded.') . '<small>'; ?>
						<?php 
							if ($pspell_missing_dict)
							{
								echo '<small style="font-weight:bold">' . $this->translate('The pspell php extension is loaded but is missing its default dictionary. Please install the aspell dictionary for your default language.') . '<small>'; 
							}
						?>
						</td>
				</tr>
				<tr>
					<td class="label"><?php print $this->translate('Enable Confirm On Send')?></td>
					<td>
						<input id="enableConfirmOnSendHidden" name="fields[enableConfirmOnSend]" type='hidden' value='0'/>
						<input id="enableConfirmOnSend" name="fields[enableConfirmOnSend]" type="checkbox" class="iphoneStyle" value="1" <?php echo ($this->global['enableConfirmOnSend'] & Atmail_Cache::TYPE_MAIL) ? 'checked' : '' ?>>
						<small><?php print $this->translate('Enable the Confirm On Send option for users. If this is enabled users will be able to choose whether or not they get a confirm message when sending an email.')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><?php print $this->translate('Enable Mail Cache')?></td>
					<td>
						<input id="cacheEnabledMailHidden" name="fields[cacheEnabled]" type='hidden' value='0'/>
						<input id="cacheEnabledMail" name="fields[cacheEnabled]" type="checkbox" class="iphoneStyle" value="1" <?php echo ($this->global['cacheEnabled'] & Atmail_Cache::TYPE_MAIL) ? 'checked' : '' ?>>
						<small><?php print $this->translate('Enable cache for Webmail data. User messages and IMAP requests are cached locally to improve performance.')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><?php print $this->translate('Enable Dav Cache')?></td>
					<td>
						<input type='hidden' value='0'/>
						<input id="cacheEnabledDav" type="checkbox" class="iphoneStyle" value="1" <?php echo ($this->global['cacheEnabled'] & Atmail_Cache::TYPE_DAV) ? 'checked' : '' ?>>
						<small><?php print $this->translate('Enable cache for Dav data. User dav requests are cached locally to improve performance.')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><?php print $this->translate('Cache Type')?></td>
					<td>
						<div class="styled-select">
						        <span class="arrow"></span>
						<select name="fields[cacheType]" id="cacheType">
							<option value="fs">Filesystem</option>
							<option value="memcache" <?php if (!extension_loaded('memcache')) echo "disabled" ?>>Memcache</option>
						</select>
						</div>
						<small><?php print $this->translate('Select the caching system that Atmail will use. The default "filesystem" cache does not require any dependencies. The "memcache" system has better performance' . (!extension_loaded('memcache') ? " but requires the memcache php extension" : "") . '.')?></small>
						<?php if (!extension_loaded('memcache')) echo '<small>' . $this->translate('The memcache php extension is currently NOT loaded.') . '<small>'; ?>
					</td>
				</tr>
			</table>				
			<table class="settings" id='memCacheOptions' style='display:none;'>

				<tr>
					<td class="label"><?php print $this->translate('Memcache Hostname')?></td>
					<td>
						<input id="cacheMemcacheServer" name="fields[cacheMemcacheServer]" type="text" value="<?php echo $this->global['cacheMemcacheServer'] ?>"/>
						<small><?php print $this->translate('Memcache Server host, ie: localhost')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Memcache Port')?></td>
					<td>
						<input id="cacheMemcachePort" name="fields[cacheMemcachePort]" type="text" value="<?php echo $this->global['cacheMemcachePort'] ?>"/>
						<small><?php print $this->translate('Memcache Server port, ie: 11211')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Memcache Timeout')?></td>
					<td>
						<input id="cacheMemcacheTimeout" name="fields[cacheMemcacheTimeout]" type="text" value="<?php echo $this->global['cacheMemcacheTimeout'] ?>"/>
						<small><?php print $this->translate('Timeout in seconds when connecting to Memcache Server.')?></small>
					</td>
				</tr>
				
			</table>
			
			<div class="divider"/>

			<table class="settings">

				<tr>
					<td class="label"><?php print $this->translate('Login Page Theme')?></td>
					<td>
						<div class="styled-select">
						        <span class="arrow"></span>
						<select name="fields[cssStyle]" id="cssStyle">
							<option value="original" selected>Original</option>
						</select>
						</div>
						<small><?php print $this->translate('Specify the login page used for the Webmail interface.')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('Theme Example')?></td>
					<td>
						<img src='<?php echo $this->siteBaseUrl ?>images/login/original.png' id='cssTheme' width='201' height='152'/>
					</td>
				</tr>

			</table>

			<div class='divider'/>

			<table class="settings">

				<tr>
					<td class="label"><?php echo $this->translate('Allow External Domains') ?></td>
					<td>
						<input type="hidden" name="fields[remoteDomainsOverwrite]" value='0'>
						<input type="checkbox" class="iphoneStyle" name="fields[remoteDomainsOverwrite]" id="remoteDomainsOverwrite" <?php echo (@$this->global['remoteDomainsOverwrite'] ? 'checked' : '') ?> value="1"><small><?php echo $this->translate('Turn on to allow any domain name to login via Webmail. Turn off to specifically define which domains can login. Any local domain automatically can access Webmail.') ?></small>
					</td>
				</tr>

				<tr id='remoteDomainsDiv'>
					<td class="label"><?php echo $this->translate('Specify Remote Domains') ?></td>
					<td>
						<textarea name='fields[remoteDomains]' id="remoteDomains"><?php
							if(is_array($this->global['remoteDomains']))
							{
								foreach($this->global['remoteDomains'] as $domain)
								{	
									echo $domain . "\n";
								}
							}
							else
							{
								echo (isset($this->global['remoteDomains']) ? $this->global['remoteDomains'] . "\n" : '');
							}
							?></textarea>
						<small><?php echo $this->translate('Specify a list of external domain-names that can access the Webmail interface. Separate each entry with a return. Useful to only allow users at a specified domain to use the Webmail interface.') ?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php echo $this->translate('Allow External Mailservers') ?></td>
					<td>
						<input type="hidden" name="fields[remoteServersOverwrite]" value='0'>
						<input type="checkbox" class="iphoneStyle" name="fields[remoteServersOverwrite]" id="remoteServersOverwrite" <?php echo (@$this->global['remoteServersOverwrite'] ? 'checked' : '') ?> value="1">
						<small><?php echo $this->translate('Turn on to allow users to login to any specified mailserver. Turn off to specifically define the mailservers user can login via Webmail.') ?></small>
					</td>
				</tr>

				<tr id='remoteServerDiv'>
					<td class="label"><?php echo $this->translate('Specify Remote Servers') ?></td>
					<td><textarea name='fields[remoteServers]' id="remoteServers"><?php
						if(is_array($this->global['remoteServers']))
							{
								foreach($this->global['remoteServers'] as $server)
								{	
									echo $server . "\n";
								}
							}
							else
							{
								echo (isset($this->global['remoteServers']) ? $this->global['remoteServers'] . "\n" : '');
							}
							?></textarea>
						<small><?php echo $this->translate('Specify a list of external IMAP servers that can access the Webmail interface. Separate each entry with a return. Useful to define a list of IMAP servers users are permitted to use. Optionally define a single mailserver to lock all users to one host.') ?></small>
					</td>
				</tr>
				<?php if( $this->global['install_type'] != 'server') :?>
				<tr>
					<td class="label"><?php print $this->translate('Default Domain')?></td>
					<td>
						<input type="text" class="large" name="fields[default_domain]" id="default_domain" value="<?php echo $this->dovecot['default_domain'] ?>" />
						<small><?php print $this->translate('Specify the default domain to append to usernames if none is given by the user logging in.')?></small>
					</td>
			   </tr>
               <?php endif; ?>
			</table>

			<div class='divider'/>

			<table class="settings">

				<tr>
					<td class="label"><?php echo $this->translate('Outgoing message method') ?></td>
					<td><?php echo $this->translate('SMTP Server:') ?>
						<input type="hidden" name="fields[smtp_auth]" value="0"/>
						<input type="radio" id="smtpserver" name="fields[smtp_auth]" value="0"><small><?php echo $this->translate('Select if messages are sent directly to an SMTP server without authentication.') ?></small><br/>
						<?php echo $this->translate('SMTP Authentication:') ?>
						<input type="radio" id='smtpauth' name="fields[smtp_auth]" value="1"><small><?php echo $this->translate('Select to send all messages via an SMTP server with authentication.') ?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><?php print $this->translate('SMTP Hostname')?></td>
					<td>
						<input type="text" class="medium" name="fields[smtphost]" id="smtphost" value="<?php echo $this->global['smtphost'] ?>" />
						<small><?php print $this->translate('Enter the SMTP server used for sending outgoing messages via Webmail. Default is 127.0.0.1 for the local server.')?></small>
					</td>
				</tr>

				<tr id='smtpAuthUserDiv'>
					<td class="label"><?php print $this->translate('SMTP Auth Username')?></td>
					<td>
						<input type="text" class="medium" name="fields[smtpauth_username]" id="smtpauth_username" value="<?php echo $this->global['smtpauth_username'] ?>" />
						<small><?php print $this->translate('Specify the SMTP username for authentication')?></small>
					</td>
				</tr>

				<tr id='smtpAuthPassDiv'>
					<td class="label"><?php print $this->translate('SMTP Auth Password')?></td>
					<td>
						<input type="password" class="medium" name="fields[smtpauth_password]" id="smtpauth_password" value="<?php echo $this->global['smtpauth_password'] ?>" />
						<small><?php print $this->translate('Specify the SMTP password for authentication')?></small>
					</td>
				</tr>

			</table>

			<!--
			<div class='divider'/>

			<table class="settings">

			<tr>
			<td class="label"><?php print $this->translate('Maximum sort limit')?></td>
			<td>
			<input type="text" name="fields[sortMaxLimit]" id="sortMaxLimit" value="<?php echo $this->global['sortMaxLimit'] ?>" />
			<small><?php print $this->translate('Server performance can drop significantly if it tries to sort too many messages in a requested folder. This option allows disabling sorting if more than the maximum number of messages are found.')?></small>
			</td>
			</tr>

			<tr>
			<td class="label"><?php print $this->translate('Threading Maximum limit')?></td>
			<td>
			<input type="text" name="fields[threadingMaxLimit]" id="threadingMaxLimit" value="<?php echo $this->global['threadingMaxLimit'] ?>" />
			<small><?php print $this->translate('Server performance can drop significantly if it tries to thread too many messages in a requested folder. This option allows disabling threading if more than the maximum number of messages are found.')?></small>
			</td>
			</tr>

			</table>
			-->

		</div>
	</form>
	<div class="divider"></div>
	<div id="back_button">
		<a href="#">
			<span class="stronglastword"><?php echo $this->translate('Save Settings') ?></span>
		</a>
	</div>
</div>

<script type="text/javascript">
$( function()
{

	$('.iphoneStyle', '#Webmail_Settings').each( function() { $(this).iphoneStyle() });
	$('.stronglastword', '#Webmail_Settings').each( function() { $(this).stronglastword() });

	$("#cssStyle").change(function()
	{

		$("#cssTheme").attr('src', '<?php echo $this->siteBaseUrl ?>images/login/' + $(this).val() + '.png');

	});

// SHow the advanced memcache options
	$("#cacheType").change(function()
	{
		if($(this).val() == 'memcache') {
			$("TABLE#memCacheOptions").show();
		} else {			
			$("TABLE#memCacheOptions").hide();
		}

	});
	
	function editpermissions()
	{

		if($("#Webmail_Settings #remoteServersOverwrite").attr('checked') != true)
		{

			$("#Webmail_Settings #remoteServerDiv").fadeTo("slow", 1);
			$("#remoteServers").removeAttr("disabled");
			$("#remoteServers").focus();

		}
		else
		{

			$("#Webmail_Settings #remoteServerDiv").fadeTo("fast", 0.5);
			$("#remoteServers").attr("disabled","disabled");

		}

		if($("#Webmail_Settings #remoteDomainsOverwrite").attr('checked') != true)
		{

			$("#Webmail_Settings #remoteDomainsDiv").fadeTo("slow", 1);
			$("#remoteDomains").removeAttr("disabled");
			$("#remoteDomains").focus();

		}
		else
		{

			$("#Webmail_Settings #remoteDomainsDiv").fadeTo("fast", 0.5);
			$("#remoteDomains").attr("disabled","disabled");

		}

	}

	function smtpauth()
	{

		if($("#Webmail_Settings #smtpauth").attr('checked') == true)
		{

			$("#Webmail_Settings #smtpAuthUserDiv").fadeTo("slow", 1);
			$("#smtpauth_username").removeAttr("disabled");
			$("#Webmail_Settings #smtpAuthPassDiv").fadeTo("slow", 1);
			$("#smtpauth_password").removeAttr("disabled");
			$("#smtpauth_username").focus();

		}
		else
		{

			$("#Webmail_Settings #smtpAuthUserDiv").fadeTo("fast", 0.5);
			$("#smtpauth_username").attr('value', '');
			$("#smtpauth_username").attr("disabled","disabled");
			$("#Webmail_Settings #smtpAuthPassDiv").fadeTo("fast", 0.5);
			$("#smtpauth_password").attr('value', '');
			$("#smtpauth_password").attr("disabled","disabled");

		}

	}
	
	function getCacheEnabled()
	{
		return <?php echo Atmail_Cache::TYPE_ALL ?> - (!$('#cacheEnabledMail').is(':checked') ? <?php echo Atmail_Cache::TYPE_MAIL_COMBINED ?> : 0) - (!$('#cacheEnabledDav').is(':checked') ? <?php echo Atmail_Cache::TYPE_DAV ?> : 0);	
	}
	
	function showCacheSettings()
	{
		var cacheSettings = getCacheEnabled();
		if(cacheSettings & <?php echo Atmail_Cache::TYPE_MAIL ?>
		|| cacheSettings & <?php echo Atmail_Cache::TYPE_DAV ?>)
		{
			$("#Webmail_Settings #cacheType").parent().parent().show();
			$("TABLE#memCacheOptions").trigger('change');
		}
		else
		{
			$("#Webmail_Settings #cacheType").parent().parent().hide();
			$("TABLE#memCacheOptions").hide();
		}
	}

	$("#Webmail_Settings #remoteServersOverwrite, #Webmail_Settings #remoteDomainsOverwrite").change(function() { editpermissions(); });
	$("#Webmail_Settings #smtpauth, #Webmail_Settings #smtpserver").change(function() { smtpauth(); });
	
	$("#Webmail_Settings #cacheEnabledDav, #Webmail_Settings #cacheEnabledMail").change(function() { showCacheSettings(); });


	$('#back_button a', '#Webmail_Settings').bind("click", function() { $("#Flash", '#Webmail_Settings').hide(); return $('#settingsForm', '#Webmail_Settings').submit()})

	// bind to the form's submit event and cancel legacy submit
	$('#settingsForm', '#Webmail_Settings').submit(function()
	{
		var cacheEnabled = getCacheEnabled();

		$("#settingsForm", '#Webmail_Settings').find('#cacheEnabledMailHidden').val(cacheEnabled);
		$("#settingsForm", '#Webmail_Settings').find('#cacheEnabledMail').val(cacheEnabled);
		$.php($("#settingsForm", '#Webmail_Settings').attr("action"),$("#settingsForm", '#Webmail_Settings').serializeArray());
		return false;

	})

	$("#Webmail_Settings #mail_type").attr('value', "<?php echo $this->global['mail_type'] ?>");
	$("#Webmail_Settings #cssStyle").attr('value', "<?php echo $this->global['cssStyle'] ?>").trigger('change');
	$("#Webmail_Settings #cacheType").attr('value', "<?php echo (array_key_exists('cacheType', $this->global) ? $this->global['cacheType'] : 'fs'); ?>").trigger('change');


<?php 	if(empty($this->global['smtpauth_username'])) : ?>
	$("#Webmail_Settings #smtpserver").attr('checked', 1);
<?php 	else : ?>
<?php 		if (isset($this->global['smtp_auth']) && !empty($this->global['smtp_auth']) && $this->global['smtp_auth'] == 1) : ?>
	$("#Webmail_Settings #smtpauth").attr('checked', 1);
<?php 		else : ?>
	$("#Webmail_Settings #smtpserver").attr('checked', 1);
<?php 		endif ?>
<?php 	endif ?>

	editpermissions();
	smtpauth();

});
</script>
