
locationId = null;

// City ////////////////////////////////////////////////////////////////////////////////////////////////////////
function cityChange() {
	dropdown = getDropdownByName('city');
	citySelected = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == citySelected) {
		regionChange();
	} else {
		emptyDropdown('neighborhood');
		populateNeighborhoodDropdown();	
	}
}

function populateCityDropdown() {
	dropdown = getDropdownByName('region');
	regionSelected = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == regionSelected) {
		regionOptions = dropdown.options;
		for ( j=0; j<regionOptions.length; j++) {
			option = regionOptions[j];
			filterDropdown('city',allcities,option.value);
		}
	} else {
		filterDropdown('city',allcities,regionSelected);
	}
	sortDropdown('city');
}

function setCityDropdown(citySelected) { 
	setDropdown('city',citySelected);
	cityChange();
}

// Country ////////////////////////////////////////////////////////////////////////////////////////////////////////
function countryChange() {
	emptyDropdown('state');
	emptyDropdown('region');
	emptyDropdown('city');
	emptyDropdown('neighborhood');
	populateStateDropdown();
	populateRegionDropdown();
	populateCityDropdown();
	populateNeighborhoodDropdown();
}

function populateCountryDropdown() {
	populateDropdown('country',allcountries);
}

function setCountryDropdown(countrySelected) {
	setDropdown('country',countrySelected);
	countryChange();
}

// Neighborhood //////////////////////////////////////////////////////////////////////////////////////////////////
function populateNeighborhoodDropdown() {
	dropdown = getDropdownByName('city');
	citySelected = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == citySelected) {
		cityOptions = dropdown.options;
		for ( j=0; j<cityOptions.length; j++) {
			option = cityOptions[j];
			filterDropdown('neighborhood',allneighborhoods,option.value);
		}
	} else {
		filterDropdown('neighborhood',allneighborhoods,citySelected);
	}
	sortDropdown('neighborhood');
}

function setNeighborhoodDropdown(neighborhoodSelected) {
	setDropdown('neighborhood',neighborhoodSelected);
}

// Region ////////////////////////////////////////////////////////////////////////////////////////////////////////
function regionChange() {
	dropdown = getDropdownByName('region');
	regionId = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == regionId) {
		stateChange();
	} else {
		emptyDropdown('city');
		emptyDropdown('neighborhood');
		populateCityDropdown();
		populateNeighborhoodDropdown();
	}
}

function populateRegionDropdown() {
	dropdown = getDropdownByName('state');
	stateSelected = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == stateSelected) {
		stateOptions = dropdown.options;
		for ( j=0; j<stateOptions.length; j++) {
			option = stateOptions[j];
			filterDropdown('region',allregions,option.value);
		}
	} else {
		filterDropdown('region',allregions,stateSelected);
	}
	sortDropdown('region');
}

function setRegionDropdown(regionSelected) {
	setDropdown('region',regionSelected);
	regionChange();
}

// State ////////////////////////////////////////////////////////////////////////////////////////////////////////
function stateChange() {
	dropdown = getDropdownByName('state');
	selectedState = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == selectedState) {
		countryChange();
	} else {
		emptyDropdown('region');
		emptyDropdown('city');
		emptyDropdown('neighborhood');
		populateRegionDropdown();
		populateCityDropdown();		
		populateNeighborhoodDropdown();	
	}
}

function populateStateDropdown() {
	dropdown = getDropdownByName('country');
	countrySelected = dropdown.options[dropdown.selectedIndex].value;
	if('blank' == countrySelected) {
		countryOptions = dropdown.options;
		for ( j=0; j<countryOptions.length; j++) {
			option = countryOptions[j];
			filterDropdown('state',allstates,option.value);
		}
	} else {
		filterDropdown('state',allstates,countrySelected);
	}
	sortDropdown('state');
}

function setStateDropdown(stateSelected) {
	setDropdown('state',stateSelected);
	stateChange();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function emptyDropdown(dropdownName) {
	dropdown = getDropdownByName(dropdownName);
	for ( i=dropdown.options.length; i>=1; i--) {
		dropdown.options[i] = null;
	}
}
 
function filterDropdown(dropdownName,dropdownOptions,filter) {
	dropdown = getDropdownByName(dropdownName);
	index = dropdown.options.length;
	for ( k=0; k<dropdownOptions.length; k++) {
		dropdownOption = dropdownOptions[k];
		if(filter == dropdownOption[0]) {
			dropdown.options[index] = new Option(dropdownOption[2],dropdownOption[1]);
			index = index + 1;
		}
	}
}

function getDropdownByName(dropdownName) {
	return document.MAINFORM.elements[dropdownName];
}

function getParent(optionList,selectedValue) {
	for ( j=0; j<optionList.length; j++) {
		option = optionList[j];
		if(selectedValue == option[1]) {
			return option;
		}
	}
}

function locationSort(a,b) {
	if(a[1] > b[1]) return 1;
	if(a[1] < b[1]) return -1;
	return 0;
}

function populateDropdown(dropdownName,dropdownOptions) {
	dropdown = getDropdownByName(dropdownName);
	
		index=1;
		for ( j=0; j<dropdownOptions.length; j++) {
			dropdownOption = dropdownOptions[j];
			dropdown.options[index] = new Option(dropdownOption[2],dropdownOption[1]);
			index = index + 1;
		}
	
}

function search() {
	clearErrors();
	
	if(document.MAINFORM.elements['neighborhood']) {
		searchDropdownForSelection('neighborhood');
		if(locationId == null || locationId == "blank")
			searchDropdownForSelection('city');
		if(locationId == null || locationId == "blank")
			searchDropdownForSelection('region');
		if(locationId == null || locationId == "blank")
			searchDropdownForSelection('state');
		if(locationId == null || locationId == "blank")
			searchDropdownForSelection('country');
		if(locationId == null || locationId == "blank")
			locationId = null;
	}
		
	today = new Date();
	expiry = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000); // plus 30 days
	checkInDate = new Date(document.MAINFORM.checkInDateString.value);
	checkOutDate = new Date(document.MAINFORM.checkOutDateString.value);
	maxFutureDate = new Date(today.getTime() + 1000*60*60*24*365*2);
	if(checkInDate > maxFutureDate)
		errorMessages[errorCount++] = "The <b>Check In</b> date cannot be more than 2 years away.";
	if(today > checkInDate) 
		errorMessages[errorCount++] = "The <b>Check In</b> date cannot be in the past.";
	if(checkOutDate > maxFutureDate)
		errorMessages[errorCount++] = "The <b>Check Out</b> date cannot be more than 2 years away.";	
	if(today > checkOutDate) 
		errorMessages[errorCount++] = "The <b>Check Out</b> date cannot be in the past.";
	if(checkInDate >= checkOutDate) 
		errorMessages[errorCount++] = "The <b>Check Out</b> date must be after the <b>Check In</b> date.";	
	else if(checkOutDate.getTime() - checkInDate.getTime() > 1000*60*60*24*31*6)
		errorMessages[errorCount++] = "The maximum duration of stay is 6 months.";
	if(errorCount > 0) showErrors();
	else {
		document.MAINFORM.action.value = 'SEARCH';
		document.MAINFORM.activity.value = 'PERFORM_LOCATION_SEARCH';
		document.MAINFORM.locationId.value = locationId;
		document.MAINFORM.submit();
	}
}

