﻿var gVN=new Array("Mã<br/>CK","Giá","KL","Tổng<br/>KLTH","Phiên 1","Phiên 2","Đặt mua","Giao dịch","Chào bán","Nước ngoài","TC","Trần","Sàn","TB","Giá<br/>TH","+/-<br/>Giá","KLTH","Mua","Bán","Ẩn các cột:","NN","Mở<br/>cửa","Cao<br/>nhất","Thấp<br/>nhất");
var gEN=new Array("Stock","Price","Vol.","Total<br/>Vol.","Session 1","Session 2","Bid","Trading","Offer","Foreign","Ref.","Ceil.","Flr.","Avr","Match<br/>Price","+/-<br/>Price","Match<br />Vol.","Buy","Sell","Hide columns:","Foreign","Open","High","Low");
var gMktVN=new Array("Đóng cửa","Mở cửa","KL định kỳ - Phiên 1","KL liên tục","KL định kỳ - Phiên 3","Giao dịch thoả thuận","Tạm ngừng giao dịch");
var gMktEN=new Array("Close","Open","Opening Auction - Session 1","Order Matching - Session 2","Closing Auction - Session 3","Put-through","Suspend trading");

var CE="color:#00ff00";
var FL="color:#ff0000";
var TC="color:#ffff00";
var UP="color:#00ff00";
var DW="color:#ff0000";
var NO="color:#ffffff";
var CH="background-color:#666666;\" bgcolor=#666666";

var intervalIndex = 0;
var intervalPrice = 0;

var gAdvances=0, gDeclines=0, gNochange=0;

