document.observe('dom:loaded', function() {
	if ($('state_field')) {
		if ($('state_field').options[$('state_field').selectedIndex].value == 'AR')
		show_counties('state_field');
	}
	if ($('ship_state_field')) {
		if ($('ship_state_field').options[$('state_field').selectedIndex].value == 'AR')
			show_counties('ship_state_field');
	}
	shipping_toggle();
});

function show_counties(element) {
	var element = $(element);
	var value = element.options[element.selectedIndex].value;
	
	if (value == 'AR') {
		new Ajax.Request('/products/checkout/counties?state='+value, {
			asynchronous:true, 
			evalScripts:true, 
			method:'get', 
			onSuccess: function(transport) {
				if (element.id == 'state_field') {
					Element.update("county-field", transport.responseText);
					$('county').show();
				}
				
				if (element.id == 'ship_state_field') {
					Element.update("ship-county-field", transport.responseText);
					$('ship-county').show();
				}
			}
		});
	}
	else {
		if (element.id == 'state_field')
			$('county').hide();
		if (element.id == 'ship_state_field')
			$('ship-county').hide();
	}
}

function shipping_toggle() {
	var shipping_toggle = $('shipping-toggle');
	if( shipping_toggle ) {
		show_shipping_form(!shipping_toggle.checked);
	}
}

function show_shipping_form(visible) {
	if (visible) { 
		$('shipping_form').show();
		$('shipping-same').hide();
	}
	else { 
		$('shipping_form').hide();
		$('shipping-same').show();
	}    
}
