var menus;

window.onload = function() {
	markCurrentPage();
	menus = getTheMenus();
	if (menus.length > 0) {
		handleMenuEvents();
		shipTo(0);
	}
}

function markCurrentPage() {
	here = new RegExp (window.location.pathname+'$');
	theLinks = document.getElementById('menu').getElementsByTagName('a');
	for (var i=0; i < theLinks.length; i++) {
		if (theLinks[i].href.search(here) > 0) {
			theLinks[i].className = 'current';
		}
	}
}

function getTheMenus() {
	return document.getElementsByName('shipping');
}

function handleMenuEvents() {
	if (menus.length > 0) {
		for (var i=0; i < menus.length; i++) {
			menus[i].onchange = function() {shipTo(this.selectedIndex);}
		}
	}
}

function shipTo(selected_i) {
	var ship2 = document.getElementsByName('shipping2');
	if (menus.length > 0) {
		for (var i=0; i < menus.length; i++) {
			// set each shipping menu to same selection
			menus[i].selectedIndex = selected_i;
			// mark flag by destination
			menus[i].parentNode.className = 'flag ship-to-' + selected_i;
			// copy cost to shipping2
			if (ship2[i]) {
				ship2[i].value = menus[i].options[menus[i].selectedIndex].value;
			}
		}
	}
}

