<div id="Flash" class="flash_notice_error"></div>

<?php $t = Zend_Registry::get('w4m70Yt0hYUdlM2r2lrrIuY0zgKG4A3vvftu4NbbCWfW'); if(!Zend_Registry::get('w4m70Yt0hYUduM2C2lrrIuY0zgKG4A3vvftl4NbbrWfW') ) : ?>
<div id="licence-error"><img class="regerror" src="<?php echo $this->siteBaseUrl; ?>/images/icons/error.png"/><span><?php echo $this->translate('Your Atmail license is invalid, use the "License" tab above to register a valid license') ?></span></div>
<?php 	elseif ( Zend_Registry::get('w4m70Yt0hYNduM2r2lrrIuY0zgKG4A3vvftl4UbbCWfW')==true ) : 
			$t = Zend_Registry::get('config')->global['install_type'];
			$storeUrl = "http://atmail.com/store/index.php?Type=" . ($t=='server'?'Server':'Webmail');
?>
<div id="licence-error"><img class="regerror" src="<?php echo $this->siteBaseUrl; ?>/images/icons/error.png"/><span><?php echo $this->translate("License(s) expired. Purchase a license via the <a href=\"" . $storeUrl . "\" target=\"_blank\">Atmail store</a>.") ?></span></div>
<?php 	elseif( $t[$this->defaultLicenseType] <= 0 ) : ?>
<div id="licence-error"><img class="regerror" src="<?php echo $this->siteBaseUrl; ?>/images/icons/error.png"/><span><?php echo $this->translate("You have reached the maximum user limit for your Atmail license, <a href='http://upgrade.atmail.com/' target='_blank'>upgrade your license</a> to add more seats") ?></span></div>
<?php 	elseif( Zend_Registry::get('config')->dovecot['authType'] == 'ldap' ) : ?>
<div id="licence-error"><img class="regerror" src="<?php echo $this->siteBaseUrl; ?>/images/icons/error.png"/><span><?php echo $this->translate('LDAP Authentication active. New users must be created directly with the LDAP server, then users can login via Webmail/POP3/IMAP/SMTP to Atmail.') ?></span></div>
<?php 	else : ?>
<form id="contactform" action="<?php echo $this->moduleBaseUrl ?>/users/create/" ENCTYPE="multipart/form-data">
<input type="hidden" name="NewContact" value="1">

<?php
$translationHash = array(
	'UserHomePhone' => $this->translate('Home Phone'),
	'UserHomeMobile' => $this->translate('Private Mobile'),
	'UserHomeFax' => $this->translate('Private Fax'),
	'UserWorkPhone' => $this->translate('Work Phone'),
	'UserWorkMobile' => $this->translate('Work Mobile'),
	'UserWorkFax' => $this->translate('Work Fax'),
	'UserHomeAddress' => $this->translate('Home Address'),
	'UserHomeCity' => $this->translate('Home City'),
	'UserHomeState' => $this->translate('Home State'),
	'UserHomeZip' => $this->translate('Home Zip'),
	'UserHomeCountry' => $this->translate('Home Country'),
	'UserWorkAddress' => $this->translate('Work Address'),
	'UserWorkCity' => $this->translate('Work City'),
	'UserWorkState' => $this->translate('Work State'),
	'UserWorkZip' => $this->translate('Work Zip'),
	'UserWorkCountry' => $this->translate('Work Country'),
	'Home' => $this->translate('Home'),
	'Work' => $this->translate('Work'),
	'Home Address' => $this->translate('Home Address'),
	'Work Address' => $this->translate('Work Address'),
	'UserFirstName' => $this->translate('First Name'),
	'UserMiddleName' => $this->translate('Middle Name'),
	'UserLastName' => $this->translate('Last Name'),
	'UserEmail' => $this->translate('Email Address'),
	'Phone' => $this->translate('Phone Number'),
	'Notes' => $this->translate('Notes'),
	'UserInfo' => $this->translate('More Information ...'),
	'UserDOB' => $this->translate('Date of Birth'),
	'UserTitle' => $this->translate('Title'),
	'UserWorkTitle' => $this->translate('Work Title'),
	'UserWorkDept' => $this->translate('Work Department'),
	'UserWorkCompany' => $this->translate('Company'), 
	'EmailAddress' => $this->translate('Email Address'), 
	'UserURL' => $this->translate('User URL'), 
	'username' => 'Username',
    'Username' => 'Username',
	'password' => 'Password',
  	'Password' => 'Password'

);

