google.setOnLoadCallback(function() {
	$(document).ready(function() {
		var PVRSRCH = {};
		
		// minimum from date is today
		PVRSRCH.minFromDate = new Date();
		// minimum to date is one week from today
		PVRSRCH.minToDate = new Date();
		PVRSRCH.minToDate.setDate(PVRSRCH.minToDate.getDate() + 7);

		// maximum from date is december 31 of NEXT year
		PVRSRCH.today = new Date();
		PVRSRCH.nextYear = PVRSRCH.today.getFullYear() + 1;
		PVRSRCH.maxFromDate = new Date(PVRSRCH.nextYear, 11, 31);
		// maximum to date is one week after december 31 of NEXT year
		PVRSRCH.maxToDate = new Date(PVRSRCH.nextYear, 11, 31);
		PVRSRCH.maxToDate.setDate(PVRSRCH.maxToDate.getDate() + 7); 
		
		// empty all locations options EXCEPT the first option for All Locations
		PVRSRCH.emptyLocations = function() {
			$("#Location option[value!='']").remove();										
		}
		
		// populate location options with the data supplied
		// empty all location options EXCEPT the first option for All Locations before adding the new options
		PVRSRCH.populateLocations = function(xml) {
			// empty Locations
			PVRSRCH.emptyLocations();
			
			// populate locations
			$(xml).find('location').each(function(){
				var locationnumber = $(this).find('locationnumber').text();
				var locationname = $(this).find('locationname').text();
				$("#Location").append("<option value='" + locationnumber + "'>" + locationname + "</option>");
			});
		}
										  
		// on change event handler for regions - populates locations drop down
		$("#Region").change(function (event) {
			$.ajax({
				type: "POST",
				url: "/includes/location_xml.cfm",
				data: ({ Region : $("#Region").val() }), 
				dataType: "xml",
				success: function(data) {  
					PVRSRCH.populateLocations(data);  
			   	},
				error: function(request, status, error) {
					// empty Locations
					PVRSRCH.emptyLocations();
				}
			});   
	    });

		// function called by beforeShowDay for fromDate datepicker
		PVRSRCH.checkFromAvailability = function(showdate){
			return PVRSRCH.checkAvailability(showdate, PVRSRCH.minFromDate, PVRSRCH.maxFromDate);
		}

		// function called by beforeShowDay for toDate datepicker
		PVRSRCH.checkToAvailability = function(showdate){
			return PVRSRCH.checkAvailability(showdate, PVRSRCH.minToDate, PVRSRCH.maxToDate);
		}
		
		// only allows saturdays to be selected
		PVRSRCH.checkAvailability = function(showdate, mindate, maxdate){
			$available = false; 
			//check if date is a Saturday and is between mindate and maxdate (inclusive)
			if (showdate.getDay() == 6 && showdate >= mindate && showdate <= maxdate) 
			{
				$available = true;
			}
			return [$available]; //return true/false for selectable, plus the appropriate class for display
		}
		
		// update the location drop down based on the region selection when the page finishes loading
		$.ajax({
			type: "POST",
			url: "/includes/location_xml.cfm",
			data: ({ Region : $("#Region").val() }), 
			dataType: "xml",
			success: function(data) {  
				PVRSRCH.populateLocations(data);  
			},
			error: function(request, status, error) {
				// empty Locations
				PVRSRCH.emptyLocations();
			}
		});   		

		// initialize from Date datepicker 
		$("#FromDate").datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'mm/dd/y',
			minDate: PVRSRCH.minFromDate,
			maxDate: PVRSRCH.maxFromDate,
			showOn: "button", 
			buttonImage: '/images/index/dates.jpg',
			buttonImageOnly: true,
			//buttonText: 'Date',
			beforeShowDay: PVRSRCH.checkFromAvailability
		});
		
		// initialize to Date datepicker 
		$("#ToDate").datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'mm/dd/y',
			minDate: PVRSRCH.minToDate,
			maxDate: PVRSRCH.maxToDate,
			showOn: "button", 
			buttonImage: '/images/index/dates.jpg',
			buttonImageOnly: true,
			//buttonText: 'Date',
			beforeShowDay: PVRSRCH.checkToAvailability
		});		
		
		// on change event handler for from date search input 
		// sets the minimum date for the To Date to the From Date
		$("#FromDate").change(function(event) {
			if ($("#FromDate").datepicker("getDate")) {			
				var tDate = $("#FromDate").datepicker("getDate");
				tDate.setDate(tDate.getDate() + 7);
				$("#ToDate").datepicker("option", "minDate", tDate);
			}
			else {
				$("#ToDate").datepicker("option", "minDate", PVRSRCH.minToDate);
			}
		});
		
		// on change event handler for To date search input 
		// sets the maximum date for the From Date to the To Date
		$("#ToDate").change(function(event) {
			if ($("#ToDate").datepicker("getDate")) {
				var fDate = $("#ToDate").datepicker("getDate");
				fDate.setDate(fDate.getDate() - 7);
				$("#FromDate").datepicker("option", "maxDate", fDate);
			}
			else {
				$("#FromDate").datepicker("option", "maxDate", PVRSRCH.maxFromDate);				
			}
		});		

		// auto complete for property name or property reference no input 
		$.ajax({
			url: "/includes/search_xml.cfm",
			dataType: "xml",
			success: function( xmlResponse ) {
				var data = $( "property", xmlResponse ).map(function() {
					return {
						value: $( "propname", this ).text() + ' (' + $( "propreferenceno", this ).text() + ')',
						id: $( "propid", this ).text()
					};
				}).get();
				$( "#PropNameOrReferenceNo" ).autocomplete({
					source: data,
					select: function(event, ui) {
						$("#PropID").val(ui.item.id);
						$("#SearchForm").submit();	
					},
					change: function(event, ui) {
						$("#PropID").val(ui.item.id);
						$("#SearchForm").submit();	
					}
				});
			}
		});
	});
});
