
function set_Default() {
	var isIE = (navigator.appName == "Microsoft Internet Explorer");
	
	var selDeviceType = document.getElementById("selDeviceType");
	var selCountry = document.getElementById("selCountry");
	var selState = document.getElementById("selState");
	var selRegion = document.getElementById("selRegion");
	
	// fill the DeviceType select control
	var i = 0;
	var defaultDeviceIndex = 0;
	for (i=0; i<regionLocations.length; i++) {
		deviceType = regionLocations[i];
		var optDeviceType = document.createElement("OPTION");
		optDeviceType.value = deviceType.DeviceTypeID;
		optDeviceType.text = deviceType.DeviceTypeName;
		optDeviceType.deviceType = deviceType;
		if (isIE) {
			selDeviceType.add(optDeviceType);
		} else {
			selDeviceType.add(optDeviceType, null);
		}
		if (deviceType.DeviceTypeID == 0)
		    defaultDeviceIndex = i;
	}
	selDeviceType.selectedIndex = defaultDeviceIndex;
	selDeviceType.disabled = false;

	var found = false;
	for(i=0; i<selDeviceType.options.length; i++) {
		var optDeviceType = selDeviceType.options.item(i);
		if (gDeviceTypeID == optDeviceType.value) {
			selDeviceType.selectedIndex = i;
			change_DeviceType();
			found = true;
			break;
		}
	}
	
	if (! found) {
	    change_DeviceType();
	} else {
	    if (gCountryID > 0) {
	        found = false;
			for(i=0; i<selCountry.options.length; i++) {
				var optCountry = selCountry.options.item(i);
				if (gCountryID == optCountry.value) {
					selCountry.selectedIndex = i;
					change_Country();
					found = true;
					break;
				}
			}
			
 	        if (found) {
		        if (gStateID > 0) {
			        for(i=0; i<selState.options.length; i++) {
				        var optState = selState.options.item(i);
				        if (gStateID == optState.value) {
					        selState.selectedIndex = i;
					        change_State();
					        break;
				        }
			        }

			        for(i=0; i<selRegion.options.length; i++) {
				        var optRegion = selRegion.options.item(i);
				        //alert(optRegion.value);
				        if (gRegionID == optRegion.value) {
					        selRegion.selectedIndex = i;
					        break;
				        }
			        }
		        }
	        }
	    }
	}
	
}

function change_DeviceType() {
	var isIE = (navigator.appName == "Microsoft Internet Explorer");
	var i = 0;
	
	var selDeviceType = document.getElementById("selDeviceType");
	var selCountry = document.getElementById("selCountry");

    var selectedCountryID = 0;	
	var selectedIndex = -1;
	
    if (selCountry.options.length > 0 && selCountry.selectedIndex != -1)	
    {
        var optSelectedCountry = selCountry.options.item(selCountry.selectedIndex);
        selectedCountryID = optSelectedCountry.value;
    }
    
    if (selectedCountryID == 0)
    {
        selectedCountryID = gCountryID;
    }
    
	selCountry.selectedIndex = 0;
	for(i=selCountry.options.length-1; i >=0; i--) {
		selCountry.remove(i);
	}
	
	
	selCountry.disabled = true;

	if (selDeviceType.options.length > 0 && selDeviceType.selectedIndex != -1) {
		var optDeviceType = selDeviceType.options.item(selDeviceType.selectedIndex);
		var deviceType = optDeviceType.deviceType;

		for (i=0; i<deviceType.Countries.length; i++) {
			var country = deviceType.Countries[i];
			var optCountry = document.createElement("OPTION");
			optCountry.value = country.CountryID;
			optCountry.text = country.CountryName;
			optCountry.country = country;
			if (isIE) {
				selCountry.add(optCountry);
			} else {
				selCountry.add(optCountry, null);
			}
			
			if (country.CountryID == selectedCountryID)
			{
			    selectedIndex = i;
			}
		    if (selectedIndex == -1 && country.CountryName == "United States") {
			    selectedIndex = i;
		    }
		}
		
		if (selectedIndex == -1)
		{
		    selectedIndex = 0;
		}

		selCountry.selectedIndex = selectedIndex;
		change_Country();
	}		
	selCountry.disabled = false;
}

