function editTableRow(tableIdName, rowIndex, tableDefinition, editWhat) {
	var table = document.getElementById(tableIdName);
	
	if(table) {
		var dr = table.rows[rowIndex];
		var inputTypes = tableDefinition[2];
		var inputTypesData = tableDefinition[3];
		var cssStyles = tableDefinition[1];
		
		if(dr && inputTypes) {			
			for(var i=0; i<dr.cells.length-1; i++) {
				var cellContent = null;
				
				if(inputTypes[i].indexOf('static:') >= 0)
					continue;

				if(inputTypes[i] == 'string') {
					cellContent = createTextBox(dr.cells[i].firstChild.name, cssStyles[i]);					
				} else if(inputTypes[i] == 'date') {
					cellContent = createDateBoxGroup(dr.cells[i].firstChild.name, cssStyles[i], false);					
				} else if(inputTypes[i] == 'select') {
					cellContent = createSelectComboElement(dr.cells[i].firstChild.name, cssStyles[i], inputTypesData[i], null);
				} else if(inputTypes[i] == 'competenceDesc') {
					//Ta je poseben
					cellContent = createCompetenceDescription(dr.cells[0].firstChild.name, dr.cells[i].firstChild.name, cssStyles[i]);
				}
				cellContent[0].value = dr.cells[i].firstChild.value;
				removeChildNodes(dr.cells[i]);
		
				for(var j=0; cellContent != null && j<cellContent.length; j++)		
					dr.cells[i].appendChild(cellContent[j]);
			}
			
			var deleteLink = createDeleteImage('Izbri<%=Utf8.sh%>i');
			deleteLink.onclick = function() {deleteTableRow(tableIdName, rowIndex, editWhat);};
			removeChildNodes(dr.cells[dr.cells.length-1]);
			dr.cells[dr.cells.length-1].appendChild(deleteLink);
			
			setChanges(true);
		}
	}
}

function deleteTableRow(tableIdName, dataRowId, deleteWhat) {
	if(true/*deletewarn(deleteWhat)*/) {
		var table = document.getElementById(tableIdName);
		
		if(table) {
			for(var i=1; i<table.rows.length; i++) {
				if(table.rows[i].id == dataRowId) {
					table.deleteRow(i);
					setChanges(true);
					
					return;
				}
			}
		}		
	}
}

function createCompetenceDescription(comboname, name, cssClass) {
	var textInput = document.createElement('textarea');
	textInput.name = name;			
	textInput.id = name;		
	textInput.className = cssClass;
	
	var helpImg = document.createElement('img');
	helpImg.src = '/images/bulb_sm.gif';	
	helpImg.onclick = function() {getCompetenceHelp(comboname);};
	
	return new Array(textInput, helpImg);
}

function createTextBox(name, cssClass) {
	var textInput = document.createElement('input');
	textInput.type = 'text';
	textInput.name = name;			
	textInput.id = name;		
	textInput.className = cssClass;
	return new Array(textInput);	
}

function createHiddenField(name) {
	var hidden = document.createElement('input');
	hidden.type = 'hidden';
	hidden.name = name;			
	hidden.id = name;		
	return new Array(hidden);	
}

function createTextAreaBox(name, cssClass) {
	var textInput = document.createElement('textarea');
	textInput.name = name;			
	textInput.id = name;		
	textInput.className = cssClass;
	
	return new Array(textInput);	
}

function createCheckBox(name, cssClass) {
	var checkInput = document.createElement('input');
	checkInput.type = 'checkbox';
	checkInput.name = name;			
	checkInput.id = name;		
	checkInput.className = cssClass;
	return new Array(checkInput);
	
}

function createCheckBoxWithLabel(name, cssClass, labelText) {
	var check = createCheckBox(name, cssClass)[0];
	var label = document.createElement('label');
	label.htmlFor = check.id;
	label.innerHTML = labelText;	
	label.style.paddingLeft = '5px';
	label.style.verticalAlign = 'middle';
	
	return new Array(check, label);	
}

