//#######################################################################################################
//###   CREATED WITHIN THE DIPLOMA THESIS <Default Inheritance for OWL-S, 2007> by Simon Ferndriger   ###
//#######################################################################################################


/* *****************************************************************/
/* GRAFICAL FUNCTIONS **********************************************/


/*
	0. INIT
	1. HTML
		1.1 [VIS]
			1.1.2 CREATE ProcessTree
				1.2.1.1 Recursive Construct [RC] Components
		1.2 [CNS]
			1.2.1 CREATE Process DropDown
				1.2.1.1 RC Process DropDown (Helper)
		1.3 [VAS]
			1.3.1 ADD Result Line
		1.4 [DIS]
		
		1.5 Elements
			1.5.1 CREATE DropDownMenu
				1.5.1.1 FILLUP DropDown Menu
			1.5.2 Get Standard HTML ID
			1.5.3 GET Namespace
			1.5.4 APPEND InputField

		1.6 Forms
		
		1.7 Navigation & Info
			1.7.1 SET Main Navigation
		
		1.8 [All]
			1.8.1 CONSTRUCT Component
	2. CSS
		2.1 Applications
			2.1.1 RESET Logfile
			2.1.2 FINALIZE [CNS only]
			2.1.3 INIT
			2.1.4 CLEAR
			2.1.5 LINK SELECTION
		2.2 Elements
			2.2.1 Visibility Functions
			2.2.2 Appearance Functions
*/


////////////////////////////////////////////////////////////////////////////////////////
// 0. INIT
////////////////////////////////////////////////////////////////////////////////////////
function init(what) {
	SET_MAIN_NAV(what);
}

////////////////////////////////////////////////////////////////////////////////////////
// 1. HTML
////////////////////////////////////////////////////////////////////////////////////////
/* 1.1 HTML Applications..............................................................*/


/* ######################################################################### */
// [VIS] 1.1.1 CREATE ProcessTree [VIS]
/* ######################################################################### */
function CREATE_VIS_ProcessTree(ServiceModel_URI) {
	
	var URI = ServiceModel_URI;
	var treeID = GET_HTML_ID('output', 'VIS', 'ProcessTree');
	var blockID = GET_HTML_ID('block', 'VIS', 'ProcessTree');
	
	// 1. Reset.....................................
	$(treeID).innerHTML = '';

	// 2. Fillup....................................
	var main_ul = CREATE_HTML_node(treeID, 'ul');
	var main_li = CREATE_HTML_node(main_ul, 'li');
	var entry = CREATE_HTML_node(main_li, 'abbr');
	
	CREATE_HTML_attribute(entry, 'title', URI);
	CREATE_HTML_textnode(entry, GET_namespace(URI) + getIDfromURI(URI));
	
	/* Recursive fillup */
	if(ProcessType(URI) == 'CompositeProcess')
		RC_VIS_ProcessTree(CONSTRUCT_Component(URI), main_ul);
	
	// 3. Show......................................
	STYLE_show(blockID);
}

