// contact / form.view.js

/*
$(document).ready(function() {
	// hide all optgroups
	$('#form_store optgroup').hide(0);
	
	// when changing the province
	$('#form_province').change(function() {
		var label = $(this).val();
		
		$('#form_store').each(function() {
			// hide the visible optgroup
			$(this).find('optgroup:visible').hide(0);
			
			// show the chosen optgroup
			$(this).find('optgroup[label=' + label + ']').show(0);
			
			// select the first option in the optgroup or nothing
			if(label.length != 0) {
				$(this).val(
					$(this).find('> optgroup:visible > option').val()
				);
			} else {
				$(this).val('');
			}
		});
	});
});
*/

$(document).ready(function() {
	var stores = $('#form_store').html();
	
	// hide all optgroups
	label = $('#form_province').val();
	$('#form_store optgroup[label!=' + label + ']').remove();
	
	// when changing the province
	$('#form_province').change(function() {
		var label = $(this).val();
		
		$('#form_store').each(function() {
			// only show the wanted stores
			$(this).find('optgroup').remove();
			$(this).append(stores);
			
			$(this).find('option:first').remove();
			$(this).find('optgroup[label!=' + label + ']').remove();
			
			// select the first option in the optgroup or nothing
			$(this).val(
				$(this).find('> optgroup:visible > option').val()
			);
		});
	});
});