// These arrays contain the IDs and names of the areas,
// ordered by their parent id. Example:
//
// areaIDs[0] = new Array(1, 2, 3);
// areaNames[0] = new Array("Canada", "Norge", "USA");
//
// Where 0 is the parent id of the areas.

var areaIDs = new Object();
var areaNames = new Object();
var areaControls = new Array();

function disableCheckBox(box)
{
	box.disabled = true;
	box.checked = false;
}

function enableCheckBox(box)
{
	box.disabled = false;
	box.checked = true;
}

function countryChange()
{
	var tempSelection = areaControls[0].options[areaControls[0].selectedIndex].value;
	
	populateBox(1, tempSelection, false);
	populateBox(2, -1, false);
	
	// If no country is chosen
	if (tempSelection == -1) 
	{
		selectedDestination.value = 0;
		enableCheckBox(chkGeneralInfo);
		enableCheckBox(chkCountryInfo);
		enableCheckBox(chkArticles);
		enableCheckBox(chkProducts);
	}
	else 
	{
		selectedDestination.value = tempSelection;
		disableCheckBox(chkGeneralInfo);
		enableCheckBox(chkCountryInfo);
		enableCheckBox(chkArticles);
		enableCheckBox(chkProducts);
	}
}

function areaChange()
{
	var tempSelection = areaControls[1].options[areaControls[1].selectedIndex].value;
	
	populateBox(2, tempSelection, false);
	
	// If no area is Chosen
	if (tempSelection == -1) 
	{
		selectedDestination.value = areaControls[0].options[areaControls[0].selectedIndex].value;
		enableCheckBox(chkCountryInfo);
		enableCheckBox(chkArticles);
		enableCheckBox(chkProducts);
	}
	else 
	{
		selectedDestination.value = tempSelection;
		disableCheckBox(chkCountryInfo);
		disableCheckBox(chkArticles);
		disableCheckBox(chkProducts);
	}
}

function cityChange()
{
	var tempSelection = areaControls[2].options[areaControls[2].selectedIndex].value;
	
	// If no city is chosen
	if (tempSelection == -1) 
	{
		selectedDestination.value = areaControls[1].options[areaControls[1].selectedIndex].value;
	}
	else 
	{
		selectedDestination.value = tempSelection;
	}
}

function populateBox(index, parentID, init)
{
	var option;
	areaControls[index].options.length = 0;
	areaControls[index].options.add(new Option(translations[index], -1));

	for (i in areaNames[parentID])
	{
		option = new Option(areaNames[parentID][i], areaIDs[parentID][i]);
		areaControls[index].options.add(option);
		
		if (init && (option.value == selectedValues[index]))
		{
			option.selected = true;
		}
	}
}

function initializeBoxes()
{
	for (i in controlNames)
	{
		areaControls.push(document.getElementById(controlNames[i]));
	}
	
	areaControls[0].onchange = countryChange;
	populateBox(0, 0, true);
	
	areaControls[1].onchange = areaChange;
	populateBox(1, selectedValues[0], true);
	
	areaControls[2].onchange = cityChange;
	populateBox(2, selectedValues[1], true);
	
	if (selectedValues[0] > 0)
	{
		selectedDestination.value = selectedValues[0];
		disableCheckBox(chkGeneralInfo);
	}
	
	if (selectedValues[1] > 0)
	{
		selectedDestination.value = selectedValues[1];
		disableCheckBox(chkCountryInfo);
		disableCheckBox(chkArticles);
		disableCheckBox(chkProducts);
	}
	
	if (selectedValues[2] > 0)
	{
		selectedDestination.value = selectedValues[2];
	}
}

