
function addOption(theSel, theText, theValue)
{
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{	
	var selLength = theSel.length;
	if(selLength>0)
	{
		theSel.options[theIndex] = null;
	}
}

function moveOptions(theSelFrom, theSelTo, childKey)
{
	
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;
	
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected && theSelFrom.options[i].value!="")
		{
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}
	
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.

	for(i=selectedCount-1; i>=0; i--)
	{
		if ( (childKey != null) && (childKey != "") )
		{
			if (childKey == "DealPercent")
            {
                var key = prompt("Please enter percent ..% Invested by this company "+selectedText[i]+" in deal (if known - otherwise leave empty):");
            } 
            else if (childKey == "ParentPercent")
            {
                var key = prompt("Please enter percent ..% Ownership of parent company "+selectedText[i]+" over this daughter company (if known - otherwise leave empty):");
            }
            else if (childKey == "TaxEquityPercent")
            {
                var key = prompt("Please enter percent ..% of Class A Equity acquired by "+selectedText[i]+" (if known - otherwise leave empty):");
            } 
            else if (childKey == "RPownersPercent")
            {
                var key = prompt("Please enter percent ..% of Project Equity owned by "+selectedText[i]+" (if known - otherwise leave empty):");
            } 
	    else {
                var key = prompt("Please enter "+childKey+" for "+selectedText[i]+" \n(if known - otherwise leave empty): ");
            }
            
			var caption = selectedText[i]+" ("+key+")";
			var value = selectedValues[i]+"^"+key;
		} else {
			var caption = selectedText[i];
			var value = selectedValues[i];
		}
		addOption(theSelTo, caption, value);
	}
	
	if (theSelTo.length < 500) {
		sortOptions(theSelTo);
	}

}


function updateValues(theSelTo, theValues)
{

	var values = ""; 
	for(i=0; i < theSelTo.options.length; i++)
	{
		values = values + "," + theSelTo.options[i].value; 
	}
	
	theValues.value = values.substring(1,32000);
}



function SdeleteOption(object,index) {
    object.options[index] = null;
}

function SaddOption(object,text,value) {
    var defaultSelected = false;
    var selected = false;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;
    object.options[object.length-1].selected = false;
}

function sortOptions(what) {
    var copyOption = new Array();
    for (var i=0;i<what.options.length;i++)
        copyOption[i] = new Array(what[i].text, what[i].value);

    copyOption.sort();//function(a,b) { return b[0]-a[0]; });

    for (var i=what.options.length-1;i>-1;i--)
        SdeleteOption(what,i);

    for (var i=0;i<copyOption.length;i++)
        SaddOption(what,copyOption[i][0],copyOption[i][1])
}

