/***************************
Author: Sherwin Sena Aborot
Date: 12/11/2008
Purpose:
	To perform dynamic functionality required by the Site Search Page
***************************/

var defaultLocationsLoaded = true;
var defaultPricesLoaded = true;

function resetSearchFields() {
		loadDefaultLocations();
		loadDefaultPrices();
	try {
		if( document.searchForm.county ) document.searchForm.county.selectedIndex = 0;
		if( document.searchForm.price ) document.searchForm.price.selectedIndex = 0;
		if( document.searchForm.stories ) document.searchForm.stories.selectedIndex = -1;
		if( document.searchForm.bedroom ) document.searchForm.bedroom.selectedIndex = -1;
		if( document.searchForm.area ) document.searchForm.area.selectedIndex = -1;
		if( document.searchForm.bathroom ) document.searchForm.bathroom.selectedIndex = -1;
	} catch(Exception) {}
}

function loadDefaultLocations() {
try {
	var locationBoxReference = document.searchForm.county;

	//clear location select box and add first option
	locationBoxReference.length = 0;
	locationBoxReference.options[locationBoxReference.options.length]=new Option("LOCATION","0");
	
	// load default locations
	for ( var i=0;i<defaultLocations.length;i++ ) {
		locationBoxReference.options[locationBoxReference.options.length]=new Option(defaultLocations[i],defaultLocations[i]);
	}
	
	defaultLocationsLoaded = true;
} catch(Exception) { alert("loadDefaultLocations: " + Exception); }
return false;
}

function loadDefaultPrices() {
try {
	var priceBoxReference = document.searchForm.price;
	
	//clear price select box and add first option
	priceBoxReference.length = 0;
	priceBoxReference.options[priceBoxReference.options.length]=new Option("PRICE","0");
	
	// load default prices
	for ( var i=0;i<defaultPrices.length;i++ ) {
		priceBoxReference.options[priceBoxReference.options.length]=new Option(defaultPrices[i],defaultPrices[i]);
	}
	
	defaultPricesLoaded = true;
} catch(Exception) { alert("loadDefaultPrices: " + Exception); }
return false;	
}

function updatePriceSelectBox(location) {
try {
	var locationBoxReference = document.searchForm.county;
	var priceBoxReference = document.searchForm.price;
	
	// LOAD DEFAULT LOCATIONS IF NO LOCATION IS SELECTED ELSE TRY LOADING PRICES WITH SELECTED LOCATION IF NO PRICE HAS BEEN SELECTED YET
	if ( locationBoxReference.selectedIndex == 0 && priceBoxReference.selectedIndex == 0 ) {
		loadDefaultLocations();
		loadDefaultPrices();
	} else if ( defaultLocationsLoaded ) {
		var matchedLocPrices = "PRICE";
		var availPriceOpt = [];
		var availPriceOptCtr = 0;			
		
		//clear price select box and add first option
		priceBoxReference.length = 0;
		priceBoxReference.options[priceBoxReference.options.length]=new Option("PRICE","0");
		
		for ( var i=0;i<counties.length;i++ ) {
			// start adding prices for selected locations
			if ( counties[i] == location ) {
				if ( matchedLocPrices.toLowerCase().indexOf(prices[i].toLowerCase()) < 0 ) {
					availPriceOpt[availPriceOptCtr] = prices[i];
					matchedLocPrices += " " + prices[i];
					availPriceOptCtr++;
				}
			}
		}

		// sort the prices available to the selected location
		availPriceOpt.sort();

		//add the sorted prices to the list box
		for ( var i=0;i<availPriceOpt.length;i++ ) {
			priceBoxReference.options[priceBoxReference.options.length]=new Option(availPriceOpt[i],availPriceOpt[i]);
		}
		
		defaultPricesLoaded = false;
	}
} catch(Exception) { alert("updatePriceSelectBox: " + Exception); }
return false;
}		

function updateLocationSelectBox(price) {
try {
	var locationBoxReference = document.searchForm.county;
	var priceBoxReference = document.searchForm.price;
	
	// LOAD DEFAULT PRICES IF NO PRICE IS SELECTED ELSE TRY LOADING LOCATIONS WITH SELECTED PRICE IF NO LOCATION HAS BEEN SELECTED YET
	if ( priceBoxReference.selectedIndex == 0 && locationBoxReference.selectedIndex == 0 ) {
		loadDefaultPrices();
		loadDefaultLocations();
	} else if ( defaultPricesLoaded ) {
		var matchedLocations = "LOCATION";
		var availLocationOpt = [];
		var availLocationOptCtr = 0;	
		
		//clear location select box and add first option
		locationBoxReference.length = 0;
		locationBoxReference.options[locationBoxReference.options.length]=new Option("LOCATION","0");
		
		for ( var i=0;i<counties.length;i++ ) {
			// start adding location for selected locations
			if ( prices[i] == price ) {
				if ( matchedLocations.toLowerCase().indexOf(counties[i].toLowerCase()) < 0 ) {
					availLocationOpt[availLocationOptCtr] = counties[i];
					matchedLocations += " " + counties[i];
					availLocationOptCtr++;
				}
			}
		}

		// sort the locations available to the selected location
		availLocationOpt.sort();

		//add the sorted locations to the list box
		for ( var i=0;i<availLocationOpt.length;i++ ) {
			locationBoxReference.options[locationBoxReference.options.length]=new Option(availLocationOpt[i],availLocationOpt[i]);
		}
		
		defaultLocationsLoaded = false;
	}
} catch(Exception) { alert("updateLocationSelectBox: " + Exception); }
return false;
}		