/*
Functions for populating fund and currency dropdowns based on accounts selection in control panel on Accounts pages.
author: gerald.garcia@barclaysglobal.com
*/

function getAccountIndex(accountId) {
  for (var i = 0; i < accounts.length; i++) {
    if (accounts[i][0] == accountId) {
      return i;
    }
  }
  return -1;
}

function getFundIndex(fundId) {
  for (var i = 0; i < funds.length; i++) {
    if (funds[i][0] == fundId) {
      return i;
    }
  }
  return -1;
}

function getCurrencyIndex(currencyCd) {
  for (var i = 0; i < currencies.length; i++) {
    if (currencies[i][0] == currencyCd) {
      return i;
    }
  }
  return -1;
}

function selectOption(selObj, optVal) {
  for (var i = 0; i < selObj.length; i++) {
    if (selObj.options[i].value == optVal) {
      selObj.options[i].selected = true;
      break;
    }
  }
}

function selectAccount(clientAccountId) {
  var accountSelect = document.getElementById("accountSelect");
  selectOption(accountSelect, clientAccountId);
  accountSelected(accountSelect);  
}

function selectFund(assetReferenceId) {
  var fundSelect = document.getElementById("fundSelect");
  selectOption(fundSelect, assetReferenceId);
  fundSelected(fundSelect);  
}

function selectCurrency(currencyCd) {
  var currencySelect = document.getElementById("currencySelect");
  selectOption(currencySelect, currencyCd);
  currencySelected(currencySelect);  
}

function accountSelected(accountSelect) {
  var fundSelect     = document.getElementById("fundSelect");
  var currencySelect = document.getElementById("currencySelect");

  var selectedAccount  = accountSelect.options[accountSelect.selectedIndex].value;
  var selectedCurrency = currencySelect.options[currencySelect.selectedIndex].value;
  
  // refresh currency dropdown
  if (selectedAccount == "") {
    initSelect(currencySelect, currencies);    
    initSelect(fundSelect, funds);
  }
  else if (selectedAccount == "SELECTMULTIPLE") {
    initSelect(currencySelect, currenciesMultiple);    
    initSelect(fundSelect, fundsMultiple);
    if (accountSelect.selectedIndex == 2) {
      accountSelect.selectedIndex = 1;
    }
    openWindow("/accounts/accounts_selector.do", 600, 500, "accountsSelector", ",top=0,left=0");
  }
  else {
    // refresh fund dropdown
    fundSelect.length = 1;
    var fundIdx = 1;
    for (var i = 0; i < accountsFunds.length; i++) {
      if (accountsFunds[i][0] == selectedAccount) {
        fundSelect.options[fundIdx++] = new Option(funds[getFundIndex(accountsFunds[i][1])][1], accountsFunds[i][1]);
      }
    }
    
    // refresh currency dropdown
    currencySelect.length = 1;
    var currencyIdx = 1;
    for (var i = 0; i < accountsCurrencies.length; i++) {
      if (accountsCurrencies[i][0] == selectedAccount) {
        currencySelect.options[currencyIdx++] = new Option(currencies[getCurrencyIndex(accountsCurrencies[i][1])][1], accountsCurrencies[i][1]);
      }
    }
  }
  currencySelect.options[0].selected = true;
  fundSelect.options[0].selected     = true;

  manageSelects();
}

function fundSelected(fundSelect) {
  var accountSelect  = document.getElementById("accountSelect");
  var currencySelect = document.getElementById("currencySelect");
  
  var selectedAccount  = accountSelect.options[accountSelect.selectedIndex].value;
  var selectedFund     = fundSelect.options[fundSelect.selectedIndex].value;
  var selectedCurrency = currencySelect.options[currencySelect.selectedIndex].value;
  
  if (selectedFund != "" && selectedAccount == "") {
    accountSelect.length = 1;
    accountIdx = 1;
    for (var i = 0; i < fundsAccounts.length; i++) {
      if (fundsAccounts[i][0] == selectedFund) {
        accountSelect.options[accountIdx++] = new Option(accounts[getAccountIndex(fundsAccounts[i][1])][1], fundsAccounts[i][1]);
      }
    }

    accountSelect.options[0].selected = true;
  }

  if (selectedFund == "") {
    initSelect(accountSelect, accounts);
    initSelect(currencySelect, currencies);
  }
  else {
    currencySelect.length = 1;
  }
  manageSelects();
}

function currencySelected(currencySelect) {
  var accountSelect = document.getElementById("accountSelect");
  var fundSelect    = document.getElementById("fundSelect");

  var selectedAccount  = accountSelect.options[accountSelect.selectedIndex].value;
  var selectedFund     = fundSelect.options[fundSelect.selectedIndex].value;
  var selectedCurrency = currencySelect.options[currencySelect.selectedIndex].value;

  if (selectedAccount == "" && selectedCurrency != "") {
    accountSelect.length = 1;
    var accountIdx = 1;
    var accountIndex = -1;
    for (var i = 0; i < currenciesAccounts.length; i++) {
      if (currenciesAccounts[i][0] == selectedCurrency) {
        accountIndex = getAccountIndex(currenciesAccounts[i][1]);
        accountSelect.options[accountIdx++] = new Option(accounts[accountIndex][1], currenciesAccounts[i][1]);
      }
    }
    accountSelect.options[0].selected = true;
  }

  if (selectedAccount == "" && selectedCurrency == "") {
    initSelect(accountSelect, accounts);
  }

  manageSelects();
}

function initSelect(selObj, arrayObj) {
  selObj.length = 1;
  idx = 1;
  for (var i = 0; i < arrayObj.length; i++) {
    selObj.options[idx++] = new Option(arrayObj[i][1], arrayObj[i][0]);
  }
  selObj.options[0].selected = true;
}

function manageSelects() {
  var accountSelect  = document.getElementById("accountSelect");
  var fundSelect     = document.getElementById("fundSelect");
  var currencySelect = document.getElementById("currencySelect");
  
  var accounts   = document.getElementById("accounts");
  var funds      = document.getElementById("funds");
  var currencies = document.getElementById("currencies");
  
  var fundCurrencyLabel = document.getElementById("fundCurrencyLabel")
  
  if (accountSelect.length > 1) {
    accounts.disabled = false;
    accounts.style.display = "block";
    
    if (accountSelect.options[1].value != "SELECTMULTIPLE" && accountSelect.length > 2) {
      accountSelect.add(new Option("Select Multiple Accounts", "SELECTMULTIPLE"), 1);
    }
  }
  else {
    accounts.disabled = true;
    accounts.selectedIndex = 1;
    //accounts.style.display = "none";
  }

  if (fundSelect.length > 2) {
    funds.disabled = false;
    funds.style.display = "block";

    currencies.disabled = false;
    currencies.style.display = "block";
    fundCurrencyLabel.src = "/images/lbl_select_fund_or_currency.gif";
  }
  else {
    funds.disabled = true;
    //funds.style.display = "none";
    funds

    currencies.disabled = true;
    //currencies.style.display = "none";
    fundCurrencyLabel.src = "/images/lbl_select_fund.gif";
  }

  if (currencySelect.length > 2) {
    currencies.disabled = false;
    currencies.style.display = "block";
    fundCurrencyLabel.src = "/images/lbl_select_fund_or_currency.gif";
  }
  else {
    currencies.disabled = true;
    //currencies.style.display = "none";
    fundCurrencyLabel.src = "/images/lbl_select_fund.gif";
  }

  accountSelect.disabled  = accounts.disabled;
  fundSelect.disabled     = funds.disabled;
  currencySelect.disabled = currencies.disabled;
}
