/* function addTechniqueSelect( fieldname, nameRoot, itemnum, optionlist)
{
	var newitem;
	newitem = "<select name='" + nameRoot + "-" + itemnum + "'>"
	newitem += optionlist
	newitem += "</select>"
	var dynamic_item_div = document.getElementById(”dynamic_div”);
	dynamic_item_div.innerHTML += newitem;
	itemcount++;
}
*/

// This is unused, but works great for duplicating div content "Add Another" button
// For example: 
//<div class="group" id="parent-addanother">
//<div class="indented-group-noborder" id="sibling-addanother">
//<input type="hidden" name="relatedtechnique-orig" value="<%=relatedtechniqueid%>">
//<select name="relatedtechniques">
//<options blah...>
//</select>
//</div>
//<input type="button" value="Add Another..." onClick="addTechniqueSelect( 'parent-addanother', 'sibling-addanother', 'end-addanother', 'numrelated', 'relatedtechnique')" class="btn" onMouseOver="this.className='btn btnhover'" onMouseOut="this.className='btn'">
//<div id="end-addanother"><input type="hidden" value="1" id="numrelated" name="numrelated"/></div>
//</div>
function addTechniqueSelect(divParent, divSibling, divInsertBefore, elementNum, elementNameChange)
{
	// Increment the number for the ID and set it
	var nodeNumElements = document.getElementById(elementNum);
	var num = parseInt(nodeNumElements.getAttribute("value"));
	num = num + 1;
	nodeNumElements.setAttribute("value", num);

	// Get the original node
	var nodeOriginal = document.getElementById(divSibling);
	var elementParent = document.getElementById(divParent);
	var elementBefore = document.getElementById(divInsertBefore);
	
	//Copy the node
	var nodeCopy = nodeOriginal.cloneNode(true);
	
	//Add it to the DOM
	elementParent.insertBefore(nodeCopy, elementBefore);

	// Set the id of the select
	var nodes
	
	nodes = nodeCopy.childNodes;
	
	var theName;
	for (var i=0, len=nodes.length; i<len; i++){
		theName = nodes[i].name;
		
		if (theName)
			if (theName.match(elementNameChange) != null) {
				nodes[i].name =  nodes[i].name + "-" + num;
			} 
	}	
	
//	alert(nodeOriginal.innerHTML);
//	alert(nodeCopy.innerHTML);

}

function dosubmit(formName){
	addHiddenInput( formName, 'relatedtechniqueselections', getSelectList( 'relatedtechniquelistright'));
	document.forms[formName].submit();
}

function addHiddenInput(theForm, name, value) {
	var e = document.createElement('input');
	e.setAttribute('type', 'hidden');
	e.setAttribute('name', name);
	e.setAttribute('value', value);
	var f = document.getElementById(theForm);
	f.appendChild(e);

}

function getSelectList( srcList){
	var srcListElm = document.getElementById(srcList);
	var listArray = new Array(srcListElm.options.length + 1);
	// element 0 in array is the length
	listArray[0] = srcListElm.options.length;
	for (var i=1; i<=srcListElm.options.length; i++){
		listArray[i] = new Array(srcListElm.options[i-1].value, srcListElm.options[i-1].text); 
	}
	return listArray; 
}

function moveOptions( srcList, destList, moveAll ) 
{
	var srcListElm = document.getElementById(srcList);
	var destListElm = document.getElementById(destList);
	moveSelectedOptions(srcListElm, destListElm);
} 

function moveSelectedOptions(from,to) {
	for (var i=0; i<from.options.length; i++) {
		for(var j=0;j<to.options.length;j++){
			if((from.options[i].selected)&&(from.options[i].text == to.options[j].text) ){
				i++;
			}
		}
		
		var o = from.options[i];
		if (i<from.options.length && o.selected) {
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
		}
	
	
	}
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = from.options[i];}
		}
	
		//reset selections to NONE
		from.selectedIndex = -1;
}

function removeOptions(fromList, removeall){
	var fromListElm = document.getElementById(fromList);
	for(var i=fromListElm.options.length-1;i>-1;i--){
		if(fromListElm.options[i].selected){
			fromListElm.options[i] = null;
		}
	}
} 