
function select_country(id) {
	JSBASE.getEl("country").length = 0;
	JSBASE.getEl("country").options[ JSBASE.getEl("country").length ] = new Option( "Select Country" );
	
	for( var key in country ) {
		var selected = ( key == id ) ? true : false ;
		JSBASE.getEl("country").options[ JSBASE.getEl("country").length ] = new Option( country[key] , key , selected , selected );
		if( selected ) {
			select_county(key);
		}
	}
	JSBASE.getEl("search_button").disabled = true;
}

function select_county(id) {
	JSBASE.getEl("county").length = 0;
	JSBASE.getEl("county").options[ JSBASE.getEl("county").length ] = new Option( "Select County" );
	
	JSBASE.getEl("borough").length = 0;
	JSBASE.getEl("borough").options[ JSBASE.getEl("borough").length ] = new Option( "Select Council" );
	
	for( var key in county ) {
		if( county[key][0] == id ) {
			JSBASE.getEl("county").options[ JSBASE.getEl("county").length ] = new Option( county[key][1] , key );
		}
	}
	JSBASE.getEl("search_button").disabled = true;
}

function select_borough(id) {
	JSBASE.getEl("borough").length = 0;
	JSBASE.getEl("borough").options[ JSBASE.getEl("borough").length ] = new Option( "Select Council" );

	for( var key in borough ) {
		if( borough[key][0] == id ) {
			JSBASE.getEl("borough").options[ JSBASE.getEl("borough").length ] = new Option( borough[key][1] , key );
		}
	}
	JSBASE.getEl("search_button").disabled = true;
}

function setFilter(id, test) {
	var el = JSBASE.getEl(id);
	if( el ) {
		for( var i = 0; i < el.options.length; i++ ) {
			el.options[i].selected = ( el.options[i].value == test ) ? true : false ;
		}
	}
}

function init_search() {

	// disable search button
	JSBASE.getEl("search_button").disabled = true;
	
	// set defaults
	for( var key in country ) {
		var selected = ( key == country_selected ) ? true : false ;
		JSBASE.getEl("country").options[ JSBASE.getEl("country").length ] = new Option( country[key] , key , selected , selected );
	}
	if( county_selected ) {
		for( var key in county ) {
			if( county[key][0] == county[county_selected][0] ) {
				var selected = ( key == county_selected ) ? true : false ;
				JSBASE.getEl("county").options[ JSBASE.getEl("county").length ] = new Option( county[key][1] , key , selected , selected );
			}
		}
	}
	if( borough_selected ) {
		for( var key in borough ) {
			if( borough[key][0] == borough[borough_selected][0] ) {
				var selected = ( key == borough_selected ) ? true : false ;
				JSBASE.getEl("borough").options[ JSBASE.getEl("borough").length ] = new Option( borough[key][1] , key , selected , selected );
			}
		}
		JSBASE.getEl("search_button").disabled = false;
	}
		
	// setup events
	JSBASE.addEvent( JSBASE.getEl("country"), "change", function(e) {
	
		var index = JSBASE.getEl("country").options.selectedIndex;
		var id = JSBASE.getEl("country").options[index].value;
		select_county(id);
	
	} );
	
	JSBASE.addEvent( JSBASE.getEl("county"), "change", function(e) {
	
		var index = JSBASE.getEl("county").options.selectedIndex;
		var id = JSBASE.getEl("county").options[index].value;
		select_borough(id);
	
	} );
	
	JSBASE.addEvent( JSBASE.getEl("borough"), "change", function(e) {
		JSBASE.getEl("search_button").disabled = false;
	} );

}