/* ######################################################################### */
// [VIS] 1.1.1.1 RECURSIVE CREATE ProcessTree
/* ######################################################################### */
function RC_VIS_ProcessTree(Component, ul_node) {
	
	// Get pointers
	var URI = Component['Perform_URI'] || Component['Process_URI'];
	var Components = _ProcessTree[URI];

	// Child nodes
	if(Components) {
		var ul = CREATE_HTML_node(ul_node, 'ul');
		for(var i=0; Components[i]; i++) {
			
			// Create Entry Node
			var li = CREATE_HTML_node(ul, 'li');
			var entry = CREATE_HTML_node(li, 'abbr');
			CREATE_HTML_attribute(entry, 'title', Components[i]['Process_URI']);
			
			// Add Type Info (if available); e.g. If-Then-Else, etc
			if(Components[i]['type']) {
				var type = CREATE_HTML_node(entry, 'small');
				CREATE_HTML_textnode(type, Components[i]['type']);
			}
			
			// Insert Process
			CREATE_HTML_textnode(entry, GET_namespace(Components[i]['Process_URI']) + getIDfromURI(Components[i]['Process_URI']));

			// Add Name Info (if available); e.g. Sequence, Choice, etc
			if(Components[i]['name']) {
				var type = CREATE_HTML_node(entry, 'small');
				CREATE_HTML_textnode(type, Components[i]['name']);
			}
			
			// Recursive Cild nodes
			RC_VIS_ProcessTree(Components[i], ul);
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////
/* 1.2 CNS ...........................................................................*/


/* ######################################################################### */
// [CNS] 1.2.1 CREATE Process DropDown
/* ######################################################################### */
function CREATE_CNS_MIR_Process_DropDown(Component, selectName, _getLocalsOnly, _usePerforms, _getAtomicProcessesOnly) {
	
	// Variables
	var selectID =  GET_HTML_ID('select', 'CNS', 'ModelingIR', selectName);
	
	// Clear DropDown
	$(selectID).innerHTML = '';
	
	// ID Array
	var ComponentArray = new Array(Component);
	
	// Recursive fillup.............................................
	if(ProcessType(Component['Process_URI']) == 'CompositeProcess')
		ComponentArray = GET_CNS_RC_Components(Component, ComponentArray);
	
	/* Empty HTML element */
	$(selectID).innerHTML = '';
	
	
	/* Fillup */
	var local_NS = getNSfromURI(Component['Process_URI']);
	for(var i=0; ComponentArray[i]; i++) {
		
		var cur_processURI = ComponentArray[i]['Process_URI'];
		var cur_processPFX =  GET_namespace(cur_processURI);
		var cur_processID = getIDfromURI(cur_processURI);
		
		var cur_performURI = ComponentArray[i]['Perform_URI'];
		var cur_performPFX =  GET_namespace(cur_performURI);
		var cur_performID = getIDfromURI(cur_performURI);
		
		// Set relevant URI fro local comparison
		var relevantURI = cur_processURI;
		if(_usePerforms)
			relevantURI = cur_performURI;
		
		// Fillup dropdown
		if((_getLocalsOnly && getNSfromURI(relevantURI) == local_NS) || !_getLocalsOnly) {
			if(_usePerforms) {
					if(cur_performURI)
						FILLUP_dropdown(selectID, cur_performPFX + cur_performID, cur_performURI);
			}
			else {
				var atomicCondition = (_getAtomicProcessesOnly && ProcessType(cur_processURI) == 'AtomicProcess');
				
				if(!_getAtomicProcessesOnly || atomicCondition)
					FILLUP_dropdown(selectID, cur_processPFX + cur_processID, cur_processURI);
			}
		}
	}
}

/* ######################################################################### */
// [CNS] 1.2.1.1 Recursive Construct [RC] Components
/* ######################################################################### */
function GET_CNS_RC_Components(Component, ComponentArray) {
	
	var URI = Component['Perform_URI'] || Component['Process_URI'];
	
	// Get Components
	var NewComponents = _ProcessTree[URI];

	// Process components found
	if(NewComponents)
		for(var i=0; NewComponents[i]; i++) {
			
			// Add current id to the array
			if(ComponentArray.indexOf(NewComponents[i]) == -1)
				ComponentArray.push(NewComponents[i]);
	
			// Recursive Cild nodes
			ComponentArray = GET_CNS_RC_Components(NewComponents[i], ComponentArray);
		}
	
	// Return the same or more process names
	return ComponentArray;
}

/* ######################################################################### */
// [CNS] 1.2.2 CREATE Expression DropDown
/* ######################################################################### */
function CREATE_CNS_MIR_Expression_DropDown(selectName, NS, ID) {
	
	// Variables
	var selectID =  GET_HTML_ID('select', 'CNS', 'ModelingIR', selectName);
	var selectedServiceModelID = GET_HTML_ID('select', 'CNS', 'ModelingIR', 'adoptServiceModel');
	var selectedServiceModel_URI = getSelectValue(selectedServiceModelID);
	
	// Check
	if(!validURI(selectedServiceModel_URI))
		return;
	
	// Clear DropDown
	$(selectID).innerHTML = '';
	
	// Set DOM from Service Model
	var url = getURLfromURI(selectedServiceModel_URI);
	
	var DOM = getDOM(url);
	
	// Check
	if(!DOM)
		return;
		
	// Get Elements
	var elements = OWL_getElementsByNodeType(DOM, NS, ID);
	var identities = new Array();
	
	// Get Identities
	for(var i=0; elements[i]; i++) {
		
		var curURI = OWL_getNodeIdentityURI(elements[i]);
		
		if(curURI)
			if(identities.indexOf(curURI) == -1)
				identities.push(curURI);
	}
	
	// Fillup Identities
	var HTML_optgroup = CREATE_optionGroup(selectID, ID);
	for(var i=0; identities[i]; i++) {
		
		var curURI = identities[i];
		var curPrefix =  GET_namespace(curURI);
		var curID = getIDfromURI(curURI);
		
		FILLUP_dropdown(HTML_optgroup, curPrefix + curID, curURI);
	}
}

/* ######################################################################### */
// [CNS] 1.2.2 CREATE IO DropDown
/* ######################################################################### */
function CREATE_CNS_MIR_IO_DropDown(selectName, AtomicProcess_URI) {
	
	// Variables
	var selectID =  GET_HTML_ID('select', 'CNS', 'ModelingIR', selectName);
	
	// Check
	if(!validURI(AtomicProcess_URI))
		return;
	
	// Clear DropDown
	$(selectID).innerHTML = '';
	
	// Get Elements
	var atomic_elements = OWL_getDOMsByURI(_process, 'AtomicProcess', AtomicProcess_URI);

	// Get Inputs/Outputs...................................................
	var AllInputs = new Array();
	var AllOutputs = new Array();
	
	for(var i=0; atomic_elements[i]; i++) {
		
		var cur_inputs = OWL_getElementsByNodeType(atomic_elements[i], _process, 'Input');
		var cur_outputs = OWL_getElementsByNodeType(atomic_elements[i], _process, 'Output');
		
		AllInputs = merge_arrays(AllInputs, cur_inputs);
		AllOutputs = merge_arrays(AllOutputs, cur_outputs);
	}
	
	// Get Identities.......................................................
	var inputURIs = new Array();
	var outputURIs = new Array();
	
	for(var i=0; AllInputs[i]; i++) {
		
		var curURI = OWL_getNodeIdentityURI(AllInputs[i]);
		if(curURI)
			if(inputURIs.indexOf(curURI) == -1)
				inputURIs.push(curURI);
	}
	for(var i=0; AllOutputs[i]; i++) {
		
		var curURI = OWL_getNodeIdentityURI(AllOutputs[i]);
		if(curURI)
			if(outputURIs.indexOf(curURI) == -1)
				outputURIs.push(curURI);
	}
	
	// Fillup Identities.....................................................
	var HTML_optgroup = CREATE_optionGroup(selectID, 'Inputs');
	for(var i=0; inputURIs[i]; i++) {
		
		var curURI = inputURIs[i];
		var curPrefix =  GET_namespace(curURI);
		var curID = getIDfromURI(curURI);
		
		FILLUP_dropdown(HTML_optgroup, curPrefix + curID, curURI);
	}
	
	var HTML_optgroup = CREATE_optionGroup(selectID, 'Outputs');
	for(var i=0; outputURIs[i]; i++) {
		
		var curURI = outputURIs[i];
		var curPrefix =  GET_namespace(curURI);
		var curID = getIDfromURI(curURI);
		
		FILLUP_dropdown(HTML_optgroup, curPrefix + curID, curURI);
	}
}


////////////////////////////////////////////////////////////////////////////////////////
/* 1.3 VAS ...........................................................................*/

/* ######################################################################### */
// [VAS] 1.3.1 ADD Result Line
/* ######################################################################### */
function ADD_ResultLine(TASK, texts, OK, _start, _detailsNr, _validateSubService) {
	
	// Variables.......................................
	var outputID =  GET_HTML_ID('output', TASK, 'Result');
	if(OK)
		var text = texts[0];
	else
		var text = texts[1];
	
	// Clear DropDown..................................
	if(_start)
		$(outputID).innerHTML = '';
	
	// Create Line.....................................
	var line = CREATE_HTML_node(outputID, 'p');
	
	// Set Marking.....................................
	if(OK)
		STYLE_set_class(line, 'OK');
	else
		STYLE_set_class(line, 'NOK');
	
	// Insert Text.....................................
	CREATE_HTML_textnode(line, text);
	
	
	// Insert Details (optional).......................
	if(_detailsNr) {
		
		var detailsID = GET_HTML_ID('output', TASK, 'ResultDetail', _detailsNr);
		
		/* Create Details Link */
		var a = CREATE_HTML_node(line, 'a');		
		CREATE_HTML_textnode(a, 'Details');
		CREATE_HTML_attribute(a, 'href', 'javascript:STYLE_switch("' + detailsID + '")');
	}
	
	// Insert validate link (optional).................
	if(_validateSubService) {
		
		var a = CREATE_HTML_node(line, 'a');		
		CREATE_HTML_textnode(a, 'Validate');
		CREATE_HTML_attribute(a, 'href', 'javascript:STYLE_set_cursor("busy"); try {if(VAS_validateSubService("' + _validateSubService + '")) alert("Substitute is valid"); else alert("Substitute is NOT valid");} catch(e) {alert(e)}  STYLE_set_cursor();');
	}
}

function ADD_ResultDetailBlock(TASK, _detailsNr) {
	
	var outputID =  GET_HTML_ID('output', TASK, 'Result');
	var detailsID = GET_HTML_ID('output', TASK, 'ResultDetail', _detailsNr);
	
	var div = CREATE_HTML_node(outputID, 'ul');
	STYLE_hide(div);
	
	CREATE_HTML_attribute(div, 'id', detailsID);
}

function ADD_ResultDetailLine(TASK, text, _detailsNr) {
																							   
	var detailsID = GET_HTML_ID('output', TASK, 'ResultDetail', _detailsNr);
	
	var li = CREATE_HTML_node(detailsID, 'li');
	CREATE_HTML_textnode(li, text);
}

////////////////////////////////////////////////////////////////////////////////////////
/* 1.5 HTML Elements..................................................................*/

function CREATE_HTML(tag, name, text) {
	$(tag).appendChild($e(name)).appendChild($t(text));
}
function CREATE_HTML_node(tag, name) {
	return $(tag).appendChild($e(name));
}
function CREATE_HTML_textnode(tag, text) {
	$(tag).appendChild($t(text));
}
function CREATE_HTML_attribute(tag, name, value) {
	$(tag).setAttribute(name, value);
}

/* ######################################################################### */
// [HTML Element] 1.5.1 CREATE DropDownMenu
/* ######################################################################### */
function CREATE_List(TASK, SECTION, NAME, texts, values) {
	
	/* Error handling~~~~~~~~~~*/
	if(texts.length != values.length) {
		error_log('Unequal number of entries for text and values for select field.');
		return;
	}
	/* ~~~~~~~~~~~~~~~~~~~~~~~~*/
	
	var selectID = GET_HTML_ID('select', TASK, SECTION, NAME); 
	var blockID = GET_HTML_ID('block', TASK, SECTION); 
	// 1. Reset.....................................
	$(selectID).innerHTML = '';
	
	// 2. Fillup....................................
	for(var i=0; values[i]; i++)
		FILLUP_dropdown(selectID, texts[i], values[i]);

	// 3. Show......................................
	STYLE_show(blockID);
}

/* ######################################################################### */
// [HTML Element] 1.5.1.1 FILLUP DropDown Menu
/* ######################################################################### */
function FILLUP_dropdown(select_or_optgroup, text, value) {

	// Creating <option> HTML elements
	var option = CREATE_HTML_node(select_or_optgroup, 'option');
	CREATE_HTML_textnode(option, text);
	
	// Create <option value="...">
	if(value)
		CREATE_HTML_attribute(option, 'value', value);
}

function CREATE_optionGroup(select_id, groupName) {
	
	// Creating <option> HTML elements
	var optgroup = CREATE_HTML_node(select_id, 'optgroup');
	CREATE_HTML_attribute(optgroup, 'label', groupName);
	
	return optgroup;
}

/* ######################################################################### */
// [HTML Element] 1.5.2 Get Standard HTML Element ID
/* ######################################################################### */
function GET_HTML_ID(HTMLElementType, TASK, BLOCK, _o_NAME) {
	
	/* Error handling~~~~~~~~~~*/
	if(!HTMLElementType || !TASK || !BLOCK) {
		error_log('Unsufficient input for HTML ID.');
		return;
	}
	/* ~~~~~~~~~~~~~~~~~~~~~~~~*/
	
	var id = 'type[TASK][BLOCK]NAME';
	
	// Handling optional name
	var name;
	if(!_o_NAME) name = '';
	else name = '_' + _o_NAME;
	
	// Replacing template with real values
	id = id.replace('type', HTMLElementType);
	id = id.replace('TASK', TASK);
	id = id.replace('BLOCK', BLOCK);
	id = id.replace('NAME', name);
	
	return id;
}

/* ######################################################################### */
// [HTML App] 1.5.3 GET Namespace
/* ######################################################################### */
function GET_namespace(URI) {
	
	var url = getURLfromURI(URI);
	var index = _NS.indexOf(url);
	
	// No clear namespace can be found
	if(!url)
		return '[?]:'
	
	// Namespace is not yet in the index
	if(index == -1) {
		_NS.push(url);
		return GET_namespace(URI);
	}
	
	// Namespace is in the index
	return 'NS' + index + ':';
}

/* ######################################################################### */
// [HTML App] 1.5.4 APPEND InputField
/* ######################################################################### */
function APPEND_inputField(parent, name, value) {
	
	var inputField = CREATE_HTML_node(parent, 'input');
	
	// Add the name
	CREATE_HTML_attribute(inputField, 'name', name);
	// Add the value
	CREATE_HTML_attribute(inputField, 'value', value);
	// Disallow editing
	CREATE_HTML_attribute(inputField, 'disabled', 'disabled');
	// Show it in a row
	CREATE_HTML_attribute(inputField, 'class', 'disabled');
}

////////////////////////////////////////////////////////////////////////////////////////
/* 1.6 HTML Forms.....................................................................*/

function getSelectValue(selectField) {
	var s = $(selectField);
	
	return s.options[s.selectedIndex].value;
}

function getCheckboxValue(checkbox) {
	return $(checkbox).checked;
}

function getRadioValue(radioButton) {
	var r = $(radioButton);
	
	for(var i=0; r[i]; i++)
		if(r[i].checked)
			return r[i].value;
}

function initInputField(inputField) {
	var i = $(inputField);
	
	i.value = '';
	i.focus();
}




////////////////////////////////////////////////////////////////////////////////////////
/* 1.7 Main Navigation & Info.........................................................*/

/* ######################################################################### */
// [HTML Nav] 1.7.1 SET Main Navigation
/* ######################################################################### */
function SET_MAIN_NAV(nav_item_text) {
	$('following').innerHTML = nav_item_text;
	$('MAIN_CONTENT').innerHTML = $('MAIN_CONTENT[' + nav_item_text + ']').innerHTML;
	

	switch(nav_item_text) {
		
		case 'Visualize Service':
			SET_infoText('VIS', 'Init', nav_item_text);
			break;
			
		case 'Create New SubService':
			SET_infoText('CNS', 'Init', nav_item_text);
			
			var fieldFocusID = GET_HTML_ID('init', 'CNS', 'ServiceName');
			if($(fieldFocusID))
				$(fieldFocusID).focus();
				
			break;
			
		case 'Validate SubService':
			SET_infoText('VAS', 'Init', nav_item_text);
			break;
			
		case 'Discover Service Substitutes':
			SET_infoText('DIS', 'Init', nav_item_text);
			break;
			
		case 'Home':
			SET_infoText('HOME', 'Init', nav_item_text);
			break;
	}
}

function SET_infoText(TASK, BLOCK, _title, _subtitle) {
	
	var infoContentID = 'InfoWindow[Content]';
	var infoTitleID = 'InfoWindow[p]contextTitle';
	infoSubtitleID = 'InfoWindow[p]contextSubtitle';
	var infoTextID = GET_HTML_ID('InfoText', TASK, BLOCK);

	// Set content
	if($(infoTextID))
		$(infoContentID).innerHTML = $(infoTextID).innerHTML;
	else
		$(infoContentID).innerHTML = '';
		
	// Set context title (change only if main task is initiated)
	if(_title)
		$(infoTitleID).innerHTML = _title;
		
	// Set context subtitle
	if(!_subtitle) _subtitle = '';
	$(infoSubtitleID).innerHTML = _subtitle;
}

/* ######################################################################### */
// [All] 1.8.1 CONSTRUCT Component
/* ######################################################################### */
function CONSTRUCT_Component(Process_URI, Perform_URI) {
	
	var Component = new Object();
	
	if(Process_URI)
		Component['Process_URI'] = Process_URI;
	/* Error handling~~~~~~~~~~*/
	else
		error_log('No Process_URI given.')
	/* ~~~~~~~~~~~~~~~~~~~~~~~~*/		
		
	if(Perform_URI)
		Component['Perform_URI'] = Perform_URI;
	
	
	// Return the component with minimum one process URI
	return Component;
}


////////////////////////////////////////////////////////////////////////////////////////
// 2. CSS
////////////////////////////////////////////////////////////////////////////////////////
/* 2.1 CSS Applications...............................................................*/

/* ######################################################################### */
// [CSS App] 2.1.1 RESET Logfile
/* ######################################################################### */
function RESET_Logfile() {
	$('log').value = '';
	STYLE_softHide('Logfile[block]textarea');
	
	STYLE_set_class('Logfile[block]', 'block');
	STYLE_set_class('Logfile[p]', 'title');
}
function SET_ERROR_Logfile() {	
	STYLE_softShow('Logfile[block]textarea');
	
	STYLE_add_class('Logfile[block]', 'error');
	STYLE_add_class('Logfile[p]', 'error');
}


/* ######################################################################### */
// [HTML App] 2.1.2 FINALIZE [CNS only]
/* ######################################################################### */
function FINALIZE_CNS(BLOCK) {
	
	var blockID = GET_HTML_ID('block', 'CNS', BLOCK);
	var finalizedBlockID = GET_HTML_ID('finalizedBlock', 'CNS', BLOCK);
	
	STYLE_hide(blockID);
	STYLE_show(finalizedBlockID);
}

/* ######################################################################### */
// [HTML App] 2.1.3 INIT [ALL]
/* ######################################################################### */
function INIT(TASK, BLOCK, _additionalInfo) {
	
	var blockID = GET_HTML_ID('block', TASK, BLOCK);
	
	// Set infoText context sensitive (for each initiated block)
	SET_infoText(TASK, BLOCK, false, BLOCK);
	
	// Perform task and block specific actions (on initiation of the block)
	if(TASK == 'CNS')
		switch(BLOCK) {
			
			case 'ModelingIR':
				var modeID = GET_HTML_ID('finalized', 'CNS', 'SuperService', 'Mode');
				var serviceModelID = GET_HTML_ID('finalized', 'CNS', 'SuperService', 'URI');
				
				// Fix Service Model if Strict IR is used
				if($(modeID).innerHTML == 'strictOriginal' || $(modeID).innerHTML == 'strictSubstitute')
					_innerScript_CNS_FIX_SM_MIR(ServiceModel($(serviceModelID).innerHTML));
					
				// Disable ProcessReplacements if strictSubstitute is used
				if($(modeID).innerHTML == 'strictSubstitute')
					_innerScript_CNS_DISABLE_PR_MIR();
			
				// Disable IO addings and deletions if strict IR is used
				if($(modeID).innerHTML == 'strictSubstitute' || $(modeID).innerHTML == 'strictOriginal')
					_innerScript_CNS_DISABLE_IO_MIR();
					
				break;
				
			case 'SubserviceCreated':
				var newServiceURI_ID = GET_HTML_ID('finalized', 'CNS', 'ServiceName', 'name');
				var createdServiceURI_ID = GET_HTML_ID('finalized', 'CNS', 'SubserviceCreated', 'ServiceURI');
				
				$(createdServiceURI_ID).innerHTML = $(newServiceURI_ID).innerHTML;
				
				// Show also the groundigns..........................................................
				var leaveOutGroundings = _additionalInfo;
				var createdGroundingsURI_ID = GET_HTML_ID('finalized', 'CNS', 'SubserviceCreated', 'GroundingsURI');
				
				break;
		}
	
	STYLE_show(blockID);
}

/* ######################################################################### */
// [HTML App] 2.1.4 CLEAR [ALL]
/* ######################################################################### */
function CLEAR(TASK, BLOCK) {
	
	var blockID = GET_HTML_ID('block', TASK, BLOCK);
	
	STYLE_hide(blockID);
}

/* ######################################################################### */
// [HTML App] 2.1.5 LINK SELECTION
/* ######################################################################### */
function LINK_SELECT(TASK, BLOCK, selected) {

	// Get possible sections
	var SECTIONs = _INDEX[TASK][BLOCK];
	
	// Going through all possibles links
	for(var i=0; SECTIONs[i]; i++) {
	
		var linkID = GET_HTML_ID('link', TASK, BLOCK,  SECTIONs[i]);
		var linkBlockID = GET_HTML_ID('linkBlock', TASK, BLOCK,  SECTIONs[i]);

		/* selected link found */
		if(SECTIONs[i] == selected) {
			STYLE_set_class(linkID, 'selected');
			STYLE_show(linkBlockID);
			
			// Loose focus
			$(linkID).blur();
		}
		else {
			
			// Only remove class if the link is not disabled
			if($(linkID).className != 'disabled')
				STYLE_remove_class(linkID);
			
			STYLE_hide(linkBlockID);
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////
/* 2.2 CSS Elements...................................................................*/


/* ######################################################################### */
// [CSS Elements] 2.2.1 Visibility Functions
/* ######################################################################### */
function STYLE_show(id_or_object) {
	$(id_or_object).className = 'visible';
}
function STYLE_hide(id_or_object) {
	$(id_or_object).className = 'hidden';
}
function STYLE_softHide(id_or_object) {
	$(id_or_object).style.visibility = 'collapse';
}
function STYLE_softShow(id_or_object) {
	$(id_or_object).style.visibility = 'visible';
}

function STYLE_switch(id_or_object) {
	
	if($(id_or_object).className == 'hidden')
		STYLE_show(id_or_object);
	else
		STYLE_hide(id_or_object);
}
function STYLE_softSwitch(id_or_object) {
	
	var v = $(id_or_object).style.visibility;
	
	if(v == 'visible')
		STYLE_softHide(id_or_object);
	else
		STYLE_softShow(id_or_object);
}

/* ######################################################################### */
// [CSS Elements] 2.2.2 Appearance Functions
/* ######################################################################### */
function STYLE_add_class(id_or_object, name) {
	$(id_or_object).className += ' ' + name;
}
function STYLE_set_class(id_or_object, name) {
	$(id_or_object).className = name;
}
function STYLE_remove_class(id_or_object) {
	$(id_or_object).className = '';
}
function STYLE_set_cursor(_busy) {
	
	if(_busy == 'busy')
		$('busy_cursor').style.display = 'block';
	else
		$('busy_cursor').style.display = 'none';
}
function STYLE_set_all_disabled(id_or_object) {
	
	var node = $(id_or_object);
	var desableElements = new Array('input', 'select');
	
	if(!node.hasChildNodes() )
		return;
		
	var children = node.childNodes;
	for(var i=0; children[i]; i++)
		if(desableElements.indexOf(children[i].nodeName.toLowerCase()) != -1)
			children[i].disabled = 'disabled';
		else
			STYLE_set_all_disabled(children[i]);
}
function STYLE_set_all_enabled(id_or_object) {
	
	var node = $(id_or_object);
	var enableElements = new Array('input', 'select');
	
	if(!node.hasChildNodes() )
		return;
		
	var children = node.childNodes;
	for(var i=0; children[i]; i++)
		if(enableElements.indexOf(children[i].nodeName.toLowerCase()) != -1)
			children[i].disabled = '';
		else
			STYLE_set_all_enabled(children[i]);
}
