﻿// JScript File
// realtime

function realtime(Tensan) {
    var Time = "";
    var ID = "", ID1 = "", ID2 = "";
      GetDataStockHa('Ho');
    ID =  setInterval(GetDataStockHa, 60000, 'Ho');
    $.cookie("SetHo", ID);
    $('#tabmain2').click(function() {
        GetDataStockHa('Ha');
        ID1 = setInterval(GetDataStockHa, 60000, 'Ha');
        if ($.cookie("SetUp") != "" || $.cookie("SetUp") != null)
            clearInterval($.cookie("SetUp"));
        if ($.cookie("SetHo") != "" || $.cookie("SetHo") != null)
            clearInterval($.cookie("SetHo"));
        $.cookie("SetHa", ID1);
    })
         $('#tabmain1').click(function() {
             GetDataStockHa('Ho');
             ID = setInterval(GetDataStockHa, 60000, 'Ho');

             if ($.cookie("SetUp") != "" || $.cookie("SetUp") != null)
                 clearInterval($.cookie("SetUp"));
             if ($.cookie("SetHa") != "" || $.cookie("SetHa") != null)
                 clearInterval($.cookie("SetHa"));
          $.cookie("SetHo", ID);
         })
         $('#tabmain3').click(function() 
       {
           clearInterval(ID);
             GetDataStockHa('Up');
             ID3 = setInterval(GetDataStockHa, 60000, 'Up');
             if ($.cookie("SetHa") != "" || $.cookie("SetHa") != null)
                 clearInterval($.cookie("SetHa"));
             if ($.cookie("SetHo") != "" || $.cookie("SetHo") != null)
                 clearInterval($.cookie("SetHo"));
             $.cookie("SetUp", ID3);
             })
}
var TimeOut;
function GetDataStockHa(Floor) {
    var urlGDT = "/DesktopModules/AG.StockThongKe/Backend/TOPCP.ashx?TopName=Home&Floor=" + Floor;
    $.get(urlGDT, function(Text) {
        var Data = Text.split("@");
        Fun_OutPutTopPricesAndVolume(Floor, Data[1], "InTop5Increase", "% Tăng giá", "Giá gần nhất", "Khối lượng", "Thay đổi");
        Fun_OutPutTopPricesAndVolume(Floor, Data[0], "InTop5Decrease", "% Giảm giá", "Giá gần nhất", "Khối lượng", "Thay đổi");
        Fun_OutPutTopPricesAndVolume(Floor, Data[2], "InTop5Trade", "% Tích cực", "Giá gần nhất", "Khối lượng", "Thay đổi");
        Fun_OutputTopForgein(Floor, Data[3], "InTop5Buy", "Mã CK", "Mua ròng(Mua-bán)", "Khối lượng", "Giá gần nhất", "Thay đổi");
        Fun_OutputTopForgein(Floor, Data[4], "InTop5Sell", "Mã CK", "Bán ròng(Bán-mua)", "Khối lượng", "Giá gần nhất", "Thay đổi");
    })
        
}
// Function truyen data hien thi web
function Fun_OutPutTopPricesAndVolume(NameFloor, Data,NameTop, Tittle1, Tittle2, Tittle3, Tittle4)
 {
    var htmlOut = "";
    var  Id = "AGStock_Stock" + NameFloor + NameTop;
    // tao 1 bien kiem tra dieu kien de hien thi data
    var check = false;
    // tao giao dien Title
    htmlOut += "<table width='100%' class='tblTop5' border='0' cellpadding='5' cellspacing='1'>";
    htmlOut += "<thead class=\"thead\">";
    htmlOut += "<tr class='trHeadTop5' height='40px'>";
    htmlOut += '<td  width = "150px">' + Tittle1 + "</td>";
    htmlOut += '<td  width = "180px">' + Tittle2 + "</td>";
    htmlOut += '<td  width = "180px">' + Tittle3 + "</td>";
    htmlOut += '<td> ' + Tittle4 + '</td>';
    htmlOut += '</tr>';
/********    truyen du lieu vao data table        *****/

    var DataResult = Data.split("|");
    var m_Dau = "";
    for (var i = 0; i < 5; i++) 
    {
        var DataValue = DataResult[i].split(";");
        switch (NameTop) {
            case "InTop5Increase":
                if (parseFloat(DataValue[3]) > 0)
                    check = true;
                break;
            case "InTop5Decrease":
                if (parseFloat(DataValue[3]) < 0)
                    check = true;
                break;
            case "InTop5Trade":
                if (parseFloat(DataValue[5]) > 0)
                    check = true;
                break;
        }
        // them dau +/ - vao phan tram thay doi va thay doi

        if (parseFloat(DataValue[3]) < 0)
            m_Dau = "";
        else
            m_Dau = "+";
        // if kiem tra ma dung thi hien thi du llieu co nguoc lai thi de trong
        htmlOut += "<tr class='trHeightTop5 even' height='25px'>";
        if (check == true) {
            htmlOut += "<td class='column1'><a class=\"astocksymbol\" style=\"color:" + DataValue[6] + "\" href='tabid/101/default.aspx#" + DataValue[0] + ",NiemYet_Default'>" + DataValue[0] + "</a></td>";
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + formatNumber_input_decimal_format_en(DataValue[2], 1) + "</td>";
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + formatNumber_input_decimal_format_en(DataValue[5], 0) + "</td>";
            var pt = parseFloat(parseFloat(DataValue[4]) * 100).toFixed(2);
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + m_Dau + "&nbsp; " + formatNumber_input_decimal_format_en(DataValue[3], 1) + "(" + m_Dau + formatNumber_input_decimal_format_en(pt, 2) + ")</td>";
        }
        else {
            htmlOut += "<td class='column1'>&nbsp;</td>";
            htmlOut += "<td >&nbsp;</td>";
            htmlOut += "<td >&nbsp;</td>";
            htmlOut += "<td > &nbsp;</td>";
        }
        check = false;
    }
    htmlOut += "</tr>";
    htmlOut += "</tbody></table>";
    $('#' + Id).html(htmlOut); 
}
function Fun_OutputTopForgein(NameFloor, Data, NameTop, Tittle1, Tittle2, Tittle3, Tittle4, Tittle5) {
    var htmlOut = "";
    // tao giao dien
    htmlOut += "<table width='100%' class='tblTop5' border='0' cellpadding='5' cellspacing='1'>";
    htmlOut += "<thead class=\"thead\">";
    htmlOut += "<tr class='trHeadTop5' height='40px'>";
    htmlOut += "<td width =\"50\">" + Tittle1 + "</td>";
    htmlOut += "<td width =\"200\">" + Tittle2 + "</td>";
    htmlOut += "<td width =\"100\">" + Tittle3 + "</td>";
    htmlOut += "<td width =\"140\">" + Tittle4 + "</td>";
    htmlOut += "<td>" + Tittle5 + "</td>";
    htmlOut += "</tr>";
    // truyen so lieu 
    var Id = "AGStock_Stock" + NameFloor + NameTop;
    // tao 1 bien kiem tra dieu kien de hien thi data
    var check = false;
    var DataResult = Data.split("|");
    var m_Dau = "";
    for (var i = 0; i < 5; i++) {
        var DataValue = DataResult[i].split(";");
        switch (NameTop)
         {
            case "InTop5Buy":
                if (parseFloat(DataValue[1]) < 0)
                    check = true;
                break;
            case "InTop5Sell":
                if (parseFloat(DataValue[1]) > 0)
                    check = true;
                break;
        }
        if (parseFloat(DataValue[4]) <= 0)
            m_Dau = "";
        else
            m_Dau = "+";
        // if kiem tra ma dung thi hien thi du llieu co nguoc lai thi de trong
        htmlOut += "<tr class='trHeightTop5 even' height='25px'>";
        if (check == true) {
            htmlOut += "<td class='column1'><a class=\"astocksymbol\" style=\"color:" + DataValue[6] + "\" href='tabid/101/default.aspx#" + DataValue[0] + ",NiemYet_Default'>" + DataValue[0] + "</a></td>";
            if (parseFloat(DataValue[1]) < 0)
                DataValue[1] = parseFloat(DataValue[1]) * (-1);
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + formatNumber_input_decimal_format_en(DataValue[1], 0) + "</td>";
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + formatNumber_input_decimal_format_en(DataValue[2], 0) + "</td>";
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + formatNumber_input_decimal_format_en(DataValue[3], 1) + "</td>";
            var pt = parseFloat(parseFloat(DataValue[5]) * 100).toFixed(2);
            htmlOut += "<td style=\"color:" + DataValue[6] + "\">" + m_Dau + "&nbsp; " + formatNumber_input_decimal_format_en(DataValue[4], 1) + "(" + m_Dau + formatNumber_input_decimal_format_en(pt, 2) + ")</td>";
        }
        else {
            htmlOut += "<td class='column1'>&nbsp;</td>";
            htmlOut += "<td >&nbsp;</td>";
            htmlOut += "<td >&nbsp;</td>";
            htmlOut += "<td > &nbsp;</td>";
            htmlOut += "<td > &nbsp;</td>";
        }
        check = false;
    }
    htmlOut += "</tr>";
    htmlOut += "</tbody></table>";
    $('#' + Id).html(htmlOut);
}

