﻿// JScript File
var idThongTinDoanhNghiep = '';
var idCongTyNiemYet = '';
var idCongTyChuaNiemYet = '';
var idCongTyUpcom = '193';
var idTraiPhieu = '103';
var languageId = '0';
function GetID() {
    switch (PortalID) {
        case '0':
            txtSearchSymbol = 'Nhập mã chứng khoán...';
            idThongTinDoanhNghiep = '72';
            idCongTyNiemYet = '331';
            idCongTyChuaNiemYet = '333';
            idCongTyUpcom = '332';
            idTraiPhieu = '334';
            languageId = '0';
            break;
        case '1':
            idThongTinDoanhNghiep = '277';
            idCongTyNiemYet = '277';
            idCongTyChuaNiemYet = '278';
            idCongTyUpcom = '295';
            idTraiPhieu = '279';
            languageId = '2';
            txtSearchSymbol = 'Symbol';
            break;
    };
}
GetID();

function runSearch() {
    loadjs('includeJs', '/DesktopModules/AG.Company_Search/SymbolCompany.ashx?lang=' + languageId);
}

function loadjs(Name, url, func) {
    $.ajax({
        url: url,
        async: false,
        cache: true,
        success: function(data) {
            $('#' + Name).append('<script>' + data + '</script>');
        }
    });
};

runSearch();

function LoadDataSymbol() {
    var value = $('#NiemYet_Company_txtCompany').val();
    value = value.toUpperCase();
    LinkToPageBySymbol(value);
};

$('#NiemYet_Company_txtCompany').ready(function() {
    if ($.cookie('symbol') != null && $.cookie('symbol') != '-1') {
        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.S + " - [" + row.CN + "]";
            },
            formatMatch: function(row, i, max) {
                return row.S + " - " + row.CN;
            },
            formatResult: function(row) {
                return row.S;
            }
        });
    }
    catch (Error) {
        //        alert(Error);
    }
    //    $('#NiemYet_Company_txtIndex').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) {
    //                if (checkSearch == 'SearchIndex') {
    //                    location.href = 'http://' + location.host + '/tabid/275/default.aspx?keyword=' + value;
    //                }
    //            }
    //            return (key != 13);
    //        }
    //    });
    $('#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 LinkToPageBySymbol(value) {
    for (var i = 0; i < JsonCompany.length; i++) {
        if (JsonCompany[i].S == value) {
            var strPage = $.cookie('page');
            var idTemplate = ''; var template = $.cookie('Template');
            if (JsonCompany[i].Fo == '3') {
                idTemplate = '1';
                $.cookie('Template', 'ChuaNiemYet');
            }
            else if (JsonCompany[i].Fo == '4') {
                idTemplate = '4';
                $.cookie('Template', 'Upcom');
            }
            else if (JsonCompany[i].CT == '3') {
                idTemplate = '3';
                $.cookie('Template', 'TraiPhieu');
            }
            else {
                idTemplate = '2';
                $.cookie('Template', 'NiemYet');
            }
            GetID();
            var strUrl = document.location.host;
            if (idTemplate == '1') {
                if (strPage != null && template == $.cookie('Template')&&strPage.indexOf('CongTy')>0) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyChuaNiemYet + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyChuaNiemYet + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '2') {
                if (strPage != null && template == $.cookie('Template') && strPage.indexOf('CongTy') > 0) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyNiemYet + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyNiemYet + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '4') {
                if (strPage != null && template == $.cookie('Template') && strPage.indexOf('CongTy') > 0) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyUpcom + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idCongTyUpcom + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '3') {
                if (strPage != null && template == $.cookie('Template') && strPage.indexOf('CongTy') > 0) {
                    document.location.href = 'http://' + strUrl + '/tabid/' + idTraiPhieu + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else document.location.href = 'http://' + strUrl + '/tabid/' + idTraiPhieu + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            }
        }
    }
    return false;
};

function LinkToPageByStock(value) {
    var strResult = '';
    for (var i = 0; i < JsonCompany.length; i++) {
        if (JsonCompany[i].S == value) {
            var strPage = $.cookie('page');
            var idTemplate = ''; var template = $.cookie('Template');
            if (JsonCompany[i].Fo == '3') {
                idTemplate = '1';
                $.cookie('Template', 'ChuaNiemYet');
            }
            else if (JsonCompany[i].Fo == '4') {
                idTemplate = '4';
                $.cookie('Template', 'Upcom');
            }
            else if (JsonCompany[i].CT == '3') {
                idTemplate = '3';
                $.cookie('Template', 'TraiPhieu');
            }
            else {
                idTemplate = '2';
                $.cookie('Template', 'NiemYet');
            }
            GetID();
            var strUrl = document.location.host;
            if (idTemplate == '1') {
                if (strPage != null && template == $.cookie('Template')) {
                    strResult = 'http://' + strUrl + '/tabid/' + idCongTyChuaNiemYet + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else strResult = 'http://' + strUrl + '/tabid/' + idCongTyChuaNiemYet + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '2') {
                if (strPage != null && template == $.cookie('Template')) {
                    strResult = 'http://' + strUrl + '/tabid/' + idCongTyNiemYet + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else strResult = 'http://' + strUrl + '/tabid/' + idCongTyNiemYet + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '4') {
                if (strPage != null && template == $.cookie('Template')) {
                    strResult = 'http://' + strUrl + '/tabid/' + idCongTyUpcom + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else strResult = 'http://' + strUrl + '/tabid/' + idCongTyUpcom + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            };
            if (idTemplate == '3') {
                if (strPage != null && template == $.cookie('Template')) {
                    strResult = 'http://' + strUrl + '/tabid/' + idTraiPhieu + '/default.aspx#' + value + ',' + template + ',' + strPage;
                }
                else strResult = 'http://' + strUrl + '/tabid/' + idTraiPhieu + '/default.aspx#' + value + ',' + $.cookie('Template') + ',' + $.cookie('Template') + '_Default';
                break;
            }
        }
    }
    return strResult;
}

