FundCentre = function() {

}
FundCentre.App = {
    sortData : function(e,col,typ) {
        var sortType = e.sortType;
        var sortRow = ((typ == 'UT') ? FundCentre.Page.el('unitTrustSortRow') : FundCentre.Page.el('lifeFundSortRow'));

        //persist sort type (ASC/DESC) at the time of postback
        FundCentre.Page.createHidden('_sortType', sortType);
        //persist id of row related to sorting
        FundCentre.Page.createHidden('_sortRowId', sortRow.id);
        //persist cell index within sort row
        var iIndex = FundCentre.Page.cellIndex(e,sortRow);
        FundCentre.Page.createHidden('_cellIndex', iIndex);
        //persist sort column
        FundCentre.Page.createHidden('_sortColumn', col);
                
        //Invoke a button click to cause (updatepanel) post for sorting.
        var btn = ((typ == 'UT') ? FundCentre.Page.item('utSortBtn') : FundCentre.Page.item('lfSortBtn'));
        btn.click();
    },
    hiliteRow : function(row,color) {
        for (iCell = 0;iCell < row.cells.length;iCell++) {
            var cell = row.cells[iCell];
            cell.style.backgroundColor = color;
            cell.style.cursor = 'default';
        }        
    },
    hiliteSort : function(e) {
        if( !e.sortType ) {
            e.sortType = 'DESC';
        }
        var classes = [{desc:'hiliteSortDesc', asc:'hiliteSortAsc'},{desc:'sortDesc',asc:'sortAsc'}];
        var css = e.hilited ? classes[1]: classes[0];
        e.className = (e.sortType == 'DESC') ? css.desc : css.asc;
        e.hilited = !e.hilited;
    },
    resetSortDisplay : function() {
        var sortTypeEl = FundCentre.Page.el('_sortType');
        if (sortTypeEl) {
            //Assume all values are there if sort type is present.
            var sortType = sortTypeEl.value;
            var sortRowId = FundCentre.Page.el('_sortRowId').value;
            var sortRow = FundCentre.Page.el(sortRowId);
            var sortCellIndex = FundCentre.Page.el('_cellIndex').value;
            var sortCell = sortRow.cells[sortCellIndex];

            //Set sort type at page load.
            sortCell.sortType = sortType;
            //Swap sort type.
            if( sortCell.sortType ) {
                sortCell.sortType = ((sortCell.sortType == 'ASC') ? 'DESC' : 'ASC');
            } else {
                sortCell.sortType = 'DESC';
            }
            sortCell.title = ((sortCell.sortType == 'DESC') ? 'Sort descending' : 'Sort ascending');
            sortCell.className = (sortCell.sortType == 'DESC') ? 'sortDesc' : 'sortAsc';
        }
    },
    onFundCentreLoad : function() {
        var optionList = FundCentre.Page.item('opt');
        if (optionList) {
            if (optionList.addEventListener) {
                optionList.addEventListener('change', FundCentre.App.showProgress, false);
            } else if (optionList.attachEvent) {
                optionList.attachEvent('onchange', FundCentre.App.showProgress);
            }
        }
    },
    showProgress : function(e) {
        var optionList = FundCentre.Page.item('opt');
        var prog = FundCentre.Page.el('progress');
        if (optionList.selectedIndex > 0) {
            prog.style.display = 'inline';
        } else {
            prog.style.display = 'none';
        }
    }
}
FundCentre.Page = {
    item : function(key) {
        var id = this.itemId(key);
        return this.el(id);
    },
    itemId : function(key) {
        var controls = this._controlList();
        return eval('controls.' + key);
    },
    el : function(id) {
        return document.getElementById(id);
    },
    createHidden : function(key,value) {
        var hidden = this.el(key);
        if( !hidden ) {
            hidden = document.createElement('INPUT');
            hidden.type = 'hidden';
            hidden.id = key;
            hidden.name = key;
            document.forms[0].appendChild(hidden);    
        }
        hidden.value = value;
        return hidden;
    },
    cellIndex : function(e,row) {
        var iIndex = -1;
        for (iCell = 0; iCell < row.cells.length; iCell++) {
            var cell = row.cells[iCell];
            if (cell == e) {
                iIndex = iCell;
                break;
            }
        }
        return iIndex;
    },
    _controlList : function() {
        return {
                'opt':'ctl00_cpHolder_Content_FundOptions',    
                'utSortBtn':'ctl00_cpHolder_Content_UTSortButton',
                'lfSortBtn':'ctl00_cpHolder_Content_LFSortButton'
               };
    }
}
if (window.addEventListener) {
    window.addEventListener('load', FundCentre.App.onFundCentreLoad, false);
    window.addEventListener('load', FundCentre.App.resetSortDisplay, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', FundCentre.App.onFundCentreLoad);
    window.attachEvent('onload', FundCentre.App.resetSortDisplay);
}


