jQuery.noConflict();
jQuery(document).ready(function(){

	jQuery("select#gym_county").change(function () {	
		var county = jQuery("#gym_county").val();
		if(county)
		{
			jQuery('#gym_facility').children().remove().end().append('<option value=""> ...Loading... </option>');
			jQuery('#gym_area').children().remove().end().append('<option value=""> ...Loading... </option>');			
			jQuery.get(script_url+"/index.php",{
				county: jQuery("#gym_county").val(),
				module: "ajax",
				action: "populate_gym_facility"
			},
			function(xml){
				add_options(xml);
			});
			function add_options(xml) {
				jQuery('#gym_facility').children().remove().end().append('<option value=""> Select Any </option>');
				jQuery('#gym_area').children().remove().end().append('<option value=""> All Areas </option>');				
				jQuery("facility",xml).each(function(id) {
					facility = jQuery("facility",xml).get(id);				
					result = '<option value="'+jQuery("fea_id",facility).text()+'">'+jQuery("feature",facility).text()+'</option>'
					jQuery("#gym_facility").append(result);
				});
				jQuery("area",xml).each(function(id) {
					area = jQuery("area",xml).get(id);				
					result = '<option value="'+jQuery("address",area).text()+'">'+jQuery("address",area).text()+'</option>'
					jQuery("#gym_area").append(result);
				});				
			}
		}		
	});
	
    jQuery("select#gym_facility").change(function () {
		jQuery('#gym_area').children().remove().end().append('<option value=""> ...Loading... </option>');
		jQuery.get(script_url+"/index.php",{
			county: jQuery("#gym_county").val(),
			facility: jQuery("#gym_facility").val(),
			module: "ajax",
			action: "populate_gym_area"
		},
		function(xml){
			add_options(xml);
		});
		function add_options(xml) {
			jQuery('#gym_area').children().remove().end().append('<option value=""> All Areas </option>');
			jQuery("area",xml).each(function(id) {
				area = jQuery("area",xml).get(id);				
				result = '<option value="'+jQuery("address",area).text()+'">'+jQuery("address",area).text()+'</option>'
				jQuery("#gym_area").append(result);
			});
		}		
	});	
	
 });
