﻿function SLibrary() {
    this.DayOfWeek = '1,2,3,4,5';

    this.script_folder = '';
    this.script_object = null;

    this.AjaxRequestURL = function (div, url) {
        var obj = document.getElementById(div); if (obj)
        { obj.innerHTML = "<img src='http://static.gafin.vn/Images/loading.gif'/>" } var xmlHttp; try { if (!document.all) { xmlHttp = new XMLHttpRequest() } else { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") } } } catch (e) { alert("Your browser does not support AJAX!"); return false }; xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (obj)
                    obj.innerHTML = xmlHttp.responseText
            }
        }; xmlHttp.open("GET", url, true); xmlHttp.send(null)
    }

    this.roundNumber = function (number, digits) {
        var mul = Math.pow(10, digits);
        var result = Math.round(number * mul) / mul;
        return result;
    }
    this.IsReloadMarket = function () {
        var StartTime = '00:00:00';
        var EndTime = '00:30:00';
        return this.IsTradingTime(StartTime, EndTime, this.DayOfWeek);
    }
    this.IsIndexTrading = function () {
        var StartTime = '08:00:00';
        var EndTime = '11:03:00';
        return this.IsTradingTime(StartTime, EndTime, this.DayOfWeek);
    }
    this.IsUpcomTrading = function () {
        var StartTime = '10:00:00';
        var EndTime = '15:03:00';
        return this.IsTradingTime(StartTime, EndTime, this.DayOfWeek);
    }
    this.IsTradingTime = function (StartTime, EndTime, DayOfWeek) {
        var startTime = new Date();
        var endTime = new Date();
        var now = new Date();
        var day = ',' + DayOfWeek + ',';

        var start = StartTime.split(':');
        var end = EndTime.split(':');

        startTime.setHours(start[0], start[1], start[2]);
        endTime.setHours(end[0], end[1], end[2]);
        return (now >= startTime && now <= endTime && day.indexOf(',' + now.getDay() + ',') >= 0);
    }
    this.gID = function (id) {
        return document.getElementById(id);
    }
    this.updateHtml = function (id, output) {
        if (this.gID(id) != null) { this.gID(id).innerHTML = output; }
    }
    this.CreateCssLink = function (href) {
        var css = document.createElement('link');
        css.type = 'text/css';
        css.rel = 'stylesheet';
        css.href = href;
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(css);
    }

    this.CreateScriptObject = function (src) {
        if (this.script_object != null) {
            this.RemoveScriptObject();
        }

        this.script_object = document.createElement('script');

        this.script_object.setAttribute('type', 'text/javascript');
        this.script_object.setAttribute('src', src);

        var head = document.getElementsByTagName('head')[0];
        head.appendChild(this.script_object);
    }

    this.AppendScript = function () {
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(this.script_object);
    }

    this.RemoveScriptObject = function () {
        if (this.script_object.parentNode != null) { this.script_object.parentNode.removeChild(this.script_object) };
        this.script_object = null;
    }
    this.FormatChangeValue = function (value, percent) {
        var output = '';
        var temp = value + '';
        if (temp.indexOf('text_color_green') >= 0 || temp.indexOf('text_color_red') >= 0 || temp.indexOf('text_color_yellow') >= 0) {
            output = temp;
        }
        else {
            if (value > 0) {
                output = '<span style="color: #008000">+' + this.FormatNumber(value) + (percent ? '(+' + this.FormatNumber(percent) + '%)' : '') + '</span>';
            }
            else if (value < 0) {
                output = '<span style="color: #cc0000">' + this.FormatNumber(value) + (percent ? '(' + this.FormatNumber(percent) + '%)' : '') + '</span>';
            }
            else {
                output = '<span style="color: #ff9900">0' + (percent ? '(0%)' : '') + '</span>';
            }
        }
        return output;
    }
    this.FormatChangeValueChart = function (value, percent, max) {
        var output = '';
        if (value < 0) { }
        var temp = value + '';
        var width = Math.round((value / max) * 30);
        if (temp.indexOf('text_color_green') >= 0 || temp.indexOf('text_color_red') >= 0 || temp.indexOf('text_color_yellow') >= 0) {
            output = temp;
        }
        else {
            if (value > 0) {
                output = '<div style="color: #008000;width:50px;float:right;">' + (percent ? '' + this.FormatNumber(percent) + '%' : '') + '</div><div style=\"width:' + width + 'px;height:10px;background-color:#008000;float:right;margin-top:2px;\">&nbsp;</div>';
            }
            else if (value < 0) {
                value = (value + '').replace("-", "");
                max = (max + '').replace("-", "");
                width = Math.round((value / max) * 30);
                output = '<div style="color: #cc0000;width:50px;float:right;">' + (percent ? '' + this.FormatNumber(percent) + '%' : '') + '</div><div style=\"width:' + width + 'px;height:10px;background-color:#cc0000;float:right;margin-top:2px;\">&nbsp;</div>';
            }
            else {
                output = '<span style="color: #ff9900">' + (percent ? '0%' : '') + '</span>';
            }
        }
        return output;
    }

    this.FormatNumber = function (value, displayZero) {
        if (value == '') return (displayZero ? '0' : '');
        try {
            var temp = value + '';
            temp = temp.replace(',', '');
            var number = parseFloat(temp);
            value = this.FormatNumber1(number, 2, '.', ',');
            return (value);
        }
        catch (err) {
            return (displayZero ? '0' : '');
        }
    }

    

    this.FormatNumber1 = function (number, decimals, decimalSeparator, thousandSeparator) {
        var number = number.toFixed(decimals);

        var temp = number.toString();

        var f = temp.substr(temp.length - decimals, decimals);

        while (f != '' && f.charAt(f.length - 1) == '0') f = f.substr(0, f.length - 1);

        if (f != '') f = decimalSeparator + f;

        var t = temp.substr(0, temp.length - 3);

        if (thousandSeparator != '' && t.length > 3) {
            h = t;
            t = '';

            for (j = 3; j < h.length; j += 3) {
                i = h.slice(h.length - j, h.length - j + 3);
                t = thousandSeparator + i + t + '';
            }

            j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
            t = j + t;
        }

        temp = t + f;

        return temp;
    }
}



var objLib = new SLibrary();
