// -------------------------------------------------------------------------------------------------------
// Functions for the fund selector drop downs
// -------------------------------------------------------------------------------------------------------

//global array of funds taht are available using the global settings below
var availableFunds = new Array();

//if required, this will be populated by the appropriate .NET FundBasket Control
var arrFundsExclude = new Array();
arrFundsExclude = [];

//global vars
//var selectedProduct = ""; //These have been moved to FundSelector.aspx to improve efficiancy, as the setAvailableFunds() gets called from both  setProductType and setServiceOffering
//var selectedServOff = ""; // when they can just be set directly. Otherwise, the large number of instructions causes a browser 'script has become unresponsive' error. (thi is caused in IE when >5M instructions have taken place without a break (5M is defined in registry)

var requiredIncOrAcc = false;
var requiredProductType = false;
var requiredServiceOffering = false;//TODO: add this to logic

var incomeOnly = null;
var hashFundsByFundCode = null;


function setAvailableFunds()
{

	
	if (typeof(arrProductGrid)=='undefined')
		return;//no ProductGrid, can't continue.
		
	if (!requiredIncOrAcc && !requiredProductType)
	{
		clearAvailableFunds();
		for (var i=0; i < arrProductGrid.length; i++)
		{
			if (arrProductGrid[i][GRID_PRODUCT].toUpperCase() == selectedProduct.toUpperCase() 
				&& arrProductGrid[i][GRID_SERV_OFF].toUpperCase() == selectedServOff.toUpperCase())
			{
				availableFunds = arrProductGrid[i][GRID_FUNDS];
				break;
			}
		}
		//if there's an inc or acc filter, use it now
		if (incomeOnly==1 || incomeOnly==0) {
		    
			var tempAvailableFunds = new Array();
			for (var i=0;i<availableFunds.length;i++)
			{
			    var theFund = GetFundFromCode(availableFunds[i]);
				if (theFund[FUND_INCOME] == incomeOnly)
				{
					tempAvailableFunds[tempAvailableFunds.length] = theFund[FUND_CODE];
				}
			}
			availableFunds = tempAvailableFunds;
		}
	}
}

function sortAsc(a,b)
{
var v;
if(a>b) v=1;
if(a<b) v=-1;
if(a==b) v=0;
return v;
}

var arrTempProductGrid = new Array();

function clearAvailableFunds()
{
	availableFunds = new Array();
}

function setProductType(value)
{
	selectedProduct = value.toUpperCase();
	//alert("Selected PPT : "+selectedProduct);
	requiredProductType = false;
	setAvailableFunds();
}

function setServiceOffering(value)
{
	selectedServOff = value;
	setAvailableFunds();
}

function setIncOrAcc(value)
{
	incomeOnly = null;
	if(value.toUpperCase()=="INC")
	{
		incomeOnly=1;
	}
	if(value.toUpperCase()=="ACC")
	{
		incomeOnly=0;
	}
	requiredIncOrAcc = false;
	setAvailableFunds();
}

function setIncOrAccRequired(required)
{
	requiredIncOrAcc = required;
}

function setProductTypeRequired(required)
{
	requiredProductType = required;
}

function ShowWarning()
{
	if (requiredProductType)
	{
		alert("Please select a product type.");
		return false;
	}
	if (requiredIncOrAcc)
	{
		alert("Please select income or accumulate.");
		return false;
	}
}

function PopulateFundManagers(selFund, selFundMan)
{
	//Make sure only valid fundmanagers get in here
	var validFundManList = new Array();
	var lastFundManIndex = -1;

	for (var i=0; i < availableFunds.length ; i++)
	{
		var theFund = GetFundFromCode(availableFunds[i]);
		if ( theFund[FUND_MANINDEX]>lastFundManIndex && !excludedFund(theFund[FUND_CODE]) )	
		{		
			validFundManList[validFundManList.length] = theFund[FUND_MANINDEX];
			lastFundManIndex = theFund[FUND_MANINDEX];
		}
	}
	
	if (!selFundMan.disabled && autofocus) selFundMan.focus();
	
	//remember currently selected
	var selectedFundMan = (selFundMan.length>1)?selFundMan.options[selFundMan.selectedIndex].value:-1;
	var selectedFundManIndex = 0;
	
	//If nowt selected and only 1 valid fund manager then select it
	if (selectedFundMan == -1 && validFundManList.length==1)
	{
		selectedFundMan = validFundManList[0];
		if (!selFund.disabled && autofocus) selFund.focus();
	}

	//clear list
	clearDropDown(selFundMan,"Select fund manager");
	
	startAt = 0;
	for (var i=0; i < validFundManList.length ; i++)
	{
		theFundMan = getFundManagerFromIndex(validFundManList[i],startAt)
		selFundMan.options[selFundMan.length] = new Option(theFundMan[FUNDMAN_NAME],validFundManList[i]);
		if(validFundManList[i]==selectedFundMan)
		{
			selectedFundManIndex = selFundMan.length-1;
		}
	}
	selFundMan.selectedIndex = selectedFundManIndex;
	
	//now populate funds list
	PopulateFunds(selFund,selFundMan);
}

