function _TOC_changeOrder(direction, obj, itemToLookFor) {
	var objLength = obj.children.length;
	for(var i=0; i<objLength; i++) {
		var el = obj.children[i];
		if(el.id == itemToLookFor.id) {
			obj.removeChild(el);
			if(direction == 'up') {
				if(i==0) { //top most element, move it to the bottom
					obj.appendChild(el);
				} else {
					obj.insertBefore(el, obj.children[i-1]);
				}
			} else if(direction == 'down') {
				if(i==objLength-1) { //bottom most element, move it to the top
					obj.insertBefore(el, obj.children[0]);
				} else {
					obj.insertBefore(el, obj.children[i+1]);
				}
			}
			break;
		}
	}
}

function _TOC_updateArticleOrderList(obj, prefix) {
	var propertyId= obj.id.substring(obj.id.lastIndexOf('_')+1);
	var param = document.getElementById('_TOC_articleIds_'+propertyId);
	if(!prefix) prefix='';
	var objLength = obj.children.length;
	var articleIdArray = [];
	for(var i=0; i<objLength; i++) {
		var elId = obj.children[i].id;
		var articleId = elId.substring("_TOC_article_".length);
		articleIdArray.push(articleId);
	}
	var articleIds = articleIdArray.join();
	param.value=prefix+articleIds;
}

function _TOC_makeVisible(element) {
	element.style.display = '';
	//$(element).show('slow');
}

function _TOC_makeInvisible(element) {
	element.style.display = 'none';
	//$(element).hide('slow');
}

function _TOC_disable(element) {
	element.disabled=true;
	element.style.color='lightgrey';
}

function _TOC_enable(element) {
	element.disabled=false;
	element.style.color='';
}

function _TOC_makeInputBlank(element) {
	element.value='';
}

var sectionIdCounter = 0;

