<?php 	if( 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 	else : ?>
<?php 		if( !is_array($this->contact) ) : ?>
<?php echo $this->translate('No user to edit') ?>
<?php 		else: ?>

<form id="contactform" action="<?php echo $this->moduleBaseUrl ?>/users/update/" ENCTYPE="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $this->contact['id'] ?>">
<input type="hidden" name="domain" value="<?php echo $this->domain ?>">
<input type="hidden" name="group" value="<?php echo $this->group ?>">
<input name="Account" id="Account" type="hidden" value="<?php echo $this->contact['Account'] ?>" />

<?php
	$username = ($pos = strpos($this->contact['Account'], '@')) !== false ? substr($this->contact['Account'], 0, $pos) : $this->contact['Account'];

$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')
);

$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')
);

?>
<div id="Flash"></div>	

<table id="contact_table">
<tbody>
	<tr>
		<td colspan="2" class="contact_field">
			<h2><?php echo $this->translate('Edit user') ?></h2>
		</td>
	</tr>	
	
	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('User Photo') ?>
		</td>
		<td id="UserPhoto" class="photoEditable" title='<?php print $this->translate('Click to upload a User Photo')?>'>
			<div>
<?php 			$src = $this->moduleBaseUrl . '/users/viewphoto/id/' . $this->contact['id'] . '/shadow/' . (strlen($this->contact['UserPhoto'])>10?md5($this->contact['UserPhoto']):rand(0,9999)); ?>
				<img id="Photo" class="photoEditable" src="<?php echo $src ?>" style="max-height: 80px; max-width: 60px;" />
			</div>
		</td>
		
	</tr>

	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Email Address') ?>
		</td>
		<td>
			<?php echo $this->contact['Account'] ?>			
		</td>
	</tr>
	
	<tr>
		<td class="contact_field" align='top'>
			User Group
		</td>
		<td class="user-group">
			<div class="styled-select">
			        <span class="arrow"></span>
					<select name="Ugroup">
						<option value="" style="font-weight:bold;"><?php echo $this->translate('Domain group') ?></option>
		<?php 			foreach( $this->groups as $group ) : ?>
						<?php if( $group == 'default') continue; ?>
						<option value="<?php echo $group ?>"<?php echo $group==$this->contact['Ugroup']?' selected':'' ?>><?php echo $group ?></option>
		<?php			endforeach ?>
					</select>
			 </div>
					<small><?php echo $this->translate('Apply service profile defined in domain or user group.') ?></small>
		</td>
	</tr>

    <tr class="admin-divider">
        <td colspan="2"><span></span></td>
    </tr>
	
	<tr class="space">	
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Username') ?> <span class='req'>*</span>
		</td>
		<td>
			<input name="NewUsername" id="NewUsername" value="<?php echo addslashes($username) ?>" />
		</td>
	</tr>
	<tr class="space">	
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Firstname') ?> <span class='req'>*</span>
		</td>
		<td>
			<input name="UserFirstName" id="UserFirstName" value="<?php echo $this->contact['UserFirstName'] ?>" />
		</td>
	</tr>

	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Lastname') ?> <span class='req'>*</span>
		</td>
		<td>
			<input name="UserLastName" id="UserLastName" value="<?php echo $this->contact['UserLastName'] ?>" />
		</td>
	</tr>

    <?php if (!$this->isExternal) { ?>
	<tr>
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Password') ?>
		</td>
		<td>
            <input type="Password" name="Password" id="Password" value="<?php echo $this->contact['Password'] ?>" />
			<small><?php echo $this->translate('Optionally specify a new password') ?></small>
		</td>
	</tr>
    <?php
    }
    ?>

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

	<tr class="space">
		<td class="contact_field" align='top'>
			<?php echo $this->translate('Quota Usage') ?>
		</td>
		<td>
			<input type="text" name="UserQuota" id="UserQuota" value="<?php echo addslashes($this->contact['UserQuota']) ?>" />
<?php 		if($this->global['install_type'] == 'server') : ?>
			<small><?php echo $this->translate('Currently using') ?> <?php echo $this->contact['UsedQuota']; ?> <?php echo $this->translate('MB of quota') ?>
					<?php if(!empty($this->contact['UsedQuota'])) : ?>
					 (<?php echo round( ($this->contact['UsedQuota'] / $this->contact['UserQuota']) * 100, 1); ?>%)
					<?php endif ?>
			</small>