$emailTranslationHash = array(
	'UserEmail' => $this->translate('User Email 1'),
	'UserEmail2' => $this->translate('User Email 2'),
	'UserEmail3' => $this->translate('User Email 3'),
	'UserEmail4' => $this->translate('User Email 4'),
	'UserEmail5' => $this->translate('User Email 5')
);
?>

<table id="contact_table">
<tbody>

	<tr>
		<td colspan="2" class="contact_field">
			<h2><?php echo $this->translate('Create a new user') ?></h2>
			<br>
		</td>
	</tr>	
	
	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Username') ?> <span class='req'>*</span>
		</td>
		<td>
			<input name="username" id="username" value="" />
			<small><?php echo $this->translate('Enter alphanumeric characters') ?></small>
		</td>
	</tr>

	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Domain') ?> <span class='req'>*</span>
		</td>
		<td class="contact_domain">
			<div class="styled-select">
			        <span class="arrow"></span>
			<select name="domain" id="domain">
<?php 		if( count($this->domains) == 0 ) : ?>
				<option value="0"><?php echo $this->translate("No seats available.") ?></option>
<?php 		endif; ?>
<?php 		foreach( $this->domains as $domain ) : ?>
				<option value="<?php echo $domain['DomainName'] ?>"><?php echo $domain['DomainName'] ?></option>
<?php 		endforeach; ?>
			</select>
			</div>
		</td>
	</tr>
	
	<tr class="admin-divider">
	        <td colspan="2"><span></span></td>
	    </tr>

	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Password') ?> <span class='req'>*</span>
		</td>
		<td>
			<input type="text" name="Password" id="Password" value="<?php echo addslashes($this->contact['password']) ?>" />
            <small><?php echo $this->translate('Use generated random password or enter a custom password') ?></small>
						
		</td>
	</tr>
	
	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Firstname') ?>
		</td>
		<td>
			<input name="UserFirstName" id="UserFirstName" value="" />
		</td>
	</tr>

	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Lastname') ?>
		</td>
		<td>
			<input name="UserLastName" id="UserLastName" value="" />
		</td>
	</tr>
	
	<tr class="admin-divider">
	        <td colspan="2"><span></span></td>
	    </tr>
	
	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Total Quota') ?> <span class='req'>*</span>
		</td>
		<td>
			
			<input type="text" name="UserQuota" id="UserQuota" value="<?php echo $this->proposedUserNewQuota ?>">
			<small><?php echo $this->translate('Specify the disk quota in MB') ?></small>
			
		</td>
	</tr>

	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('User Group') ?>
		</td>
		<td class="contact_group">
			<div class="styled-select">
			        <span class="arrow"></span>
			<select name="Ugroup" id="Ugroup">
				<option value="" style="font-weight:bold;"><?php echo $this->translate('Domain group') ?></option>
<?php 		foreach( $this->groupsAllowed as $k => $group ) : ?>
<?php			if( $group == 'default' ) continue ?>
				<option value="<?php echo addslashes($group) ?>"><?php echo $group ?></option>
<?php 		endforeach ?>
			</select>
			</div>
			<small><?php echo $this->translate('Apply service profile defined in domain or user groups.') ?></small>
		</td>
	</tr>     
	
	<tr class="admin-divider">
	        <td colspan="2"><span></span></td>
	    </tr>  

<tr id='moreInformation'>
	<td class="contact_field">

		<div id='expandMoreInfo' class="<?php echo ( empty($this->contact['ExpandMoreOptions']) ) ? "moreinfo" : "lessinfo"; ?>"><span id="expandMoreInfoText"><?php echo $this->translate('More Information') ?></span></div>
				
	</td>
	<td>
		&nbsp;
	</td>
</tr>

</table>

<table class='moreInformationField' style='display: <?php echo ( empty($this->contact['ExpandMoreOptions']) ) ? "none" : ""; ?>'>

<?php
	$allNumberFields = array('UserHomePhone', 'UserHomeMobile', 'UserHomeFax', 'UserWorkPhone', 'UserWorkMobile', 'UserWorkFax');

	$unusedNumberFields = array();
	$usedNumberFields = array();
	foreach( $allNumberFields as $k ) 
	{
		if( strlen($this->contact[$k]) == 0 )
			$unusedNumberFields[] = $k;
		else
			$usedNumberFields[] = $k;
	}

	//onchange, populate all selects with remaining options + currently selected type (dependant select group)
	$usedNumberFieldsC = count($usedNumberFields);
	for($a = -1 ; $a < $usedNumberFieldsC ; $a++ ) :
		
		// use the -1 pass to check for zero fields populated then add a blank field, else continue to 0 onwards like normal
        if( $a == -1 && count($usedNumberFields) > 0 )
			continue;
		if($a == -1) 
			$k = $allNumberFields[0];
		else 
			$k = $usedNumberFields[$a];
 
