﻿// JScript File
var TimeOut;
var LiveChartIndex = '';
function GetDataStockIndex() {
    var datetime = new Date();
    var urlGDT = "/DesktopModules/AG.StockThongKe/GetStockIndex.ashx?Time=" + datetime.getTime();

    $.ajax({
        type: "GET",
        url: urlGDT,
        //dataType: "json",
        success: function(result) {
            var date = new Date();
            if ((date.getHours() < 11 && date.getHours() > 8) || (date.getHours() == 8 && date.getMinutes > 30) && (date.getDay() != 6 && date.getDay() != 0)) {
                TimeOut = setTimeout('GetDataStockIndex()', 10000);
            }
            else {
                clearTimeout(TimeOut);
            }
            if (result != 'null') {
                LiveChartIndex = result;
                var data = new Array();
                data = result.split('|');

                var data1 = new Array();
                data1[0] = data[0];
                data1[1] = data[5];
                data1[2] = data[9];
                for (var i = 0; i < 3; i++) {
                    var data2 = new Array();
                    data2 = data1[i].split(';');
                    var index = data2[0];
                    var Change = data2[1];
                    var Exchange = data2[2];
                    var htmlOut = "";
                    htmlOut += "<table width=\"190px;\" cellpadding=\"1\" cellspacing=\"1\">";
                    htmlOut += "<tr style=\"height:20px;\">";
                    htmlOut += "<td style='font-weight:bold; font-family:tahoma; font-size:12px; color:#333;'>";
                    switch (i) {
                        case 0:
                            htmlOut += "HOSE:";
                            break;
                        case 1:
                            htmlOut += "HNX:";
                            break;
                        case 2:
                            htmlOut += "UPCOM:";
                            break;
                    }
                    htmlOut += "</td>";
                    if (Change > 0) {
                        htmlOut += "<td style='font-weight:bold; font-family:tahoma; font-size:12px; color:#0D9945;text-align:right; padding-right:20px;'>";
                        htmlOut += index;
                        htmlOut += "</td>";
                        htmlOut += "</tr>";
                        htmlOut += "<tr>";
                        htmlOut += "<td colspan='2' style='font-weight:bold; font-family:tahoma; font-size:12px; color:#0D9945;text-align:right; padding-right:20px;'>";
                        htmlOut += "<img src=\"/DesktopModules/AG.Stock/Images/ST_Len.gif\" style=\"margin-right:2px; width:19px; height:11px\">"
                        htmlOut += "(";
                        htmlOut += (formatNumber_input_decimal_format(Change, 2) + '&nbsp;&nbsp;' + formatNumber_input_decimal_format(Exchange, 2) + ' %');
                        htmlOut += ")";
                        htmlOut += "</td>";
                        htmlOut += "</tr>";
                    }
                    else if (Change == 0) {
                        htmlOut += "<td style='font-weight:bold; font-family:tahoma; font-size:12px; color:orange;text-align:right; padding-right:20px;'>";
                        htmlOut += index;
                        htmlOut += "</td>";
                        htmlOut += "</tr>";
                        htmlOut += "<tr>";
                        htmlOut += "<td colspan='2' style='font-weight:bold; font-family:tahoma; font-size:12px; color:orange;text-align:right; padding-right:20px;'>";
                        htmlOut += "■";
                        htmlOut += "(";
                        htmlOut += (formatNumber_input_decimal_format(Change, 2) + '&nbsp;&nbsp;' + formatNumber_input_decimal_format(Exchange, 2) + ' %');
                        htmlOut += ")";
                        htmlOut += "</td>";
                        htmlOut += "</tr>";
                    }
                    else {
                        htmlOut += "<td style='font-weight:bold; font-family:tahoma; font-size:12px; color:#F0323E;text-align:right; padding-right:20px;'>";
                        htmlOut += index;
                        htmlOut += "</td>";
                        htmlOut += "</tr>";
                        htmlOut += "<tr>";
                        htmlOut += "<td colspan='2' style='font-weight:bold; font-family:tahoma; font-size:12px; color:#F0323E; text-align:right;padding-right:20px;'>";
                        htmlOut += "<img src=\"/DesktopModules/AG.Stock/Images/ST_Xuong.gif\" style=\"margin-right:2px; width:19px; height:11px\">"
                        htmlOut += "(";
                        htmlOut += (formatNumber_input_decimal_format((Change), 2) + '&nbsp;&nbsp;');
                        htmlOut += (formatNumber_input_decimal_format((Exchange), 2) + ' %');
                        htmlOut += ")";
                        htmlOut += "</td>";
                    }
                    htmlOut += "</tr></table>";
                    switch (i) {
                        case 0:
                            $('#m_HOIndex').html(htmlOut);
                            break;
                        case 1:
                            $('#m_HAIndex').html(htmlOut);
                            break;
                        case 2:
                            $('#m_UPCOMIndex').html(htmlOut);
                            break;
                    }
                }
            }
            else {
                //alert('Khong lay duoc du lieu');
            }

        },
        error: function displayError(request, errorType, errorThrown) {
            try {
                //alert(errorType);
                if (errorType == 'timeout') {

                }
                if (errorType == 'error') {
                }
            }
            catch (err) {
            }
        }
    });
}


