﻿// JScript File

var _timerMKT = null;
var _timerLiveSecSTAT = null;
var _timerLiveSecBenefit = null;
var iDot=0;
setInterval('TotalMKTData()', 15000);
setInterval('LiveSecSTAT()', 30000);
setInterval('LiveSecBenefit()', 150000);
//=======================================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.StockWebServices.LiveSecurityStatus.func = myLiveSecStat;
  proxies.StockWebServices.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 = aMarketSTAT[0];
    iDot = aMarketSTAT[1];
    if(iDot=='P' || iDot=='A')
    {
        document.getElementById("MarketStatus").innerHTML = 'Dự kiến';
    }    
    else if(iDot=='C' || iDot=='K')
    {
        document.getElementById("MarketStatus").innerHTML = 'Đóng cửa';
    }
    else// if(iDot=='O')
    {
        document.getElementById("MarketStatus").innerHTML = 'Gía 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.StockWebServices.LiveBenefit.func = myLiveSecBenefit;
  proxies.StockWebServices.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.StockWebServices.GetLiveTotalMKT.func = myRsTotalMKT;
  proxies.StockWebServices.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)
{
//    var Ngay=1;
//    var VNDot1=2,DiffDot1=3,DiffRateDot1=4,TotalDot1=5,TotalShareDot1=6;
//    var VNDot2=7,DiffDot2=8,DiffRateDot2=9,TotalDot2=10,TotalShareDot2=11;
//    var VNDot3=12,DiffDot3=13,DiffRateDot3=14,TotalDot3=15,TotalShareDot3=16;
    //2|8/22/2007 12:00:00 AM|898.62|2.9|0.0032271705503995|125259|1495760|896.35|0.63|0.000702850449043343|307727|3551950|892.07|-3.65|-0.00409160716087303|545790|6263320|0|7
    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("VNDot1").innerHTML = GetTotalMKTColor(GetNumber(aLiveTotal[3],2),aLiveTotal[2]+" "+GetNumber(aLiveTotal[3],2)+" "+GetNumber(aLiveTotal[4]*100,2)+"% ");
        document.getElementById("TotalDot1").innerHTML = addCommas(aLiveTotal[5])+"Tr_ ";
        if(document.getElementById("TotalShareDot1"))
        document.getElementById("TotalShareDot1").innerHTML = addCommas(aLiveTotal[6]);
        
        //Dot 2
        document.getElementById("VNDot2").innerHTML = GetTotalMKTColor(GetNumber(aLiveTotal[9],2),aLiveTotal[8]+" "+GetNumber(aLiveTotal[9],2)+" "+GetNumber(aLiveTotal[10]*100,2)+"% ");
        document.getElementById("TotalDot2").innerHTML = addCommas(aLiveTotal[11])+"Tr_ ";
        if(document.getElementById("TotalShareDot2"))
        document.getElementById("TotalShareDot2").innerHTML = addCommas(aLiveTotal[12]);
        
        //Dot 3
        document.getElementById("VNDot3").innerHTML = GetTotalMKTColor(GetNumber(aLiveTotal[15],2),aLiveTotal[14]+" "+GetNumber(aLiveTotal[15],2)+" "+GetNumber(aLiveTotal[16]*100,2)+"% ");
        document.getElementById("TotalDot3").innerHTML = addCommas(aLiveTotal[17])+"Tr ";
        if(document.getElementById("TotalShareDot3"))
        document.getElementById("TotalShareDot3").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)
{
    if(dblNumber>0)
        return "+"+Math.round(dblNumber*Math.pow(10,intNum))/Math.pow(10,intNum);
    else
        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>';
    }
}
