/* ---------- Register jQuery events ---------- */

jQuery(document).ready(function() {
	// register the onchange listener for the drop-down menu
	jQuery('.CountryDropDown').change(function(e) {
		// change all country drop-downs together
		selectCountryInForms('.CountryDropDown', this.options[this.options.selectedIndex].value);
	});

});


/* ---------- JavaScript functions ---------- */

/*
 * Selects the country in all <select> elements with the given CSS class name (dropdownSelector).
 * This should be invoked with the proper countryId (session token) when the document is ready.
 */
function selectCountryInForms(dropdownSelector, countryId) {
	if (countryId == null) {
		// no country selected
		 countryId = '';
	}

	jQuery(dropdownSelector).each(function() {
		for (var i = 0; i < this.options.length; i++) {
			var option = this.options[i];
			if (option.value == countryId) {
				option.selected = true;
				break;
			}
		}
	});
}
