$(document).ready(function(){
	// an array of 2 letter coountry ISO codes
	// which disable branch selection for
	// countries
	var hide_branches_for = ["FR"];

	$('.branch-row').hide();
	$('#pays-enlev, #pays-dem').change(function(){
		var country_select = $(this);
		$.ajax({
			type : 'GET',
			dataType : 'html', 
			url : 'get_country_branches.php',
			data : 'country=' + $("'#" + country_select.attr('id') + " option:selected'").val() + '&field=' + $(':input:eq('+($(":input").index(country_select) + 1) +')').attr('name'),
			success: function(html){
				jQuery.each(hide_branches_for, function(){
					if(country_select.val() == this){
						hide_branch = true;
					}else{
						hide_branch = false;
					}
				});

				if(html && !hide_branch){
					$(':input:eq('+($(":input").index(country_select) + 1) +')').html(html);
					// if something broke it's probably this
					country_select.parent().parent().next().show();
					country_select.parent().parent().siblings('.form-spacer').hide();
				}else{
					$(':input:eq('+($(":input").index(country_select) + 1) +')').html(
						'<option value="-1">(not applicable)</option>'
					);
					// if something broke it's probably this
					country_select.parent().parent().next().hide();
					country_select.parent().parent().siblings('.form-spacer').show();
				}

			}
		});
	}).change();
});

