// updates company menu after page load or change in industry menu
var MAX_CHAR_COUNT = 28;
function change_menu() {
    if(document.menuForm == null) {
        return;
    }

    if(allIndustriesStr == null) {
        allIndustriesStr = "All Industries";
    }

    // -- INDUSTRY MENU --

    // selected broker
    aspId = document.menuForm.asps.value;

    if(document.menuForm.industries != null) {
        // previously selected broker
        var prevAspId = document.menuForm.prevAspId.value;

        // get current industry selection
        var selectedIndIndex = document.menuForm.industries.selectedIndex;
        if(selectedIndIndex == -1 || aspId == -1 || aspId != prevAspId) {
            selectedIndIndex = 0;
        }

        document.menuForm.prevAspId.value = aspId;

        // clear industry list from old values
        document.menuForm.industries.length = 0;

        // the first option in industry list (this is always used)
        opt = new Option("- " + allIndustriesStr + " -", 0);
        eval("document.menuForm.industries.options[0] = opt");

        // enable/disable industry menu
        if(aspId == -1) {
            // disable industry menu
            document.menuForm.industries.disabled = true;
        } else {
            // enable industry menu
            document.menuForm.industries.disabled = false;
            // count of accepted industries (j=0 is used for - All industries - option)
            var j = 1;
            // decide industry menu content
            for(i = 0; i < ind.length; i++) {
                // add industry if it belongs to this asp
                if(ind[i][0] == aspId) {
                    // new list option to be added
                    opt = new Option(ind[i][2], ind[i][1]);
                    // add option using eval (notice that we use j instead of i)
                    eval("document.menuForm.industries.options[j] = opt");
                    j++;
                }
            }
        }

        // set same industry as before menu update
        document.menuForm.industries.selectedIndex = selectedIndIndex;
    }


    // -- COMPANY MENU --
    
    if(document.menuForm.companies != null) {
        // selected industry
        var industryId = 0;
        if(document.menuForm.industries != null) {
            industryId = document.menuForm.industries.value;
        }

        // clear company list from old values
        document.menuForm.companies.length = 0;

        // count accepted companies
        var j = 0;

        // the menu index of the company that is currently viewed
        var activeCompanyIndex = 0;

        // boolean that tells if the limited companies are shown in the menu
        var notVisible = 'true';
        if(document.menuForm.notVisibleInMenu) {
            notVisible = document.menuForm.notVisibleInMenu.value;
        }

        // decide company menu content
        for(i = 0; i < comp.length; i++) {
            // use this company only if it is bought or if selling is not defined for this asp
            // setBoughtCompanies function has defined [i][6] earlier
            if(comp[i][6] == true) {

                // if all allowed in asp menu selected, add each company to the company menu
                if(aspId == -1) {
                    var acceptCompany = false;
                    var compName = comp[i][4];
                    // add asp's name after company name, since we have multiple asps
                    for(k = 0; k < asp.length; k++) {
                        if(asp[k][0] == comp[i][0]) {
                            acceptCompany = true;
                            compName += " (" + asp[k][2] + ")";
                            break;
                        }
                    }
                    // add company to the list only if its aspId was found in asp array
                    if((acceptCompany) && ((notVisible == 'false') || ((notVisible == 'true') && (!comp[i][5])))) {
                        // cut the company name if it is too long
                        if(compName.length > MAX_CHAR_COUNT) {
                            compName = compName.substring(0, MAX_CHAR_COUNT) + "..";
                        }

                        // new list option to be added
                        opt = new Option(compName, "fid=" + comp[i][3] + "&a=" + comp[i][0]);

                        //if user hasn't permission to the company, change item style
                        if (comp[i][5] == true) {
                            opt.disabled = true;
                            opt.style.className = "disabledOption";
                            opt.style.color = "#808080";
                        }

                        // add option using eval (notice that we use j instead of i)
                        eval("document.menuForm.companies.options[j] = opt");
                        if(comp[i][3] == document.menuForm.firstAnalystFmId.value) {
                            activeCompanyIndex = j;
                        }
                        j++;
                    }
                }
                //if certain asp or industry is selected, add company only if it belongs to the asp/industry
                else if(((industryId == 0 && comp[i][0] == aspId) || (comp[i][1] == industryId)) && ((notVisible == 'false') || ((notVisible == 'true') && (!comp[i][5])))) {
                    
                    // new list option to be added
                    opt = new Option(comp[i][4], "fid=" + comp[i][3] + "&a=" + comp[i][0]);   
     
                    //if user hasn't permission to the company, change item style
                    if (comp[i][5] == true) {
                        opt.disabled = true;
                        opt.style.className = "disabledOption";
                        opt.style.color = "#808080";
                    }

                    // add option using eval (notice that we use j instead of i)
                    eval("document.menuForm.companies.options[j] = opt");
                    // set active company's index if this is the company that we are currently viewing
                    if(comp[i][3] == document.menuForm.firstAnalystFmId.value) {
                        activeCompanyIndex = j;
                    } 
                    j++;
                }
            }
        }

        // select company (first in the list if industry was not selected)
        document.menuForm.companies.selectedIndex = activeCompanyIndex;
    }
}

// puts information about allowed companies to the comp array
function setUpCompanyPermission() {
    // comp[i][5]: permission or not
    // first set all companies as disallowed
    for (var i = 0; i < comp.length; i++) {
        comp[i][5] = true;
    }
    // then define allowed ones
    for (var i = 0; i < companyWithPermission.length; i++){
        for (var j = 0; j < comp.length; j++){
            if (comp[j][3] == companyWithPermission[i]){
                comp[j][5] = false;
                //j = comp.length;
                break;
            }
        }
    }
}

/**
 * Decides whether companies can be shown based on buying conditions:
 * - either company must belong to an asp that allows all of its companies to be shared
 * - or company must be bought
 */
function setBoughtCompanies() {
    // comp[i][6]: true when company can be used
    for (var i = 0; i < comp.length; i++) {
        // set company allowed in the beginning
        comp[i][6] = true;
        // continue check if there are some selling asps
        if(sellingAspIds != null) {
            for (var j = 0; j < sellingAspIds.length; j++) {
                // if the company belongs to a selling asp, set visibility to false
                if(comp[i][0] == sellingAspIds[j]) {
                    comp[i][6] = false;
                    break;
                }
            }
            // if the company belongs to a selling asp, see whether it has been bought
            if(!comp[i][6]) {
                for (var j = 0; j < boughtCompanies.length; j++) {
                    // set company as bought if it can be found from the boughtCompanies array
                    if (comp[i][3] == boughtCompanies[j]) {
                        comp[i][6] = true;
                        break;
                    }
                }
            }
        }
    }
}
