/**
 * Sahkonumerot.fi product list selection javascript functionality.
 *
 */

var selectionInfo = '';
var timer;

function load(hash)
{
    var pageNo = 1;
    var uuid = new String(hash);
    var bcmd = uuid;

    if ((uuid != null) && (uuid != ''))
    {
        uuid = uuid.replace('#', '');
        bcmd = uuid;
        var pPos = uuid.indexOf('-p');
        if ( pPos != -1 )
        {
            var pg = uuid.split('-p');
            if ( pg.length > 0 ) uuid = pg[0];
            if ( pg.length > 1 ) pageNo = parseInt(pg[1]);
        }
    }

    if ( init == false )
    {
        initNameSelection(uuid, pageNo);
        init = true;
    }
    else
    {
        var content = '';

        if ( lastCmd != bcmd )
        {
            lastCmd = bcmd;

            var tmpListId = new Date().getTime();
            listId = tmpListId;
            currentPage = pageNo;

            show('#loader-list');
            $('#product-rows').html('');

            initSelectedItems(uuid);

            if ( uuid.length > 0 )
            {
                var selBits = getSelectionBitsFromUUID(uuid);
                var params = '&uuid='+uuid;
                get_breadCrumb(params, selBits);

                /*$('#pagelink-url').val(systemUrl + selectionURL + '/?uuid=' + uuid + '&page='+currentPage);*/
                get_product_list_content('product', params + '&page='+currentPage, tmpListId);
            }
            else
            {
                var selBits = getSelectionBitsDefault();
                var params = getDefaultParams();

                get_breadCrumb(params, selBits);

                var bitsUrl = '';
                if ( selBits !== '000' )
                {
                    bitsUrl = '&view=' + selBits;
                }

                /*$('#pagelink-url').val(systemUrl + selectionURL + '/' + params + '&page='+currentPage + bitsUrl);*/
                get_product_list_content('product', params+'&page='+currentPage + bitsUrl, tmpListId);
            }
        }
    }
}


function getElemById(elemId)
{
    if (typeof document.getElementById != 'undefined')
    {
        return document.getElementById(elemId);
    }
    if (typeof document.all != 'undefined')
    {
        return document.all[elemId];
    }
    if (typeof document.layers != 'undefined')
    {
        return document.layers[elemId];
    }
    return null;
}

function emptyDiv(divId)
{
    if ((divId == null) || ( divId == ''))
    {
        return;
    }
    var div = getElemById(divId);

    if (( div == null ) || ( div == '' )) return;

    if(window.close)
    {
        div.innerHTML='&nbsp;';
    }
    else
    {
        div.textContent='&nbsp;';
    }
}

function setSelectedFunction(selected, notSelected)
{
    var elem = $('#' + selected);
    if ( elem != null ) elem.addClass('selected');

    if (notSelected instanceof Array)
    {
        for(i=0; i<notSelected.length; i++)
        {
            var elem = $('#' + notSelected[i]);
            if ( elem != null )
            {
                elem.removeClass('selected');
            }
        }
    }
    else
    {
        var elem = $('#' + notSelected);
        if ( elem != null )
        {
            elem.removeClass('selected');
        }
    }
}

function setSelectionTitle(title)
{
    $('#selection-title-heading').html('<h2>' + title + '</h2>');
}

function setSelectionInfo(info)
{
    $('#selection-title-info').html(info);
}


function getCheckSelections(check)
{
    if (( check != null ) && (( check == false ) || ( check == 0 )))
    {
        return false;
    }
    return true;
}


function populate(type)
{
    setSelectionInfo('(' + arrText['JS_Searching'] + ')');
    if ( type == 'suppliers' )
    {
        $('.selection-item-supplier').prop('disabled', true);
        populateSuppliers();
    }
    if ( type == 'brands' )
    {
        $('.selection-item-brand').prop('disabled', true);
        populateBrands();
    }
    else if ( type == 'series' )
    {
        $('.selection-item-serie').prop('disabled', true);
        populateSeries();
    }
    else if ( type == 'groups' )
    {
        $('.selection-item-group').prop('disabled', true);
        populateGroups();
    }
/*    else if ( type == 's2010' )
    {
        $('.selection-item-s2010').prop('disabled', true);
        populateS2010();
    }*/
}

