﻿
if (typeof (AG) == 'undefined') {
    AG = {};
}

if (typeof (AG.Stock) == 'undefined') {
    AG.Stock = {};
}

if (typeof (AG.Data) == 'undefined') {
    AG.Data = {};
}

if (typeof (AGFS) == 'undefined') {
    AGFS = {};
}

AGFS = {};
AGFSLib = {
    version: 1.0,
    data: [],
    //Get value from url
    request: function (param, defaultValue) {
        param = param.toLowerCase();
        if (typeof (defaultValue) == 'undefined') {
            defaultValue = '';
        }
        var regex = '[?&]' + param + '=([^&#]*)';
        var results = (new RegExp(regex)).exec(document.location.href.toLowerCase());
        if (results) return results[1];
        else {
            regex = '[?/]' + param + '/([^/#]*)';
            results = (new RegExp(regex)).exec(document.location.href.toLowerCase());
            if (results) return results[1];
        }
        return defaultValue;
    },
    getDataAsync: function (url, async, func, dataType) {
        var that = this;
        if (typeof (that.data[url]) == 'undefined') {
            $.ajax({
                url: url,
                type: 'GET',
                dataType: dataType,
                ifModified: true,
                cache: true,
                async: async,
                success: function (data) {
                    if (typeof (func) == 'function')
                        func(data);
                    that.data[url] = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
        else {
            func(that.data[url]);
        }
    },
    // Convert xml to json
    xmlToJson: function (xmlNode, parentNode) {
        var result = {}, count = 0, isArray = false;
        if (xmlNode.children) {
            xmlNode.children().each(function () {
                var children = $(this).children(), key;
                if (count == 0 && typeof (result[this.tagName]) == 'undefined' && !isArray) {
                    if (typeof (parentNode) != 'undefined' && parentNode == this.tagName + 's') {
                        key = 0;
                        isArray = true;
                        result = [];
                    }
                    else {
                        key = this.tagName;
                    }
                }
                else {
                    if (count == 0 && typeof (result[this.tagName]) != 'undefined') {
                        result = [result[this.tagName]];
                    }
                    key = ++count;
                }
                if (children.length > 0) {
                    result[key] = AGFSLib.xmlToJson($(this), key);
                }
                else {
                    result[key] = $(this).text();
                }
                children = null;
                //result[key]._index = count;
            });
        }
        else {
            return xmlNode;
        }
        return result;
    },
    enterKeyPressAction: function (e, actionNext) {
        var key;
        if (window.event) {
            key = window.event.keyCode;
        }      //IE
        else {
            key = e.which;     //firefox
        }
        if (key == 13) {
            eval(actionNext);
        }
    },
    loadjs: function (Name, url, func) {
        $.ajax({
            url: url,
            async: false,
            cache: true,
            success: function (data) {
                $('#' + Name).append('<script>' + data + '</script>');
                if (typeof (func == 'function')) {
                    func.call(this);
                };
            }
        });
    },
    OnlineUsers: function (id) {
        // load du lieu tu file handler sau do bind vao file html
        $.ajax({
            url: "/Handler/AG.Commons/OnlineUsers.aspx",
            async: true,
            cache: true,
            success: function (data) {
                $('#' + id).html(data);
            }
        });
    },
    loadData: function (Name, Url) {
        var that = this;
        $('#' + Name).html(that.template(Url, ''));

    },
    Printf: function (Data, ID) {
        var urlCreatePrintf = '/DesktopModules/AG.Article/App/SendMail-Print/CreateCachePrintf.aspx';
        var urlPrintf = '/DesktopModules/AG.Article/App/SendMail-Print/Printf.aspx?ID=' + ID;
        $.post(urlCreatePrintf, { Data: Data, ID: ID }, function (data) {
            window.open(urlPrintf, '_blank', 'toolbar=0,location=0,menubar=0,fullscreen=1,scrollbars=1');
        });
    },
    template: function (id, data) {

        return $.fn.pureJSTemplate({
            id: id,
            data: data
        });
    }
};

