$.fn.locationSelect = function() {
	
	var formName = $(this);
	
	formName.find('select').change(function(){
															 
		// Change action type so it's serialized
		var action_type = $(this).attr('name');
		$('[name=action_type]').val(action_type);
		
		var formData = $(this).parents('form').serialize();
		
		$.post('/ajax/_fetch_search_options.php',formData,function(response){
			
			var html = '<option value="">Please select...</option>';
	
			switch (action_type) {
			
				case "country":
	
					$.each(response,function(key,val) {
						
						if(key != 'total' && key != 'no_regions')				 
							html += '<option value="' + val.id + '">' + val.name + '</option>';
						
					});
					
					// Some countries don't have regions, we know this in the Ajax response:
					if(response.no_regions != true) {
						$('#region').html(html);
						$('#region_wrap').slideDown();						
						setDefault($('#sub_region'));
					}else{
						$('#sub_region').html(html);
						$('#region_wrap').slideUp('fast');						
						setDefault($('#region'));
					}
					
					$('#sub_region_wrap').slideDown('fast');
					break;
					
				case "region":
				
					if(response != null) { 
						
						$('#sub_region_wrap').slideDown('fast');
						
						$.each(response,function(key,val) {
							if(key != 'total' && key != 'no_regions')	
								html += '<option value="' + val.id + '">' + val.name + '</option>';
						});
						
					}else{
	
						$('#sub_region_wrap').slideUp();
	
					}
	
					$('#sub_region').html(html);
					break;
					
				case "sub_region":
					
					// Fetch local councils
				
					break;						
				
			}
	
			
		},"json")
		
	});

}

function setDefault(selectElement) {

	$(selectElement).html('<option val="">Please select...</option>');

}