﻿﻿﻿var sDisp = "";
var aDispList = new Array();
var sFilterOperandCore = "Core";
var sFilterOperatorCore = "";
var sFilterOperandProduct = "Product";
var sFilterOperatorProduct = "";
var iNameSort = 1;
var iSectorSort = 1;
var iActiveSort = 1;
var iCol = 0;
var iDispPanel = 0;
var iDispPanelPrevious = -1;
var sURL = "";

function el(elementId) { return document.getElementById(elementId); }

function fundSub() {

   var aSub = new Array();
   
   if (window.XMLHttpRequest) {
      xmlSubSet = new XMLHttpRequest();
   }
   else {// code for IE6, IE5
      xmlSubSet = new ActiveXObject("Microsoft.XMLHTTP");
   }

   var aLoc = location.href.split("/");
   sURL = aLoc[3];

   xmlSubSet.open("GET", "/" + aLoc[3] + "/resources/funds/subset.xml", false);
   xmlSubSet.send();
   xmlSubSetDoc = xmlSubSet.responseXML;

   var oTempSub = xmlSubSetDoc.getElementsByTagName("subset"); 

   for (var iSub = 0; iSub < oTempSub.length; iSub++) {
      aSub[iSub] = oTempSub[iSub].getElementsByTagName("name")[0].childNodes[0].nodeValue;
   }

   return aSub.sort()
}

function fundAll() {
   var aFunds = new Array();

   if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
   }
   else {// code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.open("GET", "/web_assets/funds/fundlist.xml", false);
   xmlhttp.send();
   xmlDoc = xmlhttp.responseXML;

   var oTempFund = xmlDoc.getElementsByTagName("fund");
   
   for (var iFunds = 0; iFunds < oTempFund.length; iFunds++) {
      aFunds[iFunds] = new Array();
      aFunds[iFunds][0] = oTempFund[iFunds].getElementsByTagName("name")[0].childNodes[0].nodeValue;
      //alert(oTempFund[iFunds].getElementsByTagName("name")[0].childNodes[0].nodeValue);
      aFunds[iFunds][1] = oTempFund[iFunds].getElementsByTagName("description")[0].childNodes[0].nodeValue;
      aFunds[iFunds][2] = oTempFund[iFunds].getElementsByTagName("details")[0].childNodes[0].nodeValue;
      aFunds[iFunds][3] = oTempFund[iFunds].getElementsByTagName("documentation")[0].childNodes[0].nodeValue;
      aFunds[iFunds][4] = oTempFund[iFunds].getElementsByTagName("sector")[0].childNodes[0].nodeValue;
      aFunds[iFunds][5] = oTempFund[iFunds].getElementsByTagName("core")[0].childNodes[0].nodeValue;
      aFunds[iFunds][6] = oTempFund[iFunds].getElementsByTagName("product")[0].childNodes[0].nodeValue;
      aFunds[iFunds][7] = oTempFund[iFunds].getElementsByTagName("active")[0].childNodes[0].nodeValue;
   }

   return aFunds.sort();
}

function createFund(name, description, details, documentation, sector, core, product, active) {
   this.Fundname = name;
   this.Description = description;
   this.Details = details;
   this.Documentation = documentation;
   this.Sector = sector;
   this.Core = core;
   this.Product = product;
   this.Active = active;

   //alert("this.Core = " + this.Core + "\nthis.Sector" + this.Sector);
}


function winnowFunds(aSubSet, aUniverse) {
   oDispList = new Array();

   var iUni = 0;
   for (var iSub = 0 ; iSub < aSubSet.length; iSub++) {
      for (; aUniverse[iUni][0] != aSubSet[iSub] ; iUni++) { } // iter through unmatching universe
      if (aUniverse[iUni][0] == aSubSet[iSub]) {
         oDispList[iSub] = new createFund(aUniverse[iUni][0], aUniverse[iUni][1], aUniverse[iUni][2], aUniverse[iUni][3], aUniverse[iUni][4], aUniverse[iUni][5], aUniverse[iUni][6], aUniverse[iUni][7]);
      }
   }

   return oDispList;
}