function _TOC_addToSectionList(list, name, description) {
	if(name == '') return;
	var liElement = document.createElement('li');
	liElement.id = '_TOC_tmp_sectionId_'+sectionIdCounter;
	if(name != '') {
		liElement.innerHTML = '<div class="_TOC_section_name" id="_TOC_tmp_sectionId_name_'+sectionIdCounter+'">'+name+'</div>';
		if(description != '') {
			liElement.innerHTML += '<div class="_TOC_section_description" id="_TOC_tmp_sectionId_description_'+sectionIdCounter+'">'+description+'</div>';
		}
		
		liElement.innerHTML += '<div id="_TOC_tmp_sectionId_newname_'+sectionIdCounter+'" style="display:none" >'+
					'Name:<input type="text" id="_TOC_tmp_sectionId_newname_input_'+sectionIdCounter+'" value="'+name+'" /><br />'+
					'Description:<input type="text" id="_TOC_tmp_sectionId_newdescription_input_'+sectionIdCounter+'" value="'+description+'"  />'+
					'<input type="button" value="Rename" '+
					'onClick="document.getElementById(\'_TOC_tmp_sectionId_description_'+sectionIdCounter+'\').innerHTML=document.getElementById(\'_TOC_tmp_sectionId_newdescription_input_'+sectionIdCounter+'\').value;'+
					'document.getElementById(\'_TOC_tmp_sectionId_name_'+sectionIdCounter+'\').innerHTML=document.getElementById(\'_TOC_tmp_sectionId_newname_input_'+sectionIdCounter+'\').value;'+
					'_TOC_enable(document.getElementById(\'_TOC_tmp_sectionId_rename_link_'+sectionIdCounter+'\'));'+
					'_TOC_makeInvisible(document.getElementById(\'_TOC_tmp_sectionId_newname_'+sectionIdCounter+'\'));'+
					'_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_name_'+sectionIdCounter+'\'));'+
					'_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_description_'+sectionIdCounter+'\'));'+
					'_TOC_setInput(document.getElementById(\'_TOC_fm_save_toc\'),\'_TOC_propertyName_'+sectionIdCounter+'\',\'_TOC_propertyName_'+sectionIdCounter+'\',document.getElementById(\'_TOC_tmp_sectionId_newname_input_'+sectionIdCounter+'\').value,\'hidden\');'+
					'_TOC_setInput(document.getElementById(\'_TOC_fm_save_toc\'),\'_TOC_propertyDescription_'+sectionIdCounter+'\',\'_TOC_propertyDescription_'+sectionIdCounter+'\',document.getElementById(\'_TOC_tmp_sectionId_newdescription_input_'+sectionIdCounter+'\').value,\'hidden\');" /> | '+
					'<input type="button" value="Cancel" '+
					'onClick="_TOC_enable(document.getElementById(\'_TOC_tmp_sectionId_rename_link_'+sectionIdCounter+'\'));'+
					'_TOC_makeInvisible(document.getElementById(\'_TOC_tmp_sectionId_newname_'+sectionIdCounter+'\'));'+
					'_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_name_'+sectionIdCounter+'\'));'+
					'_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_description_'+sectionIdCounter+'\'));" /></div>';
		
		var tmpSectionNotToBeAddedId = liElement.id.replace('tmp_sectionId','tmpSectionNotToBeAdded');
		liElement.innerHTML += '<div id="_TOC_tmp_sectionId_'+sectionIdCounter+'_changeOrderButtons">'+
					'<a onClick="_TOC_changeOrder(\'up\',document.getElementById(\''+list.id+'\'), document.getElementById(\''+liElement.id+'\'));" style="cursor:pointer" ><img alt="Move section up" border="0" height="16" hspace="0" src="http://imaging.cmpmedica.com/shared/arrows/02_up.png" class="_TOC_orderButtonImage" vspace="2" width="16" /></a>'+
					'<a onClick="_TOC_changeOrder(\'down\',document.getElementById(\''+list.id+'\'), document.getElementById(\''+liElement.id+'\'));" style="cursor:pointer" ><img alt="Move section up" border="0" height="16" hspace="0" src="http://imaging.cmpmedica.com/shared/arrows/02_down.png" class="_TOC_orderButtonImage" vspace="2" width="16" /></a>'+
					'<a onClick="_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_newname_'+sectionIdCounter+'\'));'+
					'_TOC_makeInvisible(document.getElementById(\'_TOC_tmp_sectionId_name_'+sectionIdCounter+'\'));'+
					'_TOC_makeInvisible(document.getElementById(\'_TOC_tmp_sectionId_description_'+sectionIdCounter+'\'));'+
					'_TOC_disable(document.getElementById(\'_TOC_tmp_sectionId_rename_link_'+sectionIdCounter+'\'));"'+
					'style="cursor:pointer" id="_TOC_tmp_sectionId_rename_link_'+sectionIdCounter+'" >Rename</a> | '+
					'<a onClick="_TOC_markToBeRemoved(document.getElementById(\''+liElement.id+'\'));document.getElementById(\''+liElement.id+'\').id=\''+tmpSectionNotToBeAddedId+'\';_TOC_makeInvisible(document.getElementById(\'_TOC_tmp_sectionId_removeProperty_link_'+sectionIdCounter+'\'));_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_reinstateProperty_link_'+sectionIdCounter+'\'));" style="cursor:pointer" id="_TOC_tmp_sectionId_removeProperty_link_'+sectionIdCounter+'" >Remove Section</a>'+
					'<a onClick="_TOC_unMarkToBeRemoved(document.getElementById(\''+tmpSectionNotToBeAddedId +'\'));document.getElementById(\''+tmpSectionNotToBeAddedId +'\').id=\''+liElement.id+'\';_TOC_makeVisible(document.getElementById(\'_TOC_tmp_sectionId_removeProperty_link_'+sectionIdCounter+'\'));_TOC_makeInvisible(document.getElementById(\'_TOC_tmp_sectionId_reinstateProperty_link_'+sectionIdCounter+'\'));" style="cursor:pointer;display:none;" id="_TOC_tmp_sectionId_reinstateProperty_link_'+sectionIdCounter+'" >Reinstate Section</a></div>';
		liElement.innerHTML += '<ul id="'+'_TOC_articleIdsList_tmpsectionId'+sectionIdCounter+'" class="standard-disc-ul"></ul>';
	}
	//liElement.innerHTML += '<br />';
	list.insertBefore(liElement,list.firstChild);
	sectionIdCounter++;
}