function createDateBoxGroup(name, cssClass, showTime) {
	var dateInput = document.createElement('input');
	dateInput.type = 'text';
	dateInput.name = name;			
	dateInput.id = name;		
	dateInput.className = cssClass;
	
	var pattern = (showTime == true)?'%d.%m.%Y %H:%M':'%d.%m.%Y';
	var randomButtonId = 'b' + Math.round(1000*Math.random());
	for(var i=0; i<10000; i++) {
		if(document.getElementById(randomButtonId) != null)
			randomButtonId = 'b' + Math.round(1000*Math.random());
		else
			break;
			
		if(i==9999) {
			alert('Neuspesno generiranje oznake novega gumba. Moz>en razlog: Ivan Cankar se je reinkarniral in ravnokar koncuje svoj novi roman - Na klancu 2.');
			return;
		}				
	}	
	var buttonCal = document.createElement('button');
	buttonCal.id = randomButtonId;
	buttonCal.className = 'formbutton';
	buttonCal.innerHTML = '...';
	
	
	var calendarScript = document.createElement('script');
	calendarScript.type='text/javascript';
	calendarScript.text = 
    'Calendar.setup({' + 
        'inputField     :    \"' + name + '\",' +       
        'ifFormat       :    \"' + pattern + '\",' +   
        'button         :    \"' + randomButtonId + '\",' +  
        'firstDay       : 1,' + 	     
        'singleClick    :    true ,' + 
        'showsTime      :    ' + showTime + ',' +
		'onClose        :    function(cal) { cal.hide();}' +
	'})';
	
	return new Array(dateInput, buttonCal, calendarScript);
}

function createSelectComboElement(name, cssClass, options, selectedOption) {
	var selectBox = document.createElement('select');
	selectBox.name = name;
	selectBox.id = name;
	selectBox.className = cssClass;
	
	for(var i=0; i<options.length; i++) {		
		var optionBox = document.createElement('option');
		optionBox.value = options[i][0];
		optionBox.innerHTML = options[i][1];
		
		if(options[i][0] == selectedOption)
			optionBox.selected = 'selected';
		
		selectBox.appendChild(optionBox);
	}
	
	return new Array(selectBox);
}

function createFileElement(name) {
	var fileElem = document.createElement('input');
	fileElem.type = 'file';
	fileElem.lang = 'en-gb';
	fileElem.name = name;
	
	return new Array(fileElem);
}

function addTableRow(tableIdName, tableDefinition) {
	var table = document.getElementById(tableIdName);
	
	var columns, columnsSize;
	
	columns = tableDefinition[0];
	columnsCssStyles = tableDefinition[1];		
	columnsType = tableDefinition[2];
	columnsTypeData = tableDefinition[3];
	maxInputs = tableDefinition[4];
	
	if(table) {
		var lastRow = table.rows.length;
		if(lastRow > maxInputs) {
			alert('Preveliko stevilo vnosov.');
			return;
		}
			
		var newRow = table.insertRow(lastRow);
		var newRowId = Math.round(100000*Math.random());
		
		for(var i=0; i<10000; i++) {
			if(document.getElementById(tableIdName+'_'+newRowId) != null)
				newRowId = Math.round(100000*Math.random());
			else
				break;
			
			if(i==9999) {
				alert('Neuspesno generiranje oznake nove vrstice. Mozen razlog: vesolje se je odlocilo, da se malo posali z vami.');
				return;
			}				
		}
		
		newRow.id = tableIdName+'_'+newRowId;
		
		for(var i=0; i<columns.length; i++) {
			var addedCell = newRow.insertCell(i);
			var cellContent = null;
			
			if(columnsType[i].indexOf('static:') >= 0) {
				var defTextStart = columnsType[i].indexOf(':');			
				var defText = columnsType[i].substring(defTextStart+1, columnsType[i].length);
				
				addedCell.innerHTML = defText;
			} else if(columnsType[i] == 'string') {
				cellContent = createTextBox(columns[i]+newRowId, columnsCssStyles[i]);
			} else if(columnsType[i] == 'date') {
				cellContent = createDateBoxGroup(columns[i]+newRowId, columnsCssStyles[i], false);
			} else if(columnsType[i] == 'select') {
				cellContent = createSelectComboElement(columns[i]+newRowId, columnsCssStyles[i], columnsTypeData[i], null);
			} else if(columnsType[i] == 'competenceDesc') {
				//Ta je poseben
				cellContent = createCompetenceDescription(columns[0]+newRowId, columns[i]+newRowId, columnsCssStyles[i]);
			}
				
			for(var j=0; cellContent != null && j<cellContent.length; j++) {
				addedCell.appendChild(cellContent[j]);
			}
		}
				
		var editingCell = newRow.insertCell(columns.length);
		var deleteLink = createDeleteImage('Izbri<%=Utf8.sh%>i');
		deleteLink.onclick = function() {deleteTableRow(tableIdName, newRow.id, '');};
		editingCell.appendChild(deleteLink);
		
		setChanges(true);						
	}	
}

