	/**
	 * Toggle the visibility of a layer
	 */
	function toggleLayer(whichLayer) {
		var style2 = document.getElementById(whichLayer).style;         
		if (style2.display == "" || style2.display == "none") {
			style2.display = "block";
		} else {
			style2.display = "none";
		}
	}
	
	/**
	 * Fetches the AIBL data fields available for selection
	 */
	function loadData() {
		getfieldsforcategories('Demographic');
		getfieldsforcategories('Vital Signs');
		getfieldsforcategories('Medical History');
		getfieldsforcategories('Neuropsych');
		getfieldsforcategories('RBM');
		getfieldsforcategories('Pathology');
		getfieldsforcategories('PIB');
		getfieldsforcategories('Physical Activity');
	}
	
	function handleEvent(e) {
		updateList('field-list',getFormElement(e).value);
	}
	
	/**
	 * Convenience method to load the given list
	 * before making a call to add a single item
	 */
	function updateList(list, item) {
		var e = getFormElement(list);
		update(e, item);
				
		//update hidden field on form
		var hidden = getFormElement('fields');
		var fields = "";
		for(i=0;i<e.length;i++) {
			var field = e.options[i].text;
			fields = fields + field + ',';
		}
		hidden.value = fields;
	}
	
	/**
	 * Adds/removes the given item to/from the given list
	 */
	function update(e, item) {
		if(notInList(e,item)) {
			var len = e.length++; // Increment the size of list and return the size
  			e.options[len].value = item;
  			e.options[len].text = item;
  			e.selectedIndex = len;
		} else {
			//remove from list
			for(i=0;i<e.length;i++) {
				var txt = e.options[i].text;
				if(txt == item) {
					e.remove(i);
					break;
				}	
			}	
		}
	}
	
	/**
	 * Move the selected item up one place in the given list
	 */
	function up(whichElement) {
		var e = getFormElement(whichElement);
		if(!isListEmpty(e) && isListSelected(e) 
								&& !isFirstInList(e)) {
			var selected = e.selectedIndex;
			var moveText1 = e[selected-1].text;
	        var moveText2 = e[selected].text;
	        var moveValue1 = e[selected-1].value;
	        var moveValue2 = e[selected].value;
	        e[selected].text = moveText1;
	        e[selected].value = moveValue1;
	        e[selected-1].text = moveText2;
	        e[selected-1].value = moveValue2;
	        e.selectedIndex = selected-1;
		}
	}
	
	/**
	 * Move the selected item down one place in the given list
	 */
	function down(whichElement) {
		var e = getFormElement(whichElement);
		if(!isListEmpty(e) && isListSelected(e) 
								&& !isLastInList(e)) {
			var selected = e.selectedIndex;
			var moveText1 = e[selected+1].text;
	       	var moveText2 = e[selected].text;
	       	var moveValue1 = e[selected+1].value;
	       	var moveValue2 = e[selected].value;
	       	e[selected].text = moveText1;
	       	e[selected].value = moveValue1;
	       	e[selected+1].text = moveText2;
	       	e[selected+1].value = moveValue2;
	       	e.selectedIndex = selected+1;
		}		
	}
	
	function getFormElement(whichElement) {
		return document.getElementById(whichElement);
	}
	
	function isListEmpty(list) {
		return list.length == 0;
	}
	
	function isListSelected(list) {
		return list.selectedIndex != -1;
	}
	
	function isFirstInList(list) {
		return list.selectedIndex == 0;
	}
	
	function isLastInList(list) {
		return list.selectedIndex == (list.length - 1);
	}
	
	function notInList(list, item) {
		for(i=0;i<list.length;i++) {
			var txt = list.options[i].text;
			if(txt == item) {
				return false;
			}	
		}
		return true;
	}
	
	function clear(whichElement) {
		var element = getFormElement(whichElement);
		element.value = "";
	}
			