function populateSuppliers(checkSelections)
{
    show('#loader-data');

    checkSelections = getCheckSelections(checkSelections);

    selectionInfo = '';
    setSelectionTitle(arrText['Selection_Suppliers']);
    setSelectedFunction('selection-suppliers', new Array('selection-brands', 'selection-series', 'selection-groups'));

    $('#tip2').html(arrText['Tips_Select_Supplier']);

    if (checkSelections==true)
    {
        osuSel = getSelected(osuOrder, 'supplier_sel_');
    }

    show('#supplier-selection');
    hide('#brand-selection');
    hide('#serie-selection');
    hide('#group-selection');

    var params = getSelectionParams();

    if (params != false)
    {
        var content = get_selection_content('supplier', params);

        if( content != '')
        {
            osuOrder = content.split('\n');
        }
    }
    else
    {
        osuOrder = osuOrderAll;
        selectionInfo = $('#name-selection-title-info').html();
    }

    clearSelections();

    itemCount = 0;
    if (( osuOrder != null ) && ( osu != null ))
    {
        itemCount = osuOrder.length;
    }

    itemsPerColumn = getItemsPerColumn(itemCount, columns);
    if ( itemsPerColumn == 0 )
    {
        $('#supplier-selection-col-1').append('<div class=\"msg-no-matches\">' + arrText['No_Options_To_Show'] + '</div>');
    }

    count = 0;
    columnNo = 1;
    for ( i=0; i<osuOrder.length; i++)
    {
        if (( osuOrder[i] == null ) || ( osuOrder[i] == '' )) continue;

        var id = '' + osuOrder[i];
        var selected = '';

        if ( isSelectedSupplier(id) ) selected = ' checked';

        addSelectionItem('supplier-selection-col-' + columnNo, 'Supplier_ID', 'supplier_sel_' + id, id, osu['sel'+id], selected, 'supplier', '');

        count++;

        if (count>=itemsPerColumn)
        {
            count = 0;
            columnNo++;
        }
    }
    setSelectionInfo(selectionInfo);
    hide('#loader-data');
}

function populateBrands(checkSelections)
{
    show('#loader-data');
    checkSelections = getCheckSelections(checkSelections);

    selectionInfo = '';

    setSelectionTitle(arrText['Selection_Brands']);
    setSelectedFunction('selection-brands', new Array('selection-suppliers', 'selection-series', 'selection-groups'));
    $('#tip2').html(arrText['Tips_Select_Brand']);

    if (checkSelections==true)
    {
        obrSel = getSelected(obrOrder, 'brand_sel_');
    }

    hide('#supplier-selection');
    hide('#serie-selection');
    hide('#group-selection');
    show('#brand-selection');


    var params = getSelectionParams();

    if ( params != false )
    {
        var content = get_selection_content('brand', params);
        if( content != '')
        {
            obrOrder = content.split('\n');
        }
    }
    else
    {
        obrOrder = obrOrderAll;
        selectionInfo = $('#name-selection-title-info').html();
    }

    clearSelections();

    itemCount = 0;
    if (( obrOrder != null ) && ( obr != null ))
    {
        itemCount = obrOrder.length;
    }

    itemsPerColumn = getItemsPerColumn(itemCount, columns);
    if ( itemsPerColumn == 0 )
    {
        $('#brand-selection-col-1').append('<div class=\"msg-no-matches\">' + arrText['No_Options_To_Show']  + '</div>');
    }

    count = 0;
    columnNo = 1;
    for ( i=0; i<obrOrder.length; i++)
    {
        if (( obrOrder[i] == null ) || ( obrOrder[i] == '' )) continue;

        var id = '' + obrOrder[i];
        var selected = '';

        if ( isSelectedBrand(id) ) selected = ' checked';

        addSelectionItem('brand-selection-col-' + columnNo, 'Brand_ID', 'brand_sel_' + id, id, obr['sel'+id], selected, 'brand', '');

        count++;

        if (count>=itemsPerColumn)
        {
            count = 0;
            columnNo++;
        }
    }

    setSelectionInfo(selectionInfo);
    hide('#loader-data');
}

