
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
    var i, j;
    var prompt;

    // empty existing items
    for (i = selectCtrl.options.length; i >= 0; i--) {
        selectCtrl.options[i] = null;
    }
    prompt = (itemArray != null) ? goodPrompt : badPrompt;
    if (prompt == null) {
        j = 0;
    } else {
        selectCtrl.options[0] = new Option(prompt);
        j = 1;
    }
    if (itemArray != null) {
        // add new items
        for (i = 0; i < itemArray.length; i++)
        {
            selectCtrl.options[j] = new Option(itemArray[i][0]);
            if (itemArray[i][1] != null) {
                selectCtrl.options[j].value = itemArray[i][1];
            }
            j++;
        }
        // select first item (prompt) for sub list
        selectCtrl.options[0].selected = true;
    }
}

// check which option button is selected and update pulldown accordingly
function update_list(From, Dest)
{

    fillSelectFromArray(Dest, destinations[From.selectedIndex]);
	
	//if there is only one choice we need to populate types immediately 
    if(Dest.options.length == 1)
    {
    	update_types(Dest.form.type, From, Dest);
    }
}

function fillTypeFromList(subjectList, Origin, Dest)
{
	//get the shortcode for both From and Dest
	var fromshort, toshort, typelist
	fromshort = Origin.options[Origin.selectedIndex].text;
	toshort = Dest.options[Dest.selectedIndex].text;
	
	//alert(fromshort + " " + toshort);
	//get the array back
	typelist = new Array();
	typelist = types[fromshort][toshort];
	//if array is more than one add 'all'	
	if(typelist.length > 1)
	{
		typelist.unshift("all");
	}
	//generate options
	var i, j;
	//empty current list	
	for (i = subjectList.options.length; i >= 0; i--) {
        subjectList.options[i] = null;
    }
	
	j = 0;
	var nomaltype, firstUCtype;
	for (i = 0; i < typelist.length; i++)
   {
	//alert(i + ": " + typelist[i]);
   	firstUCtype = typelist[i].substr(0,1).toUpperCase() + typelist[i].substr(1);
   	normaltype = typelist[i];
   	subjectList.options[j] = new Option(firstUCtype);
   	subjectList.options[j].value = normaltype;
   	j++;
   }
   if(j == 1)
   {
   	subjectList.options[0].selected = true;
   }
}

function update_types(Type, Origin, Dest)
{
	if((Origin.length > 0) && (Dest.length > 0))
	{
		if(Origin.selectedIndex >= 0 && Dest.selectedIndex >= 0)
		{
			if((Origin.options[Origin.selectedIndex].value != -1) && (Dest.options[Dest.selectedIndex].value != -1))
			{		
				fillTypeFromList(Type, Origin, Dest);
			}
		}
	}
}

function showRepeats(me)
{
    meSelect = document.getElementsByName("repeat" + (me))[0];
    index = meSelect.selectedIndex;

    if (index > 0 && me < 9)      // Repeat was selected, show the rest
    {
        span = document.getElementById("span_repeat" + (me+1));
        span.style.display = "inline";
    }
    else if (index <=0 )
    {
        // Check if the next select box is also 0, if so, hide it
        nextSelect = document.getElementsByName("repeat" + (me+1))[0];
        if (nextSelect.selectedIndex <= 0)
        {
            span = document.getElementById("span_repeat" + (me+1));
            span.style.display = "none";
        }
    }
}
