/*
 * Initialize town selection list based on initial state of the form
 */
function initTownSelection(dataArray, formName, availableLocationsSelectBoxId, 
	selectedLocationsSelectBoxId, stateSelectionRadioId, selectedLocationsHiddenId) 
{
	var availableLocations = document.getElementById(availableLocationsSelectBoxId);
	var selectedLocations  = document.getElementById(selectedLocationsSelectBoxId);	
	var selectedLocationsValue = document.getElementById(formName + ':' + selectedLocationsHiddenId);
	var stateSelection = document.forms[formName].elements[formName + ':' + stateSelectionRadioId];
	
	// Init selected locations, only if state & town is the selected tab
	if(selectedLocationsValue != null){
		var locationIds = selectedLocationsValue.value.split(',');
		for(var i = 0; i < locationIds.length; i++) {
			if(locationIds[i].length > 0) {
				var location = getLocation(dataArray, locationIds[i]);
				
				var option = new Option(location[3], location[1]);
				
				try {
					selectedLocations.add(option, findInsertBeforeOption(dataArray, selectedLocations, option));
				} catch (ex) {
					selectedLocations.add(option, findInsertBeforeIndex(dataArray, selectedLocations, option));
				}
			}
		}
		
		// Init available locations	
		if (stateSelection != null) {
			var state = getSelectedState(stateSelection);
			if(state != null) {
				initAvailableLocations(dataArray, formName, availableLocationsSelectBoxId, selectedLocationsSelectBoxId, state);
			}
		}
	}
}

/*
 * Initialize town selection list based on selected State
 */
function initAvailableLocations(dataArray, formName, availableLocationsSelectBoxId, selectedLocationsSelectBoxId, state) {
//	alert('Initializing town selection...');
	var availableLocations = document.getElementById(availableLocationsSelectBoxId);
	var selectedLocations  = document.getElementById(selectedLocationsSelectBoxId);
	
	availableLocations.options.length = 0;
	for(var i = 0; i < dataArray.length; i++) {
		if(dataArray[i][4] == state) {
			var option = new Option(dataArray[i][2], dataArray[i][1]);
			
			if(!isLocationSelected(selectedLocations, option.value)) {
				try {
					availableLocations.add(option, null);
				} catch (ex) {
					availableLocations.add(option);
				}
			}
		}
	}
}

function addSelectedLocations(dataArray, formName, availableLocationsSelectBoxId, selectedLocationsSelectBoxId, selectedLocationsHiddenId) {
	var availableLocations = document.getElementById(availableLocationsSelectBoxId);
	var selectedLocations  = document.getElementById(selectedLocationsSelectBoxId);
	var selectedLocationsValue = document.getElementById(formName + ':' + selectedLocationsHiddenId);
	
	var selectedOptions = getSelectedOptions(availableLocations);
	for(var i = selectedOptions.length -1; i >= 0; i--) {
		var location = getLocation(dataArray, selectedOptions[i].value);
		var option = new Option(location[3], location[1]);
		
		removeMatchingOptions(selectedLocations, option);
			
		try {
			selectedLocations.add(option, findInsertBeforeOption(dataArray, selectedLocations, option));
		} catch (ex) {
			selectedLocations.add(option, findInsertBeforeIndex(dataArray, selectedLocations, option));
		}
	}
	
	removeAvailableLocations(dataArray, availableLocations);
	
	updateSelectLocationsValue(selectedLocations, selectedLocationsValue);
}

function removeSelectedLocations(dataArray, formName, availableLocationsSelectBoxId, selectedLocationsSelectBoxId, stateSelectionRadioId, selectedLocationsHiddenId) {
	var availableLocations = document.getElementById(availableLocationsSelectBoxId);
	var selectedLocations  = document.getElementById(selectedLocationsSelectBoxId);
	var selectedLocationsValue = document.getElementById(formName + ':' + selectedLocationsHiddenId);
	var stateSelection = document.forms[formName].elements[formName + ':' + stateSelectionRadioId];
	
	if (stateSelection != null) {
		addAvailableLocations(dataArray, availableLocations, selectedLocations, stateSelection);
	}
	var selectedIndices = getSelectedIndices(selectedLocations);
	for(var i = selectedIndices.length -1; i >= 0; i--) {
		selectedLocations.remove(selectedIndices[i]);
	}
	
	updateSelectLocationsValue(selectedLocations, selectedLocationsValue);
}

function updateSelectLocationsValue(selectedLocations, selectedLocationsValue) {
	var value = '';
	
	for(var i = 0; i < selectedLocations.options.length; i++) {
		value += selectedLocations.options[i].value;
		
		if(i < selectedLocations.options.length -1) {
			value +=',';
		}	
	}
	
	selectedLocationsValue.value = value;
}

function removeAvailableLocations(dataArray, availableLocations) {
	var selectedIndices = getSelectedIndices(availableLocations);
	for(var i = selectedIndices.length -1; i >= 0; i--) {
		var option = availableLocations.options[selectedIndices[i]];
		
		if(isTown(option.value)) {
			var townAreas = getTownAreas(dataArray, option.value);
			
			if(townAreas.length > 0) {				
				for(var i2 = townAreas.length -1; i2 >= 0; i2--) {
					var index = getOptionIndex(availableLocations, townAreas[i2][1]);
					if(index != -1) {
						availableLocations.remove(index);
					}
				}				
			}
		}
		availableLocations.remove(selectedIndices[i]);
	}
}

