﻿function LoadImage(id, src) {
    id.src = src;
    id.onerror = null;
}

function adjustHeights() {    
    var max = 0;
    var len;   
    for (var i = 0; i < arguments.length; i++) {       
        len = $('#' + arguments[i]).outerHeight();       
        if (len > max) { max = len; }
    }
    var pad = 0;
    var bor = 0;
    for (var i = 0; i < arguments.length; i++) {        
        pad = parseInt($('#' + arguments[i]).css("padding-top"), 10) + parseInt($('#' + arguments[i]).css("padding-bottom"), 10);      
        $('#' + arguments[i]).height(max - pad);
    }
}


function adjustHeightsHome() {
    var max = 0;
    var index = 0;
    var len;
    for (var i = 0; i < arguments.length; i++) {
        len = $('#' + arguments[i]).outerHeight();
        if (len > max) { max = len;index = i; }
    }
    var pad = 0;
    var bor = 0;
    for (var i = 0; i < arguments.length; i++) {
        pad = parseInt($('#' + arguments[i]).css("padding-top"), 10) + parseInt($('#' + arguments[i]).css("padding-bottom"), 10);
        $('#' + arguments[i]).height(max - pad);
    }
    if (index != 2) { $('#tcty').height($('#home3').height() - 257); }
}


var offsetfromcursorX = 12 //Customize x offset of tooltip
var offsetfromcursorY = 10 //Customize y offset of tooltip

var offsetdivfrompointerX = 10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY = 14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="gafin_tip"></div>') //write out tooltip DIV
document.write('<img id="gafin_tip_pointer" style="display:none;" src="">') //write out pointer image

var ie = document.all
var ns6 = document.getElementById && !document.all
var enabletip = false
if (ie || ns6)
    var tipobj = document.all ? document.all["gafin_tip"] : document.getElementById ? document.getElementById("gafin_tip") : ""

function ietruebody() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}
function ddrivetip(title, images, sapo, e) {
    e.title = '';

    if (ns6 || ie) {
        tipobj.style.width = "300px"

        if (typeof thecolor != "undefined" && thecolor != "") tipobj.style.backgroundColor = "";

        tipobj.innerHTML = '<div class="contentip"><h3>' + title + '</h3><p>' + sapo + '</p></div>';

        enabletip = true
        return false;
    }
}

function positiontip(e) {
    if (enabletip) {
        var nondefaultpos = false
        var curX = (ns6) ? e.pageX : event.clientX + ietruebody().scrollLeft;
        var curY = (ns6) ? e.pageY : event.clientY + ietruebody().scrollTop;
        //Find out how close the mouse is to the corner of the window
        var winwidth = ie && !window.opera ? ietruebody().clientWidth : window.innerWidth - 20
        var winheight = ie && !window.opera ? ietruebody().clientHeight : window.innerHeight - 20

        var rightedge = ie && !window.opera ? winwidth - event.clientX - offsetfromcursorX : winwidth - e.clientX - offsetfromcursorX
        var bottomedge = ie && !window.opera ? winheight - event.clientY - offsetfromcursorY : winheight - e.clientY - offsetfromcursorY

        var leftedge = (offsetfromcursorX < 0) ? offsetfromcursorX * (-1) : -1000

        //if the horizontal distance isn't enough to accomodate the width of the context menu
        if (rightedge < tipobj.offsetWidth) {
            //move the horizontal position of the menu to the left by it's width
            tipobj.style.left = curX - tipobj.offsetWidth + "px"
            nondefaultpos = true
        }
        else if (curX < leftedge)
            tipobj.style.left = "5px"
        else {
            //position the horizontal position of the menu where the mouse is positioned
            tipobj.style.left = curX + offsetfromcursorX - offsetdivfrompointerX + "px"
            // pointerobj.style.left = curX + offsetfromcursorX + "px"
        }

        //same concept with the vertical position
        if (bottomedge < tipobj.offsetHeight) {
            tipobj.style.top = curY - tipobj.offsetHeight - offsetfromcursorY + "px"
            nondefaultpos = true;
        }
        else {
            tipobj.style.top = curY + offsetfromcursorY + offsetdivfrompointerY + "px"
            //pointerobj.style.top = curY + offsetfromcursorY + "px";
        }
        tipobj.style.visibility = "visible"

    }
}
function hideddrivetip() {
    if (ns6 || ie) {
        enabletip = false;
        tipobj.style.visibility = "hidden";
        tipobj.style.left = "-1000px";
        tipobj.style.backgroundColor = '';
        tipobj.style.width = '';
    }
}
document.onmousemove = positiontip;