?>
	<tr>
		<td class="contact_field">
			<script type="text/javascript" language="JavaScript">
            	$allNumberFields = {<?php foreach($allNumberFields as $option) echo "'" . $option . "':'" . translateHash($option, $translationHash) . "', " ?> '':''}
			</script>
			<div class="styled-select">
			        <span class="arrow"></span>
			<select name="numberFieldName[]">
				<option selected value="<?php echo addslashes($k) ?>"><?php echo translateHash($k, $translationHash) ?>&nbsp;&nbsp;</option>
				<?php foreach( $unusedNumberFields as $k2 ) { echo '<option value="' . addslashes($k2) . '">' . translateHash($k2, $translationHash) . '</option>' . "\n			";	} ?>

			</select>
			</div>
		</td>
		<td class='contact_field_phone'>
			<input id="Phone" name="numberValue[]" value="" />
			<img src="<?php echo $this->siteBaseUrl ?>images/icons/btn_remove.png" width="12" height="12" alt="remove" onclick="removeClicked(this, $allNumberFields)" style="cursor:pointer">
			<img src="<?php echo $this->siteBaseUrl ?>images/icons/btn_add.png" width="12" height="12" alt="add" onclick="addClicked(this, $allNumberFields)" style="cursor:pointer;  <?php
			if($a+1 < $usedNumberFieldsC || $a+1 == count($allNumberFields) ) echo ' display:none;'	?>">
		</td>
	</tr>
<?php endfor; ?>

	<!-- Address div -->
	<?php
	$AddressName = 'Home Address';
	?>
	<tr id='UserHome'>
		<td class="contact_field" valign='top'>
			<?php echo translateHash('Home Address', $translationHash) ?>
		</td>
		<td>		
			<input name="UserHomeAddress" id="UserHomeAddress" value="" />
			<br/>
			<input name="UserHomeCity" id="UserHomeCity" value="" />
			<br/>
			<input name="UserHomeState" id="UserHomeState" value="" />
			<br/>
			<input name="UserHomeZip" id="UserHomeZip" value="" />
			<br/>
			<input name="UserHomeCountry" id="UserHomeCountry" value="" />
			<br/>

		</td>
	</tr>
	<tr class="admin-divider">
	        <td colspan="2"><span></span></td>
	    </tr>
	<tr id='UserWork'>
		<td class="contact_field" valign='top'>
			<?php echo addslashes(translateHash('Work Address', $translationHash)) ?>
		</td>
		<td>		
			<input name="UserWorkAddress" id="UserWorkAddress" value="" />
			<br/>
			<input name="UserWorkCity" id="UserWorkCity" value="" />
			<br/>
			<input name="UserWorkState" id="UserWorkState" value="" />
			<br/>
			<input name="UserWorkZip" id="UserWorkZip" value="" />
			<br/>
			<input name="UserWorkCountry" id="UserWorkCountry" value="" />
			<br/>

		</td>
	</tr>
	
	<tr class="admin-divider">
	        <td colspan="2"><span></span></td>
	    </tr>
<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserWorkCompany', $translationHash) ?>	
	</td>
	<td>
		<input name="UserWorkCompany" id="UserWorkCompany" value="" />
	</td>
</tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserTitle', $translationHash) ?>	
	</td>
	<td>
		<input name="UserTitle" id="UserTitle" value="" />
	</td>
</tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserMiddleName', $translationHash) ?>	
	</td>
	<td>
		<input name="UserMiddleName" id="UserMiddleName" value="" />
	</td>
</tr>

<tr class="admin-divider">
        <td colspan="2"><span></span></td>
    </tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserDOB', $translationHash) ?>	
	</td>
	<td>
		<input name="UserDOB" id="UserDOB" value="" class='datepicker'/>
	</td>
</tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserURL', $translationHash) ?>	
	</td>
	<td>
		<input name="UserURL" id="UserURL" value="" />
	</td>
</tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserWorkTitle', $translationHash) ?>	
	</td>
	<td>
		<input name="UserWorkTitle" id="UserWorkTitle" value="" />
	</td>
</tr>

