﻿// JScript File
var txtSearchSymbol = '';
var idThongTinDoanhNghiep = '116';
var idCongTyNiemYet = '101';
var idCongTyChuaNiemYet = '102';
var idCongTyUpcom = '193';
var idTraiPhieu = '103';
var languageId = '0';
switch (PortalID) {
    case '0':
        txtSearchSymbol = 'Nhập mã CK...';
        idThongTinDoanhNghiep= '116';
    idCongTyNiemYet= '101';
    idCongTyChuaNiemYet= '102';
    idCongTyUpcom= '193';
    idTraiPhieu= '103';
    languageId= '0';
        break;
    case '1':
        idThongTinDoanhNghiep = '277';
        idCongTyNiemYet = '277';
        idCongTyChuaNiemYet = '278';
        idCongTyUpcom = '295';
        idTraiPhieu = '279';
        languageId = '2';
        txtSearchSymbol = 'Symbol';
        break;
}
function   runSearch() {
    loadjs('includeJs', '/DesktopModules/AG.ThongTinDoanhNghiep/region/CompanySearch/SymbolCompany.ashx?' + 'lang=' +languageId, function() { });
}

function   loadjs(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);
            };
        }
    });
};

runSearch();

$('#NiemYet_Company_FormCompany').ready(function() {
    if ($.cookie('symbol') != null && $.cookie('symbol') != '-1' && $.cookie('symbol') != 'null') {
        document.getElementById('NiemYet_Company_txtCompany').value = $.cookie('symbol');
    }
    else document.getElementById('NiemYet_Company_txtCompany').value = txtSearchSymbol;
    try {
        $('#NiemYet_Company_txtCompany').autocomplete(JsonCompany, {
            minChars: 0,
            width: 310,
            matchContains: false,
            autoFill: false,
            formatItem: function(row, i, max) {
                return row.SymbolId + " - [" + row.CompanyName + "]";
            },
            formatMatch: function(row, i, max) {
                return row.SymbolId + " - " + row.CompanyName;
            },
            formatResult: function(row) {
                return row.SymbolId;
            }
        });
    }
    catch (Error) {
    }

//    $('#NiemYet_Company_txtCompany').blur(function() {
//        var value = $('#NiemYet_Company_txtCompany').val();
//        LinkToPageBySymbol(value);
//    });
    $('#NiemYet_Company_txtCompany').keypress(function(e) {
        if (e.which == 13) {
            var key;
            if (window.event)
                key = window.event.keyCode; //IE
            else
                key = e.which; //firefox     
            if (key == 13) {
                var value = $('#NiemYet_Company_txtCompany').val();
                LinkToPageBySymbol(value);
            }
            return (key != 13);
        }
    });
});    

function LoadData(value) {
    value = value.toUpperCase();
    LinkToPageBySymbol(value);    
};

function LinkToPageBySymbol(value) {
    for (var i = 0; i < JsonCompany.length; i++) {
        if (JsonCompany[i].SymbolId == value) {
            var strPage = $.cookie('page');
            var idTemplate = ''; var template = $.cookie('Template');
            if (JsonCompany[i].FloorId == '3') {
                idTemplate = '1';
                $.cookie('Template', 'ChuaNiemYet');
            }
            else if (JsonCompany[i].FloorId == '4') {
                idTemplate = '4';
                $.cookie('Template', 'UPCOM');            
            }
            else if (JsonCompany[i].CompanyTypeId == '3') {
                idTemplate = '3';
                $.cookie('Template', 'TraiPhieu');
            }
            else {
                idTemplate = '2';
                $.cookie('Template', 'NiemYet');
            }
            var strUrl = document.location.host;
            if (idTemplate == '1') {
                if (strPage != null && template == $.cookie('Template')) {
                    document.location.href = 'http://' + strUrl + '/tabid/'+idCongTyChuaNiemYet+'/default.aspx#' + value + ',' + strPage;
                 }
                 else document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyChuaNiemYet + '/default.aspx#' + value + ',' + $.cookie('Template') + '_Default';
                 break;
            };
            if (idTemplate == '2') {
                if (strPage != null && template == $.cookie('Template')) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyNiemYet + '/default.aspx#' + value + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyNiemYet + '/default.aspx#' + value + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '3') {
                if (strPage != null && template == $.cookie('Template')) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyUpcom + '/default.aspx#' + value + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyUpcom + '/default.aspx#' + value + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '4') {
                if (strPage != null && template == $.cookie('Template')) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idTraiPhieu + '/default.aspx#' + value + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idTraiPhieu + '/default.aspx#' + value + ',' + $.cookie('Template') + '_Default';
                break;
            }
        }
    }
    return false;
};


