include mixins
	
form.form-horizontal.subroot#form-involvededitor
	.new_flex_zero(style="padding-bottom: 20px;")
		.container-fluid
			h4 Beteiligten aus manuellen Angaben hinzufügen
	
			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="history.back();") 
				span(class="glyphicon glyphicon-remove-circle", aria-hidden="true")
				span 
				span Abbrechen
	
	.new_flex_one(style="padding-bottom: 200px;")
		.container-fluid
			.form-group
				label(for='edit_group', class="col-sm-2 control-label") Gruppe
				.col-sm-10
					input(id='edit_group', type="text", class="form-control noEnterSubmit", value="#{involved.group}")
					.help-block.with-errors

			.form-group
				label(for='edit_surname', class="col-sm-2 control-label") Nachname
				.col-sm-10
					input(id='edit_surname', type="text", class="form-control noEnterSubmit", value="#{involved.surname}", required)
					.help-block.with-errors
			
			.form-group
				label(for='edit_forename', class="col-sm-2 control-label") Vorname
				.col-sm-10
					input(id='edit_forename', type="text", class="form-control noEnterSubmit", value="#{involved.forename}")
					.help-block.with-errors

			.form-group
				label(for='edit_street', class="col-sm-2 control-label") Straße
				.col-sm-10
					input(id='edit_street', type="text", class="form-control noEnterSubmit", value="#{involved.street}")
					.help-block.with-errors

			.form-group
				label(for='edit_street_nr', class="col-sm-2 control-label") Hausnummer
				.col-sm-3
					input(id='edit_street_nr', type="text", class="form-control noEnterSubmit", value="#{involved.street_nr}")
					.help-block.with-errors
						
			.form-group
				label(for='edit_postalcode', class="col-sm-2 control-label") Postleitzahl
				.col-sm-3
					input(id='edit_postalcode', type="text", class="form-control noEnterSubmit", value="#{involved.postalcode}")
					.help-block.with-errors

			.form-group
				label(for='edit_location', class="col-sm-2 control-label") Ort
				.col-sm-10
					input(id='edit_location', type="text", class="form-control noEnterSubmit", value="#{involved.location}")
					.help-block.with-errors

			.form-group
				label(for='edit_email', class="col-sm-2 control-label") eMail
				.col-sm-10
					input(id='edit_email', type="email", class="form-control noEnterSubmit", value="#{involved.email}")
					.help-block.with-errors

			.form-group
				label(for='edit_telephonenumber', class="col-sm-2 control-label") Telefonnummer
				.col-sm-10
					input(id='edit_telephonenumber', type="tel", class="form-control noEnterSubmit", value="#{involved.telephonenumber}", required)
					.help-block.with-errors

								
	script.
		var all_locations = [
								{plz: "96050", loc: "Bamberg"},
								{plz: "96450", loc: "Coburg"},
								{plz: "96465", loc: "Neustadt bei Coburg"},
								{plz: "96472", loc: "Rödental"},
								{plz: "96487", loc: "Dörfles-Esbach"}
							];
		
		var locationMatcher = function(locations) {
			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(locations, function(i, loc) {
					console.log('location: ', loc);
					if (substrRegex.test(loc.plz)) {
						matches.push(loc);
					}
				});

				cb(matches);
			};
		};
		
		$("#edit_postalcode").typeahead({
			hint: true,
			highlight: true,
			minLength: 2
		},
		{
			name: 'locations',
			source: locationMatcher(all_locations),
			display: 'plz',
			limit: 15
		}).bind('typeahead:select typeahead:autocomplete', function(ev, suggestion) {
			console.log(suggestion);
			$("#edit_location").val(suggestion.loc);
		});
		
		// KEEP IN SYNC WITH involved.cpp 'construct_name'
		function construct_involved_name(data)  {
			var group_given = data.group.length > 0;
			
			return (group_given ? data.group + "  [" : "") + 
					data.surname + (data.surname.length === 0 || data.forename.length === 0 ? "" : ", ") + data.forename + 
					(group_given ? "]" : "");
		}
		
		$('#form-involvededitor').validator().on('submit', function(e) {
			if( ! e.isDefaultPrevented() )
			{
				e.preventDefault();
				
				var data = {
					for_role: "#{for_role}",
					id   	: "other",
					group	: $("#edit_group").val(),
					forename: $("#edit_forename").val(),
					surname : $("#edit_surname").val(),
					street	: $("#edit_street").val(),
					street_nr: $("#edit_street_nr").val(),
					postalcode: $("#edit_postalcode").val(),
					location: $("#edit_location").val(),
					email: $("#edit_email").val(),
					telephonenumber: $("#edit_telephonenumber").val()
				};
				
				data.name = construct_involved_name(data);

				switch("#{type}") {
					case "add":
						pcal_pentry_edit_manager.add_involved(data);
						break;
					case "edit":
						data.uid = "#{involved.uid}";
						pcal_pentry_edit_manager.edit_involved(data);
						break;
				}
			}
		});