function populateSeries(checkSelections)
{
    show('#loader-data');
    checkSelections = getCheckSelections(checkSelections);

    selectionInfo = '';

    setSelectionTitle(arrText['Selection_Series']);
    setSelectedFunction('selection-series', new Array('selection-brands', 'selection-suppliers', 'selection-groups'));
    $('#tip2').html(arrText['Tips_Select_Serie']);

    if (checkSelections==true)
    {
        oseSel = getSelected(oseOrder, 'serie_sel_');
    }
    hide('#supplier-selection');
    hide('#brand-selection');
    hide('#group-selection');
    show('#serie-selection');

    var params = getSelectionParams();

    if ((params != null) && (params != false))
    {
        var content = get_selection_content('serie', params);

        if( content != '')
        {
            oseOrder = content.split('\n');
        }
    }
    else
    {
        oseOrder = oseOrderAll;
        selectionInfo = $('#name-selection-title-info').html();
    }
    clearSelections();

    itemCount = 0;
    if (( oseOrder != null ) && ( ose != null ))
    {
        itemCount = oseOrder.length;
    }

    itemsPerColumn = getItemsPerColumn(itemCount, columns);
    if ( itemsPerColumn == 0 )
    {
        $('#serie-selection-col-1').append('<div class=\"msg-no-matches\">' + arrText['No_Options_To_Show'] + '</div>');
    }

    count = 0;
    columnNo = 1;
    for ( i=0; i<oseOrder.length; i++)
    {
        if (( oseOrder[i] == null ) || ( oseOrder[i] == '' )) continue;

        var id = '' + oseOrder[i];
        var selected = '';

        if ( isSelectedSerie(id) ) selected = ' checked';

        addSelectionItem('serie-selection-col-' + columnNo, 'Serie_ID', 'serie_sel_' + id, id, ose['sel'+id], selected, 'serie', '');

        count++;

        if (count>=itemsPerColumn)
        {
            count = 0;
            columnNo++;
        }
    }
    setSelectionInfo(selectionInfo);
    hide('#loader-data');
}


function populateGroups(checkSelections)
{
    show('#loader-data');
    checkSelections = getCheckSelections(checkSelections);

    selectionInfo = '';

    setSelectionTitle(arrText['Selection_Groups']);
    setSelectedFunction('selection-groups', new Array('selection-suppliers', 'selection-series', 'selection-brands'));
    $('#tip2').html(arrText['Tips_Select_Group']);

    if (checkSelections==true)
    {
        grpSel = getSelected(grpOrder, 'group_sel_');
    }

    hide('#supplier-selection');
    hide('#brand-selection');
    hide('#serie-selection');
    show('#group-selection');

    var params = getSelectionParams();

    if ( params != false )
    {
        var content = get_selection_content('group', params);
        if( content != '')
        {
            grpOrder = content.split('\n');
        }
    }
    else
    {
        grpOrder = grpOrderAll;
        selectionInfo = $('#name-selection-title-info').html();
    }

    clearSelections();

    itemCount = 0;
    if (( grpOrder != null ) && ( grp != null ))
    {
        itemCount = grpOrder.length;
    }

    itemsPerColumn = getItemsPerColumn(itemCount, columns);
    if ( itemsPerColumn == 0 )
    {
        $('#group-selection-col-1').append('<div class=\"msg-no-matches\">' + arrText['No_Options_To_Show'] + '</div>');
    }

    count = 0;
    columnNo = 1;
    for ( i=0; i<grpOrder.length; i++)
    {
        if (( grpOrder[i] == null ) || ( grpOrder[i] == '' )) continue;

        var id = '' + grpOrder[i];
        var selected = '';

        if ( isSelectedGroup(id) ) selected = ' checked';

        addSelectionItem('group-selection-col-' + columnNo, 'Product_Group_ID', 'group_sel_' + id, id, grp['sel'+id], selected, 'group', '');

        count++;

        if (count>=itemsPerColumn)
        {
            count = 0;
            columnNo++;
        }
    }

    setSelectionInfo(selectionInfo);
    hide('#loader-data');
}

