<!-- 

	var DB = new Object(); //global variable
	var objComponentVisbility = new Object();
	
	function initialiseDisplay() {
		generateObjComponentVisibility();
		processVisibility();
	}
	
	function generateObjComponentVisibility() {
		// Generate a structure (objComponentVisibility) as described below. This is used to 
		// process component visibility.
		//
		// objComponentVisbility
		// ---------------------
		// objComponentVisbility := [ answers, visible, parents ] (indexed by component)
		// parents := [ p ] (indexed by answer option)
		// p := parent id
		// visible := visibility (true, false, null = unknown)
		// answer := answer of current componenet 
		//
		// Reference
		// ---------
		// DB := [ A ] (indexed by component)
		// A := [ B ] 
		// C := { ans, child }
		// ans := option id
		// child := [ comp ]
		// comp := child component ids
		//
		var currentParent;
		var childArray;
		var currentChild;
		var currentOption;
		var currentComponent;

		// Generate objComponentVisbility from DB
		for (var db in DB) {
			// Add objComponentVisbility
			addIfNotExists( objComponentVisbility, db, new Object() );
			addIfNotExists( objComponentVisbility[db], 'answer', null );
			addIfNotExists( objComponentVisbility[db], 'visible', null );
			addIfNotExists( objComponentVisbility[db], 'parents', null );
			addIfNotExists( objComponentVisbility[db], 'currentVisible', null ); 
					
			currentParent = db;
			
			// For each option array
			for (var co in DB[db]) {
				childArray = DB[db][co].child
				currentOption = DB[db][co].ansID
				
				for (var cc in childArray) {
					currentChild = childArray[cc];
					
					// Add objComponentVisbility
					addIfNotExists( objComponentVisbility, currentChild, new Object() );
					addIfNotExists( objComponentVisbility[currentChild], 'answer', null );
					addIfNotExists( objComponentVisbility[currentChild], 'visible', null );
					addIfNotExists( objComponentVisbility[currentChild], 'parents', new Object() );	
					addIfNotExists( objComponentVisbility[db], 'currentVisible', null );
					
					// Add the parent
					addIfNotExists( objComponentVisbility[currentChild]['parents'], currentOption, currentParent );
				}
			}		
		}
	}
	
	function resetVisibility() {
		for( var cc in objComponentVisbility ) {
			objComponentVisbility[cc]['visible'] = null;
		}
	}
	
	function processVisibility() {
		// This function processses the visibility of dynamic questions. The logic used is:
		// 
		// 1. While finished is false
		// 	  a. Loop through objComponentVisbility:
		//	  	 i. For each component
		//       	- If there are no parents, process visibility
		//       	- If the visibility of all parents have been processed, process visibility
		//       	- Otherwise go to next component
		//       ii. If process visibility
		//          - get the answer for the component
		//          - set the visibility for the component to true if any of it's answer option is selected, and
		//            that parent is visible, otherwise it is set to false
		//          - mark the component as processed.
		//       iii. If any component has been processed, set finished to false, otherwise set finish to true.	
		//
		var currentParent;
		var currentComponent;	
		var finished = false;
		var visible = false;
		var processVisible = false;
		var formName = "";
		var iterations = 0;
		var iterationMax = 20;
			
		// Reset objComponentVisbility first
		resetVisibility();
			
		// Process visibility of components
		while ( !finished && iterations < iterationMax ) {
			iterations++;
			finished = true;
			// Loop through objComponentVisbility and process visibility
			for (var cc in objComponentVisbility) {
				currentComponent = objComponentVisbility[cc];
				
				if (currentComponent['visible'] == null) {
					visible = false;
					processVisible = false;
					
					// if there are no parents, process visibility
					if ( currentComponent.parents == null ) {
						processVisible = true;
						visible = true;
					}
					else { // if all parents visibility are known, process visibility
						processVisible = true;
						for (var cp in currentComponent['parents']) {
							currentParent = objComponentVisbility[currentComponent['parents'][cp]];
							
							if (currentParent.visible == null) {
								processVisible = false;
								break;
							}
							else {
								// if any answer option is visible and selected, this component is also visible
								visible |= ( currentParent['visible'] && (currentParent['answer'] == cp) );
							}
						}
					}
					
					// Process visibility
					if ( processVisible ) {
						currentComponent['visible'] = visible;
						// Get answer for the component
						formName = getFormName( cc );
						if (formName != "") {
							if(currentComponent['answer'] == null) {
								currentComponent['answer'] = getCurrentValue( cc, formName );
							}
						}
						// show or hid component
						if (currentComponent['visible']) {
							if(currentComponent['currentVisible'] == null || currentComponent['currentVisible'] == false) {
								show([ cc ]);
								currentComponent['currentVisible'] = true;
							}
						}
						else {
							if(currentComponent['currentVisible'] == null || currentComponent['currentVisible'] == true) {
								hide([ cc ]);
								currentComponent['currentVisible'] = false;
							}
						}
						finished = false;
					}
				}
			}
		}
	} 
	
	function addIfNotExists( str, key, elem ) {
		if ( typeof(str[ key ]) == 'undefined' || str[key] == null ) {
			str[key] = elem;
		}
	}	
			
	function getObjByNameOrId( tag ){
		var elem = document.getElementById(tag);
		
		if (elem == null) {
			elem = document.getElementsByName(tag);
			if (typeof(elem.length) != 'undefined' && elem.length > 0) {
				elem = elem[0];
			}
		} 
		return elem;
	}
	
	function getFormName( tag ) {
		var elem = getObjByNameOrId(tag);
		if (elem != null && typeof(elem.form) != 'undefined' ) {
			return elem.form.name;
		}
		return "";
	}
	
	function changeDiv(obj){
		// set the current value for the obj
		objComponentVisbility[obj.name]['answer'] = null;				
		processVisibility();
	}
	
	
	
	//check whether a question is dynamic	
	function isDynamicQuestion(question) {
		for (var i in DB) {
			if (question == i) {
				return true;
			}
		}
		return false;
	}
	
	//get the current select form value specified by the element name
	function getCurrentValue(elemName,formName) {
		// Modified loop to work with IE:
		// http://www.claws-and-paws.com/node/1086
		// for (var e in document.forms[formName].elements) {
		for (var e=0; e < document.forms[formName].elements.length; e++) {
			var elem = document.forms[formName].elements[e];
			if (elem.name == elemName || elem.id == elemName) {
				if ( elem.type == 'text' || elem.type == 'select-one' || elem.type == 'hidden') {
					return elem.value;
				} else if (elem.type == 'radio') {
					if( typeof(elem.length) == 'undefined') {//single radio button!
						if (elem.checked == true) {
							return elem.value;
						}
					} else {
						for (var i =0; i < elem.length; i++) {
							if( elem[i].checked) {
								return elem[i].value;
							}
						}
					}
				} else if (elem.type == 'checkbox') {
					if (elem.checked == true) {
						return elem.value;
					}
				} else {//radio button group
					if (typeof (elem.length) != 'undefined') {
						for (var i =0; i < elem.length; i++) {
							if( elem[i].checked) {
								return elem[i].value;
							}
						}
					}
				}
			}
		}
		return '';
	}
	
	function getStyleObject(objectId) {
		if (document.getElementById && document.getElementById(objectId)) {
			return document.getElementById(objectId).style;
		} else if (document.all && document.all(objectId)) {
				return document.all(objectId).style;
		} else {
			return false;
		}
	}

	
	//function to hide div blocks
	function hide(items) {
		var theStyle;
		var theDiv = "";
		if (items) {
			for (var i = 0; i < items.length; i++) {
				theDiv = "block_" + items[i];
				theStyle = getStyleObject(theDiv);
				if (theStyle != false) {
					theStyle.display = "none";
				}
		    }
		}
	}
	
	//function to show div blocks
	function show(items) {
		var theStyle;
		var theDiv = "";
		if (items) {
			for (var i = 0; i < items.length; i++) {
				theDiv = "block_" + items[i];
				theStyle = getStyleObject(theDiv);
				if (theStyle != false) {
					theStyle.display = '';
				}
			}
		}
	}
	
	function showHide(the_div)
	{
	  var the_style = getStyleObject(the_div);
	  if (the_style != false)
	  {
	  	if (the_style.display == 'none') {
	    	the_style.display = '';
	  	} else {
	  		the_style.display = 'none';
	  	}
	  }
	}

// -->