function change_Country() {
	var isIE = (navigator.appName == "Microsoft Internet Explorer");
	var i = 0;
	
	var selCountry = document.getElementById("selCountry");
	var selState = document.getElementById("selState");
	var selRegion = document.getElementById("selRegion");
	
    var selectedStateID = 0;	
    var selectedRegionID = 0;	
	var selectedIndex = 0;
	
	
    if (selState.options.length > 0 && selState.selectedIndex != -1)	
    {
        var optSelectedState = selState.options.item(selState.selectedIndex);
        selectedStateID = optSelectedState.value;
    }
    
    if (selectedStateID == 0)
    {
        selectedStateID = gStateID;
    }


	selState.selectedIndex = 0;
	for(i=selState.options.length-1; i > 0; i--) {
		selState.remove(i);
	}
	
	selState.disabled = true;
    
	if (selCountry.options.length > 0 && selCountry.selectedIndex != -1) {
		var optCountry = selCountry.options.item(selCountry.selectedIndex);
		var country = optCountry.country;

		for (i=0; i<country.States.length; i++) {		    
			var state = country.States[i];
			var optState = document.createElement("OPTION");
			optState.value = state.StateID;
			optState.text = state.StateName;
			optState.state = state;
			if (isIE) {
				selState.add(optState);
			} else {
				selState.add(optState, null);
			}

			if (state.StateID == selectedStateID)
			{
			    selectedIndex = i+1;
			}
		}
		
		if (country.HasStates)
		{
		    selState.selectedIndex = selectedIndex;
		    change_State();
    	    selState.disabled = false;
    	}
    	else
    	{
    	    set_RegionList(country);
    	}
	}
}

function change_State() {
	var isIE = (navigator.appName == "Microsoft Internet Explorer");
	var i = 0;
	
	var selState = document.getElementById("selState");
	var selRegion = document.getElementById("selRegion");
	selRegion.disabled = true;
		
	if (selState.selectedIndex > 0) {
		var optState = selState.options.item(selState.selectedIndex);
		var state = optState.state;
        set_RegionList(state);	
    }	
    else
    {
	    selRegion.selectedIndex = 0;
	    for(i=selRegion.options.length-1; i > 0; i--) {
		    selRegion.remove(i);
	    }        
    }
}

function set_RegionList(container)
{
	var isIE = (navigator.appName == "Microsoft Internet Explorer");
	var i = 0;
	
	var selRegion = document.getElementById("selRegion");
	
    var selectedRegionID = 0;	
	var selectedIndex = 0;
    if (selRegion.options.length > 0 && selRegion.selectedIndex != -1)	
    {
        var optSelectedRegion = selRegion.options.item(selRegion.selectedIndex);
        selectedRegionID = optSelectedRegion.value;
    }
    
    if (selectedRegionID == 0)
    {
        selectedRegionID = gRegionID;
    }

	selRegion.selectedIndex = 0;
	for(i=selRegion.options.length-1; i > 0; i--) {
		selRegion.remove(i);
	}
	selRegion.disabled = true;
	
	for (i=0; i<container.Regions.length; i++) {
		var region = container.Regions[i];
		var optRegion = document.createElement("OPTION");
		optRegion.value = region.RegionID;
		optRegion.text = region.RegionName;
		optRegion.region = region;
		if (isIE) {
			selRegion.add(optRegion);
		} else {
			selRegion.add(optRegion, null);
		}

		if (region.RegionID == selectedRegionID)
		{
			selectedIndex = i+1;
		}
	}
	selRegion.selectedIndex = selectedIndex;
    selRegion.disabled = false;		    		
}

	