function populateNames(showAll, checkSelections)
{
    show('#loader-name');
    if (( showAll == null ) || ( showAll == '' )) showAll = false;
    namesAll = showAll;
    checkSelections = getCheckSelections(checkSelections);

    if ( showAll )
    {
        setSelectedFunction('selection-names-all', 'selection-names-harmonized');
    }
    else
    {
        setSelectedFunction('selection-names-harmonized', new Array('selection-names-all'));
    }

    if (checkSelections==true)
    {
        checkSelectedNames(showAll);
    }

    for (i=1; i<=columns; i++)
    {
        emptyDiv('name-selection-col-' + i);
    }

    items = getNameItemCount(showAll);
    itemsPerColumn = getItemsPerColumn(items, columns);
    if ( itemsPerColumn == 0 )
    {
        if ( namesAll )
        {
            $('#name-selection-col-1').append('<div class=\"msg-no-matches\">' + arrText['No_General_Name_Options'] + '</div>');
        }
        else
        {
            $('#name-selection-col-1').append('<div class=\"msg-no-matches\">' + arrText['No_Harmonized_General_Name_Options'] + '<br /><br /> <a href=\"javascript: populateNames(true);\">' + arrText['Show_All_General_Names'] + ' &raquo;</a></div>');
        }
    }

    count = 0;
    columnNo = 1;

    for ( i=0; i < optOrder.length; i++)
    {
        id = '' + optOrder[i];

        if ( showAll || ( optHarm[id] == 1 ))
        {
            selected = '';
            if ( isSelectedName(id) ) selected = ' checked';

            var title = opt[id];
            if ( optCnt[id] != null )
            {
                title += ' <span>(' + optCnt[id] + ')</span>';
            }
            var extraClass = '';
            if ( showAll )
            {
                if (optHarm[id] == 1)
                {
                    extraClass = 'harmonized';
                }
                else
                {
                    extraClass = 'unharmonized';
                }
            }
            addSelectionItem('name-selection-col-' + columnNo, 'Name', 'name_sel_' + id, id, title, selected, 'name', extraClass);

            count++;
            if (count>=itemsPerColumn)
            {
                count = 0;
                columnNo++;
            }
        }
    }
    hide('#loader-name');
}

function populateProductList()
{
    var tmpListId = new Date().getTime();

    $('#product-rows').html('');
    show('#loader-list');
    setBasketAddFunctionState(false, '', '');

    var content = '';
    var currentPage = parseInt($('#currentPage').val());
    var hash = '';

    if (( associationId != null )&&(associationId != ''))
    {
        /*$('#pagelink-url').val(systemUrl + selectionURL + '/?associationId=' + associationId + '&page='+currentPage);*/
        get_product_list_content('product', '&associationId='+associationId+'&page='+currentPage, '');
    }
    else
    {
        listId = tmpListId;

        var pageParam = '';
        if ( currentPage > 1 )
        {
            pageParam = '-p' + currentPage;
        }
        var selBits = getSelectionBits();

        var params = getListParams(false);

        if ( params['noSelections'] == true )
        {
            var cmd = '';
            if ( currentPage > 1 )
            {
                cmd = '-p'+currentPage;
            }
            lastCmd = cmd;

            get_breadCrumb(params['selection'], selBits);

            var bitsUrl = '';
            if ( selBits !== '000' )
            {
                bitsUrl = '&view=' + selBits;
            }

            /*$('#pagelink-url').val(systemUrl + selectionURL + '/' + params['default'] + '&page='+currentPage + bitsUrl);*/
            get_product_list_content('product', params['selection']+'&page='+currentPage + bitsUrl, tmpListId);
            $.history.load(cmd);
        }
        else
        {
            var uuid = get_selection_content('searchRef', params['selection']);

            if ( uuid != '' )
            {
                var cmd = selBits + '-' + uuid + pageParam;
                lastCmd = cmd;

                get_breadCrumb(params['selection'], selBits);

                /*$('#pagelink-url').val(systemUrl + selectionURL + '/?uuid=' +selBits + '-' + uuid + '&page='+currentPage);*/
                get_product_list_content('product', '&uuid=' + selBits + '-' + uuid + '&page='+currentPage, tmpListId);

                $.history.load(cmd);
            }
        }
    }
}


