//
// Javascript functions for dPublish GUI elements
//
// dependencies: dreamFuncs.js
// dependencies: divFuncs.js
//

// field functions
function findFormField(formName,fieldName) {
	fieldObj = null;

    form = MM_findObj(formName);

	if (form != null) {
		for(index = 0; index < form.elements.length; index++) {
			if (form.elements[index].name == fieldName) {
				fieldObj = form.elements[index];
				break;
			}
		}
	}
	
	return fieldObj;
}

// field activation functions
function setFieldState(fieldObj,onOff) {
	if (fieldObj != null) {
		fieldObj.disabled = !onOff;
	}
}

function toggleFieldState(fieldObj) {
	if (fieldObj != null) {
		offOn = fieldObj.disabled;
		fieldObj.disabled = !offOn;
	}
}

// check box functions
function setCheckBox(fieldObj,checked) {
	if (fieldObj != null) {
		fieldObj.checked = checked;
	}
}

function toggleCheckBox(fieldObj) {
	if (fieldObj != null) {
		checked = fieldObj.checked;
		fieldObj.checked = !checked;
	}
}

function setAllFormCheckBoxes(formName,fieldName,onOff) {
    form = MM_findObj(formName);

	for(index = 0; index < form.elements.length; index++) {
		if (form.elements[index].name == fieldName) {
			form.elements[index].checked = onOff;
		}
	}
}	

// selection functions
function getSelectionListSingleSelection(fieldObj) {
	selectedIndex = -1;
	
	if (fieldObj != null) {
		selectedIndex = fieldObj.selectedIndex;
	}

	return selectedIndex;
}

function setSelectionListSingleSelection(fieldObj,selectIndex) {
	if (fieldObj != null) {
		fieldObj.selectedIndex = selectIndex;
	}
}

function setSelectionListSingleSelectionByItemValue(fieldObj,itemValue) {
	if (fieldObj != null) {
		listLength = getSelectionListItemCount(fieldObj);
		for (index = 0; index < listLength; index++) {
			value = "" + fieldObj.options[index].value;
			if (value == ("" + itemValue)) {
				fieldObj.selectedIndex = index;
				break;
			}
		}
	}
}

function getSelectionListItemCount(fieldObj) {
	optionCount = 0;
	if (fieldObj != null) {
		value = fieldObj.options.length;
	}

	return value;
}

function getSelectionListItemName(fieldObj,itemIndex) {
	value = null;
	if (fieldObj != null) {
		value = fieldObj.options[itemIndex].text;
	}

	return value;
}

function getSelectionListItemValue(fieldObj,itemIndex) {
	value = null;
	if (fieldObj != null) {
		value = fieldObj.options[itemIndex].value;
	}

	return value;
}

function clearSelectionList(fieldObj) {
	if (fieldObj != null) {
		listLength = getSelectionListItemCount(fieldObj);
		for (index = 0; index < listLength; index++) {
			fieldObj.options[0] = null;
		}
	}
}

function addSelectionListItem(fieldObj,itemName,itemValue) {
	if (fieldObj != null) {
		listLength = getSelectionListItemCount(fieldObj);
		newItem = new Option(itemName,itemValue);
		fieldObj.options[listLength] = newItem;
	}
}

function removeSelectionListItem(fieldObj,itemIndex) {
	if (fieldObj != null) {
		listLength = fieldObj.options.length;
		if (itemIndex != -1) {
			if (itemIndex != listLength-1) {
				fieldObj.options[itemIndex] = null;
			}
		} else {
			alert("You must select an item in the list to delete.");
		}
	}
}

function removeSelectedSelectionListItem(fieldObj) {
	if (fieldObj != null) {
		selectedItem = fieldObj.selectedIndex;
		removeSelectionListItem(fieldObj,selectedItem);
	}
}

// display block functions
function activateBlock(blockName,onOff) 
{
	if (onOff == "true") {
		setDIVProp(blockName,"display","block");
	} else {
		setDIVProp(blockName,"display","none");
	}
}
