﻿var timerActive = false;

function FlipTimer(Start) 
{
    timerActive = Start;

    if (Start) {
        setTimeout('DoGet()', 500);
    }        
}

function DoGet() {
    if (timerActive) {
        var filterText = document.getElementById('filter').value;

        if (filterText.length > 0) {
            GetPortfolio(filterText);
        }
        else {
            GetPortfolio("");
        }     
    }

    timerActive = false;
}

function GetPortfolio(FilterText) {                
    $.ajax({
        type: "POST",
        url: "/FundFiling/Services/FundFilingService.asmx/GetListItems",
        data: "{ 'QueryType':'Portfolio', 'Predicate':'" + FilterText + "'}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(Portfolios) {
            $("#lstAvailable").replaceWith("<select id='lstAvailable' name='lstAvailable' size='25' style='width: 350px; height: 300px' multiple='multiple'>" + Portfolios.d[0] + "</select>");
        },
        error: function(Error) {
            alert("Error " + Error.status + ": " + Error.statusText + "\n" + Error.responseText)            
        }
    });
}

function MoveSelectedFunds(Source, Target, HiddenFieldId) {        
    var source = document.getElementById(Source);
    var target = document.getElementById(Target);
    var hiddenFieldList = document.getElementById(HiddenFieldId);

    if ((source != null) && (target != null)) {                        
        while (source.options.selectedIndex >= 0) {            
            var selectedFundValue = source.options[source.options.selectedIndex].value + '~';
            
            var newOption = new Option();
            newOption.text = source.options[source.options.selectedIndex].text;
            newOption.value = source.options[source.options.selectedIndex].value;

            target.options[target.length] = newOption;            
            source.remove(source.options.selectedIndex);            
            
            if (hiddenFieldList.value.indexOf(selectedFundValue) == -1) {
                hiddenFieldList.value += selectedFundValue;                 
            }
            else {                
                hiddenFieldList.value = hiddenFieldList.value.replace(selectedFundValue, "");                
            }
        }

        $(target).sort("asc");
    }
}

$.fn.sort = function(ascending) {
    var sortDirection = typeof (ascending) == "undefined" ? true : !!ascending;
    this.each(
		function() {
		    // get options
		    var optionAttributes = this.options;
		    // get number of options
		    var optionCount = optionAttributes.length;
		    // create an array for sorting
		    var optionArray = [];
		    // loop through options, adding to sort array
		    for (var i = 0; i < optionCount; i++) {
		        optionArray[i] = {
		            value: optionAttributes[i].value,
		            text: optionAttributes[i].text
		        }
		    }
		    // sort items in array
		    optionArray.sort(
				function(option1, option2) {
				    // option text is made lowercase for case insensitive sorting
		            option1_text = option1.text.toLowerCase();
				    option2_text = option2.text.toLowerCase();
				    // if options are the same, no sorting is needed
				    if (option1_text == option2_text) return 0;
				    if (sortDirection) {
				        return option1_text < option2_text ? -1 : 1;
				    }
				    else {
				        return option1_text > option2_text ? -1 : 1;
				    }
				}
			);
		    // change the options to match the sorted array
			for (var i = 0; i < optionCount; i++) {
			    optionAttributes[i].text = optionArray[i].text;
			    optionAttributes[i].value = optionArray[i].value;
		    }
		}
	)
    return this;
};