// bit1 = view (0: group, 1: supplier, 2: brand, 3: s2010)
function getSelectionBit1()
{
    var bit1 = 0;
    if ((selectionType == null)||(selectionType == '')||(selectionType == 'group'))
    {
        bit1 = 0;
    }
    else if ( selectionType == 'supplier' )
    {
        bit1 = 1;
    }
    else if ( selectionType == 'brand' )
    {
        bit1 = 2;
    }
    else if ( selectionType == 's2010' )
    {
        bit1 = 3;
    }
    return bit1;
}

// bit2 = name selection view (0: harmonized, 1: all)
function getSelectionBit2()
{
    if (( namesAll != null )&&( namesAll == true ) || (namesAll == 1) )
    {
        return 1;
    }
    return 0;
}

// bit3 = selection view (0: supplier, 1: brand, 2: serie, 3: group) //, 4: s2010 code)
function getSelectionBit3()
{
    var tab = getActiveTab();

    if ( tab == 'supplier' )
    {
        return 0;
    }
    else if ( tab == 'brand' )
    {
        return 1;
    }
    else if ( tab == 'serie' )
    {
        return 2;
    }
    else if ( tab == 'group' )
    {
        return 3;
    }
/*    else if ( tab == 's2010' )
    {
        return 4;
    }*/

    return 0;
}

function getSelectionBitsDefault()
{
    var bit1 = getSelectionBit1();
    var bit2 = '0';
    var bit3 = '0';

    if ((selectionType == null)||(selectionType == '')||(selectionType == 'group')||(selectionType == 's2010'))
    {
        bit3 = '0';
    }
    else if ( selectionType == 'supplier' )
    {
        bit3 = '1';
    }
    else if ( selectionType == 'brand' )
    {
        bit3 = '2';
    }
/*    else if ( selectionType == 's2010' )
    {
        bit = '4';
    }*/
    var bitsel = bit1 + '' + bit2 + '' + bit3;
    return bitsel;
}

function getSelectionBits()
{
    var bit1 = getSelectionBit1();
    var bit2 = getSelectionBit2();
    var bit3 = getSelectionBit3();
    var bitsel = bit1 + '' + bit2 + '' + bit3;
    return bitsel;
}

function getSelectionBitsFromUUID(uuid)
{
    if ((uuid==null) || (uuid == ''))
    {
        return '000';
    }
    if (uuid.length == 3)
    {
        return uuid;
    }
    if (uuid.length > 3)
    {
        var parts = uuid.split('-');
        if (parts.length == 0)
        {
            return '000';
        }
        if (parts[0].length == 3 )
        {
            return parts[0];
        }
        return '';
    }
    return '';
}

function clearSelections(selectionTitle)
{
    for (i=1; i<=columns; i++)
    {
        if (( selectionTitle == null ) || ( selectionTitle == '' ))
        {
            emptyDiv('serie-selection-col-' + i);
            emptyDiv('brand-selection-col-' + i);
            emptyDiv('supplier-selection-col-' + i);
            emptyDiv('group-selection-col-' + i);
           // emptyDiv('s2010-selection-col-' + i);
        }
        else
        {
            emptyDiv(selectionTitle + '-selection-col-' + i);
        }
    }
}

function addSelectionItem(columnId, inputName, inputId, value, title, selected, type, extraClass)
{
    $('#' + columnId).append('<div class=\"selection-item ' + extraClass+ '\"><input type=\"checkbox\" name=\"'
        + inputName + '\" id=\"' + inputId + '\" class=\"selection-item-' + type + '\" value=\"'
        + value + '\"' + selected
        +' onClick=\"addSelection(\'' + type + '\');\">' + title + '</div>');
}

function isSelectedSupplier(id)
{
    if (( id == null ) || ( id == '' ) || ( osuSel == null )) return false;

    for (var i=0; i < osuSel.length; i++)
    {
        if (id == osuSel[i]) return true;
    }
    return false;
}