function ShowExchange() {
    var strOut = "";
    var url = "http://data3.gafin.net/iRequest.ashx?jsoncallback=?&t=3";
    $.getJSON(url, function (data) {
        var alt = true;
        strOut = strOut.concat(" <div class=\"thead\"> <div class=\"a\"> NT </div> <div class=\"b\"> Mua TM </div> <div class=\"c\"> Mua CK </div> <div class=\"d\"> Bán </div> </div>");
        $.each(data, function (i, item) {

            strOut = strOut.concat("<div class=\"" + (alt ? "itemalter" : "item") + "\">");
            strOut = strOut.concat("    <div class=\"a\">").concat(data[i].Symbol).concat("</div>");
            strOut = strOut.concat("    <div class=\"b\">").concat(objLib.FormatNumber(data[i].MuaTM, true)).concat("</div>");
            strOut = strOut.concat("    <div class=\"c\">").concat(objLib.FormatNumber(data[i].MuaCK, true)).concat("</div>");
            strOut = strOut.concat("    <div class=\"d\">").concat(objLib.FormatNumber(data[i].Ban, true)).concat("</div>");
            strOut = strOut.concat("</div>");
            alt = !alt;
        });
        $("#divExchange").html(strOut);
        $("#divDateExchange").html(data[0].DateUpdate);
    });
}

function ShowGold() {
    var strOut = "";
    var url = "http://data3.gafin.net/iRequest.ashx?jsoncallback=?&t=6";
    $.getJSON(url, function (data) {
        var alt = true;
        strOut = strOut.concat(" <div class=\"thead\"> <div class=\"a\"> Vàng </div> <div class=\"b\"> Mua vào </div> <div class=\"c\"> Bán ra </div></div> ");
        $.each(data, function (i, item) {

            strOut = strOut.concat("<div class=\"" + (alt ? "itemalter" : "item") + "\">");
            strOut = strOut.concat("    <div class=\"a\">").concat(data[i].Symbol).concat("</div>");
            strOut = strOut.concat("    <div class=\"b\">").concat(objLib.FormatNumber(data[i].MuaTM, true)).concat("</div>");
            strOut = strOut.concat("    <div class=\"c\">").concat(objLib.FormatNumber(data[i].Ban, true)).concat("</div>");
            strOut = strOut.concat("</div>");
            alt = !alt;
        });
        $("#divGold").html(strOut);
        $("#divDateGold").html(data[0].DateUpdate);
    });
}

function ShowWorld() {
    var strOut = "";
    var url = "http://data3.gafin.net/iRequest.ashx?jsoncallback=?&t=4";
    $.getJSON(url, function (data) {
        $.each(data, function (i, item) {
                                                                                   
            strOut = strOut.concat("<div class=\"item\">");
            strOut = strOut.concat("    <div class=\"a\">").concat(data[i].Symbol).concat("</div>");
            strOut = strOut.concat("    <div class=\"b\">").concat(objLib.FormatNumber(data[i].Value, true)).concat("</div>");
            strOut = strOut.concat("    <div class=\"c ").concat(data[i].Format).concat("1\">&nbsp;</div>");
            strOut = strOut.concat("    <div class=\"d\">").concat(data[i].Sign).concat(data[i].Change).concat("</div>");
            strOut = strOut.concat("    <div class=\"e\">").concat(objLib.FormatNumber(data[i].Percent, true)).concat("%</div>");
            strOut = strOut.concat("</div>");
        });
        $("#divWorld").html(strOut);
        $("#divDateWorld").html(data[0].DateUpdate);
    });
}

