﻿var timeoutID = 0;
var sortField = "ProfitLossPercentage";
var sortDirection = "Down";
var pageSize = 13;
var currentPageIndex = 1;
var totalPages = 1;
var oldHtml;
var cellIndexToCompare = 2; // column index representing symbol price for Watchlist table
function OnCompleted(jsonResult) {
    try {
        if (jsonResult.TotalRows > 0) {
            oldHtml = $get("dResultOpenPositions").cloneNode(true); // save current html before retrieving new one
            $get("dResultOpenPositions").innerHTML = jsonResult.Html;
            totalPages = Math.ceil(jsonResult.TotalRows / pageSize);
            buildPager("opFooter");
            flashTable(oldHtml, $get("dResultOpenPositions"));
        }
        else {
            //var tournament = document.getElementById(tournamentID);
            $get("dResultOpenPositions").innerHTML = "No Open Positions";
        }
    }
    catch (ex) { }
}

function OnError(e) {
}


//function getOpenPositions()
//{
//    try 
//    {   
//        var portfolio = document.getElementById(portfolioID); 
//        NTRN.UI.WallStreetSurvivor.WebServices.OpenPositionsService.GetOpenPositions(sortField, sortDirection, pageSize, currentPageIndex, portfolio.value,OnCompleted, OnError);
//    }
//    catch(e) {}
//    timeoutID = setTimeout("getOpenPositions()", timer);
//}

// Compare previous data with current to find which symbol had a price change
function flashTable(oldHtml, newHtml) {
    try {
        if (oldHtml.innerHTML != '') {
            var oldRows = oldHtml.getElementsByTagName("tr");
            var newRows = newHtml.getElementsByTagName("tr");
            for (var i = 1; i < oldRows.length - 2; i++) {
                for (var x = 1; x < newRows.length - 2; x++) {
                    if (oldRows[i].id == newRows[x].id) {
                        var oldPrice, newPrice;
                        if (navigator.appName == "Microsoft Internet Explorer") // IE
                        {
                            oldPrice = oldRows[i].childNodes[2].innerText;
                            newPrice = newRows[x].childNodes[2].innerText;
                        }
                        else // FF
                        {
                            oldPrice = oldRows[i].childNodes[5].innerHTML;
                            newPrice = newRows[x].childNodes[5].innerHTML;
                        }

                        if (oldPrice.substring(0, 1) == '$') oldPrice = oldPrice.substring(1);
                        if (newPrice.substring(0, 1) == '$') newPrice = newPrice.substring(1);

                        if (parseFloat(oldPrice) > parseFloat(newPrice)) {

                            newRows[x].className = "bg-open-positions-red";
                        }
                        else if (parseFloat(oldPrice) < parseFloat(newPrice)) {
                            newRows[x].className = "bg-open-positions-green";
                        }
                    }
                }
            }
        }
    }
    catch (e) { }

    setTimeout("resetColors()", 2000);
}

// Reset colors by change className for each Price Cell
function resetColors() {
    try {
        var rows = $get("dResultOpenPositions").getElementsByTagName("tr");
        for (var i = 1; i < rows.length - 2; i++) {
            if (i % 2 == 0) {
                rows[i].className = "alt";
            }
            else {
                rows[i].className = "bg-open-positions-white";
            }

        }
    }
    catch (e) { }
}

// Sorting Functions
function sortTable(field) {
    clearTimeout(timeoutID);
    sortField = field;
    sortDirection = sortDirection == "Up" ? "Down" : "Up";
    getOpenPositions();
}

// Paging Functions
function createPages() {
    var pagesTD = '';
    for (var i = 0; i <= totalPages - 1; i++) {
        pageIndex = i + 1;
        if (currentPageIndex == i + 1) {
            pagesTD += '<li class="first"><a class="current" style="cursor:pointer; " onclick="gotoPage_op(' + pageIndex + ');">' + pageIndex + '</a></li>';
        }
        else {
            pagesTD += '<li><a style="cursor:pointer; " onclick="gotoPage_op(' + pageIndex + ');">' + pageIndex + '</a></li>';
        }
    }
    return pagesTD;
}

function buildPager(footerID) {
    try {
        var footer = document.getElementById(footerID);
        if (!footer) return;
        //var div = document.createElement('div');
        footer.innerHTML = '<center><ul class="paging">' + createPages() + '</ul></center>';
        //footer.appendChild(div);
    }
    catch (e) { }
}

function gotoPage(pageIndexSelected) {
    clearTimeout(timeoutID);
    currentPageIndex = pageIndexSelected;
    getOpenPositions();
}

function gotoPrevious() {
    clearTimeout(timeoutID);
    currentPageIndex = currentPageIndex > 1 ? currentPageIndex - 1 : currentPageIndex;
    getOpenPositions();
}
function gotoNext() {
    clearTimeout(timeoutID);
    currentPageIndex = currentPageIndex < totalPages ? currentPageIndex + 1 : currentPageIndex;
    getOpenPositions();
}

function trade(symbol, quantity) {
    var tournament = document.getElementById(tournamentID);
    window.location.href = "../Trading/Trade.aspx?symbol=" + symbol + "&quantity=" + quantity + "&tournamentID=" + tournament.value;
}

function quote(symbol) {
    window.location.href = "/Public/Research/Quotes.aspx?symbol=" + symbol;
}
