function changeAreas()
{	
	if($("select#country").size()) var myVal = $("select#country").val();
	else var myVal = $("select#list-in").val();
	
	// put any items in the trash back first
	$("select#trash option.int-region").insertAfter($("select#region option").eq(0));
	$("select#trash option").appendTo("select#region");
		
	// which one is now selected
	if(myVal == "GB" || myVal == "4")
	{
		// stash any us options
		$("select#region option.us-region, select#region option.int-region").appendTo("select#trash");
	}
	else if(myVal == "US" || myVal == "2")
	{			
		// stash any uk options			
		$("select#region option.uk-region, select#region option.int-region").appendTo("select#trash");
	}
	// dont change the region if the value is zero (all or unselected)
	else if(myVal == "0") ;
	// when listing a dog, if other is selected, highlight the international region
	else
	{			
		// stash any uk options			
		$("select#region").get(0).selectedIndex = 0;
	}
}

function sortBy(views, init)
{
	if(views)
	{
		// remove old button
		$("#order-by-button").remove();
		
		// add date button
		$("#order-by").after("<a href='#' id='order-by-button'><img src='images/sort_date_button.gif' /></a>");
		
		// views search
		$("#order-by").val("views DESC");
		
		// add functionality
		$("#order-by-button").click(function()
		{
			sortBy(0, 0);
		});
	}
	else
	{
		// remove old button
		$("#order-by-button").remove();
		
		// add views button
		$("#order-by").after("<a href='#' id='order-by-button'><img src='images/sort_button.gif' /></a>");
		
		// normal search
		$("#order-by").val("");
		
		// add functionality
		$("#order-by-button").click(function()
		{
			sortBy(1, 0);
		});
	}
	
	// submit form
	if(!init) $("#dog-search").submit();
}

$("document").ready(function()
{
	// by default country is all, when a country is selected, the reigons from the other country must be hidden
	$("select#country, select#list-in").change(changeAreas);
	
	// if a user selects a region, make sure the relevant counrty is selected
	$("select#region").change(function()
	{
		//alert($("select#country").val());
		// us region, select us
		if($(this.options[this.selectedIndex]).hasClass("us-region") && $("select#country").val() != "US") 
		{
			// set hidden dog country value for search pages
			if($("input#dog-country").size())
			{
				$("input#dog-country").val("US");
			}
			else
			{
				$("select#country").get(0).selectedIndex=1;
			
				$("select#country").change();
			}
		}
		
		else if($(this.options[this.selectedIndex]).hasClass("uk-region") && $("select#country").val() != "GB") 
		{
			// set hidden dog country value for search pages
			if($("input#dog-country").size())
			{
				$("input#dog-country").val("GB");
			}
			else
			{
				$("select#country").get(0).selectedIndex=0;
			
				$("select#country").change();
			}
		}
			
	});
	
	// trigger change functions 
	changeAreas();
	
	// add sort button where neccessary
	if($("#order-by").size())
	{
		
		// are we already ordering by views
		if($("#order-by").val().length)
		{
			sortBy(1, 1);
		}
		else
		{
			sortBy(0, 1);
		}
	}
});