function isSelectedBrand(id)
{
    if (( id == null ) || ( id == '' ) || ( obrSel == null )) return false;

    for (var i=0; i < obrSel.length; i++)
    {
        if (id == obrSel[i]) return true;
    }
    return false;
}

function isSelectedSerie(id)
{
    if (( id == null ) || ( id == '' ) || ( oseSel == null )) return false;

    for (var i=0; i < oseSel.length; i++)
    {
        if (id == oseSel[i]) return true;
    }
    return false;
}

function isSelectedGroup(id)
{
    if (( id == null ) || ( id == '' ) || ( grpSel == null )) return false;

    for (var i=0; i < grpSel.length; i++)
    {
        if (id == grpSel[i]) return true;
    }
    return false;
}

/*function isSelectedS2010Code(id)
{
    if (( id == null ) || ( id == '' ) || ( s2010Sel == null )) return false;

    for (var i=0; i < s2010Sel.length; i++)
    {
        if (id == s2010Sel[i]) return true;
    }
    return false;
}*/

function isSelectedName(id)
{
    if (( id == null ) || ( id == '' ) || ( optSel == null )) return false;

    var idm = null;

    var ids = new String(id);
    var posi = ids.indexOf(',');
    if ( posi !=-1 )
    {
        idm = ids.split(',');
    }

    for (var i=0; i < optSel.length; i++)
    {
        if (id == optSel[i]) return true;

        if ( idm !==null )
        {
            for (var idx=0; idx < idm.length; idx++ )
            {
                if ( idm[idx] == optSel[i] ) return true;
            }
        }
    }

    return false;
}

function getNameItemCount(showAll)
{
    if (( optOrder == null ) || ( optOrder == '' ))
    {
        return 0;
    }
    if ( showAll )
    {
        return optOrder.length;
    }
    else
    {
        sum = 0;
        for(i=0; i<optOrder.length; i++)
        {
            id = '' + optOrder[i];
            if ((optHarm[id] != null) && (optHarm[id] == 1))
            {
                sum++;
            }
        }
        return sum;
    }
}

function getSelected(optOrder, prefix)
{
    var sel = new Array();
    var cnt = 0;
    for(i=0; i<optOrder.length; i++)
    {
        id = '' + optOrder[i];
        var elem = getElemById(prefix+ id);
        if ( elem != null )
        {
            if ( elem.checked == true )
            {
                sel[cnt] = id;
                cnt++;
            }
        }
    }
    return sel;
}

function checkSelectedNames(showAll)
{
    if (( showAll == null ) || ( showAll == '' )) showAll = 0;

    optTmp = new Array();
    //optSel = new Array();
    cnt = 0;
    for(i=0; i<optOrder.length; i++)
    {
        id = '' + optOrder[i];
        if ( showAll || (optHarm[id] == 1))
        {
            var elem = getElemById('name_sel_'+ id);
            if ( elem != null )
            {
                if ( elem.checked == true )
                {
       //             optSel[id] = 1;
                    optTmp[cnt] = id;
                    cnt++;
                }
            }
        }
    }
    optSel = optTmp;
    return optTmp;
}

function getItemsPerColumn(itemCount, cols)
{
    itemsPerColumn = itemCount / cols;
    itemsPerColumnInt = parseInt(itemsPerColumn, 10);
    if ( itemsPerColumnInt < itemsPerColumn )
    {
        itemsPerColumn = itemsPerColumnInt+1;
    }
    return itemsPerColumn;
}

function isVisible(label)
{
    elem = $(label);
    if (( elem != null ) && ( elem.length > 0 ))
    {
        return !(elem.hasClass('hidden'));
    }
    return false;
}

function minimize(label)
{
    hide('#' + label + '-selection');
    hide('#' + label + '-minimize');
    show('#' + label + '-maximize');
}

function maximize(label)
{
    show('#' + label + '-selection');
    show('#' + label + '-minimize');
    hide('#' + label + '-maximize');
}

