dojo.require("dijit.form.Button");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.TitlePane");

function showPane() {
  dijit.byId('customizePane').show();
  //dijit.byId('customizeButton').setDisabled(1);
}

function savePrefs() {
 dijit.byId('customizePane').hide();
 
 funds = document.portfolioPrefs.elements;
 var fundList='';
 for  (i=0;i<funds.length;i++) {

   if(funds[i].checked) {
      if(fundList == '') {
        fundList = funds[i].value;
      }
      else {
        fundList += ","+funds[i].value;
      }    
   }
 }
 oldFunds = Get_Cookie('jwsFunds');
 if (oldFunds != fundList) {
   SetCookie('jwsFunds',fundList,30000);
   loadFunds();
 }
}

function cancelPrefs() {
 dijit.byId('customizePane').hide();
 setPrefs();	
}

function init()
{
  var customizeButton = dijit.byId('customizeButton');
  var saveButton = dijit.byId('saveButton');
  var cancelButton = dijit.byId('cancelButton');
  
  dojo.connect(customizeButton, 'onClick', 'showPane');
  dojo.connect(saveButton, 'onClick', 'savePrefs');
  dojo.connect(cancelButton, 'onClick', 'cancelPrefs');
  loadFunds();
  setPrefs();
}

function setPrefs () {
  var fundPrefs = Get_Cookie('jwsFunds');

  funds = document.portfolioPrefs.elements;
  for  (i=0;i<funds.length;i++) {
    funds[i].checked=0;
  }

  if(fundPrefs) {
    for  (i=0;i<funds.length;i++) {
      if(fundPrefs.indexOf(funds[i].value) != -1) {
         funds[i].checked=1;
      }
    }
  }  
}

dojo.addOnLoad(init);

function loadFunds() 
{
  var funds = Get_Cookie('jwsFunds');
  if(!funds || funds == '') {
      funds = '004,008';
  }  
  getDocument("/tools/nav/embed?style=nav_data&FundIDList="+funds);
}


function populateFundTracker(response) {

    date = response.getElementsByTagName('result')[0].getElementsByTagName('date')[0].firstChild.data;	
    showYTD = response.getElementsByTagName('result')[0].getElementsByTagName('ytdValid')[0].firstChild.data;	
    funds=response.getElementsByTagName('result')[0].getElementsByTagName('fundList')[0].getElementsByTagName('fund');


    var fundList = "<p><span class='fund'>Portfolio Watcher as of "+date+"</span></p>";

    fundList += "<table><tr valign='bottom' class='articleBody'>"
    fundList += "<td><img src='/images/shim.gif' height='1' width='75'></td>";
    fundList += "<td align='center'><img src='/images/shim.gif' height='1' width='40'><br><b>NAV</b></td>";
    fundList += "<td align='center'><img src='/images/shim.gif' height='1' width='40'><br><b>MOP</b></td>";
    fundList += "<td align='center'><img src='/images/shim.gif' height='1' width='40'><br><b>Daily<br>Change</b></td>";
    fundList += "<td align='center'><b>Standardized<br>Return</b></td></tr>";


    for (i = 0; i<funds.length; i++) {
        var name = unescape(funds[i].getAttribute('name'));

        var cls = funds[i].getAttribute('class');
        var cusip = funds[i].getAttribute('cusip');
        var symbol = funds[i].getAttribute('symbol');
        var fundID = funds[i].getAttribute('masterID');
        var nav = funds[i].getElementsByTagName('nav')[0].firstChild.data;
        var mop = funds[i].getElementsByTagName('mop')[0].firstChild.data;
        var dailyChange = funds[i].getElementsByTagName('dailyChange')[0].firstChild.data;

	fundList += "<tr class='articleBodySmall'>";
	fundList += "<td>"+name+" ("+symbol+")</td>";
	fundList += "<td align='center'>"+nav+"</td>";
	fundList += "<td align='center'>"+mop+"</td>";
	fundList += "<td align='center'>"+dailyChange+"</td>";
	fundList += "<td align='center'><a href='/fund/view/fund-"+fundID+"#returns' target='_new'>Click Here...</a></td>";
	fundList += "</tr>";

    }

    fundList += "</table>";

    document.getElementById('fundTrackerContent').innerHTML = fundList;
    showObject('fundTracker');
}