var offsetHeight=0;
var offsetLeft="0px";
var browser="IE";
var language=0;
var gText;
var gMkt;
function GetValues()
{
    var lang=get_cookie("LANGUAGE");
    if (lang=="en-US")
    {
        language=1;
        if (CookieStockName=="SSI.BOND")
            gText=gBondEN;
        else if (CookieStockName=="SSI.REPOS")
            gText=gReposEN;
        else
            gText=gEN;
        gMkt=gMktEN;
    }
    else
    {
        language=0;
        if (CookieStockName=="SSI.BOND")
            gText=gBondVN;
        else if (CookieStockName=="SSI.REPOS")
            gText=gReposVN;
        else
            gText=gVN;
        gMkt=gMktVN;
    }
    if (navigator.userAgent.indexOf("Firefox")!=-1)
    {
        offsetHeight = 16;
        offsetLeft = "0px";
        browser = "FF"
    }
    else if (navigator.userAgent.indexOf("Opera")!=-1)
    {
        offsetHeight = 16;
        offsetLeft = "2px";
        browser = "OP";
    }
    else
    {
        offsetLeft = "2px";
    }
}
function doShow()
{
    document.getElementById("StockList").style.display="inline";
}
function doHide()
{
    document.getElementById("StockList").style.display="none";
    DrawPrice(CookieStockName);
}
function CreateStockList(result)
{
    try
    {
        if ((typeof(result)=="undefined") || (result==null) || (result=="") || (result=="undefined"))
            return;
        var cookiename=CookieStockName;
        var stocks=result.split("#");
        var selectedStocks=get_cookie(cookiename);
        var brow=false;
        var table="<table style=\"color:White\">";
        
        for (var i=0; i<stocks.length; i++)
        {
            var tr="";
            var s = stocks[i].split("|");
            var isselected=0;
            if (selectedStocks!=null && selectedStocks!="undefined")
                if (selectedStocks.indexOf(s[0]) != -1)
                    isselected=1;
            var rowcount=12;
            if ((i/rowcount)==Math.round(i/rowcount))
            {
                tr+="<tr>";
                brow=true;
            }
            tr+=CreateCheckBox(stocks[i],isselected,cookiename);
            if (((i+1)/rowcount)==Math.round((i+1)/rowcount))
            {
                tr+="</tr>";
                brow=false;
            }
            table=table+tr;
        }
        if (brow)
            table=table+"</tr>";
        if (table != "")
            table=table+"</table>";
        var StocksList=document.getElementById("divStockList");
        StocksList.innerHTML=table;
    }
    catch(e)
    {}
}
function CreateCheckBox(stock,selected)
{
    var s=stock.split("|");
    var td="<td><input type=checkbox id="+s[0]+" title='"+s[1]+"'";
    td=td+" onclick=\"StockClick(this)\"";
    if (selected==1)
        td=td+" checked=true style=\"background-color:red\"";
    td=td+" />"+s[0]+"</td>";
    return td;
}
function StockClick(chk)
{
    change_cookie(chk,CookieStockName);
    //DrawPrice(CookieStockName);
}
function SetHideColumn(col,text,cols,cookiename)
{
    var selected=0;
    if (cols!=null && cols!="undefined")
        if (cols.indexOf(col) != -1)
            selected=1;
    
    var o="<input type=checkbox id="+col;
    o=o+" onclick=\"HideColumn(this,'"+cookiename+"')\"";
    if (selected==1)
        o=o+"checked=true style=\"background-color:red\"";
    o=o+">"+text+"&nbsp;&nbsp;&nbsp;</>";
    return o;
}
function HideColumn(chk,cookie_name)
{
    change_cookie(chk,cookie_name);
    //DrawPrice(cookie_name);
}
function GetColor(Price,PriceTC,Ceiling,Floor)
{
    if (Price==null || Price=="undefined" || Price=="")
        return NO;
    if (PriceTC==0 && Ceiling==0 && Floor==0)
        return UP;
    var nPrice=new Number(Price);
    if (nPrice <= Floor)
        return FL;
    else if (nPrice >= Ceiling)
        return CE;
    else if ((nPrice > Floor) && (nPrice < PriceTC))
        return DW;
    else if (nPrice==PriceTC)
        return TC;
    else if ((nPrice > PriceTC) && (nPrice < Ceiling))
        return UP;
    else
        return NO;
}
function GetColorAT(Price,PriceTC,Ceiling,Floor,ProjectOpen)
{
    if (Price==null || Price=="undefined" || Price=="")
        return NO;
    if (PriceTC==0 && Ceiling==0 && Floor==0)
        return UP;
    var nPrice=0;
    if (Price=="ATO" || Price=="ATC")
    {
        if (ProjectOpen==null || ProjectOpen=="undefined" || ProjectOpen=="")
            return NO;
        nPrice=new Number(ProjectOpen);
    }
    else
        nPrice=new Number(Price);
    if (nPrice <= Floor)
        return FL;
    else if (nPrice >= Ceiling)
        return CE;
    else if ((nPrice > Floor) && (nPrice < PriceTC))
        return DW;
    else if (nPrice==PriceTC)
        return TC;
    else if ((nPrice > PriceTC) && (nPrice < Ceiling))
        return UP;
    else
        return NO;
}
function PAT(c,w,Price,PriceTC,Ceiling,Floor,ProjectOpen,OldPrice)
{
    var td="<td class="+c+" width="+w+" style=\"";
    td+=GetColorAT(Price,PriceTC,Ceiling,Floor,ProjectOpen);
    if (Price!=OldPrice)
        td+=";"+CH;
    else
        td+="\"";
    td+=">"+FormatNum(Price)+"</td>";
    return td;
}
function P(c,w,Price,PriceTC,Ceiling,Floor,OldPrice)
{
    var td="<td class="+c+" width="+w+" style=\"";
    td+=GetColor(Price,PriceTC,Ceiling,Floor);
    if (Price!=OldPrice)
        td+=";"+CH;
    else
        td+="\"";
    td+=">"+FormatNum(Price)+"</td>";
    return td;
}
function DiffP(c,w,Price,PriceTC,Ceiling,Floor,OldPrice)
{
    var nPrice = Diff(Price,PriceTC);
        
    var td="<td class="+c+" width="+w+" nowrap=true style=\"";
    td+=GetColor(Price,PriceTC,Ceiling,Floor);
    if (Price!=OldPrice)
        td+=";"+CH;
    else
        td+="\"";
    td+=">"+nPrice+"</td>";
    return td;
}
function KL(c,w,v,o)
{
    var td="<td class="+c+" width="+w;
    if (v!=o)
        td+=" style=\""+CH;
    td+=">"+v+"</td>";
    return td;
}
function KLHA(c,w,v,o)
{
    var td="<td class="+c+" width="+w;
    if (v!=o)
        td+=" style=\""+CH;
    if (v!=null && v!="" && v!="undefined")
        v=v*100;
    td+=">"+v+"</td>";
    return td;
}
function Image(Price,PriceTC)
{
    var path="<img src=App_Themes/Default/Images/";
    if (Price==""||Price=="0")
    {
        path+="equal.gif />";
    }
    else if (PriceTC==""||PriceTC=="0")
    {
        path+="up.gif />";
        gAdvances += 1;
    }
    else if (new Number(Price)>new Number(PriceTC))
    {
        path+="up.gif />";
        gAdvances += 1;
    }
    else if (new Number(Price)<new Number(PriceTC))
    {
        path+="down.gif />";
        gDeclines += 1;
    }
    else        
    {
        path+="equal.gif />";   
        gNochange += 1;     
    }
    return path;
}
function GetIndexColor(color)
{
    if (color > 0)
    {
        return "#00ff00";
    }
    if (color < 0)
    {
        return "#ff0000";
    }
    return "#ffff00";
}
function mover(tr)
{
    tr.style.backgroundColor = "#666666";
    for (i=0; i<tr.cells.length; i++)
    {
        tr.cells[i].style.backgroundColor = "#666666";
    }
}
function mout(tr)
{
    tr.style.backgroundColor = "transparent";
    for (i=0; i<tr.cells.length; i++)
    {
        tr.cells[i].style.backgroundColor = "";
    }
}
function GetWidth(id)
{
    try
    {
        var th = document.getElementById(id);
        var w = th.offsetWidth;
//        if (browser=="IE" && id=="th1")
//            w+=2;
        return w + "px";
    }
    catch(e)
    {
        //alert("GetWidth " + id + ": " + e.message);
    }
}

