//Form Engine Javascript library
function fe_process() {
	var root = new Array();
	//Grab list of root blocks for this form
	for(var i=0; i < control.length; i++) {
		if (fe_get_parent(control[i][0]) == "") {
			root[root.length] = control[i][0];
		}
	}
	//Process register to ensure root blocks are added
	var creg = root;
	//Add items not in root to creg
	for (var j=0; j < register.length; j++) {
		var insert = true;
		for (var k=0; k < root.length; k++) {
		   if (root[k] == register[j]) {
		   	  insert = false;
		   }
		}		
		if (insert == true) {
			creg[creg.length] = register[j];
		}
	}
	//Return the list
	document.getElementById('ctrl').value = creg.toString();	
	return true;
}

function fe_change(obj) {
	//Search through for a script that is effected by this question
	var lscript = fe_findscript(obj.id);
	//Test result
	if (lscript != "") {
		fe_process_question(obj, obj.id, lscript);
		fe_refresh();
	}
}
//Specifically for fields without an id e.g. checkboxes and radio buttons
function fe_change_option(obj, id) {
	//Search through for a script that is effected by this question
	var lscript = fe_findscript(id);
	//Test result
	if (lscript != "") {
		fe_process_question(obj, id, lscript);
		fe_refresh();
	}
}
//Process the question
function fe_process_question(obj, id, lscript) {
	//Compare answer in hide list
	var hide = false;
	for (var v=0; v < lscript[2].length; v++) {
		if (lscript[2][v] == obj.value) {
			hide = true;
			break;
		}
	}
	//Evaluate the answer
	if (hide == false) {
		//Change the control tree - set the children in script to "1"
		fe_set_control(lscript[3], 1);
	} else {
		//Change the control tree - set the children in script to "0"
		fe_set_control(lscript[3], 0);
	}
}
//Finds a script relating to the question
function fe_findscript(id) {
	var qscript = "";
	for (var i=0; i < scripts.length; i++) {
		if (scripts[i][1] == id) {
			qscript = scripts[i];
		}
	}
	return qscript;
}
//Set the blocks requested to the status
function fe_set_control(groups, status) {
	if (groups != "") {
		//Iterate through groups
		for (var x=0; x < groups.length; x++) {
			//Iterate through control data
			for (var y=0; y < control.length; y++) {
				if (control[y][0] == groups[x]) {
					control[y][1] = status;
					break;
				}
			}
		}
	}
}
//Refresh the view status of the form
function fe_refresh() {
	register = new Array();
	for (var i=0; i < control.length; i++) {
		var children = new Array();
		//Get children for the current block
		for (var j=0; j < control[i][2].length; j++) {
			var ch = fe_children(control[i][2][j]);
			if (ch.length != 0) {
				children = children.concat(ch);
			}
		}
		//Set the parent's visibility status
		if (control[i][1]==0) {
			//If parent is hidden, turn off the children
			for (var k=0; k < children.length; k=k+3) {
				fe_vis(children[k], 0); //Hide them
			}
		} else {
			//If a block above the child is hidden, then it must be hidden too,
			//Else the block must default
			for (var k=0; k < children.length; k=k+3) {
				//Get branch hidden status
				if (fe_isBranchHidden(children[k]) == true) {
					//Hide tehe block as a parent is hidden
					fe_vis(children[k], 0);
				} else {
					//Return blocks previous state
					fe_vis(children[k], children[k+1]);
				}
			}
		}
	}
}
//Find out a branch's status
function fe_isBranchHidden(block_id) {
	var cond = true;
	var val = block_id;
    while (cond == true) {
    	var parent = fe_get_parent(val);
    	if (parent != "") {
    	   if (parent[1] == 0) {
    	   	   //Parent in branch is hidden!
    	   	   return true;
    	   }
    	} else {
    		//Reached the top of the tree!
    		return false;
    	}
    	//Find the next parent
    	val = parent[0];
    }
}

//Returns the parent control object
function fe_get_parent(block_id) {
	for (var i=0; i < control.length; i++) {
		if (control[i][2].length != 0) {
			//Children present
			for (var j=0; j < control[i][2].length; j++) {
				if (control[i][2][j] == block_id) 
				   return control[i];
			}
		}
	}
	return "";
}
//this function needs to start an iterative process
function fe_children(parent_id) {
	var children = new Array();
	var z = fe_get_children(parent_id);
	children = children.concat(z);
	for (var j=0; j < z.length; j++) {
		children = children.concat(fe_children(z[j][0]));
	}
	return children;
}
function fe_get_children(parent_id) {
	for (var i=0; i < control.length; i++) {
		if (control[i][0] == parent_id)  {
			return control[i];
		}
	}
	return new Array();
}
//Block flip
function fe_vis(block_id, status) {
	
	var ot = document.getElementById('block' + block_id);
	var hi = "";
    //Is it a header css?
    if (ot.className == "f_hidden_header" || (ot.className == "f_show_header")) {
    	hi = "_header";
    }	
    var css = "f_show" + hi;
	if (status == 0) { 
		 css = 'f_hidden' + hi;
	} else {
		//Add visible item to the register
		register[register.length] = block_id;
	}
	document.getElementById('block' + block_id).className=css;
}