function ShowEmodity() {
    var strOut = "";
    var url = "http://data3.gafin.net/iRequest.ashx?jsoncallback=?&t=5";
    $.getJSON(url, function (data) {
        var alt = true;
        strOut = strOut.concat("<div class=\"thead\"><div class=\"a\">Tên</div><div class=\"b\">Giá</div><div class=\"c\"></div><div class=\"d\">Thay đổi</div><div class=\"e\">%</div></div>");
        $.each(data, function (i, item) {
            strOut = strOut.concat("<div class=\"" + (alt ? "itemalter" : "item") + "\">");
            strOut = strOut.concat("    <div class=\"a\">").concat(data[i].Symbol).concat("</div>");
            strOut = strOut.concat("    <div class=\"b\">").concat(objLib.FormatNumber(data[i].Value, true)).concat("</div>");
            strOut = strOut.concat("    <div class=\"c ").concat(data[i].Format).concat("2\">&nbsp;</div>");
            strOut = strOut.concat("    <div class=\"d\">").concat(data[i].Sign).concat(data[i].Change).concat("</div>");
            strOut = strOut.concat("    <div class=\"e\">").concat(objLib.FormatNumber(data[i].Percent, true)).concat("%</div>");
            strOut = strOut.concat("</div>");
            alt = !alt;
        });
        $("#divEmodity").html(strOut);
        $("#divDateEmodity").html(data[0].DateUpdate);
    });
}
function ShowHomeBar() {
    var strOut = "";
    var url = "http://data3.gafin.net/iRequest.ashx?jsoncallback=?&t=8";
    $.getJSON(url, function (data) {

        $.each(data, function (i, item) {

            var change, sign, format, symbol;
            symbol = data[i].Symbol.toLowerCase();

            $("#" + symbol + "Sell").html(objLib.FormatNumber(data[i].Sell, true));
            $("#" + symbol + "SellChange").html("(<span class=\"t" + data[i].SellFormat + "\">" + data[i].SellSign + objLib.FormatNumber(data[i].SellChange, true) + "</span>)");
            $("#" + symbol + "Buy").html(objLib.FormatNumber(data[i].Buy, true));
            $("#" + symbol + "BuyChange").html("(<span class=\"t" + data[i].BuyFormat + "\">" + data[i].BuySign + objLib.FormatNumber(data[i].BuyChange, true) + "</span>)");

        });

    });
}
function ShowHomeIndex() {
    var strOut = "";
    var url = "http://data3.gafin.net/iRequest.ashx?jsoncallback=?&t=7";
    $.getJSON(url, function (data) {
        var change, sign, format;
       

        change = data.OilMarket.Change; sign = change > 0 ? "+" : "";
        $("#oilValue").html(objLib.FormatNumber(data.OilMarket.Price, true) + "(<span class=\"" + getFormat(change) + "\">" + sign + change + "</span>)");
        $("#usdTygia").html("Tỷ giá USD: " + objLib.FormatNumber(data.USD.BuyTransfer, true));

        $("#usdSell").html(objLib.FormatNumber(data.USD.Sell, true));
        change = data.USD.Change; sign = change > 0 ? "+" : "";
        $("#usdSellChange").html("(<span class=\"" + getFormat(change) + "\">" + sign + change + "</span>)");
        $("#usdBuy").html(objLib.FormatNumber(data.USD.BuyTransfer, true));
        $("#usdBuyChange").html("(<span class=\"" + getFormat(change) + "\">" + sign + change + "</span>)");

        $("#eurSell").html(objLib.FormatNumber(data.EUR.Sell, true)); 
        change = data.EUR.Change; sign = change > 0 ? "+" : ""; format = getFormat(change); change = objLib.roundNumber(change, 0);
        $("#eurSellChange").html("&nbsp;(<span class=\"" + format + "\">" + sign + change + "</span>)");
        $("#eurBuy").html(objLib.FormatNumber(data.EUR.BuyTransfer, true));
        $("#eurBuyChange").html("&nbsp;(<span class=\"" + format + "\">" + sign + change + "</span>)");

     
    });
}



function OnEnter(e) {
    if (!e) var e = window.event;

    if (e) {
        if (e.keyCode == 13) {
            e.cancelBubble = true;
            e.returnValue = false;
            e.cancel = true;
            onSearch();
            return false;
        }
    }
    return true;
}
function onSearch() {
    var symbol = '';
    var arrRadio = document.forms[0].elements['radioSearch'];
    var typeSearch = getCheckedValue(arrRadio);
    if (typeSearch == 1) {
        symbol = $("#search").val();
        if (symbol == '' || symbol == undefined) { return; }
        onRedirectCompanyData(symbol, "hose", symbol);
    }
    else {
        symbol = $("#searchNews").val();
        if (symbol == '' || symbol == undefined) { return; }
        var link = '/Pages/Search.aspx?q=' + symbol;
        document.location = link;
    }
}

function getCheckedValue(radioObj) {
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
            return radioObj.value;
        else
            return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}


function onRedirectBySymbol(data, value) {
    var host = '/';
    var symbol = data.split('-');
    onRedirectCompanyData(symbol[0], symbol[1], UnicodeToKoDauAndGach(value));

}
//function onRedirectCompanyData(symbol, tradeCenter, title) {
//    var host = '/';
//    var symbol = (symbol + '').toUpperCase();
//    var link = host + tradeCenter + '/' + symbol + '-' + title + '.htm';
//    if (symbol.indexOf("VNINDEX") > -1)
//        link = host + 'Lich-su-giao-dich-VNINDEX/Trang-1-0-tab-1.htm';
//    if (symbol.indexOf('HNX-INDEX') > -1)
//        link = host + 'Lich-su-giao-dich-HNX-INDEX/Trang-1-0-tab-1.htm';
//    if (symbol.indexOf('UPCOM-INDEX') > -1)
//        link = host + 'Lich-su-giao-dich-UPCOM-INDEX/Trang-1-0-tab-1.htm';
//    document.location = link;
//}

