var browserType
if (document.all) browserType = 'ie'; else browserType = 'ff';
// ---------- add functions ----------
function addRowToTable(tbl, position)
{ 
	var row = tbl.insertRow(position); 
	return row; 
}
function addRowAttribute(row, attribute, value)
{ 
	if (browserType == 'ie' && attribute == 'class') 
		row.setAttribute('className', value); 
	else 
		row.setAttribute(attribute, value); 
	return row; 
}
function addRowAttributes(row, attributes)
{
	for (var attribute in attributes) {
		if (browserType == 'ie' && attribute == 'class') 
			row.setAttribute('className', attributes[attribute]); 
		else 
			row.setAttribute(attribute, attributes[attribute]); 
	}
	return row; 
}
function addCellToRow(row, index)	
{ 
	var cell = row.insertCell(index); 
	return cell; 
}
function addCellAttribute(cell, attribute, value)
{ 
	if (browserType == 'ie' && attribute == 'class') 
		cell.setAttribute('className', value); 
	else 
		cell.setAttribute(attribute, value); 
	return cell;
}
function addCellAttributes(cell, attributes)
{
	for (var attribute in attributes) {
		if (browserType == 'ie' && attribute == 'class') 
			cell.setAttribute('className', attributes[attribute]);	
		else 
			cell.setAttribute(attribute, attributes[attribute]); 
	}
	return cell; 
}
function addTextToCell(cell, text)
{
	cell.innerHTML = text;
	return cell;
}
function createElement(type)
{
	var element = document.createElement(type);
	return element;
}
function addElementAttribute(element, attribute, value)
{
	if (browserType == 'ie' && attribute == 'class') 
		element.setAttribute('className', value); 
	else 
		element.setAttribute(attribute, value); 
	return element;
}
function addElementAttributes(element, attributes)
{
	for (var attribute in attributes) {
		if (browserType == 'ie' && attribute == 'class') 
			element.setAttribute('className', attributes[attribute]); 
		else 
			element.setAttribute(attribute, attributes[attribute]); 	
	}
	return element; 
}
function addElementToCell(cell, element)
{
	cell.appendChild(element);
	return cell;
}
// ---------- remove functions ----------
function removeLastRowFromTable(tbl, position, confirmText)
{
	if (confirm(confirmText)) tbl.deleteRow(position);
}
function removeRowFromTable(tbl, position)
{
	tbl.deleteRow(position);
}

// ---------- SELECT element functions ------------
function addListElement(elementName, valueIndex, valueText) 
{
	var listSelected= document.getElementById(elementName);
	var listCount	= listSelected.length;
	var inList		= false;
	var index		= -1;
		
	for(var i=0; i < listCount; i++) {
		if (listSelected.options[i].value == valueIndex) {
			inList = true;
			index = i;
			break;
		}
	}

	if (! inList) {
		var myNewOption = new Option(valueText, valueIndex);
		addElementAttribute(myNewOption, 'ondblclick', "removeListElement('" + elementName + "', " + valueIndex + ")");
		listSelected.options[listCount] = myNewOption;
		return true;
	} else {
		alert("Element je vec u listi");
		return false;
	}
}

function removeListElement(elementName, valueIndex) 
{
	var listSelected= document.getElementById(elementName);
	var listCount	= listSelected.length;
	var index		= -1;
		
	for(var i=0; i < listCount; i++) {
		if (listSelected.options[i].value == valueIndex) {
			index = i;
			break;
		}
	}

	if (index >= 0) {
		if (confirm("Obrisati element?")) {
			listSelected.remove(index);
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
	
function addListElements(elelentAllName, elementSelectedName) 
{
	var listAll				= document.getElementById(elelentAllName);
	var listSelected		= document.getElementById(elementSelectedName);
	var listAllCount		= listAll.length;
	var listSelectedCount	= listSelected.length;
	var inList;
		
	for(var a=0; a < listAllCount; a++) {
		if (listAll.options[a].selected) {
			inList	= false;
			for(var s=0; s < listSelectedCount; s++) {
				if (listAll.options[a].value == listSelected.options[s].value) {
					inList = true;
					break;
				}
			}
			if (!inList) {
				var myNewOption = new Option(listAll.options[a].text, listAll.options[a].value);
				listSelected.options[listSelectedCount++] = myNewOption;	
			}
		}
	}
}

function removeListElements(elementName, deleteFromList) 
{
	if (confirm(deleteFromList)) {
		var listSelected		= document.getElementById(elementName);
		var listSelectedCount	= listSelected.length;
		
		for(var s=listSelectedCount-1; s >= 0; s--) {
			if (listSelected.options[s].selected) listSelected.remove(s);
		}
		return true;
	} else {
		return false;
	}
}

function selectAll(elementName)
{
	var listSelected		= document.getElementById(elementName);
	var listSelectedCount	= listSelected.length;
	
	for(var i=0; i < listSelectedCount; i++) listSelected.options[i].selected = true;
	
	return true;
}