<?php endif ?>
			
		</td>
	</tr>
	
	<tr>
		<td class="contact_field" align='top'><?php echo $this->translate('User Enabled') ?>
		</td>
		<td>
		    <input name="UserStatus" type='hidden' value='1' />
			<input name="UserStatus" type="checkbox" id="UserStatus" value="0"<?php echo ($this->contact['UserStatus']==0?' checked':'') ?> />
			
		</td>
	</tr>
    <tr class="admin-divider">
        <td colspan="2"><span></span></td>
    </tr>
<?php $allNumberFields = array('UserHomePhone', 'UserHomeMobile', 'UserHomeFax', 'UserWorkPhone', 'UserWorkMobile', 'UserWorkFax'); ?>
<script type="text/javascript" language="JavaScript">
	$allNumberFields = {<?php foreach($allNumberFields as $option) echo "'" . $option . "':'" . translateHash($option, $translationHash) . "', " ?> '': ''}
</script>
<?php
	$unusedNumberFields = array();
	$usedNumberFields = array();
	foreach( $allNumberFields as $kv ) 
	{
	
		if( strlen($this->contact[$kv]) == 0 )
			$unusedNumberFields[] = $kv;
		else
			$usedNumberFields[] = $kv;
	
	}
	//if record empty/new then pop first unused off and push it onto used cos we need to show at least one option in the ui
	if( count($usedNumberFields) == 0 )
		$usedNumberFields[] = array_shift($unusedNumberFields);
				
	//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 class="dependantNumberFields">
		<td class="contact_field">
			<div class="styled-select">
			        <span class="arrow"></span>
			<select name="numberFieldName[]" class="dependantMultiSelect">
				<option selected value="<?php echo $k ?>"><?php echo translateHash($k, $translationHash) ?>&nbsp;&nbsp;</option>
				<?php foreach( $unusedNumberFields as $k2 ) { echo '<option value="' . $k2 . '">' . translateHash($k2, $translationHash) . '</option>' . "\n			";	} ?>

			</select>
			</div>
		</td>
		<td class='contact_field_phone'>
			<input id="Phone" name="numberValue[]" value="<?php echo addslashes($this->contact[$k]) ?>" />
			<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 -->
<tr class="space" id='UserHome'>
	<td class="contact_field" valign='top'>
		<?php echo $this->translate('Home Address') ?>
	</td>
	<td>
		<input name="UserHomeAddress" id="UserHomeAddress" value="<?php echo addslashes($this->contact['UserHomeAddress']) ?>" />
		<br/>
		<input name="UserHomeCity" id="UserHomeCity" value="<?php echo addslashes($this->contact['UserHomeCity']) ?>" />
		<br/>
		<input name="UserHomeState" id="UserHomeState" value="<?php echo addslashes($this->contact['UserHomeState']) ?>" />
		<br/>
		<input name="UserHomeZip" id="UserHomeZip" value="<?php echo addslashes($this->contact['UserHomeZip']) ?>" />
		<br/>
		<input name="UserHomeCountry" id="UserHomeCountry" value="<?php echo addslashes($this->contact['UserHomeCountry']) ?>" />
		<br/>
	</td>
</tr>

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

<tr class="space" 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" : ""; ?>'>

<tr>
	<td class="contact_field" align='top'>
		<?php echo translateHash('UserWorkCompany', $translationHash) ?>	
	</td>
	<td>
		<input name="UserWorkCompany" id="UserWorkCompany" value="<?php echo addslashes($this->contact['UserWorkCompany']) ?>" />
	</td>
</tr>

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

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

<?php
$UserDOB = $this->contact['UserDOB'];
$UserDOB = str_replace("00:00:00", '', $UserDOB);
?>

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

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

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

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

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

</tbody>
</table>

