$(function()
{
    $("form.auto_counties select[@name='county']").attr("disabled", "disabled");
	
	$("form.auto_counties select[@name='state']").change(function()
	{
	    $("form.auto_counties select[@name='county']").attr("disabled", "disabled");
		
		if ($(this).val() != -1)
		{
    	    $.get("/counties.php",
                { states: $(this).val() },
                function(data)
        		{			    
                    $("form.auto_counties select[@name='county']").each(function()
                    {
    				    $(this).empty();
                        $(this).append(data);
    					$(this).attr("disabled", "");
                    });
                }
        	);
		}
	});
	
	$("form.auto_counties").submit(function()
	{
	    var thisForm = $(this);
		
	    $(".countyStateMessage").hide();
		
		var selectedCounty = $(thisForm).find("select[@name='county'] option:selected").val();
		
	    if (selectedCounty == -1 || ! selectedCounty || $("form.auto_counties select[@name='state']").val() == -1)
		{
		    if (selectedCounty != "CAN" && selectedCounty != "MEX")
			{
		        //$(".countyStateMessage").show();
		        //return false;
			}
		}
		
		var requiredEmail = $(thisForm).find(".requiredEmail");
		
		$(requiredEmail).css("color", "black");
		
		if ($(requiredEmail).val().indexOf("@") == -1)
		{
		    $(requiredEmail).css("color", "red");
			$("label[@for='email']").css("color", "red");
		    return false;
		}
	});
});