function searchDropdownForSelection(dropdownName) {
	dropdown = getDropdownByName(dropdownName);
	for ( i=0; i<dropdown.options.length ; i++) {
		if(dropdown.options[i].selected)
			locationId = dropdown.options[i].value;
	}
}

function setDropdown(dropdownName,dropdownValue) {
	dropdown = getDropdownByName(dropdownName);
	
		for(i=0; i<dropdown.options.length; i++) {
			if(dropdown.options[i].value == dropdownValue) {
				dropdown.selectedIndex = i;
				break;
			}
		}
	
}

function setDefaultLocation(locId) {
	populateCountryDropdown();
	if(locId.length == 0) {
		locId = 1000; // United States
		setCountryDropdown(locId);
		locId = 2000; // Florida
		setStateDropdown(locId);
		locId = 3000; // Disney - Orlando Region
		setRegionDropdown(locId);
	} else {
		neighborhoodDefault = null;
		cityDefault = null;
		regionDefault = null;
		stateDefault = null;
		countryDefault = null;
		for(i=0; i<allneighborhoods.length; i++) {
			neighborhood = allneighborhoods[i];
			if(neighborhood[1] == locId) {
				neighborhoodDefault = locId;
				city = getParent(allcities,neighborhood[0]);
				cityDefault = city[1];
				region = getParent(allregions,city[0]);
				regionDefault = region[1];
				state = getParent(allstates,region[0]);
				stateDefault = state[1];
				country = getParent(allcountries,state[0]);
				countryDefault = country[1];
				break;
			}
		}
		if(neighborhoodDefault == null) {
			for(i=0; i<allcities.length; i++) {
				city = allcities[i];
				if(city[1] == locId) {
					cityDefault = locId;
					region = getParent(allregions,city[0]);
					regionDefault = region[1];
					state = getParent(allstates,region[0]);
					stateDefault = state[1];
					country = getParent(allcountries,state[0]);
					countryDefault = country[1];
					break;
				}
			}
		}
		if(cityDefault == null) {
			for(i=0; i<allregions.length; i++) {
				region = allregions[i];
				if(region[1] == locId) {
					regionDefault = locId;
					state = getParent(allstates,region[0]);
					stateDefault = state[1];
					country = getParent(allcountries,state[0]);
					countryDefault = country[1];
					break;
				}
			}
		}
		if(regionDefault == null) {
			for(i=0; i<allstates.length; i++) {
				state = allstates[i];
				if(state[1] == locId) {
					stateDefault = locId;
					country = getParent(allcountries,state[0]);
					countryDefault = country[1];
					break;
				}
			}
		}
		if(stateDefault == null) {
			for(i=0; i<allcountries.length; i++) {
				country = allcountries[i];
				if(country[1] == locId) {
					countryDefault = locId;
					break;
				}
			}
		}
		if(countryDefault != null)	setCountryDropdown(countryDefault);
		if(stateDefault != null) setStateDropdown(stateDefault);
		if(regionDefault != null) setRegionDropdown(regionDefault);
		if(cityDefault != null) setCityDropdown(cityDefault);
		if(neighborhoodDefault != null) setNeighborhoodDropdown(neighborhoodDefault);
	}
}

function sortDropdown(dropdownName) {
	dropdown = getDropdownByName(dropdownName);
    var copyDropdown = new Array();
    if(dropdown.options.length > 1) {
	    for (var i=1; i<dropdown.options.length; i++) {
	        copyDropdown[i-1] = new Array(dropdown[i].value,dropdown[i].text);
		}
	    copyDropdown.sort(locationSort);
	    emptyDropdown(dropdownName);
	    for (var i=0; i<copyDropdown.length; i++) {
			dropdown.options[dropdown.length] = new Option(copyDropdown[i][1],copyDropdown[i][0]);
		}
	}
}

function useDestination(locId) {
	locationId = locId;
}
