J(document).ready(
	function() 
	{
		
		fillCountriesSC();
		fillStates(226);
		
		J('#countries').change(
			function() { 
				fillStates( J('#countries option[selected]').attr('value') ) 
		});
		
		J('#submit').click(
			function() 
			{
				J('#buy_direct_form').ajaxSubmit(
				{
					success:  processResponse,
					dataType: 'json'
				});
				
				return false;
			}
		);
		
		J('#outside_uscan').click(
			function() 
			{
				if(J('#outside_uscan').attr('checked') == true)
				{
					fillCountries();
					J('#states').empty();
					J('#postal_req').empty();
					J('#state_req').empty();
				}
				else
				{
					fillCountriesSC();
					fillStates(226);
					J('#postal_req').append('*');
					J('#state_req').append('*');
				}
			}
		);
		
	}
)

function processResponse( response ) 
{ 
	if ( response.show_dealers ) 
	{
		window.location = '/site_forms/show_dealers/';
	}
	
	if ( response.success ) 
	{
		window.location = '/site_forms/form_thnks/d2c_300';
	} 
	else 
	{
		setErrors( response.errors );
	}
}

/*
function myfillCountries(field_id) 
{ 
	var str = '';
	
	// USA and Canada to come first
	str += '<option value="226">'+countries[226].name+'</option>';
	str += '<option value="38">'+countries[38].name+'</option>';
	
	for ( var id in countries ) { 
		if ( id != '226' && id != '38' ) {
			str += '<option value='+id+'>'+countries[id].name+'</option>';
		}
	}
	J('#'+field_id).empty();
	J('#'+field_id).append(str);
}*/