function AGStock_Display_Ha(tabHa, Floor) {
 
    var tabLeft = 'Stock'+ Floor + 'Top5L';
    var tabCenter = 'Stock' +  Floor +'Top5C';
    var tabRight = 'Stock' +  Floor + 'Top5R';
    var link = "linkTop5" + Floor;
    var top5Trade = 'AGStock_Stock' + Floor + 'InTop5Trade';
    var Top5Buy = 'AGStock_Stock' + Floor + 'InTop5Buy';
    var Top5Sell = 'AGStock_Stock' + Floor + 'InTop5Sell';
    var top5Increase = 'AGStock_Stock' + Floor + 'InTop5Increase';
    var top5Decrease = 'AGStock_Stock' + Floor + 'InTop5Decrease';
   
    for (var i = 1; i < 6; i++) {
        document.getElementById(tabLeft + i).className = "S_bgTabHoseL";
        document.getElementById(tabCenter + i).className = "S_bgTabHoseC";
        document.getElementById(tabRight + i).className = "S_bgTabHoseR";
        document.getElementById(link + i).className = "linkTop5HoN";
    }
    document.getElementById(tabLeft + tabHa).className = "S_bgTabHoseActiveL";
    document.getElementById(tabCenter + tabHa).className = "S_bgTabHoseActiveC";
    document.getElementById(tabRight + tabHa).className = "S_bgTabHoseActiveR";
    document.getElementById(link + tabHa).className = "linkTop5HoA";
    switch (tabHa) {
        case 1:
            $('#'+ top5Increase).show();
            $('#'+ top5Decrease).hide();
            $('#'+ top5Trade).hide();
            $('#'+ Top5Buy).hide();
            $('#'+ Top5Sell).hide();
            break;
        case 2:
            $('#'+ top5Increase).hide();
            $('#'+ top5Decrease).show();
            $('#'+ top5Trade).hide();
            $('#'+ Top5Buy).hide();
            $('#'+ Top5Sell).hide();
            break;
        case 3:
            $('#'+ top5Increase).hide();
            $('#'+ top5Decrease).hide();
            $('#'+ top5Trade).show();
            $('#'+ Top5Buy).hide();
            $('#'+ Top5Sell).hide();
            break;
        case 4:
            $('#'+ top5Increase).hide();
            $('#'+ top5Decrease).hide();
            $('#'+ top5Trade).hide();
            $('#'+ Top5Buy).show();
            $('#'+ Top5Sell).hide();
            break;
        case 5:
            $('#'+ top5Increase).hide();
            $('#'+ top5Decrease).hide();
            $('#'+ top5Trade).hide();
            $('#'+ Top5Buy).hide();
            $('#'+ Top5Sell).show();
            break;
    }
 

}