function _TOC_updateSectionOrder(param, obj) {
	var objLength = obj.children.length;
	var sections = [];
	for(var i=0; i<objLength; i++) {
		var elId = obj.children[i].id;
		var sectionData = '';
		if(elId.indexOf('_TOC_tmp_sectionId_')>=0) {
			var tmpSectionName = '';
			var tmpSectionDescription = '';
			var tmpSectionChildrenLength = obj.children[i].children.length;
			for(var j=0; j<tmpSectionChildrenLength; j++) {
				var tmpSectionChildId = obj.children[i].children[j].id;
				if(tmpSectionChildId.indexOf('_TOC_tmp_sectionId_name_')>=0) {
					tmpSectionName = obj.children[i].children[j].innerHTML;
				} else if(tmpSectionChildId.indexOf('_TOC_tmp_sectionId_description_')>=0) {
					tmpSectionDescription = obj.children[i].children[j].innerHTML;
				}
			}
			var tmpSectionId = elId.substring('_TOC_'.length).replace(/_/g,'');
			if(tmpSectionId != '') {
				sectionData = tmpSectionId;
			}
			if(tmpSectionId != '' && tmpSectionName != '') {
				sectionData += ':::'+tmpSectionName;
			}
			if(tmpSectionId != '' && tmpSectionName != '' && tmpSectionDescription != '') {
				sectionData += ':::'+tmpSectionDescription;
			}
		} else if(elId.indexOf('_TOC_propertyId_')>=0) {
			var indexOfPropertyId = elId.indexOf('_TOC_propertyId_')+('_TOC_propertyId_'.length);
			sectionData = elId.substring(indexOfPropertyId);
		}
		if(sectionData != '') {
			sections.push(sectionData);
		}
	}
	if(sections.length > 0) {
		param.value = sections.join('|||');
		return true;
	} else {
		return false;
	}
}

function _TOC_setInput(formElement,inputId,name,value,type) {
	var inputEl = document.getElementById(inputId);
	if(inputEl == null) {
		inputEl = document.createElement('input');
		inputEl.type = type;
		inputEl.name = name;
		inputEl.value = value;
		inputEl.id = inputId;
		formElement.insertBefore(inputEl,formElement.children[formElement.length-1]);
	} else {
		inputEl.type = type;
		inputEl.name = name;
		inputEl.value = value;
	}
}

function _TOC_unMarkToBeRemoved(element) {
	element.style.textDecoration='';
	element.style.color='';
}

function _TOC_markToBeRemoved(element) {
	element.style.textDecoration='line-through';
	element.style.color='gray';
}

function _TOC_appendToInput(formElement, inputId, name, value, type, delimiter) {
	var inputEl = document.getElementById(inputId);
	if(inputEl == null) {
		inputEl = document.createElement('input');
		inputEl.type = type;
		inputEl.name = name;
		inputEl.value = value;
		inputEl.id = inputId;
		formElement.insertBefore(inputEl,formElement.children[formElement.length-1]);
	} else {
		if(inputEl.value == '') {
			inputEl.value = value;
		} else if(inputEl.value.indexOf(delimiter+value)<0) {
			inputEl.value = inputEl.value+delimiter+value;
		}
	}
}

function _TOC_removeFromInput(formElement, inputId, value, delimiter) {
	var inputEl = document.getElementById(inputId);
	if(inputEl != null) {
		var newValue = '';
		var oldValue = inputEl.value;
		if(oldValue.indexOf(delimiter)>=0) {
			var valueIndex = oldValue.indexOf(value);
			if(valueIndex>0) {
				valueIndex = oldValue.indexOf(delimiter+value);
				newValue = oldValue.substring(0,valueIndex)+oldValue.substring(valueIndex+(delimiter+value).length);
			} else if(valueIndex == 0) {
				newValue = oldValue.substring(value.length+delimiter.length);
			}
		} else if(oldValue == value) {
			inputEl.value = '';
		}
		if(newValue != '') {
			inputEl.value = newValue;
		}
	}
}

function _TOC_moveToList(direction, parentList, childList, item) {
	var parentListLength = parentList.children.length;

	var childFound=false;

	for(var i=0; i<parentListLength; i++) {
		for(var j=0; j<parentList.children[i].children.length; j++) {
			var el = parentList.children[i].children[j];
			if(el.id == childList.id) {
				if(direction == 'up') {
					if(i==0) { //top most element, move it to the bottom
						parentList.children[parentListLength-1].children[parentList.children[parentListLength-1].children.length-1].appendChild(item);
					} else {
						parentList.children[i-1].children[parentList.children[i-1].children.length-1].appendChild(item);
					}
				} else if(direction == 'down') {
					if(i==parentListLength-1) { //bottom most element, move it to the top
						parentList.children[0].children[parentList.children[0].children.length-1].appendChild(item);
					} else {
						parentList.children[i+1].children[parentList.children[i+1].children.length-1].appendChild(item);
					}
				}
				childFound=true;
				break;
			}
		}
		if(childFound) {
			break;
		}
	}
	//childList.removeChild(item);
}