function swapDealerResults(sValue) {
	if(sValue != "" && sValue != "UnitedStates")
	{
		toggleUsRegions(false);
	}
	else
	{			
		toggleUsRegions(true);
	}
	
	if (sValue != "UnitedStates") {
		var iOffset = $("#" + sValue).get(0).offsetTop;	
		$("#regionResults").get(0).scrollTop = parseInt(iOffset, 10);
	}
	$("#dealerFlash a").removeClass("selected");
	$("#dealerFlash #map" + sValue + " a").addClass("selected");
}

function toggleUsRegions(bShow)
{
	if (bShow)
	{
		$("#overlayResults").show();
		$("#regionResults").hide();	
	}
	else
	{
		$("#overlayResults").hide();		
		$("#regionResults").show();
	}
}

function clearUSInputs()
{
	// set state and zip code to default
	$("input[@name='zip']").val("Enter ZIP code");
	$("select[@name='state']").val("");
}

$(document).ready(function() {
	// when dropdown changes, show new region and scroll to it
    $("select[@name='region']").change(function(e) {
		e.preventDefault();
				
		var sValue = $(this).val();
		swapDealerResults(sValue);
		clearUSInputs();		
    });
		
	// link click-- do swap and change select value
	$("#dealerFlash a").click(function(e) {
		e.preventDefault();
		var sId = this.href.match(/#(\w+)/)[1];
		swapDealerResults(sId);
		$("option[@value='" + sId + "']").get(0).selected = true;
		clearUSInputs();
	});
	
	if (window.parent && window.parent.sDealerLocatorMode != "US")
	{
		if($("select[@name='state']").val() == "" && $("input[@name='zip']").val().length >= 14)
		{
			toggleUsRegions(false);
		}
		else
		{
		toggleUsRegions(true);
		}
	}
	else
	{
		toggleUsRegions(true);
	}
	
	// when dropdown changes, clear zip and region
	$("select[@name='state']").change(function(e) {
		e.preventDefault();
		// set region and zip code to default
		sDealerLocatorMode = 'US';
		$("input[@name='zip']").val("Enter ZIP code");
		$("select[@name='region']").val("");
    });
	
		// when dropdown changes, clear state and region
	$("input[@name='zip']").click(function(e) {
		e.preventDefault();
		// set region and zip code to default
		sDealerLocatorMode = 'US';
		$("input[@name='zip']").val("");
		$("select[@name='state']").val("");
		$("select[@name='region']").val("");
    });
});




