﻿function UpcomStockListCallback(result, eventArgs)
{
//alert(result);
    CreateStockList(result);
}
// Create the header of page (VNIndex, Change, Market state ...)
function UpcomIndexCallback(result, eventArgs)
{
    try
    {
        if ((typeof(result)=="undefined") || (result==null) || (result=="") || (result=="undefined"))
            return;
        var a=result.split("|");
        var i=0;
        var tr=document.getElementById("trHeader");
        if (tr.cells.length==0)
        {
            for (i=0;i<10;i++)
                tr.insertCell(i);
            tr.cells[3].style.color="#00ff00";
            tr.cells[4].style.color="#ffff00";
            tr.cells[5].style.color="#ff0000";
            tr.cells[2].style.whiteSpace="nowrap";
        }
        tr.cells[0].innerHTML=a[0];
        tr.cells[1].innerHTML=a[2];
        tr.cells[2].innerHTML=GetChangeIndex(a[1],a[2]);
        tr.cells[1].style.color=tr.cells[2].style.color=GetIndexColor(a[2]-a[1]);
        
        var advances = get_cookie("up_advances");
        if (advances==null) advances=0;
        var nochange = get_cookie("up_nochange");
        if (nochange==null) nochange=0;
        var declines = get_cookie("up_declines");
        if (declines==null) declines=0;
        tr.cells[3].innerHTML=advances;
        tr.cells[4].innerHTML=nochange;
        tr.cells[5].innerHTML=declines;
        tr.cells[6].innerHTML=addCommas(a[4]);
        tr.cells[7].innerHTML=addCommas(a[5]);
        tr.cells[8].innerHTML=MarketState(a[9]);
        tr.cells[9].innerHTML=a[10];
    }
    catch(e)
    {}
}
function CreateUpcomPriceHeader()
{
    var h="";
    h+="<table class=Grd border=1 cellspacing=0 cellpadding=0>"

    h+="<tr class=RowHeader>";

	h+="<td colspan=2 rowspan=2 class=H1 height=41px>"+gText[0]+"</td>";

	h+="<th colspan=3 class=H2>"+gText[1]+"</th>";

	h+="<th colspan=6 class=H1>"+gText[6]+"</th>";
	h+="<th colspan=3 class=H2>"+gText[7]+"</th>";
	h+="<th colspan=6 class=H1>"+gText[8]+"</th>";
	
	h+="<th colspan=4 class=H2>"+gText[1]+"</th>";
//	h+="<th rowspan=2 class=H2>"+gText[3]+"</th>";
	h+="<th rowspan=2 class=H2 width="+GetWidth("th6")+">"+gText[3]+"</th>";
	
	h+="<th colspan=2 class=H2>"+gText[20]+"</th>";
    h+="</tr>";
    
    h+="<tr class=RowHeader>";

	h+="<th class=H2 width="+GetWidth("th2")+">"+gText[10]+"</th>";
	h+="<th class=H2 width="+GetWidth("th3")+">"+gText[11]+"</th>";
	h+="<th class=H2 width="+GetWidth("th4")+">"+gText[12]+"</th>";

	h+="<th class=H1 width="+GetWidth("th5")+">"+gText[2]+"<br/>3</th>";
	h+="<th class=H1 width="+GetWidth("th6")+">"+gText[1]+"<br/>3</th>";
	h+="<th class=H1 width="+GetWidth("th7")+">"+gText[2]+"<br/>2</th>";
	h+="<th class=H1 width="+GetWidth("th8")+">"+gText[1]+"<br/>2</th>";
	h+="<th class=H1 width="+GetWidth("th9")+">"+gText[2]+"<br/>1</th>";
	h+="<th class=H1 width="+GetWidth("th10")+">"+gText[1]+"<br/>1</th>";

	h+="<th class=H2 width="+GetWidth("th11")+">"+gText[14]+"</th>";
	h+="<th class=H2 width="+GetWidth("th12")+">"+gText[15]+"</th>";
	h+="<th class=H2 width="+GetWidth("th13")+">"+gText[16]+"</th>";

	h+="<th class=H1 width="+GetWidth("th14")+">"+gText[1]+"<br/>1</th>";
	h+="<th class=H1 width="+GetWidth("th15")+">"+gText[2]+"<br/>1</th>";
	h+="<th class=H1 width="+GetWidth("th16")+">"+gText[1]+"<br/>2</th>";
	h+="<th class=H1 width="+GetWidth("th17")+">"+gText[2]+"<br/>2</th>";
	h+="<th class=H1 width="+GetWidth("th18")+">"+gText[1]+"<br/>3</th>";
	h+="<th class=H1 width="+GetWidth("th19")+">"+gText[2]+"<br/>3</th>";

	h+="<th class=H2 width="+GetWidth("th20")+">"+gText[21]+"</th>";
	h+="<th class=H2 width="+GetWidth("th21")+">"+gText[22]+"</th>";
	h+="<th class=H2 width="+GetWidth("th22")+">"+gText[23]+"</th>";
	h+="<th class=H2 width="+GetWidth("th23")+">"+gText[13]+"</th>";
	
	h+="<th class=H2 width="+GetWidth("th24")+">"+gText[17]+"</th>";
	h+="<th class=H2 width="+GetWidth("th25")+">"+gText[18]+"</th>";
	
    h+="</tr>";
    h+="<tr height=1><td width="+GetWidth("th1")+"></td><td width="+GetWidth("thImg")+"></td><td colspan=25></td></tr>";
    
    h+="</table>";
    
    var HaHeader=document.getElementById("UpcomHeaderPrice");
    if (HaHeader)
    {
        HaHeader.innerHTML=h;
        HaHeader.className=browser;
        var HaPrice=document.getElementById("UpcomPrice");
        if (HaPrice)
        {
            HaHeader.style.width = (HaPrice.offsetWidth)+"px";
        }
    }
    return;
}
// Create the header of the list of prices
function UpcomPriceHeader()
{
    var h="";

    h+="<tr class=RowHeader>";
	h+="<td colspan=2 rowspan=2 class=H1 height=40px>"+gText[0]+"</td>";
	h+="<th colspan=3 class=H2>"+gText[1]+"</th>";

	h+="<th colspan=6 class=H1>"+gText[6]+"</th>";
	h+="<th colspan=3 class=H2>"+gText[7]+"</th>";
	h+="<th colspan=6 class=H1>"+gText[8]+"</th>";
	
	h+="<th colspan=4 class=H2>"+gText[1]+"</th>";
	h+="<th id=th6 rowspan=2 class=H2>"+gText[3]+"</th>";
	h+="<th colspan=2 class=H2>"+gText[20]+"</th>";
    h+="</tr>";
    
    h+="<tr class=RowHeader>";
	h+="<th id=th2 class=H2>"+gText[10]+"</th>";
	h+="<th id=th3 class=H2>"+gText[11]+"</th>";
	h+="<th id=th4 class=H2>"+gText[12]+"</th>";
	
	h+="<th id=th5 class=H1>"+gText[2]+"<br/>3</th>";
	h+="<th id=th6 class=H1>"+gText[1]+"<br/>3</th>";
	h+="<th id=th7 class=H1>"+gText[2]+"<br/>2</th>";
	h+="<th id=th8 class=H1>"+gText[1]+"<br/>2</th>";
	h+="<th id=th9 class=H1>"+gText[2]+"<br/>1</th>";
	h+="<th id=th10 class=H1>"+gText[1]+"<br/>1</th>";

	h+="<th id=th11 class=H2>"+gText[14]+"</th>";
	h+="<th id=th12 class=H2>"+gText[15]+"</th>";
	h+="<th id=th13 class=H2>"+gText[16]+"</th>";

	h+="<th id=th14 class=H1>"+gText[1]+"<br/>1</th>";
	h+="<th id=th15 class=H1>"+gText[2]+"<br/>1</th>";
	h+="<th id=th16 class=H1>"+gText[1]+"<br/>2</th>";
	h+="<th id=th17 class=H1>"+gText[2]+"<br/>2</th>";
	h+="<th id=th18 class=H1>"+gText[1]+"<br/>3</th>";
	h+="<th id=th19 class=H1>"+gText[2]+"<br/>3</th>";

	h+="<th id=th20 class=H2>"+gText[21]+"</th>";
	h+="<th id=th21 class=H2>"+gText[22]+"</th>";
	h+="<th id=th22 class=H2>"+gText[23]+"</th>";
	h+="<th id=th23 class=H2>"+gText[13]+"</th>";
	
	h+="<th id=th24 class=H2>"+gText[17]+"</th>";
	h+="<th id=th25 class=H2>"+gText[18]+"</th>";

    h+="</tr>";
    h+="<tr height=0><td id=th1></td><td id=thImg></td><td colspan=25></td></tr>";
    return h;    
}
// Create the list of prices
function UpcomPriceCallback(result,eventArgs)
{
    try
    {
        var LastValue=CheckChangeData(result);
        if (LastValue=="")
            return;
        DrawUpcomPrice(result, LastValue);
    }    
    catch(e)
    {}
}
function DrawUpcomPrice(result, LastValue)
{
    var selectedStocks=get_cookie(CookieStockName);
    
    var grid="<table class=Grd border=1 cellspacing=0 cellpadding=0>";
    grid=grid+UpcomPriceHeader();
    var grid1="";
    var grid2="";
    var line="";
    var rows=result.split("#");
    var old=LastValue.split("#");
    if (rows.length != old.length)
        old=rows;
    gAdvances=0;
    gDeclines=0;
    gNochange=0;
    
    //alert(result);
    for (var i=0; i<rows.length; i++)
    {
        var v="";
        var tr="<tr height=18px onmouseover=mover(this) onmouseout=mout(this)>";
        var a=rows[i].split("|");
        var b=old[i].split("|");
        // Stock symbol
        tr+="<td class=MaCK width=3% title="+GetTitle(a[0])+">"+a[0]+"</td>";
        //Image
        tr+="<td class=Img width=1%>"+Image(a[10],a[1])+"</td>";  
        // Gia TC
        tr+="<td class=TD3 width=4% style=\""+TC+"\">"+FormatNum(a[1])+"</td>";
        // Gia Tran
        tr+="<td class=TD3 width=4% style=\""+CE+"\">"+FormatNum(a[2])+"</td>";
        // Gia San
        tr+="<td class=TD4 width=4% style=\""+FL+"\">"+FormatNum(a[3])+"</td>";
        
        //KL mua 3
        tr+=KL("TD1","5%",a[4],b[4]);
        //Gia mua 3
        tr+=P("TD1","4%",a[5],a[1],a[2],a[3],b[5]);
        //KL mua 2
        tr+=KL("TD1","5%",a[6],b[6]);
        //Gia mua 2
        tr+=P("TD1","4%",a[7],a[1],a[2],a[3],b[7]);
        //KL mua 1
        tr+=KL("TD1","5%",a[8],b[8]);
        //Gia mua 1
        tr+=P("TD2","4%",a[9],a[1],a[2],a[3],b[9]);
        
        //Gia tam khop
        tr+=P("TD3","4%",a[10],a[1],a[2],a[3],b[10]);
        //Chenh lech
        tr+=DiffP("TD3","3%",a[10],a[1],a[2],a[3],b[10]);
        //KL tam khop
        tr+=KL("TD4","4%",a[11],b[11]);

        //Gia ban 1
        tr+=P("TD1","4%",a[12],a[1],a[2],a[3],b[12]);
        //KL ban 1
        tr+=KL("TD1","5%",a[13],b[13]);
        //Gia ban 2
        tr+=P("TD1","4%",a[14],a[1],a[2],a[3],b[14]);
        //KL ban 2
        tr+=KL("TD1","5%",a[15],b[15]);
        //Gia ban 3
        tr+=P("TD1","4%",a[16],a[1],a[2],a[3],b[16]);
        //KL ban 3
        tr+=KL("TD2","5%",a[17],b[17]);

        // Gia mo cua
        tr+=P("TD3","4%",a[18],a[1],a[2],a[3],a[18]);
        // Gia cao nhat
        tr+=P("TD3","4%",a[19],a[1],a[2],a[3],a[19]);
        // Gia thap nhat
        tr+=P("TD3","4%",a[20],a[1],a[2],a[3],a[20]);
        // Gia Trung binh
        tr+=P("TD4","4%",a[21],a[1],a[2],a[3],a[21]);
        
        // Tong KLTH
        tr+=KL("TD3","5%",a[22],b[22]);
        
        //Nuoc ngoai Mua - Ban
        tr+=KL("TD3","4%",a[23],b[23]);
        tr+=KL("TD4","4%",a[24],b[24]);
        
        tr+="</tr>";
        var isselected=0;
        if (selectedStocks!=null && selectedStocks!="undefined")
            if (selectedStocks.indexOf(a[0]) != -1)
                isselected=1;
        if (isselected==1)
            grid1=grid1+tr;
        else
            grid2=grid2+tr;
    }
    if (grid1 != "")
        line="<tr><td colspan=27 bgcolor=white height=1></td></tr>";
    grid=grid+grid1+line+grid2+"</table>";
    
    write_cookie("up_advances", gAdvances, 1);
    write_cookie("up_declines", gDeclines, 1);
    write_cookie("up_nochange", gNochange, 1);
    
    var RsltElem=document.getElementById("UpcomPrice");
    RsltElem.innerHTML=grid;
    
    CreateUpcomPriceHeader();
}