//Common functions
function addRowInDynamicTable(tableIdName, tableDefinition, noEntryId, noEntryLimitRowNumber) {
	var table = document.getElementById(tableIdName);
	
	var columns, columnsSize;
	
	columns = tableDefinition[0];
	columnsCssStyles = tableDefinition[1];		
	columnsType = tableDefinition[2];
	columnsTypeData = tableDefinition[3];
	maxInputs = tableDefinition[4];
	
	if(table) {
		var lastRow = table.rows.length;
		if(lastRow > maxInputs) {
			alert('Preveliko stevilo vnosov.');
			return;
		}
			
		var newRow = table.insertRow(lastRow);
		var newRowId = Math.round(100000*Math.random());
		
		for(var i=0; i<10000; i++) {
			if(document.getElementById(tableIdName+'_'+newRowId) != null)
				newRowId = Math.round(100000*Math.random());
			else
				break;
			
			if(i==9999) {
				alert('Neuspesno generiranje oznake nove vrstice. Mozen razlog: vesolje se je odlocilo, da se malo posali z vami.');
				return;
			}				
		}
		
		newRow.id = tableIdName+'_'+newRowId;
		
		for(var i=0; i<columns.length; i++) {
			var addedCell = newRow.insertCell(i);
			var cellContent = null;
			
			if(columnsType[i].indexOf('static:') >= 0) {
				var defTextStart = columnsType[i].indexOf(':');			
				var defText = columnsType[i].substring(defTextStart+1, columnsType[i].length);
				
				addedCell.innerHTML = defText;
			} else if(columnsType[i] == 'string') {
				cellContent = createTextBox(columns[i]+newRowId, columnsCssStyles[i]);
			} else if(columnsType[i] == 'date') {
				cellContent = createDateBoxGroup(columns[i]+newRowId, columnsCssStyles[i], false);
			} else if(columnsType[i] == 'select') {
				cellContent = createSelectComboElement(columns[i]+newRowId, columnsCssStyles[i], columnsTypeData[i], null);
			} else if(columnsType[i] == 'competenceDesc') {
				//Ta je poseben
				cellContent = createCompetenceDescription(columns[0]+newRowId, columns[i]+newRowId, columnsCssStyles[i]);
			}
				
			for(var j=0; cellContent != null && j<cellContent.length; j++) {
				addedCell.appendChild(cellContent[j]);
			}
		}
				
		var editingCell = newRow.insertCell(columns.length);
		var deleteLink = createDeleteImage('Izbri<%=Utf8.sh%>i');
		deleteLink.onclick = function() {deleteRowInDynamicTable(tableIdName, newRow.id, noEntryId, noEntryLimitRowNumber);};
		editingCell.appendChild(deleteLink);
		
		onAddingRowInDynamicTable(noEntryId);
		
		setChanges(true);						
	}	
}

function deleteRowInDynamicTable(tableId, deletedRowId, noEntryId, limitRows) {
	var table = document.getElementById(tableId);
	
	for(var i=0; i<table.rows.length; i++) {
		if(table.rows[i].id == deletedRowId) {
			table.deleteRow(i);
			setChanges(true);
					
			var noEntry = document.getElementById(noEntryId);
			noEntry.style.visibility = (table.rows.length <= limitRows)?'visible':'hidden';		
			return;
		}
	}
}

function onAddingRowInDynamicTable(noEntryId) {
	if(noEntryId == null)
		return;
	var noEntry = document.getElementById(noEntryId);
	noEntry.style.visibility = 'hidden';
}

function addDTRow(table, rowNum, rowId, cellsContent) {
	var row = table.insertRow(rowNum);
	row.id = rowId;
	for(var i=0; i<cellsContent.length; i++) {
		row.insertCell(i);
		row.cells[i].innerHTML = cellsContent[i];
	}
}

function createFFTable() {
	var table = document.createElement('table');
	
	table.width = "100%";
	table.style.cellspacing = 0;
	table.className = 'tablenopadding content';
	
	return table;
}

function addFFTableRow(table, caption, arrayOfFF) {
	var lastRow = table.rows.length;			
	var newRow = table.insertRow(lastRow);
	
	var captionCell = newRow.insertCell(0);
	var ffCell = newRow.insertCell(1);
	captionCell.innerHTML = caption;
	
	appendChildArray(ffCell, arrayOfFF);	
}

function addFFTableRowHeader(table) {
	var th1 = document.createElement('th');
	th1.style.width = '35%';
	var th2 = document.createElement('th');
	th2.style.width = '65%';
		
	table.appendChild(th1);
	table.appendChild(th2);	
}

function appendChildArray(parent, arrayOfChildren) {
	for(var j=0; arrayOfChildren != null && j<arrayOfChildren.length; j++) {
		parent.appendChild(arrayOfChildren[j]);
	}
}