function cpTieredSelector_Change(source, index, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams) {
	var selectedValue = source.options[source.selectedIndex].value;
	var hiddenField = $get(hiddenFieldId);
	
	var next = $get(hiddenFieldId + index);
	if (next != null) cpTieredSelector_Remove(next.parentNode.parentNode, index, hiddenFieldId);
	
	if (selectedValue == '') {
		if (index <= 2) {
			hiddenField.value = '';
		} else { 
			var previous = $get(hiddenFieldId + (index - 2)).childNodes[0];
			// when the value is set to nothing make sure that an inaccessible value is not inadvertantly selected
			if (previous.options[previous.selectedIndex].text == 'Inaccessible Value')
			{
				previous.options[0].selected = true;
				cpTieredSelector_Change(previous, index - 1, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams);
				return;
			}
			hiddenField.value = previous.options[previous.selectedIndex].value;
		}
	} else {
		hiddenField.value = selectedValue;
		Centralpoint.Web.UI.WebServices.TieredSelector.Render(index, success, tableName, textField, valueField, parentField, selectedValue, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, '', false, enabled, addParams, eval(success));
	}
	
	// remove the inaccessible value node when the selection is changed
	var last = source.length - 1;
	if (last > 0) {
		if (source.options[last].text == 'Inaccessible Value') {
			source.remove(last);
			source.parentNode.style.display = 'none';
		}
	}
	//alert('hiddenField.value: ' + hiddenField.value);
}

function cpTieredSelector_Remove(parentNode, index, hiddenFieldId) {
	var i = index;
	var selectorNode = $get(hiddenFieldId + i);
	
	while (selectorNode != null) {
		parentNode.removeChild(selectorNode.parentNode);
		i++;
		selectorNode = $get(hiddenFieldId + i);
	}
}

function cpTieredSelector_SelectByValue(parentId, value) {
	//alert(parentId + ' - ' + value);
	var parent = $get(parentId);
	
	// if the parent control is not available wait for it to become available
	if (parent == null) {
		setTimeout('cpTieredSelector_SelectByValue(\'' + parentId + '\', \'' + value.replace(/'/g, '\\\'') + '\')', 100);
		return;
	}
	
	var source = parent.childNodes[0];
	var inaccessible = true;
	for (var i = 0; i < source.options.length; i++) {
		var option = source.options[i];
		if (option.value == value) {
			option.selected = true;
			inaccessible = false;
			break;
		}
	}
	
	// if the item doesn't exist add it to the selector as an inaccessible value
	if (inaccessible) cpTieredSelector_AppendOption(source, 'Inaccessible Value', value, true);
}

