/*
jsdefault.js
common JavaScript functions
(c) 2005 Bioforsk, Norway
Author: Tor-Einar Skog, Hyperlinkto AS
*/

/*
Appends a selected option in sourceList with id=sourceList to the list with id=destListId
*/
function copySelectedOptionTo(sourceListId, destListId, newOptionId)
{
	var sourceList = document.getElementById(sourceListId);
	var sIndex = sourceList.selectedIndex;
	var option = sourceList.options[sIndex];
	var optionValue = sourceList.options[sIndex].value;
	var optionLabel = sourceList.options[sIndex].text;
	var destList = document.getElementById(destListId);
	var newOption = new Option(optionLabel, optionValue);
	newOption.id = newOptionId;
	destList.appendChild(newOption);
}

/*
Deletes the currently selected option in list with id=listId
*/
function deleteSelectedOption(listId)
{
	var list = document.getElementById(listId);
	list.options[list.selectedIndex] = null;
}

/*
Moves the selected option one place up in the list
*/
function moveSelectedOptionUp(listId)
{
	var list = document.getElementById(listId);
	var sIndex = list.selectedIndex;
	if(sIndex > 0)
	{
		var temp1 = new Option(list.options[sIndex - 1].text,list.options[sIndex - 1].value);
		temp1.id = list.options[sIndex - 1].id;
		var temp2 = new Option(list.options[sIndex].text,list.options[sIndex].value);
		temp2.id = list.options[sIndex].id;
		list.options[sIndex - 1] = temp2;
		list.options[sIndex] = temp1;
		list.options[sIndex -1].selected=true;
	}
}

/*
Moves the selected option one place down in the list
*/
function moveSelectedOptionDown(listId)
{
	var list = document.getElementById(listId);
	var sIndex = list.selectedIndex;
	if(sIndex < list.options.length -1 )
	{
		var temp1 = new Option(list.options[sIndex + 1].text,list.options[sIndex + 1].value);
		temp1.id = list.options[sIndex + 1].id;
		var temp2 = new Option(list.options[sIndex].text,list.options[sIndex].value);
		temp2.id = list.options[sIndex].id;
		list.options[sIndex + 1] = temp2;
		list.options[sIndex] = temp1;
		list.options[sIndex + 1].selected=true;
	}
}

/*
Creates a random id for an element, an id that does not exist in the document
beforehand
*/
function createNewId()
{
	var id="id_";
	var number = 0;
	while(document.getElementById(id + number) != null)
	{
		number = Math.round(Math.random() * 1000000);
	}
	//alert(id + number);
	return id + number;
}

/*
Moves a table row up (if it's not already on top)
*/
function moveRowUp(startObject)
{
	var row = getParentByTagName(startObject, "tr");
	var table = getParentByTagName(startObject, "table");
	var tBody = table.getElementsByTagName("tbody").item(0);
	var rows = table.rows;
	for(var i=0;i<rows.length;i++)
	{
		if(rows.item(i) == row && i > 1)
		{
			var rowBefore = rows.item(i-1);
			var thisRow = rows.item(i);
			tBody.insertBefore(thisRow, rowBefore);
			return;
		}
	}
}

function deleteRow(startObject)
{
	var row = getParentByTagName(startObject, "tr");
	var table = getParentByTagName(startObject, "table");
	var tBody = table.getElementsByTagName("tbody").item(0);
	tBody.removeChild(row);
}

/*
Moves a table row down (if it's not already at the bottom)
*/
function moveRowDown(startObject)
{
	var row = getParentByTagName(startObject, "tr");
	var table = getParentByTagName(startObject, "table");
	
	var tBody = table.getElementsByTagName("tbody").item(0);
	var rows = table.rows;
	for(var i=0;i<rows.length;i++)
	{
		if(rows[i] == row && i < rows.length -1)
		{
			var rowAfter = rows.item(i+1);
			var thisRow = rows.item(i);
			tBody.insertBefore(rowAfter, thisRow);
			return;
		}
	}
}

/*
Returns the first found parent node with the given tag name
*/
function getParentByTagName(anObject, tag)
{
	if(isEmpty(anObject) || isEmpty(anObject))return null;
	tag = tag.toLowerCase();
	while(!isEmpty(anObject))
	{
		if(anObject.nodeName.toLowerCase()==tag)return anObject;
		anObject=anObject.parentNode;
	}
}

/*
Ever been curious about what an object contains? This funtion returns a string with all properties
and methods
*/
function reflectObject(anObject)
{
	var propertyList = "";
	for (var prop in anObject) 
		propertyList += prop + ": " + anObject[prop] + "\n";
	
	return propertyList;
}

/* returns a new table row */
function getNewRow()
{
	return document.createElement("tr");
}

/* returns a new table cell */
function getNewCell()
{
	return document.createElement("td");
}

/* appends text to an element */
function appendText(element,text)
{
	var textNode = document.createTextNode(text);
	element.appendChild(textNode);
	return element;
}

/* returns a new text area */
function getNewTextArea(name, cols, rows)
{
	var textArea = document.createElement("textarea");
	textArea.name=name;
	textArea.rows=rows;
	textArea.cols=cols;
	
	return textArea;
}
/* returns a new button */
function getNewButton(label, onClick)
{
	var button = document.createElement("input");
	button.type="button";
	button.value=label;
	button.onclick=onClick;
	
	return button;
}

/* Returns a new hidden form variable */
function getNewHiddenVariable(name, value)
{
	var retVal = document.createElement("input");
	retVal.type="hidden";
	retVal.name = name;
	retVal.value = value;
	
	return retVal;
}

/*
Detect if a variable is logically empty
*/
function isEmpty(prm)
{
	return(!isSet(prm) || ((typeof prm).toLowerCase()=="string" && prm.length==0));
}

/*
Detect if a variable is set
*/
function isSet(prm)
{
	var und;
	return(prm!=und && prm!=null);
}