function getFunds() {
   // this has become the de-facto initilasation function

   var aSubSet = fundSub();
   var aUniverse = fundAll();

   aDispList = winnowFunds(aSubSet, aUniverse);
   //claw back some RAM
   aSubSet = "";
   aUniverse = "";
   sortNameDisp();

}

function sortNameDisp() {

   if(iNameSort) {
      aDispList.sort(sortName);
      el("nameImage").src = "/web_assets/funds/sortDown.gif";
      el("sectorImage").src = "/web_assets/funds/sortBlank.gif";
      el("activeImage").src = "/web_assets/funds/sortBlank.gif";
      iNameSort = 0;
   } else {
      aDispList.sort(sortNameInverse); 
      el("nameImage").src = "/web_assets/funds/sortUp.gif";
      el("sectorImage").src = "/web_assets/funds/sortBlank.gif";
      el("activeImage").src = "/web_assets/funds/sortBlank.gif";
      iNameSort = 1;
   }

    el("name").style.backgroundColor = "#ffffff";
    el("name").style.borderBottomColor = "#ffffff";
    el("sector").style.backgroundColor = sTabColor;
    el("sector").style.borderBottomColor = sBorderColor;
    el("active").style.backgroundColor = sTabColor;
    el("active").style.borderBottomColor = sBorderColor;

   showFunds();
}

function sortName(a, b) {
    var x = a.Fundname.toLowerCase();
    var y = b.Fundname.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
 }


function sortNameInverse(a, b) {
    var x = a.Fundname.toLowerCase();
    var y = b.Fundname.toLowerCase();
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
 }

function sortSectorDisp() {

   if(iSectorSort) {
      aDispList.sort(sortSector);
      el("nameImage").src = "/web_assets/funds/sortBlank.gif";
      el("sectorImage").src = "/web_assets/funds/sortDown.gif";
      el("activeImage").src = "/web_assets/funds/sortBlank.gif";
      iSectorSort = 0;
   } else {
      aDispList.sort(sortSectorInverse);
      el("nameImage").src = "/web_assets/funds/sortBlank.gif";
      el("sectorImage").src = "/web_assets/funds/sortUp.gif";
      el("activeImage").src = "/web_assets/funds/sortBlank.gif";
      iSectorSort = 1;
   }

    el("name").style.backgroundColor = sTabColor;
    el("name").style.borderBottomColor = sBorderColor;
    el("sector").style.backgroundColor = "#ffffff";
    el("sector").style.borderBottomColor = "#ffffff";
    el("active").style.backgroundColor = sTabColor;
    el("active").style.borderBottomColor = sBorderColor;

   showFunds();

}