function OnScroll(id, top)
{
    var stock = document.getElementById("StockList");
    if (stock)
    {
        if (stock.style.display == "" || stock.style.display == "inline")
        {
            top += stock.offsetHeight;
        }
    }
    var header = document.getElementById(id);
    if (header)
    {
        if (document.documentElement.scrollTop > top)
        {
            header.style.display = "inline";
        }
        else
        {
            header.style.display = "none";
        }
    }
    return;
}
function GetTitle(id)
{
    try
    {
        var chk = document.getElementById(id);
        return "'" + chk.title + "'";
    }
    catch(e)
    {
    }
    return "";
}
function DrawPrice(cookie_name)
{
    var divLastValue = document.getElementById("LastValue");
    if ((typeof(divLastValue) != "undefined") && (divLastValue != null) && (divLastValue != "undefined"))
    {
        var LastValue = divLastValue.innerText;
        if (cookie_name == 'SSI.HOSE' || cookie_name == 'HO.COLUMN')
            DrawHoPrice(LastValue, LastValue);
        else if (cookie_name == 'SSI.HASTC' || cookie_name == 'HA.COLUMN')
            DrawHaPrice(LastValue, LastValue);
        else if (cookie_name == 'SSI.UPCOM')
            DrawUpcomPrice(LastValue, LastValue);
    }
}
function ClearSelectedStocks()
{
    clearAll(CookieStockName);
    //DrawPrice(CookieStockName);
}
function PopUpSSIWeb()
{
    var w = window.open('http://www.ssi.com.vn/');
    w.focus();
}
function MarketState(x)
{
    var s = gMkt[0];
    if (x=="1" || x=="True" || x=="5")
        s=gMkt[1];
    else if (x=="P")
        s=gMkt[2];
    else if (x=="O")
        s=gMkt[3];
    else if (x=="A")
        s=gMkt[4];
    else if (x=="C")
        s=gMkt[5];
    else if (x=="10")
        s=gMkt[6];
    
    return s;
}
function GetChangeIndex(i1,i2)
{
    try
    {
        var changeIndex = (parseFloat(i2-i1)).toFixed(2);
        var changePercent = (parseFloat((changeIndex * 100)/i1)).toFixed(2);
        return changeIndex + " (" + changePercent + " %)";
    }
    catch (e)
    {
        //alert(e.message);
    }
    return "";
}
