include mixins

- var my_roles_string = JSON.stringify(roles);
- var my_already_defined_roles = JSON.stringify(already_defined_roles);

form(class="form-horizontal subroot full_height", id="form-roleeditor")
	.new_flex_zero(style="padding-bottom: 20px;")
		.container-fluid
			h4 Namen für den Dienst angeben

			input(type="hidden", name="role_uid", id="role_uid", value="#{role_uid}")
			button(class="btn btn-success btn-sm", type="submit", id="submit_button") 
				span(class="glyphicon glyphicon-ok", aria-hidden="true")
				span 
				span Übernehmen
				
			button(class="btn btn-warning btn-sm", type="button", onclick="goto_mainpage();") 
				span(class="glyphicon glyphicon-remove-circle", aria-hidden="true")
				span 
				span Abbrechen

	.new_flex_one
		.container-fluid
			.form-group
				label(for='role_name', class="col-sm-2 control-label") Name
				.col-sm-10
					input(type="text", name="role_name", id="role_name", class="col-sm-12 typeahead form-control", data-role_is_unique="bar", value="#{role_name}", required)
					.help-block.with-errors

	script.
		var substringMatcher = function(strs) {
			return function findMatches(q, cb) {
				var matches, substringRegex;

				// an array that will be populated with substring matches
				matches = [];

				// regex used to determine if a string contains the substring `q`
				substrRegex = new RegExp(q, 'i');

				// iterate through the pool of strings and for any string that
				// contains the substring `q`, add it to the `matches` array
				$.each(strs, function(i, str) {
					if (substrRegex.test(str)) {
						matches.push(str);
					}
				});

				cb(matches);
			};
		};

		var roles = !{my_roles_string};
								
		$("#role_name").typeahead({
			hint: true,
			highlight: true,
			minLength: 0
		},
		{
			name: 'roles',
			source: substringMatcher(roles)
		});
			
		$('#form-roleeditor').validator({ 
			custom: {
						role_is_unique: function(el) 
						{ 
							var already_defined_roles = !{my_already_defined_roles};
							
							for(idx in already_defined_roles)
							{
								if(already_defined_roles[idx] === $("#role_name").val())
									return "Ein Dienst mit gleichem Namen existiert bereits; bitte wählen Sie einen anderen Namen!";
							}
						}
					}
		}).on('submit', function(e) {
			if( ! e.isDefaultPrevented() )
			{
				e.preventDefault();
				
				var data = {
					name 		: $("#role_name").val(),
					uid			: $("#role_uid").val()
				};
				
				switch("#{type}")
				{
					case "add":
						pcal_pentry_edit_manager.event_add_role(data);
						break;
					case "edit":
						pcal_pentry_edit_manager.event_edit_role(data);
						break;
				}
				
			}
		});
