var rearrangeSelectOptionsOrdering = ['CHE', 'DEU', 'AUT'];

function rearrangeSelectOptions(select) {
	select = $(select);
	var options = select.select('option');
	
	// set up an index <country code> -> <node>
	var optionsIndex = new Array();
	
	options.each(
		function (n) {
			optionsIndex[n.value] = n;
		}
	);
	
	// build the new option array
	var newOptions = new Array();
	
	// first the top sorting
	rearrangeSelectOptionsOrdering.each(
		function (n) {
			newOptions[newOptions.length] = optionsIndex[n];
			optionsIndex[n].renderedAlready = true;
		}
	);
	
	// all the rest
	options.each(
		function (n) {
			if (!n.renderedAlready) {
				newOptions[newOptions.length] = n;
			}
		}
	);
	
	// copy the new options into the select box and set the new selected
	select.options[select.selectedIndex].isReallySelected = true;
	select.options[select.selectedIndex].selected = 'selected';
	select.options.length = 0;
	var newSelected = 0;
	newOptions.each(
		function (n) {
			select.options[select.options.length] = n;
			if (!!n.isReallySelected) {
				newSelected = select.options.length - 1;
			} else {
				n.selected = false;
			}
		}
	);
	select.selectedIndex = newSelected;
}

document.observe('dom:loaded',
	function () {
		var select = $('shopBillingAddressForm-country');
		if (select) {
			rearrangeSelectOptions(select);
		}
		
		select = $('tx-srfeuserregister-pi1-static_info_country');
		if (select) {
			rearrangeSelectOptions(select);
		}
	}
);