function addAvailableLocations(dataArray, availableLocations, selectedLocations, stateSelection) {
	var selectedOptions = getSelectedOptions(selectedLocations);
	
	var state = getSelectedState(stateSelection);	
	
	for(var i = 0; i < selectedOptions.length; i++) {
		var location = getLocation(dataArray, selectedOptions[i].value);
		
		if(state != null && location[4] == state) {
			var option = new Option(location[2], location[1]);
				
			try {
				availableLocations.add(option, findInsertBeforeOption(dataArray, availableLocations, option));
			} catch (ex) {
				availableLocations.add(option, findInsertBeforeIndex(dataArray, availableLocations, option));
			}
			
			if(isTown(option.value)) {
				var townAreas = getTownAreas(dataArray, location[1]);
				for(var i2 = 0; i2 < townAreas.length; i2++) {
					option = new Option(townAreas[i2][2] + " " + townAreas[i2][1], townAreas[i2][1]);
					try {
						availableLocations.add(option, findInsertBeforeOption(dataArray, availableLocations, option));
					} catch (ex) {
						availableLocations.add(option, findInsertBeforeIndex(dataArray, availableLocations, option));
					}
				}
			}
		}
	}
}

function getTownId(locationId) {
	var i = locationId.indexOf('.A.');
	if(i != -1) {
		return locatoinId.substr(0, i);
	} else {
		return locationId;
	}
}

function isTown(locationId) {
	return locationId.indexOf('.A.') == -1;
}

function isArea(locationId) {
	return locationId.indexOf('.A.') != -1;
}

function getTownAreas(dataArray, townId) {
	var townAreas = new Array();
	for(var i = 0; i < dataArray.length; i++) {
		if(dataArray[i][1] != townId && dataArray[i][1].match(townId) != null) {
			townAreas[townAreas.length] = dataArray[i];
		}
	}
	return townAreas;
}

function getLocation(dataArray, locationId) {
	for(var i = 0; i < dataArray.length; i++) {
		if(dataArray[i][1] == locationId) {
			return dataArray[i];
		}
	}
}

function getSelectedOptions(controlObj) {	
	var selectedOptions = new Array();
	for(var i = 0; i < controlObj.options.length; i++) {
		if(controlObj.options[i].selected) {
			selectedOptions.push(controlObj.options[i]);
		}
	}
	
	return selectedOptions;
}

function isLocationSelected(selectedLocations, locationId) {
	for(var i = 0; i < selectedLocations.options.length; i++) {
		if(selectedLocations.options[i].value == locationId) {
			return true;
		}
	}	
	return false;
}

function getSelectedIndices(controlObj) {	
	var selectedIndices = new Array();
	for(var i = 0; i < controlObj.options.length; i++) {
		if(controlObj.options[i].selected) {
			selectedIndices[selectedIndices.length] = i;
		}
	}
	
	return selectedIndices;
}

function getSelectedState(stateSelection) {
	for(var i = 0; i < stateSelection.length; i++) {
		if(stateSelection[i].checked) {
			return stateSelection[i].value;
		}
	}
	return null;
}

function getOptionIndex(controlObj, locationId) {
	for(var i = 0; i < controlObj.options.length; i++) {
		if(controlObj.options[i].value == locationId) {
			return i;
		}
	}
	return -1;
}

function sortLocationsSelectBox(dataArray, controlObj) {	
	var sortedOptions = new Array();
	for(var i = 0; i < controlObj.options.length; i++) {
		sortedOptions[i] = new Array();
		sortedOptions[i][0] = getLocation(dataArray, controlObj.options[i].value)[0];
		sortedOptions[i][1] = controlObj.options[i];
	}
	sortedOptions.sort(compareOptions);
	
	controlObj.options.length = 0;
	for(var i = 0; i < sortedOptions.length; i++) {
		controlObj.options[i] = sortedOptions[i][1];
	}
}

function findInsertBeforeOption(dataArray, selectBox, newOption) {
	var newOptionSortOrder = parseInt(getLocation(dataArray, newOption.value)[0]);

	for(var i = 0; i < selectBox.options.length; i++) {
		var optionSortOrder = parseInt(getLocation(dataArray, selectBox.options[i].value)[0]);
		if(newOptionSortOrder < optionSortOrder) {
			return selectBox.options[i];
		}
	}
	
	return null;
}

function findInsertBeforeIndex(dataArray, selectBox, newOption) {
	var newOptionSortOrder = parseInt(getLocation(dataArray, newOption.value)[0]);

	for(var i = 0; i < selectBox.options.length; i++) {
		var optionSortOrder = parseInt(getLocation(dataArray, selectBox.options[i].value)[0]);
		if(newOptionSortOrder < optionSortOrder) {
			return i;
		}
	}
	
	return selectBox.options.length;
}

function removeMatchingOptions(selectBox, option) {
	var matchingOptionsIndices = new Array();
	for(var i = 0; i < selectBox.options.length; i++) {
		if(selectBox.options[i].value.match(option.value) != null) {
			matchingOptionsIndices[matchingOptionsIndices.length] = i;
		}
	}
	
	for(var i = matchingOptionsIndices.length -1; i >= 0; i--) {
		selectBox.remove(matchingOptionsIndices[i]);
	}
}

function compareOptions(a, b) {
	return a[0] - b[0];
}