function cpTieredSelector_AppendOption(source, text, value, selected)
{
  var option = document.createElement('option');
  option.text = text;
  option.value = value;
  option.selected = selected;

  try {
    source.add(option, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    source.add(option); // IE only
  }
}

function cpTieredSelector_RenderAfterParent(index, success, tableName, textField, valueField, parentField, parentValue, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, parentId, listSelectionMode, force, enabled, addParams) {
	if (parentId != '') {
		var parent = $get(parentId);
		
		// if the parent control is not available wait for it to become available
		if (parent == null) {
			setTimeout('cpTieredSelector_RenderAfterParent(' + index + ', \'' +  success + '\', \'' +  tableName + '\', \'' +  textField.replace(/'/g, '\\\'') + '\', \'' +  valueField + '\', \'' 
				+  parentField + '\', \'' +  parentValue + '\', \'' +  filter.replace(/'/g, '\\\'') + '\', ' + hasAncestorsTable + ', ' +  prefixFields + ', ' +  useDev + ', \'' +  hiddenFieldId + '\', \'' 
				+  parentId + '\', \'' + listSelectionMode + '\', ' + force + ', ' + enabled + ', \'' + addParams + '\')', 100);
			return;
		}
	}
	setTimeout('Centralpoint.Web.UI.WebServices.TieredSelector.Render(' + index + ', \'' +  success + '\', \'' +  tableName + '\', \'' +  textField.replace(/'/g, '\\\'') + '\', \'' +  valueField + '\', \'' 
		+  parentField + '\', \'' +  parentValue + '\', \'' +  filter.replace(/'/g, '\\\'') + '\', ' + hasAncestorsTable + ', ' +  prefixFields + ', ' +  useDev + ', \'' +  hiddenFieldId + '\', \'' 
		+ listSelectionMode + '\', \'\', ' + force + ', ' + enabled + ', \'' + addParams + '\', eval(' +  success + '))', 100);
}

function cpTieredSelector_Toggle(clientId, index, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams) {
	var image = $get('img' + clientId);
	var intSep = image.src.lastIndexOf('/') + 1;
	var strSrc = image.src.substring(intSep).toLowerCase();
	if (strSrc != 'plus.gif') {
		image.src = image.src.substring(0, intSep) + 'Plus.gif';
		$get('divc' + clientId).style.display = 'none';
	} else {
	    Centralpoint.Web.UI.WebServices.TieredSelector.Render(index, success, tableName, textField, valueField, parentField, $get('cbx' + clientId).value, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, $get(hiddenFieldId).value, false, enabled, addParams, eval(success));
	}
}

function cpTieredSelector_CheckChanged(source, clientId, index, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams) {
	var hiddenField = $get(hiddenFieldId);
	var checkbox = $get('cbx' + clientId);
	
	hiddenField.value = hiddenField.value.toLowerCase();
	var values = hiddenField.value.split(', ');
	hiddenField.value = checkbox.checked ? checkbox.value : '';
	for (var i = 0; i < values.length; i++) {
		if ((values[i].trim() != '') && (values[i] != checkbox.value.toLowerCase())) {
			if (hiddenField.value != '') hiddenField.value += ', ';
			hiddenField.value += values[i];
		}
	}
	
	cpTieredSelector_CheckChangedBase(source, clientId, index, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams);
}

function cpTieredSelector_CheckChangedBase(source, clientId, index, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams) {
	var image = $get('img' + clientId);
	var imageSrc = image.src.substring(image.src.lastIndexOf('/') + 1).toLowerCase();

	if (source.checked && (imageSrc == 'plus.gif'))
		cpTieredSelector_Toggle(clientId, index, success, tableName, textField, valueField, parentField, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, selectionMode, enabled, addParams);
}

function cpTieredSelector_ExpandAfterParent(index, success, tableName, textField, valueField, parentField, parentValue, filter, hasAncestorsTable, prefixFields, useDev, hiddenFieldId, listSelectionMode, enabled, addParams) {
	var childrenId = 'divc' + hiddenFieldId + '_' + index + '_' + parentValue;
	var children = $get(childrenId);
	//alert(parentValue + ': ' + (children == null));
	
	// if the parent control is not available wait for it to become available
	if (children == null) {
		setTimeout('cpTieredSelector_ExpandAfterParent(' + index + ', \'' +  success + '\', \'' +  tableName + '\', \'' +  textField.replace(/'/g, '\\\'') + '\', \'' +  valueField + '\', \'' 
			+  parentField + '\', \'' +  parentValue + '\', \'' +  filter.replace(/'/g, '\\\'') + '\', ' + hasAncestorsTable + ', ' +  prefixFields + ', ' +  useDev + ', \'' 
			+  hiddenFieldId + '\', \'' + listSelectionMode + '\', ' + enabled + ', \'' + addParams + '\')', 100);
		return;
	}
	
	setTimeout('Centralpoint.Web.UI.WebServices.TieredSelector.Render(' + (index + 1) + ', \'' +  success + '\', \'' +  tableName + '\', \'' +  textField.replace(/'/g, '\\\'') + '\', \'' +  valueField + '\', \'' 
		+  parentField + '\', \'' +  parentValue + '\', \'' +  filter.replace(/'/g, '\\\'') + '\', ' + hasAncestorsTable + ', ' +  prefixFields + ', ' +  useDev + ', \'' +  hiddenFieldId + '\', \'' + listSelectionMode + '\', \'' 
		+ $get(hiddenFieldId).value.replace(/'/g, '\\\'') + '\', false, ' + enabled + ', \'' + addParams + '\', eval(' +  success + '))', 100);
}

function cpTieredSelector_ShowInaccessibleValuesItem(hiddenFieldId) {
	var item = $get('div' + hiddenFieldId + '_1_[cp:placeholders key=\'InaccessibleValues\' /]');
	if (item == null) {
		setTimeout('cpTieredSelector_ShowInaccessibleValuesItem(\'' + hiddenFieldId + '\')', 100);
		return;
	}
	item.style.display = 'block';
}

function cpTieredSelector_ToggleHeight(source, divId) {
    var div = $get(divId);
	if (source.title == 'Click here to limit the selector\'s height using scroll bars.') {
		cpTieredSelector_ResizeDiv(divId);
		source.src = '/Integrations/Centralpoint/Resources/Controls/CpTieredSelectorLimited.png';
		source.title = 'Click here to allow the selector to resize to fit it\'s contents.';
	} else {
		div.style.height = 'auto';
		source.src = '/Integrations/Centralpoint/Resources/Controls/CpTieredSelectorDynamic.png';
		source.title = 'Click here to limit the selector\'s height using scroll bars.';
	}
	return false;
}

function cpTieredSelector_ResizeDiv(divId, limit) {
	if (limit == undefined) limit = 152;
	var div = $get(divId);
	if (div != null) {
		var height = div.scrollHeight;
		if (height > limit)
			height = limit;
		else if (height < 15)
			height = 15;
		div.style.height = height + 'px';
	}
}
