<?php
add_filter( 'ninja_forms_enqueue_scripts', 'hook_javascript_40' );
function hook_javascript_40() {
	?>
		<script>
		// START JAVASCRIPT
		var service_id;
		var venueObj = new Array();
		var myOpts;
		var root_url = 'https://terminsnipe_p:9Hplg7MU214jFl7bkX1NF7@service.berlin.de.zinomedia.de/www/lib/terminsnipe_p/php-crud-api/api.php';
		var venuesChecked = new Array();

		jQuery(document).on('nfFormReady', function(e, layoutView) {
			nfRadio.channel('nfMP').on('change:part', function() {
				
				if (is_mp_page() == 3) { // DIENSTLEISTUNG WAEHLEN PAGE
					jQuery('#nf-field-50').on('change', function() { // GET VENUES
						venueObj = [];
						service_id = this.value;
						
						var url = root_url + '/records/mapping_services?include=VENUE_ID&filter=SERVICE_ID,eq,' + service_id;

						jQuery.getJSON(url, null, function(data) {
							jQuery.each(data.records, function(index, item) {
								venue_ids += item.VENUE_ID + ',';
							});
							var venue_ids = venue_ids.substring(0, venue_ids.length - 1);

							url = root_url + '/records/venues/' + venue_ids + '?include=NAME,ID';
							jQuery.getJSON(url, null, function(data) {
								jQuery.each(data, function(index, item) {
									if (item != null) {
										venueObj.push({
											ID: item.ID,
											NAME: item.NAME
										});
									}
								});

								venueObj.sort(function(a, b) {
									return a.NAME.localeCompare(b.NAME);
								});
	
							});
						});
						console.log(venueObj);
					});

				}
				else if (is_mp_page() == 5) { // STANDORTE AUSWAEHLEN PAGE
					//populate_select('#nf-field-96');
					resetPart();
					populate_table('table-standorte-auswaehlen');
					jQuery("#modal-5e6603a34d72d .checkbox-venue").change(function() { venueChecked(this) });
					jQuery("#button-stadorte-abschliessen").click(function() { jQuery('#modal-5e6603a34d72d .uabb-modal-close').click(); });
					
					
					jQuery('#nf-field-186:not(.bound)').addClass('bound').on('click',  openStandorteModal);						
					jQuery( "#nf-field-186" ).trigger( "click" );				
				}

			});
		});
		
		function allowNext(bool, selector) {
			// DEFAULT
			jQuery('#' + selector).removeClass('nf-fail nf-error nf-pass');
			jQuery(".nf-next").css("pointer-events", "auto");
			
			if (bool == 1) {
				jQuery('#' + selector).addClass('nf-pass');
				jQuery(".nf-next").css("pointer-events", "auto");
			}
			else if (bool == 0) {
				jQuery('#' + selector).addClass('nf-fail nf-error');
				jQuery(".nf-next").css("pointer-events", "none");
			}	
		}	
		
		function resetPart() {
			venuesChecked = [];
			jQuery('#nf-field-186')[0].value = '';
			jQuery("#table-standorte-auswaehlen").empty();
			allowNext(0, 'nf-field-186-wrap');
		}				
			
		function openStandorteModal() {					
			jQuery('#nf-field-186').blur();							
			jQuery('#standorte-auswaehlen-modal').click(); 
		}
			
		function venueChecked(checkbox) {
			if(checkbox.checked) {
				console.log('checked');
				venuesChecked.push(checkbox.value);
			}
			else {
				console.log('unchecked');
				removeA(venuesChecked, checkbox.value);
			}
			
			if (typeof venuesChecked !== 'undefined' && venuesChecked.length > 0) {
				jQuery('#nf-field-186')[0].value = venuesChecked.length + ' erfolgreich ausgewählt';					
				console.log('venueIDs: ' + venuesChecked.join(','));
				jQuery('#nf-field-187')[0].defaultValue = venuesChecked.join(',');					
				allowNext(1, 'nf-field-186-wrap');
			}
			else if (typeof venuesChecked !== 'undefined' && venuesChecked.length == 0) {
				jQuery('#nf-field-186')[0].value = '';
				jQuery('#nf-field-187')[0].defaultValue = '';
				allowNext(0, 'nf-field-186-wrap');
			}
			
			jQuery('#nf-field-187').trigger('change');
			
			console.log(venuesChecked);
		}
			
		function populate_table(selector) {
			venueObj.forEach(function(arrayItem) {
				jQuery('#' + selector).append('<tr><td>' + arrayItem.NAME + '</td><td><input type="checkbox" name="venue-id" class="checkbox-venue" value="' + arrayItem.ID + '"></td></tr>');
			});				
		}

		function populate_select_from_htmlcollection(id, populateSource, startIndex, endIndex) {
			console.log('populating...');
			jQuery(id + ' option').remove();

			for (var i = startIndex; i < endIndex; i++) {
				jQuery(id).append(
					jQuery("<option></option>")
					.text(populateSource[i].text)
					.val(populateSource[i].value)
				);
			}
		}

		function populate_select(id) {
			console.log('populating...');
			jQuery(id + ' option').remove();
			venueObj.forEach(function(arrayItem) {
				jQuery(id).append(
					jQuery("<option></option>")
					.text(arrayItem.NAME)
					.val(arrayItem.ID)
				);
			});
		}

		function is_mp_page() {
			var ids = ['nf-field-72', 'nf-field-73', 'nf-field-74', 'nf-field-88'];
			for (i = 0; i < ids.length; i++) {
				var element = document.getElementById(ids[i]);
				if (typeof(element) != 'undefined' && element != null) {
					return parseInt(element.value);
				}
			}
		}
		// END JAVASCRIPT
		</script>				
	<?php
}