1<script type="text/x-jquery-tmpl" id="exec_params_row">
2	<tr class="form_row">
3		<td>
4			<input type="text" id="exec_params_#{rowNum}_exec_param" name="exec_params[#{rowNum}][exec_param]" maxlength="255" style="width: <?= ZBX_TEXTAREA_STANDARD_WIDTH ?>px;">
5		</td>
6		<td>
7			<button type="button" id="exec_params_#{rowNum}_remove" name="exec_params[#{rowNum}][remove]" class="<?= ZBX_STYLE_BTN_LINK ?> element-table-remove"><?= _('Remove') ?></button>
8		</td>
9	</tr>
10</script>
11<script type="text/javascript">
12	jQuery(document).ready(function($) {
13		var old_media_type = $('#type').val();
14
15		// type of media
16		$('#type').change(function() {
17			var media_type = $(this).val();
18
19			switch (media_type) {
20				case '<?= MEDIA_TYPE_EMAIL ?>':
21					$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #smtp_security, #smtp_authentication')
22						.closest('li')
23						.show();
24					$('#exec_path, #gsm_modem, #jabber_username, #eztext_username, #eztext_limit, #exec_params_table')
25						.closest('li')
26						.hide();
27					$('#eztext_link').hide();
28
29					// radio button actions
30					toggleSecurityOptions();
31					toggleAuthenticationOptions();
32					setMaxSessionsType(media_type);
33
34					$('#passwd').parent().prev().find('label').removeClass('<?= ZBX_STYLE_FIELD_LABEL_ASTERISK ?>');
35					break;
36
37				case '<?= MEDIA_TYPE_EXEC ?>':
38					$('#exec_path, #exec_params_table').closest('li').show();
39					$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #gsm_modem, #jabber_username, #eztext_username, #eztext_limit, #passwd, #smtp_verify_peer, #smtp_verify_host, #smtp_username, #smtp_security, #smtp_authentication')
40						.closest('li')
41						.hide();
42					$('#eztext_link').hide();
43					setMaxSessionsType(media_type);
44					break;
45
46				case '<?= MEDIA_TYPE_SMS ?>':
47					$('#gsm_modem').closest('li').show();
48					$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #exec_path, #jabber_username, #eztext_username, #eztext_limit, #passwd, #smtp_verify_peer, #smtp_verify_host, #smtp_username, #smtp_security, #smtp_authentication, #exec_params_table')
49						.closest('li')
50						.hide();
51					$('#eztext_link').hide();
52					setMaxSessionsType(media_type);
53					break;
54
55				case '<?= MEDIA_TYPE_JABBER ?>':
56					$('#jabber_username, #passwd').closest('li').show();
57					$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #exec_path, #gsm_modem, #eztext_username, #eztext_limit, #smtp_verify_peer, #smtp_verify_host, #smtp_username, #smtp_security, #smtp_authentication, #exec_params_table')
58						.closest('li')
59						.hide();
60					$('#eztext_link').hide();
61					setMaxSessionsType(media_type);
62
63					$('#passwd').parent().prev().find('label').addClass('<?= ZBX_STYLE_FIELD_LABEL_ASTERISK ?>');
64					break;
65
66				case '<?= MEDIA_TYPE_EZ_TEXTING ?>':
67					$('#eztext_username, #eztext_limit, #passwd').closest('li').show();
68					$('#eztext_link').show();
69					$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #exec_path, #gsm_modem, #jabber_username, #smtp_verify_peer, #smtp_verify_host, #smtp_username, #smtp_security, #smtp_authentication, #exec_params_table')
70						.closest('li')
71						.hide();
72					setMaxSessionsType(media_type);
73
74					$('#passwd').parent().prev().find('label').addClass('<?= ZBX_STYLE_FIELD_LABEL_ASTERISK ?>');
75					break;
76			}
77		});
78
79		// clone button
80		$('#clone').click(function() {
81			$('#mediatypeid, #delete, #clone').remove();
82			$('#chPass_btn').hide();
83			$('#passwd').prop('disabled', false).show();
84			$('#update').text(<?= CJs::encodeJson(_('Add')) ?>);
85			$('#update').val('mediatype.create').attr({id: 'add'});
86			$('#description').focus();
87		});
88
89		// Trim spaces on sumbit. Spaces for script parameters should not be trimmed.
90		$('#media_type_form').submit(function() {
91			var maxattempts = $('#maxattempts'),
92				maxsessions_type = $('#maxsessions_type :radio:checked').val(),
93				maxsessions = $('#maxsessions');
94
95			if ($.trim(maxattempts.val()) === '') {
96				maxattempts.val(0);
97			}
98
99			if (maxsessions_type !== 'custom') {
100				maxsessions.val(maxsessions_type === 'one' ? 1 : 0);
101			}
102			else if (maxsessions_type === 'custom' && $.trim(maxsessions.val()) === '') {
103				maxsessions.val(0);
104			}
105
106			$(this).trimValues([
107				'#description', '#smtp_server', '#smtp_port', '#smtp_helo', '#smtp_email', '#exec_path', '#gsm_modem',
108				'#jabber_username', '#eztext_username', '#smtp_username', '#maxsessions'
109			]);
110		});
111
112		$('#maxsessions_type :radio').change(function() {
113			toggleMaxSessionsVisibility($(this).val());
114		});
115
116		// Refresh field visibility on document load.
117		$('#type').trigger('change');
118		$('#maxsessions_type :radio:checked').trigger('change');
119
120		$('input[name=smtp_security]').change(function() {
121			toggleSecurityOptions();
122		});
123
124		$('input[name=smtp_authentication]').change(function() {
125			toggleAuthenticationOptions();
126		});
127
128		/**
129		 * Show or hide "SSL verify peer" and "SSL verify host" fields.
130		 */
131		function toggleSecurityOptions() {
132			if ($('input[name=smtp_security]:checked').val() == <?= SMTP_CONNECTION_SECURITY_NONE ?>) {
133				$('#smtp_verify_peer, #smtp_verify_host').prop('checked', false).closest('li').hide();
134			}
135			else {
136				$('#smtp_verify_peer, #smtp_verify_host').closest('li').show();
137			}
138		}
139
140		/**
141		 * Show or hide "Username" and "Password" fields.
142		 */
143		function toggleAuthenticationOptions() {
144			if ($('input[name=smtp_authentication]:checked').val() == <?= SMTP_AUTHENTICATION_NORMAL ?>) {
145				$('#smtp_username, #passwd').closest('li').show();
146			}
147			else {
148				$('#smtp_username, #passwd').val('').closest('li').hide();
149			}
150		}
151
152		/**
153		 * Show or hide concurrent sessions custom input box.
154		 *
155		 * @param {string} maxsessions_type		Selected concurrent sessions value. One of 'one', 'unlimited', 'custom'.
156		 */
157		function toggleMaxSessionsVisibility(maxsessions_type) {
158			var maxsessions = $('#maxsessions');
159
160			if (maxsessions_type === 'one' || maxsessions_type === 'unlimited') {
161				maxsessions.hide();
162			}
163			else {
164				maxsessions.show().select().focus();
165			}
166		}
167
168		/**
169		 * Set concurrent sessions accessibility.
170		 *
171		 * @param {number} media_type		Selected media type.
172		 */
173		function setMaxSessionsType(media_type) {
174			var maxsessions_type = $('#maxsessions_type :radio');
175
176			if (media_type == <?= MEDIA_TYPE_SMS ?>) {
177				maxsessions_type.prop('disabled', true).filter('[value=one]').prop('disabled', false);
178			}
179			else {
180				maxsessions_type.prop('disabled', false);
181			}
182
183			if (old_media_type != media_type) {
184				old_media_type = media_type;
185				maxsessions_type.filter('[value=one]').click();
186			}
187		}
188
189		$('#exec_params_table').dynamicRows({ template: '#exec_params_row' });
190
191		$('#chPass_btn').on('click', function() {
192			$(this).hide();
193			$('#passwd')
194				.show()
195				.prop('disabled', false)
196				.focus();
197		});
198	});
199</script>
200