function PopulateFunds(selFund, selFundMan)
{
		var selectedFund = (selFund.length>1 && selFund.selectedIndex!= -1 )?selFund.options[selFund.selectedIndex].value:-1;
		var selectedFundMan = selFundMan.options[selFundMan.selectedIndex].value;
		
		var selectedFundIndex = 0;
		clearDropDown(selFund,"Select fund");
		
		
		if (selectedFundMan>-1)
		{
			//var startAt = 0; //speed up
			for (var i=0;i<availableFunds.length;i++)
			{	
                var theFund = GetFundFromCode(availableFunds[i]);
				if (theFund[FUND_MANINDEX]==selectedFundMan && !excludedFund(theFund[FUND_CODE]))
				
				{
					selFund.options[selFund.length] = new Option(theFund[FUND_NAME],theFund[FUND_CODE]);
					if (theFund[FUND_CODE]==selectedFund) //keep the fund showing if its still valid
					{
						selectedFundIndex = selFund.length - 1;
					}
				}
			}
		}
		selFund.selectedIndex = selectedFundIndex;
}

function getFundFromIndex(lookUpIndex, startFrom)
{
	return arrFunds[lookUpIndex];
}



function getFundManagerFromIndex(lookUpIndex, startFrom)
{
	return arrFundManagers[lookUpIndex];
}
/*
function clearSelect(selectCtrl,prompt)
{
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null; 
	};
	
	selectCtrl.options[0] = new Option(prompt,-1);
	selectCtrl.options[1] = new Option("- - - - - - - - - - - - - - - - - - - - - - -",-1);
}
*/

//Fund Functions
function GetFundFromCode(code) {
    if (hashFundsByFundCode == null) {
        hashFundsByFundCode = new Object();
        for (var i = 0; i < arrFunds.length; i++) {
            hashFundsByFundCode[arrFunds[i][FUND_CODE]] = arrFunds[i];
        }
    }

    if (hashFundsByFundCode[code] != 'undefined') {
        return hashFundsByFundCode[code];
    }
   
	return false;
}

function IsAnIsa(fund)
{
	return (selectedProduct.toUpperCase().indexOf("ISA")>-1 || selectedProduct.toUpperCase().indexOf("PEP"))
}

function IsSasOrCash(fund)
{
	return (fund[FUND_CODE].toString().toUpperCase()==CASH_RESERVE_FUND_ID) ? "Yes" : "No" ;
}

function IsCashReserve(fund)
{
	//alert("fund[FUND_CODE].toUpperCase()='"+fund[FUND_CODE].toUpperCase()+"'\n"+(fund[FUND_CODE].toUpperCase()==CASH_RESERVE_FUND_ID));
	return (fund[FUND_CODE].toString().toUpperCase()==CASH_RESERVE_FUND_ID) ;
}

function IsAnIncomeFund(fund)
{
	return (fund[FUND_INCOME]===1);	
}

function getfundmanidfromfundcode(fundcode, arrFunds){
	debug("getfundmanidfromfundcode");
	var i, retval, blnStop = false;
	for(i=0;i<arrFunds.length && !blnStop;i++){
		if(arrFunds[i][5] == fundcode){
			retval = arrFunds[i][1];
			blnStop = true;
		}
	}
	return retval;
}

// checks to see if given fundid is in the excluded list.
function excludedFund(fundcode){
	var i, retval = false;
	if(arrFundsExclude != null){
		for(i=0; i < arrFundsExclude.length; i++){
			if(fundcode == arrFundsExclude[i]){
				retval = true;
			}
		}
	};
	return retval;
}