function addSelection(type)
{
    clearTimeout( timer );
    if ( type == 'name' )
    {
        setSelectionInfo('(' + arrText['JS_Searching'] + ')');

        var tab = getActiveTab();
        if ( tab == 'serie' )
        {
            $('.selection-item-serie').prop('disabled', true);
        }
        else if ( tab == 'brand' )
        {
            $('.selection-item-brand').prop('disabled', true);
        }
        else if ( tab == 'supplier' )
        {
            $('.selection-item-supplier').prop('disabled', true);
        }
        else if ( tab == 'group' )
        {
            $('.selection-item-group').prop('disabled', true);
        }
        //handleSelection(type);
        //timer = setTimeout( 'handleSelection(\''+type+ '\');', 650);
        timer = setTimeout( 'handleSelection(\''+type+ '\');', 10);
    }
    else
    {
        show('#product-list');
        //handleSelection(type);
        timer = setTimeout( 'handleSelection(\''+type+ '\');', 8 );
    }
}

function handleSelection(type)
{
    $('#currentPage').val(1);

    if ( type == 'name' )
    {
        var tab = getActiveTab();

        if ( tab == 'supplier' )
        {
            populateSuppliers();
        }
        else if ( tab == 'brand' )
        {
            populateBrands();
        }
        else if ( tab == 'serie' )
        {
            populateSeries();
        }
        else if ( tab == 'group' )
        {
            populateGroups();
        }
/*        else if ( tab == 's2010' )
        {
            populateS2010();
        }*/
        populateProductList();
    }
    else
    {
        populateProductList();
    }
}

function getActiveTab()
{
    if ( isVisible('#supplier-selection') ) return 'supplier';
    if ( isVisible('#brand-selection') ) return 'brand';
    if ( isVisible('#serie-selection') ) return 'serie';
    if ( isVisible('#group-selection') ) return 'group';
    return false;
}

function getNameSelections()
{
    var sel = null;
    if ( $('#selection-names-harmonized').hasClass('selected') == true )
    {
       sel = checkSelectedNames(false);
    }
    else
    {
       sel = checkSelectedNames(true);
    }

    if (( sel == null )||(sel=='')||(sel.length == 0))
    {
        return false;
    }
    return sel;
}

function getSelectionParams()
{
    sel = getNameSelections();

    if ( sel == false ) return false;

    var params = '';
    var info = '';
    if ( sel.length > 0 )
    {
        params += '&name=';// + sel;
        for(j=0; j<sel.length; j++)
        {
            if ( j > 0 )  params += ',';
            params += (sel[j]+'');
        }
        if ( sel.length == 1 )
        {
            info = '(' + opt[sel[0]] + ')';
        }
        else
        {
            info = '(' + arrText['Multiple_Selections'] + ')';
        }
    }
    else
    {
        info = $('#name-selection-title-info').html();
    }

    for (var param in selections )
    {
        if (param != 'name')
        {
            params += '&'+param+'='+selections[param];
        }
    }

    if ( info != '' )
    {
        selectionInfo = info;
    }

    return params;
}

function getDefaultParams()
{
    var params = '';
    for (var param in selections )
    {
        if (param != 'name')
        {
            params += '&'+param+'='+selections[param];
        }
    }
    return params;
}

function getListParams()
{
    var params = new Array();
    params['selection'] = '';
    params['default'] = '';
    params['noSelections'] = true;

    for (var param in selections )
    {
        if (param != 'name')
        {
            params['selection'] += '&'+param+'='+selections[param];
            if ( params['default'] == '' )
                params['default'] = '?';
            else
                params['default'] += '&';
            params['default'] += param+'='+selections[param];
        }
    }

    var tab = getActiveTab();

    if ( tab != false )
    {
        sel = getNameSelections();

        if (( sel != false ) && (sel != ''))
        {
            params['selection'] += '&name=' + arrayToParamString(sel);
            params['noSelections'] = false;
        }
    }

    if ( tab == 'supplier' )
    {
        var osuSel = getSelected(osuOrder, 'supplier_sel_');
        if ( osuSel.length > 0 )
        {
            params['selection'] += '&supplier=' + arrayToParamString(osuSel);
            params['noSelections'] = false;
        }
    }
    else if ( tab == 'brand' )
    {
        obrSel = getSelected(obrOrder, 'brand_sel_');
        if (obrSel.length > 0 )
        {
            params['selection'] += '&brand=' + arrayToParamString(obrSel);
            params['noSelections'] = false;
        }
    }
    else if (tab == 'serie' )
    {
        oseSel = getSelected(oseOrder, 'serie_sel_');
        if (oseSel.length > 0 )
        {
            params['selection'] += '&serie=' + arrayToParamString(oseSel);
            params['noSelections'] = false;
        }
    }
    else if (tab == 'group' )
    {
        grpSel = getSelected(grpOrder, 'group_sel_');
        if (grpSel.length > 0 )
        {
            params['selection'] += '&group=' + arrayToParamString(grpSel);
            params['noSelections'] = false;
        }
    }
/*    else if (tab == 's2010' )
    {
        s2010Sel = getSelected(s2010Order, 's2010_sel_');
        if (s2010Sel.length > 0 )
        {
            params['selection'] += '&code=' + arrayToParamString(s2010Sel);
            params['noSelections'] = false;
        }
    }*/

    return params;
}