<tr class="admin-divider">
        <td colspan="2"><span></span></td>
    </tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserWorkDept', $translationHash) ?>	
	</td>
	<td>
		<input name="UserWorkDept" id="UserWorkDept" value="" />
	</td>
</tr>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('Notes', $translationHash) ?>	
	</td>
	<td>
		<textarea name="UserInfo" id='UserInfo'></textarea>
	</td>
</tr>

</tbody>
</table>

<div id="back_button" class='savecontact' style="margin-left: 17px;"><a href=""><span>Create<strong> User</strong></span></a></div>

</form>
<?php endif; ?>
<script type="text/javascript" language="JavaScript">
$( function() {  

	// Add the contactid for a "New" record
	$('.user_row[contactid=""]').find('.contactRow').attr('value', '<?php echo $this->contact['id'] ?>');
	$('.user_row[contactid=""]').attr('contactid', '<?php echo $this->contact['id'] ?>');

	// Specify which fields can be edited, numeric, alpha, etc
	$('#UserQuota').numeric();
	$('#username').alphanumeric({allow:".-"});
    $('#username').focus();
	
	var domain = $('#nav_secondary LI.current').attr('DomainName');

	// Set default lengths
	$('#contactform input:text').attr('maxlength', '128');
	
	domainsAllowed = new Array("<?php echo implode('", "', $this->domainsAllowed) ?>");
	groupsAllowed = new Array("<?php echo implode('", "', $this->groupsAllowed) ?>");

	// Translations of default state names
	formNames = new Array();
<?php
	foreach( $translationHash as $hash => &$translation )
		echo "\tformNames['" . $hash . "'] = \"" . addslashes(translateHash($hash, $translationHash)) . "\"\n";
?>
	
	// Remove default name suggestions
	$('input, textarea', '#contactform').bind('focus click', function() 
	{
		
		if( formNames[$(this).attr('id')] && $(this).attr('value') == formNames[$(this).attr('id')] )
		{ 
			
			$(this).attr('value','')
			$(this).select()
			$(this).removeClass('default')
			
		}
		
	})

	$('input:not(#UserQuota):not(#username), textarea', '#contactform').bind('blur', function() 
	{
		
		if( !$(this).attr('value') || $(this).attr('value') == formNames[$(this).attr('id')]) 
		{

			$(this).attr('value', formNames[$(this).attr('id')] )
			$(this).attr('class', 'default')

		}
		
	})
	
	//prepopulate empty fields
	$('input, textarea', '#contactform').trigger('blur')
	
	function updateUsernameDescription()
	{
		var username = $('#username','#contactform').is('.default') ? '' : $('#username', '#contactform').attr('value');
		var domain = $('#domain', '#contactform').attr('value')
		var firstname = $('#UserFirstName','#contactform').is('.default') ? '' : $('#UserFirstName', '#contactform').attr('value');
		var lastname = $('#UserLastName','#contactform').is('.default') ? '' : $('#UserLastName', '#contactform').attr('value');
			
		if(firstname == '' && lastname == '' && username == '')
			return;
		
		$(".user_row[contactid=<?php echo $this->contact['Account'] ?>]").find('.first_name').html( htmlentities(firstname) );
		$(".user_row[contactid=<?php echo $this->contact['Account'] ?>]").find('.last_name').html( htmlentities(lastname) );
		$(".user_row[contactid=<?php echo $this->contact['Account'] ?>]").find('.email').html( htmlentities(username + '@' + domain) );
	}
	
	// update the Firstname / Lastname on the view
	$('#UserFirstName', '#contactform').bind('blur', function() 
	{
		//if default value then dont update
		if( $(this).attr('value') == formNames[$(this).attr('id')] || $(this).attr('value') == '' )
			var string = '';
		else
			var string = htmlentities($(this).attr('value'))
			
		updateUsernameDescription();
	})
	
	$('#UserLastName', '#contactform').bind('blur', function() 
	{
		
		//if default value then dont update
		if( $(this).attr('value') == formNames[$(this).attr('id')] || $(this).attr('value') == '' )
			var string = '';
		else
			var string = htmlentities($(this).attr('value'))

		updateUsernameDescription();
	})
	
	$('#username, #domain', '#contactform').bind('keyup', function()
	{
		var currentUsername = $('#username', '#contactform').val();
		var currentDomain = $('#domain', '#contactform').val();
		if(currentUsername.indexOf(currentDomain) != -1)
		{
			$.simpleFlash("<?php echo $this->translate('WARNING : The username entered contains the domain name.');?>", 'important');	
		}
	});
	
	$('#username, #domain', '#contactform').bind('blur change', function() 
	{
		//if default value then dont update
		username = $('#username', '#contactform').attr('value')
		domain = $('#domain', '#contactform').attr('value')
		if( username == formNames['username'] )
			username = ''	
		updateUsernameDescription();	
	})

	// Address handling to remove/set Home defaults
	$('#deleteUserWork').bind('click', function() {
		$.each( ['#UserWorkAddress', '#UserWorkCity', '#UserWorkState', '#UserWorkZip', '#UserWorkCountry'], function(i, n) {
		  $(n).attr('value',  formNames[$(n).attr("id")] );
		  $(n).addClass('default');
		});			
	});

	// Address handling to remove/set Work defaults
	$('#deleteUserHome').bind('click', function() {
		$.each( ['#UserHomeAddress', '#UserHomeCity', '#UserHomeState', '#UserHomeZip', '#UserHomeCountry'], function(i, n) {
		  $(n).attr('value',  formNames[$(n).attr("id")] );
		  $(n).addClass('default');
		});			
	});

	// Show more information ( Notes )
	$('#expandMoreInfo').bind('click', function() {
		
		if($('.moreInformationField').is(':hidden')) {
			$('.moreInformationField').show();
			$('#expandMoreInfo').removeClass('moreinfo');
			$('#expandMoreInfo').addClass('lessinfo');
		} else {
			$('.moreInformationField').hide();
			$('#expandMoreInfo').removeClass('lessinfo');
			$('#expandMoreInfo').addClass('moreinfo');
		}
		
		 $('#UserInfo').attr('value',  formNames[$('#UserInfo').attr("id")] );
		 $('#UserInfo').addClass('default');
		
	});
	
	// UserDOB toggle
	$('#UserDOB').datepicker(
	{ 
		showStatus: true, 
		initStatus: 'UserDOB', 
		rangeSelect: false, 
		defaultDate: 'today',
		dateFormat: 'yy-mm-dd',
		yearRange: '-90:+25',
		defaultDate: '-25y', 
		changeMonth: true, 
		changeYear: true 
    })
	
	$('form#contactform').submit( function() 
	{ 

		//if domain group selected and domain not in allowed domains then fail (only allowed groups will show)

		if( !$(this).attr("action") ) 
		{
			alert('Form submission aborted: Action must be set')
			return false
		}

		// Form validation
		if( !$("#username", '#contactform').attr('value') ) 
		{
			alert("Please specify a username")
			$("#username").focus()
			return false
		}

        //Password checks
		//if($("#password", '#contactform').attr('value').length < 4)
		//{
		//	alert("Please specify a password with at least 4 characters")
		//	$("#password").focus()
		//	return false
		//}

		if(!$("#UserQuota", '#contactform').attr('value')) 
		{
			alert("Please specify a user quota")
			$("#UserQuota").focus()
			return false
		}
		
		// Set all default values to null, not to pollute the DB with default text
		$("#contactform .default").attr('value', '')
		//$("#contact_table .default").attr('value', '');
		//$('.full_name .default').attr('value', '');
		
		$.php( $(this).attr("action"), $(this).serializeArray() )
		
		$('#action_contact_deleteuser a').attr('disabled', '');
		$('#action_contact_edituser a').attr('disabled', '');

		return false

	})

	// bind to the form's submit event and cancel legacy submit 
	$('.savecontact').bind('click', function() { $('form#contactform').submit(); return false; })

	//select currently selected domain in domain dropdown
	if( $('ul#nav_secondary', '#Users').children('li.current').attr('grouptype') == 'domain' )
	{

		domainname = $('ul#nav_secondary', '#Users').children('li.current').attr('domainname');
		$('form#contactform select#domain option', '#Users').each( function(i, selected) 
		{
			
			if( $(selected).attr('value') == domainname )
			{
				
				$(this).parent().attr('selectedIndex', i)
				return
				
			}

		})
			
	}
	
	//select currently selected group in domain dropdown
	if( $('ul#nav_secondary', '#Users').children('li.current').attr('grouptype') != 'domain' )
	{

		groupname = $('ul#nav_secondary', '#Users').children('li.current').attr('groupname');
		$('form#contactform select#Ugroup option', '#Users').each( function(i, selected) 
		{
			
			if( $(selected).attr('value') == groupname )
			{
				
				$(this).parent().attr('selectedIndex', i)
				return
				
			}

		})
			
	}
	
	
})

</script>