﻿$(document).ready(function () {

	$.getScript('/js/jquery.dependent.js', function () {

		if ($('.location-dropdown-side').length > 0) {

			var getValues = function (el) {

				// declare variables
				var value = ($(el).val());
				var output = [];

				// if the value is empty, or disabled, return empty array
				if (value == '' || $(el).attr('disabled')) {
					return output;
				}

				// make the ajax call
				$.ajax({
					async: false,
					url: '/base/ipro/GetLocations/' + value,
					success: function (data) {
						// loop through each of the returned xml nodes
						$(data).find('node').each(function () {
							// push the item onto the array
							output.push({ text: $(this).attr('name'), value: $(this).attr('id') });
						});
					},
					dataType: 'xml'
				});

				// return the array
				return output;
			};

			// set the dependency of the dropdown-lists
			$('.location-dropdown-side').dependent({
				values: getValues
			});

		} // end if .location-dropdown

	});

	// date-picker for the search calendar
	if ($('.dates-row').length > 0 && $('.dates-row').find('.datepicker').length > 0) {
		var dates = $('.dates-row').find('.datepicker').datepicker({
			showOn: 'both',
			buttonImage: '/images/ico-calendar.gif',
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy',
			constrainInput: true,
			onSelect: function (selectedDate) {
				var option = this.id == "between-date" ? "minDate" : "maxDate",
				instance = $(this).data("datepicker"),
				date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
				dates.not(this).datepicker("option", option, date);
			}
		});
	}

});
