﻿// JScript File

var _timerMKT = null;
var _timerLiveSecSTAT = null;
var _timerLiveSecBenefit = null;
var iDot=0;
setInterval('TotalMKTData()', 15000);
setInterval('LiveSecSTAT()', 15000);
setInterval('LiveSecBenefit()', 15000);
//=======================================Phần hiện thông tin về trạng thái thị trường===============================

//Hàm này được gọi từ form
function LiveSecSTAT()
{
  if (proxies.IsActive())
  {
    _timerLiveSecSTAT = window.setTimeout(LiveSecSTAT, 300, "javascript");
    return;
  }
  //Lấy thông tin trạng thái trị trường
  proxies.HASTCStockWebServices.LiveSecurityStatus.func = myLiveSecStat;
  proxies.HASTCStockWebServices.LiveSecurityStatus();
}

function myLiveSecStat(retVal)
{
    myResultLiveSecStat(retVal);
}

function myResultLiveSecStat(Result)
{
    if(Result=='' || Result==null || Result=="")
    {
        return;
    }
    var TempResult = Result + '';
    //Mảng tạm một chiều, chứa thông tin của trạng thái thị trường
    aMarketSTAT = new Array();
    aMarketSTAT = Result.split("|");
    document.getElementById("MarketSTAT").innerHTML = '<span class="VNIndex">&nbsp;|&nbsp;' + aMarketSTAT[0] + '</span>';
    iDot = aMarketSTAT[1];
    if(iDot=='5')
    {
        document.getElementById("MarketStatus").innerHTML = 'Đóng cửa';
    }
    else
    {
        document.getElementById("MarketStatus").innerHTML = 'Giá khớp';
    }
}

//=======================================Phần hiện thông tin về thực hiện quyền===============================
//Hàm này được gọi từ form
function LiveSecBenefit()
{
  if (proxies.IsActive())
  {
    _timerLiveSecBenefit = window.setTimeout(LiveSecBenefit, 300, "javascript");
    return;
  }
  //Lấy thông tin trạng thái trị trường
  proxies.HASTCStockWebServices.LiveBenefit.func = myLiveSecBenefit;
  proxies.HASTCStockWebServices.LiveBenefit();
}

function myLiveSecBenefit(retVal)
{
    myResultLiveSecBenefit(retVal);
}

function myResultLiveSecBenefit(Result)
{
    if(Result=='' || Result==null || Result=="")
    {
        return;
    }
    var TempResult = Result + '';
    //Mảng tạm một chiều, chứa thông tin của trạng thái thị trường
    var aBenefits = new Array();
    aBenefits = Result.split("**");
    var strReturn = "";
    if(aBenefits.length>1)
    {
        for(i=0;i<aBenefits.length-1;i++)
        {
            var aBenefit = aBenefits[i].split("|");
            strReturn += '<span class="BenefitCompany">'+ aBenefit[0] + '</span><span class="BenefitStatus"> ' + aBenefit[1] + '</span>  ';
        }
        document.getElementById("lstBenefit").innerHTML = strReturn;
    }
}

//=======================================Phần hiện thông tin về toàn thị trường===============================

//Hàm này được gọi từ form
function TotalMKTData()
{
  if (proxies.IsActive())
  {
    _timerMKT = window.setTimeout(TotalMKTData, 300, "javascript");
    return;
  }
  //Lấy thông tin toàn thị trường
  proxies.HASTCStockWebServices.GetLiveTotalMKT.func = myRsTotalMKT;
  proxies.HASTCStockWebServices.GetLiveTotalMKT();
}

//Đây là hàm trung gian
function myRsTotalMKT(retVal)
{
    myResultTotalMKT(retVal);
}


//Hàm này thực sự sử lý chuỗi đầu vào để hiện thông tin lên form
function myResultTotalMKT(sTotalMKTresult)
{
    if ((sTotalMKTresult == null) || (sTotalMKTresult == ''))
    {
        return;
    }    
    var aLiveTotal = new Array();
    var sTempMKTRs = sTotalMKTresult + ''; //force a string
    aLiveTotal = sTempMKTRs.split("|"); //LiveTotal là mảng các chuỗi
    if (aLiveTotal.length > 0)
    {
        //Dot 1
        document.getElementById("HASTCIndex").innerHTML = GetTotalMKTColor(GetNumber(aLiveTotal[15],2),GetNumber(aLiveTotal[14],2)+" "+GetNumber(aLiveTotal[15],2)+" "+GetNumber(aLiveTotal[16],2)+"% ");
        document.getElementById("TotalDot1").innerHTML = addCommas(aLiveTotal[17])+" Tr ";
        if(document.getElementById("TotalShare1"))
        document.getElementById("TotalShare1").innerHTML = addCommas(aLiveTotal[18]);
    }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function GetNumber(dblNumber,intNum)
{
    return Math.round(dblNumber*Math.pow(10,intNum))/Math.pow(10,intNum);
}
function GetTotalMKTColor(Diff,Text)
{
    if(parseFloat(Diff)>0)
    {
        return '<span class="IndexInc">'+Text+'</span>';
    }
    else if(parseFloat(Diff)<0)
    {
        return '<span class="IndexDec">'+Text+'</span>';
    }
    else
    {
        return '<span>'+Text+'</span>';
    }
}