function sortSector(a, b) {
   var x = a.Sector.toLowerCase();
   var y = b.Sector.toLowerCase();
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortSectorInverse(a, b) {
   var x = a.Sector.toLowerCase();
   var y = b.Sector.toLowerCase();
   return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}

function sortActiveDisp() {
      
   if(iActiveSort) {
      aDispList.sort(sortActive);
      el("nameImage").src = "/web_assets/funds/sortBlank.gif";
      el("sectorImage").src = "/web_assets/funds/sortBlank.gif";
      el("activeImage").src = "/web_assets/funds/sortDown.gif";
      iActiveSort = 0;
   } else {
      aDispList.sort(sortActiveInverse);
      el("nameImage").src = "/web_assets/funds/sortBlank.gif";
      el("sectorImage").src = "/web_assets/funds/sortBlank.gif";
      el("activeImage").src = "/web_assets/funds/sortUp.gif";
      iActiveSort = 1;
   }

   el("name").style.backgroundColor = sTabColor;
   el("name").style.borderBottomColor = sBorderColor;
   el("sector").style.backgroundColor = sTabColor;
   el("sector").style.borderBottomColor = sBorderColor;
   el("active").style.backgroundColor = "#ffffff";
   el("active").style.borderBottomColor = "#ffffff";

   showFunds();

}

function sortActive(a, b) {
   var x = a.Active.toLowerCase();
   var y = b.Active.toLowerCase();
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortActiveInverse(a, b) {
   var x = a.Active.toLowerCase();
   var y = b.Active.toLowerCase();
   return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}

function filterCore(sOperand, sOperator) {
   sFilterOperandCore = sOperand;
   sFilterOperatorCore = sOperator;
   showFunds();
}

function filterProduct(sOperand, sOperator) {
   sFilterOperandProduct = sOperand;
   sFilterOperatorProduct = sOperator;
   showFunds();
}


function showFunds() {

   var bFirstTime = true;
   var iFirstFund = 0; 

   sDisp = "<div id=\"fundNamesList\">";
   
   for (var iDisp = 0; iDisp < aDispList.length; iDisp++) {
      if ((aDispList[iDisp][sFilterOperandCore] == sFilterOperatorCore || sFilterOperatorCore == "") && (aDispList[iDisp][sFilterOperandProduct] == sFilterOperatorProduct || sFilterOperatorProduct == "")) {
         if(bFirstTime) {
            iFirstFund = iDisp;
            bFirstTime = false;
         }
         sDisp = sDisp + "<div class=\"fundSlot\" id=\"divFundSlot" + iDisp + "\">\n"
         sDisp = sDisp + "   <div class=\"fundName\" onclick=\"displayPanel(" + iDisp + ");\"><p>" + aDispList[iDisp].Fundname + "</p></div>\n";
         sDisp = sDisp + "</div>\n";
      }
   }
   el("fundDisp").innerHTML = sDisp + "</div>";
   
   iDispPanelPrevious = -1;
   if(!bFirstTime) {
      displayPanel(iFirstFund);
   } else {
      el("panelDisp").innerHTML = "<h3>No relevant funds</h3>";
   }
}

function colSet(col) {

   iCol = col;
   displayPanel(iDispPanel);


}

function displayPanel(panelId) {
   iDispPanel = panelId;
   var sPanelDisp = "";

   switch(iCol) {
      case 0:
        el("colDescription").style.backgroundColor = "#ffffff";
        el("colDescription").style.borderBottomColor = "#ffffff";
        el("colDetails").style.backgroundColor = sTabColor;
        el("colDetails").style.borderBottomColor = sBorderColor;
        el("colDocumentation").style.backgroundColor = sTabColor;
        el("colDocumentation").style.borderBottomColor = sBorderColor;
        sPanelDisp = sPanelDisp + "<div class=\"fundDescription\">" + aDispList[iDispPanel].Description + "</div>\n";
        break;

      case 1:
        el("colDescription").style.backgroundColor = sTabColor;
        el("colDescription").style.borderBottomColor = sBorderColor;
        el("colDetails").style.backgroundColor = "#ffffff";
        el("colDetails").style.borderBottomColor = "#ffffff";
        el("colDocumentation").style.backgroundColor = sTabColor;
        el("colDocumentation").style.borderBottomColor = sBorderColor;
        sPanelDisp = sPanelDisp + "<div class=\"fundDescription\">" + aDispList[iDispPanel].Details + "</div>\n";
        break;

      case 2:
        el("colDescription").style.backgroundColor = sTabColor;
        el("colDescription").style.borderBottomColor = sBorderColor;
        el("colDetails").style.backgroundColor = sTabColor;
        el("colDetails").style.borderBottomColor = sBorderColor;
        el("colDocumentation").style.backgroundColor = "#ffffff";
        el("colDocumentation").style.borderBottomColor = "#ffffff";
        sPanelDisp = sPanelDisp + "<div class=\"fundDescription\">" + aDispList[iDispPanel].Documentation + "</div>\n";
        break;
   }

   el("panelDisp").innerHTML = sPanelDisp;

   el("panelDisp").style.minHeight = el("fundDisp").offsetHeight + 20 + "px";

   if(iDispPanelPrevious != iDispPanel) {
      el("divFundSlot" + panelId).style.backgroundColor = "#ffffff";
      el("divFundSlot" + panelId).style.backgroundImage = "url()";
      el("divFundSlot" + panelId).style.cursor = 'default';
      if(iDispPanelPrevious != -1) {
         el("divFundSlot" + iDispPanelPrevious).style.cursor = 'pointer';
         el("divFundSlot" + iDispPanelPrevious).style.backgroundImage = "url('/" + sURL + "/resources/images/sideTab.gif')";
         el("divFundSlot" + iDispPanelPrevious).style.backgroundColor = sTabColor;
      }
      iDispPanelPrevious = iDispPanel;
   }

}