function onRedirectCompanyData(symbol, tradeCenter, title) {
    var host = '/';
    var symbol = (symbol + '').toUpperCase();
    var link = host + 'tong-quan/' + symbol + '-' + title + '.htm';
    if (symbol.indexOf("VNINDEX") > -1)
        link = host + 'Lich-su-giao-dich-VNINDEX/Trang-1-0-tab-1.htm';
    if (symbol.indexOf('HNX-INDEX') > -1)
        link = host + 'Lich-su-giao-dich-HNX-INDEX/Trang-1-0-tab-1.htm';
    if (symbol.indexOf('UPCOM-INDEX') > -1)
        link = host + 'Lich-su-giao-dich-UPCOM-INDEX/Trang-1-0-tab-1.htm';
    document.location = link;
}
function gid(id) {
    return document.getElementById(id);
}
function ShowBoxRegisMail() {
    gid("ifrRegisMail").src = "/regisemail.aspx";
    showPopup('boxRegisMail');
    document.getElementById("bgFilter").style.display = 'block';
    document.getElementById("bgFilter").style.zIndex = '1002';

    var screenH = 480;
    if (parseInt(navigator.appVersion) > 3) {
        screenH = screen.height;
    }
    else if (navigator.appName == "Netscape"
    && parseInt(navigator.appVersion) == 3
    && navigator.javaEnabled()
   ) {
        var jToolkit = java.awt.Toolkit.getDefaultToolkit();
        var jScreenSize = jToolkit.getScreenSize();
        screenH = jScreenSize.height;
    }

    document.getElementById("bgFilter").style.height = screenH + 'px';
}


function HuyDangKi() {
    var img = new Image();
    img.src = "/LetterUser.aspx?type=2&mail=" + gid('txtRegisEmail').value;
    hidePopUp('boxUnSubcribe');
}

function hidePopUp(controlID) {
    document.getElementById(controlID).className = "";
    document.getElementById(controlID).style.display = 'none';
    document.getElementById("bgFilter").style.display = 'none';


}
function showPopup(controlID) {
    var control = document.getElementById(controlID);
    if (control) {
        control.style.display = 'block';
        control.style.zIndex = '1003';
        var top = (window.screen.availHeight - control.offsetHeight) / 2 - 100;
        var left = (document.documentElement.offsetWidth - control.offsetWidth) / 2;
        left += (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
        top += (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

        control.style.left = (left) + 'px';
        control.style.top = (top) + 'px';

        return true;
    }
    return false;
}
function ShowBoxUnSubcribe() {
    showPopup('boxUnSubcribe');
    document.getElementById("bgFilter").style.display = 'block';
    document.getElementById("bgFilter").style.zIndex = '1002';
    document.getElementById("bgFilter").style.height = document.getElementById('maindiv').offsetHeight + 'px';
}




function showHideTab(index) {
    if (index == 1) {
        $('#divDateExchange').show();
        $('#divDateGold').hide();
        $('#divExchange').show();
        $('#divGold').hide();
        $('#tab1').removeClass().addClass('vheadactive');
        $('#tab2').removeClass().addClass('vheadtitle'); 
    }
    else {
        $('#divDateExchange').hide();
        $('#divDateGold').show();
        $('#divExchange').hide();
        $('#divGold').show();
        $('#tab2').removeClass().addClass('vheadactive');
        $('#tab1').removeClass().addClass('vheadtitle'); 
    }

}


/*Quang cao*/
function RunBanner(rawdata, zone_id, obj) {

    this.current = 0;
    this.zoneId = zone_id;
    this.len = rawdata.data.length;
    this.data = rawdata.data;
    this.isRotate = this.len > 0;
    this.isFirst = true;
    if (this != null) obj = this;

    this.Show = function () {
        if (obj.isFirst) { obj.ChangeBanner(); obj.isFirst = false; }
        if (obj.isRotate) {          
            window.setInterval(obj.ChangeBanner, 23000);
        }
    }

    this.ChangeBanner = function () {
        try {
            obj.current++;
            if (obj.current >= obj.len) obj.current = 0;
            document.getElementById(obj.zoneId).innerHTML = BuildHTML(obj.data[obj.current]);

        }
        catch (e) {}
    }
}
function BuildHTML(obj) {
    if (obj.Type == 1) {
        return '<a href="' + obj.Link + '" target="_blank"><img style="border:none; padding:0; margin:0;" src="' + obj.Url + '" width="' + obj.Width + 'px" height="' + obj.Height + '"/></a>'
    }
    else if (obj.Type == 2) {
        return '<embed src="' + obj.Url + '" width="' + obj.Width + 'px" height="' + obj.Height + '"></embed>';
    }
    return "";
}


