function getChildren(fieldId, browseTarget) {
	if (jQuery("#" + fieldId + " li").length == 1) {
		jQuery.post(browseTarget, {
			layout : "none",
			browseitems : jQuery("#" + fieldId).attr("browseitems"),
			parentNodeId : fieldId
		}, function(data) {
			if (data.search("DOCTYPE html PUBLIC") != -1) {
				alert("Session expired, please log in again");
				document.location = '/';
				return;
			}

			jQuery("#" + fieldId + " > ul").html(data);
			jQuery("#" + fieldId).treeview( {
				add : data,
				collapsed : true
			});
			if (jQuery("#" + fieldId + " :checkbox.include")[0].checked)
				checkChildren(fieldId);
		});
	}
}

function checkChildren(fieldId) {

	var includeBox = jQuery("#" + fieldId + " :checkbox.include")[0];

	if (typeof (includeBox) != "undefined") {
		var includeChildren = jQuery("#" + fieldId + " li > *:checkbox.include");
		if (includeBox.checked && includeChildren.length > 0) {
			jQuery.each(includeChildren, function() {
				this.checked = true;
				this.disabled = true;
			});
		} else {
			jQuery.each(includeChildren, function() {
				this.checked = false;
				this.disabled = false;
			});
		}
	}
}

function updateSelection(fieldId, browseTarget) {
	updateSelectionImpl(fieldId, browseTarget, true);
}

function updateSelectionImpl(fieldId, browseTarget, updateCriteria) {
	checkChildren(fieldId);

	var includeBox = jQuery("#" + fieldId + " :checkbox.include")[0];

	var selectionType = (jQuery("#" + fieldId + " :checkbox.include")[0].checked) ? "INCLUDED"
			: "UNSELECTED";
	jQuery.post(browseTarget, {
		layout : "none",
		browseitems : jQuery("#" + fieldId).attr("browseitems"),
		selectionType : selectionType
	}, function(data) {
		if (data.search("DOCTYPE html PUBLIC") != -1) {
			alert("Session expired, please log in again");
			document.location = '/';
			return;
		}
		
		var result = eval("(" + data + ")");
		printTypeResult(result.companySearch, null, result.message, result.prettyResultCount);
	});
}