<div id="back_button" class='savecontact'><a href=""><span>Update<strong> User</strong></span></a></div>

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

	$('select.dependantMultiSelect', '#Users').each( function() {
		$(this).bind('change', function() {
			//var_dump($allNumberFields) 
			updateDependantSelects( $(this).attr('name'), $allNumberFields )
		})
	})
	
    // Make sure the password row is set to the stored password ( no browser trying to set )
    //$('#Password').attr('value', '<?php $this->contact['Password'] ?>')

	// 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'] ?>');
	
	// Configure Ajax uploader for image
	new AjaxUpload('#UserPhoto', {
		 
		// Location of the server-side upload script
		action: "<?php echo $this->moduleBaseUrl ?>/users/addphoto",
		 
		// File upload name
		name: 'UserPhoto',
		 
		// Additional data to send
		data: {
			id: '<?php echo $this->contact['id'] ?>'
		},
		
		// Submit file after selection
		autoSubmit: true,
		
		onSubmit: function(file, extension) { 
			extension = extension.toLowerCase();
			if (! (extension && /^(jpg|png|jpeg|gif)$/.test(extension))){
				// extension is not allowed
				alert('<?php echo $this->translate("Error: Invalid file extension - Jpeg, PNG or GIF allowed") ?>');
				// cancel upload
				return false;
			}  

			// Replace the existing image with a spinner
			$("#Photo").attr('src', '<?php echo $this->siteBaseUrl ?>images/25.gif');
			$("#Photo").hide();
			$($("#Photo")[0]).load(function() { $("#Photo").show(); });

		},

		onComplete: function(file, response) {
		
			$('#Photo').attr('src', "<?php echo $this->moduleBaseUrl ?>/users/viewphoto/id/<?php echo $this->contact['id'] ?>/shadow/1/" + Math.round(Math.random() * 9999));			
		
			$($("#Photo")[0]).load(function() { $("#Photo").css('padding', '0px'); });
		}

	});
	
	var domain = $('#nav_secondary LI.current').attr('DomainName');

	$('#username').focus();
	$('#domain').attr('value', domain);
	
	// Set default lengths
	$('#contactform input:text').attr('maxlength', '128');
	
	// 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:not(#Password), 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(#Password), textarea', '#contactform').bind('blur', function()
	{
		//alert(fieldObject.id);
		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:not(#Password), textarea', '#contactform').trigger('blur')
	
	// Live 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'))
		$(".user_row[contactid=<?php echo $this->contact['Account'] ?>]").find('.first_name').html(string)
	
	})
	
	$('#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'))
		$(".user_row[contactid=<?php echo $this->contact['Account'] ?>]").find('.last_name').html(string)
	
	})
	
	$('#username, #domain', '#contactform').bind('blur', function() 
	{
		
		//if default value then dont update
		username = $('#username', '#contactform').attr('value')
		domain = $('#domain', '#contactform').attr('value')
		if( username == formNames[username] )
			username = ''
		if( domain == formNames[domain] )
			domain = ''
			
		$(".user_row[contactid=<?php echo $this->contact['Account'] ?>]").find('.email').html( htmlentities(username) + '@' + htmlentities(domain) )
	
	})
	$('#NewUsername', '#contactform').bind('blur', function() 
	{	
		domain = "<?php $temp = explode('@', $this->contact['Account']); if(isset($temp[1])) echo $temp[1]; ?>";
		selector = $(".user_row[contactid=<?php echo $this->contact['Account'] ?>]");
		
		//if default value then dont update
		username = $('#NewUsername', '#contactform').attr('value')

		if( username == formNames[username] )
			username = ''
		
		selector.attr('contactid', htmlentities(username) + '@' + htmlentities(domain));
		selector.find('.email').html( htmlentities(username) + '@' + htmlentities(domain) );
		selector.find('.contactRow').attr('value',	htmlentities(username) + '@' + htmlentities(domain) );
		
	})
	
	// 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('fast');
			$('#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, 
		dateFormat: 'yy-mm-dd',
		yearRange: '-90:+25',
		defaultDate: '-25y', 
		changeMonth: true, 
		changeYear: true, 
		onSelect: function(input, inst)
		{ 
			$(this).removeClass('default');
		}
    });
	
	// bind to the form's submit event and cancel legacy submit 
    $('.savecontact').bind('click', function() { $('form#contactform').submit(); return false; })
	
	$('form#contactform').submit(function(event) 
	{ 

		if( $(this).attr("action") == false ) 
		{
			alert('Form submission aborted: Action must be set');
			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', '');

		//$('#contactform').ajaxSubmit(); 

		//try show previously hidden the top right button (edit group settings) so not mistaken for save user button
		$("#top_right_button", "#Users").show();
		$.php($(this).attr("action"),$(this).serializeArray());
        return false

	})

	$('#UserStatus').iphoneStyle();

	//try hide the top right button (edit group settings) so not mistaken for save user button
	$("#top_right_button", "#Users").hide();


})
</script>
<?php 		endif;
		endif;
