﻿CompanyCategory = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'CompanyCategory',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchCompanyCategory.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.CompanyCategory) != "undefined")
                        jsonData = [jsonData.CompanyCategory];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;

                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    }
};CompanyInfomation = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'CompanyInfomation',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},
    //Primaky key    
    pkData: {},
    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchCompany.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.Company) != "undefined")
                        jsonData = [jsonData.Company];
                    if (typeof (that.data[requestName]) == 'undefined') that.data[requestName] = [];
                    if (typeof (that.pkData[requestName]) == "undefined") that.pkData[requestName] = {};
                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {
                        position = AG.ClientAPI.findData(jsonData[i], that.pkData[requestName], 'Id');
                        if (position < 0) {
                            position = that.data[requestName].push(jsonData[i]) - 1;
                            that.pkData[requestName][jsonData[i].Id] = {};
                            that.pkData[requestName][jsonData[i].Id].position = position;
                        }
                        else {
                            for (key in jsonData[i]) {
                                if (key != "length" && typeof (jsonData[i][key]) != "undefined" && typeof (that.data[requestName][position]) != "undefined") {
                                    that.data[requestName][position][key] = jsonData[i][key];
                                }
                            }
                        }
                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    },
    getCount: function (data, getFields, order, filters, joinTable) {
        var that = this;
        var strConditions = AG.ClientAPI.getCondition(data, getFields, order, filters, joinTable);
        var result;
        if (AG.ClientAPI.data['/Handler/WS.StockData/CountCompany.ashx' + 'Conditions=' + strConditions] == undefined) {
            $.ajax({
                url: '/Handler/WS.StockData/CountCompany.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'text',
                cache: true,
                async: false,
                success: function (data) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return 0;
                }
            });
            AG.ClientAPI.data['/Handler/WS.StockData/CountCompany.ashx' + 'Conditions=' + strConditions] = result;
        }
        else {
            result = AG.ClientAPI.data['/Handler/WS.StockData/CountCompany.ashx' + 'Conditions=' + strConditions];
        }
        return result;
    },
    toExcel: function (fileName, data, getFields, order, filters, joinTable) {
        var strConditions = AG.ClientAPI.getCondition(data, getFields, order, filters, joinTable);
        document.location.href = '/Handler/StockReportToExcel/Company.aspx' + '?Conditions=' + strConditions + '&FileName=' + fileName;
    }
};Event = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'Event',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},
    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchEvent.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.Event) != "undefined")
                        jsonData = [jsonData.Event];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {
                        position = that.data[requestName].push(jsonData[i]) - 1;
                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    },
    getCount: function (data, getFields, order, filters, joinTable) {
        var that = this;
        var strConditions = AG.ClientAPI.getCondition(data, getFields, order, filters, joinTable);
        var result;
        if (AG.ClientAPI.data['/Handler/WS.StockData/CountEvent.ashx' + 'Conditions=' + strConditions] == undefined) {
            $.ajax({
                url: '/Handler/WS.StockData/CountEvent.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'text',
                cache: true,
                async: false,
                success: function (data) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return 0;
                }
            });
            AG.ClientAPI.data['/Handler/WS.StockData/CountEvent.ashx' + 'Conditions=' + strConditions] = result;
        }
        else {
            result = AG.ClientAPI.data['/Handler/WS.StockData/CountEvent.ashx' + 'Conditions=' + strConditions];
        }
        return result;
    }
}; News = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'News',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchNews.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.News) != "undefined")
                        jsonData = [jsonData.News];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;

                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    },
    getCount: function (data, getFields, order, filters, joinTable) {
        var that = this;
        var strConditions = AG.ClientAPI.getCondition(data, getFields, order, filters, joinTable);
        var result;
        if (AG.ClientAPI.data['/Handler/WS.StockData/CountNews.ashx' + 'Conditions=' + strConditions] == undefined) {
            $.ajax({
                url: '/Handler/WS.StockData/CountNews.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'text',
                cache: true,
                async: false,
                success: function (data) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return 0;
                }
            });
            AG.ClientAPI.data['/Handler/WS.StockData/CountNews.ashx' + 'Conditions=' + strConditions] = result;
        }
        else {
            result = AG.ClientAPI.data['/Handler/WS.StockData/CountNews.ashx' + 'Conditions=' + strConditions];
        }
        return result;
    }
}; FinancialReport = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'FinancialReport',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockReport/SearchFinancialReport.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'POST',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    that.sortTreeData(data, requestName);
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    },
    sortTreeData: function (data, requestName) {
        var that = this;
        var jsonData = AGFSLib.xmlToJson($('Document', data));
        if (typeof (jsonData.length) == "undefined" && typeof (jsonData.FieldCategory) != "undefined")
            jsonData = [jsonData.FieldCategory];
        that.data[requestName] = [];

        var pkJsonData = {};
        for (var i = 0; i < jsonData.length; i++) {
            pkJsonData[jsonData[i].Id] = {};
            pkJsonData[jsonData[i].Id].position = i;
            pkJsonData[jsonData[i].Id].childId = [];
        }
        pkJsonData['00000000-0000-0000-0000-000000000000'] = {};
        pkJsonData['00000000-0000-0000-0000-000000000000'].childId = [];
        var position = 0, rootPosition = 0;
        for (var i = 0; i < jsonData.length; i++) {
            position = AG.ClientAPI.findData(jsonData[i], pkJsonData, 'ParentId');
            if (position >= 0) {
                pkJsonData[jsonData[i].ParentId].childId.push({ position: i, index: jsonData[i].Index });
            }
            else {
                pkJsonData['00000000-0000-0000-0000-000000000000'].childId.push({ position: i, index: jsonData[i].Index });
            }
        }
        that.findChildField(requestName, pkJsonData, jsonData, '00000000-0000-0000-0000-000000000000', 0);
    },
    findChildField: function (requestName, objChild, objData, Id, level) {
        var that = this;
        objChild[Id].childId.sort(SortIndex);
        for (var i = 0; i < objChild[Id].childId.length; i++) {
            that.addChildField(requestName, objChild, objData, objData[objChild[Id].childId[i].position].Id, level, i);
        }
    },
    addChildField: function (requestName, objChild, objData, Id, level, index) {
        var that = this;
        objData[objChild[Id].position].level = level + 1;
        position = that.data[requestName].push(objData[objChild[Id].position]) - 1;
        that.findChildField(requestName, objChild, objData, objData[objChild[Id].position].Id, level + 1);
    },
    getFinancialInfoById: function (requestName, data, field, order, filter) {
        var that = this;
        AG.ClientAPI.addCondition(that, requestName, data, field, order, filter, undefined);
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockReport/SearchFinancialReport.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'POST',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    that.sortTreeData(data, requestName);
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    }
};
function SortIndex(a, b) {
    return a.index - b.index;
};FinancialTime = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'FinancialTime',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchFinancialTime.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.FinancialTime) != "undefined")
                        jsonData = [jsonData.FinancialTime];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;

                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    }
};InsiderTransaction = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'InsiderTransaction',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchInsiderTransaction.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.InsiderTransaction) != "undefined")
                        jsonData = [jsonData.InsiderTransaction];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;

                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    },
    getCount: function (data, getFields, order, filters, joinTable) {
        var that = this;
        var strConditions = AG.ClientAPI.getCondition(data, getFields, order, filters, joinTable);
        var result;
        if (AG.ClientAPI.data['/Handler/WS.StockData/CountInsiderTransaction.ashx' + 'Conditions=' + strConditions] == undefined) {
            $.ajax({
                url: '/Handler/WS.StockData/CountInsiderTransaction.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'text',
                cache: true,
                async: false,
                success: function (data) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return 0;
                }
            });
            AG.ClientAPI.data['/Handler/WS.StockData/CountInsiderTransaction.ashx' + 'Conditions=' + strConditions] = result;
        }
        else {
            result = AG.ClientAPI.data['/Handler/WS.StockData/CountInsiderTransaction.ashx' + 'Conditions=' + strConditions];
        }
        return result;
    }
}; Leadership = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'Leadership',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchLeadership.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.Leadership) != "undefined")
                        jsonData = [jsonData.Leadership];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;

                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    }
};Shareholder = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'Shareholder',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchShareholder.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.Shareholder) != "undefined")
                        jsonData = [jsonData.Shareholder];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;

                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    },
    getCount: function (data, getFields, order, filters, joinTable) {
        var that = this;
        var strConditions = AG.ClientAPI.getCondition(data, getFields, order, filters, joinTable);
        var result;
        if (AG.ClientAPI.data['/Handler/WS.StockData/CountShareholder.ashx' + 'Conditions=' + strConditions] == undefined) {
            $.ajax({
            url: '/Handler/WS.StockData/CountShareholder.ashx',
            data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
            type: 'GET',
            dataType: 'text',
            cache: true,
            async: false,
            success: function (data) {
                result = data;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                return 0;
            }
        });
        AG.ClientAPI.data['/Handler/WS.StockData/CountShareholder.ashx' + 'Conditions=' + strConditions] = result;
        }
        else {
            result = AG.ClientAPI.data['/Handler/WS.StockData/CountShareholder.ashx' + 'Conditions=' + strConditions];
        }
        return result;
    }
};StockExchange = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'StockExchange',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            $.ajax({
                url: '/Handler/WS.StockData/GetStockExchange.ashx',
                data: 'Id=' + that.conditions[that.pkConditions[requestName].position].data[0].Id,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.StockExchange) != "undefined")
                        jsonData = [jsonData.StockExchange];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;


                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    }
};YearPlanning = {
    //private property
    sessionDate: new Date().getTime(),
    interval: false,
    version: [],
    name: 'YearPlanning',
    //Conditions
    conditions: [],
    //key condition
    pkConditions: {},
    //Output Document
    data: {},


    //methos
    //loadData
    loadData: function (requestName) {
        var that = this;
        if (AG.ClientAPI.checkRequest(that, requestName)) {
            var strConditions = AG.ClientAPI.getConditions(that, requestName);
            $.ajax({
                url: '/Handler/WS.StockData/SearchYearPlanning.ashx',
                data: 'Conditions=' + strConditions + '&t=' + that.sessionDate,
                type: 'GET',
                dataType: 'xml',
                cache: true,
                async: false,
                success: function (data) {
                    var jsonData = AGFSLib.xmlToJson($('Document', data));
                    if (typeof (jsonData.length) == "undefined" && typeof (jsonData.YearPlanning) != "undefined")
                        jsonData = [jsonData.YearPlanning];
                    that.data[requestName] = [];

                    var position = 0;
                    for (var i = 0; i < jsonData.length; i++) {

                        position = that.data[requestName].push(jsonData[i]) - 1;


                    }
                    that.pkConditions[requestName].isRun = true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
    }
};