function arrayToParamString(arr)
{
    if ((arr == null) || (arr == ''))
    {
        return '';
    }

    var limiter = ',';

    var ret = '';

    if ( arr.length > 0 )
    {
        for(j=0; j<arr.length; j++)
        {
            if ( j > 0 ) ret += limiter;
            ret += new String(arr[j]);
        }
    }
    return arr;
}


function setPage(page)
{
    var val = parseInt(page);
    if ( val > 0 )
    {
        $('#currentPage').val(val);
        populateProductList();
    }
}

function get_product_list_content(type, params, curListId)
{
    $.ajax({
           type: "POST",
           data: "Module=Product&File=index&Action=getData&type="+type+params,
           url: ajaxUrl,
           async: true,
           success: function(listContent) {
              if ( listContent != '' )
              {
                  if (( listId != '' ) && ( curListId != listId ))
                  {
                    return;
                  }
                  pos = listContent.indexOf('[PageSelectionEnd]');
                  if ( pos > -1 )
                  {
                      var pageSelection = listContent.substr(0, pos);
                      listContent = listContent.substr(pos+18);
                      $('#page-selection').html(pageSelection);
                      $('#page-selection-footer').html(pageSelection);
                  }
                  pos = listContent.indexOf('[BasketAddLinkTitle]');
                  if ( pos > -1 )
                  {
                      var basketAddLinkTitle = listContent.substr(0, pos);
                      listContent = listContent.substr(pos+20);
                      setBasketAddFunctionState(true, params, basketAddLinkTitle);
                  }
                  else
                  {
                      setBasketAddFunctionState(true, '', '');
                  }

                  hide('#loader-list');
                  $('#product-rows').html(listContent);

                  if (( associationId != null )&&(associationId != ''))
                  {
                        if ( listContent == '' )
                        {
                            hide('#similar-products');
                        }
                        else
                        {
                            // lije
                            //show('#similar-products');
                            $('#similar-products').css('visibility', 'visible');

                        }
                  }
                  setTableTitlesWidth();
              }
              else
              {
                  setBasketAddFunctionState(true, '', '');
                  hide('#loader-list');
              }
          }
    });
}


function get_selection_content(type, params)
{
    var content = $.ajax({
                         type: "POST",
                         data: "Module=Product&File=index&Action=getData&type="+type+params,
                         url: ajaxUrl,
                         async: false
                         }).responseText;
    return content;
}

function set_parameter(type, val)
{
    var params = '&value=' + val;
    var content = $.ajax({
                         type: "POST",
                         data: "Module=Product&File=index&Action=setParam&type="+type+params,
                         url: ajaxUrl,
                         async: false
                         }).responseText;
    return content;
}


function get_breadCrumb(params, selBits)
{
    bitsUrl = '&view=' + selBits;

    var dataUrl = "Module=Product&File=index&Action=getData&type=path&selection="+selectionType+params+bitsUrl;

    $.ajax({
           type: "POST",
           data: dataUrl,
           url: ajaxUrl,
           async: true,
           success: function(pathContent) {
              $('#breadcrumb-path').html(pathContent);
          }
          });
    return true;
}

