function isNavigator()
{
    return browserObject.ns;
}

function isInternetExplorer()
{
    return browserObject.ie;
}

function notFilteredItem(value, filteredValuesArray)
{
	var returnBool = true;
	for(i in filteredValuesArray)
	{
		//alert("filteredValuesArray[i] = "+filteredValuesArray[i]+", optionValue = "+optionValue);		
		if(filteredValuesArray[i] == value)
			returnBool = false;
	}
	return returnBool;
}

function copyOption(opt)
{
    return new Option(opt.text, opt.value, false, false);
}

function copyOptions(fromOptionsArray, toArray) {

    // Erase everything in the toArray first...
    for (var j = toArray.length -1 ; j >= 0;  j--) {
        toArray[j] = null;
    }

    // Now copy the options.
    for (var i=0; i < fromOptionsArray.length; i++) {
        toArray[i] = copyOption(fromOptionsArray[i]);
    }
}

function sortSelect(obj) 
{
	var o = new Array();
	for (var i=0; i<obj.options.length; i++) 
	{
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
	}
	
	o = o.sort
	( 
		function(a,b) 
		{ 
			var aText = a.text+"";
			var bText = b.text+"";
			if(!isNull(aText)) aText = aText.toLowerCase();
			if(!isNull(bText)) bText = bText.toLowerCase();
			
			if ((aText+"") < (bText+"")) { return -1; }
			if ((aText+"") > (bText+"")) { return 1; }
			return 0;
		} 
	);

	for (var i=0; i<o.length; i++) 
	{
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}


// Insert the given option into the given list, which is
// either alphabetically sorted or not.  Returns 'true'
// if we think the list should be redrawn (currently
// estimated by figuring out if the new item is the
// longest).

function internal_insertOptionIntoSelectList(opt, list, sorted, unique)
{
    //figure out if unique option is needed - if so, and not unique - return
	 if(unique)
	 {
	 		for (var i = 0; i < list.options.length; i++) 
			{
            if (list.options[i].text == opt.text)
                return false;
        	}
	 }
	 
	 //figure out if we need to redraw
    var redraw = false;
    {
        var maxlength = 0;
        for (var i = 0; i < list.options.length; i++) {
            if (list.options[i].text.length > maxlength)
                maxlength = list.options[i].text.length;
        }

        if (maxlength <= opt.text.length)
            redraw = true;
    }

    // Then insert the item
    if (sorted) {

        for (var i = 0; i < list.options.length; i++) {
            if (opt.text.toLowerCase() < list.options[i].text.toLowerCase()) {
                
                // Move the rest of the items up
                for (var j = list.options.length - 1; j >= i; j--) 
                    list.options[j+1] = copyOption(list.options[j]);

                list.options[i] = copyOption(opt);
                return redraw;
            }
        }

        list.options[list.options.length] = copyOption(opt);

    } else {

        // Just insert it at the end
        list.options[list.options.length] = copyOption(opt);
    }

    return redraw;
}

function internal_insertUniqueOptionIntoSelectList(opt, list, sorted)
{
	return internal_insertOptionIntoSelectList(opt, list, sorted, true)
}


function insertOptionIntoSelectList(opt, list, sorted)
{
    if (internal_insertOptionIntoSelectList(opt, list, sorted)) {
        if (isNavigator())
            history.go(0);
    }
}

function insertOptionIntoSelectListNoRedraw(opt, list, sorted)
{
    return internal_insertOptionIntoSelectList(opt, list, sorted);
}

function insertStringIntoSelectList(str, list, sorted)
{
    if (internal_insertOptionIntoSelectList(new Option(str, str, false, false), list, sorted)) {
        if (isNavigator())
            history.go(0);
    }
}

function removeSelectedItemsFromList(list, filteredValuesArray)
{
    var i = 0;
    while (i < list.options.length) 
	 {
        if (list.options[i].selected) 
		  {
            if(!filteredValuesArray || notFilteredItem(list.options[i].value, filteredValuesArray))
					list.options[i] = null;
        } 
		  i++;
    }

    return true;
}

function removeItemsFromListByOptionValue(optValue, list)
{
    var i = 0;
    while (i < list.options.length) {
        if (list.options[i].value == optValue) {
            list.options[i] = null;
        } else
            i++;
    }

    return true;
}

function makeItemInListSelected(optValue, list, singleSelect)
{
    var i = 0;
    while (i < list.options.length) {
        if (list.options[i].value == optValue) {
            list.options[i].selected = true;
        } 
		else if (singleSelect) {
		    list.options[i].selected = false;
		}
        
		i++;
    }

    return true;
}

function removeAllItemsFromList(list)
{
    while (list.options.length > 0) {
        list.options[0] = null;
    }

    return true;
}

function copySelectedItemsListToList(from, to, sorted, filteredValuesArray)
{
    var redraw = false;
    var i = 0;
    	 
	 while (i < from.options.length) 
	 {
    		if (from.options[i].selected) 
			{
      		if(!filteredValuesArray || notFilteredItem(from.options[i].value, filteredValuesArray))
				{
					internal_insertUniqueOptionIntoSelectList(from.options[i], to, sorted);
         		redraw = true;
				}
      	} 
			i++;
    }

    if (redraw && isNavigator())
        history.go(0);

    return true;
}

function moveSelectedItemsListToList(from, to, sorted)
{
    var redraw = false;
    var i = 0;
    while (i < from.options.length) {
        if (from.options[i].selected) {
            internal_insertOptionIntoSelectList(from.options[i], to, sorted);
            from.options[i] = null;
            redraw = true;
        } else
            i++;
    }

    if (redraw && isNavigator())
        history.go(0);

    return true;
}

function moveSelectedItemsUp(list)
{
    if (list.options.length <= 1)
        return true;

    // Can't move the top item up, so we sit still
    if (list.options[0].selected)
        return true;

    for (i = 1; i < list.options.length; i++) {
        if (list.options[i].selected) {
            var temp = copyOption(list.options[i-1]);
            list.options[i-1] = copyOption(list.options[i]);
            list.options[i] = copyOption(temp);
            list.options[i-1].selected = true;
            list.options[i].selected = false;
        }
    }

    return true;
}

function moveSelectedItemsDown(list)
{
    if (list.options.length <= 1)
        return true;

    // If the bottom item's selected, we can't move them down
    if (list.options[list.options.length-1].selected)
        return true;
  
    for (i = list.options.length - 2; i >= 0; i--) {
        if (list.options[i].selected) {
            var temp = copyOption(list.options[i+1]);
            list.options[i+1] = copyOption(list.options[i]);
            list.options[i] = copyOption(temp);
            list.options[i+1].selected = true;
            list.options[i].selected = false;
        }
    }

    return true;
}

function flattenList(inputList) {
  var outputString = new String();
  for (var i = 0; i< inputList.length; i++) {
    outputString += escape(inputList[i].value) + " "; 
  }
  return outputString;
}

//  Utility routines - fetch the currently selected item from a list.
//  This assumes a single-select list, of course.

function getSelectedItem(select_element)
{
    for (var i = 0; i < select_element.options.length; i++) {
        if (select_element.options[i].selected)
            return select_element.options[i].value;
    }

    return "";
}

//  Same thing, but returns an array of selected items from a
//  multiple selection select_element.

function getSelectedItems(select_element)
{
    var result = new Array();

    for (var i = 0; i < select_element.options.length; i++) {
        if (select_element.options[i].selected)
            result[result.length] = select_element.options[i].value;
    }

    return result;
}

//  Set the selected item.  Has no effect if the item does not
//  exist in the list.

function setSelectedItem(select_element, value)
{
    for (var i = 0; i < select_element.options.length; i++) {
        if (select_element.options[i].value == value) {
            select_element.selectedIndex = i;
			//select_element.options[i].selected = true;
            return;
        }
    }
}

function setAllOptions(select_element, checked)
{
	for( i=0; i < select_element.options.length; i++)
	{
		select_element.options[i].selected = checked;
	}
}

