
function doStateSelect(code) {
	// show the state div given the state code
	found = false;
	divs = document.getElementsByTagName("div");
	for (i = 0; i < divs.length; i++) {
		div = divs[i];
		if(!div) continue;
		
		var divId = String(div.id);
		
		// if div id starts with "div_state_"
		var divIdStart = "div_state_";
		if (divId.indexOf(divIdStart) == 0) {
			
			// get the state code of the div
			var divCode = divId.substring(divIdStart.length, divIdStart.length + 2);
			
			// hide or show the div
			if (divCode == code) {
				show(divId);
				found = true;
			} else {
				hide(divId);
			}
		}
	}
	if (found == false) {
		show("div_state_LS_country_US");
	}
}

function doCountrySelect(code) {
	
	// show the country div given the iso 2 country code
	if (code == "LS" || document.getElementById("div_country_" + code)) {
		
		divs = document.getElementsByTagName("div");
		for (i = 0; i < divs.length; i++) {
			div = divs[i];
			if(!div) continue;
			
			var divId = String(div.id);
			
			// if div id starts with "div_country_"
			var divIdStart = "div_country_";
			if (divId.indexOf(divIdStart) == 0) {
				
				// get the country code of the div
				var divCode = divId.substring(divIdStart.length, divIdStart.length + 2);
				
				// hide or show the div
				if (divCode == code) {
					show(divId);
				} else {
					hide(divId);
				}
			}
		}
		
		// show the state list
		doStateSelect("LS");
		
		// tell the flash movie to show the region
		map_DoJavascriptCommand("COUNTRY_SELECT", code);
	}
}

function doRegionSelect(code) {
	
	// show the region div given the region code
	if (code == "LS" || document.getElementById("div_region_" + code)) {
	
		divs = document.getElementsByTagName("div");
		for (i = 0; i < divs.length; i++) {
			div = divs[i];
			if(!div) continue;
			
			var divId = String(div.id);
			
			// if div id starts with "div_region_"
			var divIdStart = "div_region_";
			if (divId.indexOf(divIdStart) == 0) {
				
				// get the region code of the div
				var divCode = divId.substring(divIdStart.length, divIdStart.length + 2);
				
				// hide or show the div
				if (divCode == code) {
					show(divId);
				} else {
					hide(divId);
				}
			}
		}
		
		// show the country list
		doCountrySelect("LS");
		
		// tell the flash movie to show the region
		map_DoJavascriptCommand("REGION_SELECT", code);
		
	}
}

function doWorldSelect() {
	
	// show the region list
	doRegionSelect("LS");
	
	// tell the flash movie to show the world
	map_DoJavascriptCommand("WORLD_SELECT", "WORLD");

}

function map_DoJavascriptCommand(command, args) {
	
	// select the region in the flash movie
	var mapObj = InternetExplorer ? map : document.map;
	if (isMovieLoaded(mapObj)) {
		
		// set the command and argument
		mapObj.SetVariable("javascriptCommand", command);
		mapObj.SetVariable("javascriptArgument", args);
	}
}


var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

// handle all the the FSCommand messages in a Flash movie
function map_DoFSCommand(command, args) {
	
	if (command == "STATE_SELECT") {
		doStateSelect(args);
	} else if (command == "COUNTRY_SELECT") {
		doCountrySelect(args);
	} else if (command == "REGION_SELECT") {
		doRegionSelect(args);
	} else if (command == "WORLD_SELECT") {
		doWorldSelect();
	}

}

// hook for Internet Explorer 
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub map_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call map_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}

// checks if movie is completely loaded.
// returns true if yes, false if no.
function isMovieLoaded(theMovie) {
  // first make sure the movie's defined
  if (typeof(theMovie) != "undefined") {
    // if it is, check how much of it is loaded
    return theMovie.PercentLoaded() == 100;
  } else {
    // if the movie isn't defined, it's not loaded
    return false;
  }
}


