var _aryAllGroups = null;
var _aryAllBrands = null;
var _aryAllColors = null;

var _arySelectedGroups = null;
var _arySelectedBrands = null;
var _arySelectedColors = null;

var _groups = null;
var _brands = null;
var _colors = null;

function subgroup_all_onclick(checkbox)
{
    if(checkbox.checked)
        _groups.value = "all";
    else
        _groups.value = "";
    
    init();
    
    checkGroups();
    
    var aryValidBrands = getValidBrands();
    enableBrands(aryValidBrands);
    validateSelectedBrands(aryValidBrands);
    checkBrands();
    
    var aryValidColors = getValidColors();
    enableColors(aryValidColors);
    validateSelectedColors(aryValidColors);
    checkColors();
}

function brand_all_onclick(checkbox)
{
    if(checkbox.checked)
        _brands.value = "all";
    else
        _brands.value = "";
    
    init();
    
    checkBrands();
    
    var aryValidColors = getValidColors();
    enableColors(aryValidColors);
    validateSelectedColors(aryValidColors);
    checkColors();
}

function color_all_onclick(checkbox)
{
    if(checkbox.checked)
        _colors.value = "all";
    else
        _colors.value = "";     
    
    init();
    
    checkColors();
}

function subgroup_onclick(checkbox, id)
{
    init();
    
    if(checkbox.checked)
    {
        _arySelectedGroups.push(id);
        
        if(_brands.value.length == 0)
        {
            document.getElementById("brand_all").disabled = false;
            document.getElementById("brand_all").checked = true;
            _brands.value = "all";
        }
        
        if(_colors.value.length == 0)
        {
            document.getElementById("color_all").disabled = false;
            document.getElementById("color_all").checked = true;
            _colors.value = "all";
        }
    }
    else
    {
        for(var i = 0; i < _arySelectedGroups.length; i++)
        {
            if(parseInt(id, 10) == parseInt(_arySelectedGroups[i], 10))
            {
                _arySelectedGroups.splice(i, 1);
                break;
            }
        }
    }
    
    _groups.value = arrayToString(_arySelectedGroups);
    
    checkGroups();
    
    var aryValidBrands = getValidBrands();
    enableBrands(aryValidBrands);
    validateSelectedBrands(aryValidBrands);
    checkBrands();
    
    var aryValidColors = getValidColors();
    enableColors(aryValidColors);
    validateSelectedColors(aryValidColors);
    checkColors();
}

function brand_onclick(checkbox, id)
{
    init();
    
    if(checkbox.checked)
    {
        _arySelectedBrands.push(id);
        
        if(_colors.value.length == 0)
        {
            document.getElementById("color_all").disabled = false;
            document.getElementById("color_all").checked = true;
            _colors.value = "all";
        }
    }
    else
    {
        for(var i = 0; i < _arySelectedBrands.length; i++)
        {
            if(parseInt(id, 10) == parseInt(_arySelectedBrands[i], 10))
            {
                _arySelectedBrands.splice(i, 1);
                break;
            }
        }
    }
    
    _brands.value = arrayToString(_arySelectedBrands);
    
    checkBrands();
    
    var aryValidColors = getValidColors();
    enableColors(aryValidColors);
    validateSelectedColors(aryValidColors);
    checkColors();
}

function color_onclick(checkbox, color)
{
    init();
    
    if(checkbox.checked)
        _arySelectedColors.push(color);
    else
    {
        for(var i = 0; i < _arySelectedColors.length; i++)
        {
            if(color == _arySelectedColors[i])
            {
                _arySelectedColors.splice(i, 1);
                break;
            }
        }
    }
    
    _colors.value = arrayToString(_arySelectedColors);
    
    checkColors();
}

function init()
{
    eval("_aryAllGroups = subgroups_" + _mainGroupId);
    eval("_aryAllBrands = brands_" + _mainGroupId);
    eval("_aryAllColors = colors_" + _mainGroupId);
    
    _groups = document.getElementById("SubProductGroupIDs");
    _brands = document.getElementById("BrandIDs");
    _colors = document.getElementById("Colors");
    
    if(_groups.value.toLowerCase() == "all" || _groups.value.length == 0)
        _arySelectedGroups = new Array();
    else
        _arySelectedGroups = _groups.value.split(",");
    
    if(_brands.value.toLowerCase() == "all" || _brands.value.length == 0)
        _arySelectedBrands = new Array();
    else
        _arySelectedBrands = _brands.value.split(",");
        
    if(_colors.value.toLowerCase() == "all" || _colors.value.length == 0)
        _arySelectedColors = new Array();
    else
        _arySelectedColors = _colors.value.split(",");  
}

function getValidBrands()
{
    var aryValidBrands = new Array();
    
    if(_groups.value.toLowerCase() == "all")
    {
        for(var i = _aryAllBrands.length - 1; i >= 0; i--)
        {
            //set id and index
            var ix = _aryAllBrands[i].indexOf("|");            
            var id = _aryAllBrands[i].substr(ix + 1);
            
            aryValidBrands.push(id);
        }
    }
    else if(_groups.value.length == 0)
    {
        //
    }
    else if(_groups.value.length > 0)
    {
        for(var i = 0; i < _arySelectedGroups.length; i++)
        {
            eval("var brands = brands_" + _arySelectedGroups[i]);
            for(var j = 0; j < brands.length; j++)
            {
                var ix = brands[j].indexOf("|");            
                var id = brands[j].substr(ix + 1);
                
                var brandFound = false;                
                for(var k = 0; k < aryValidBrands.length; k++)
                {
                    if(parseInt(id, 10) == parseInt(aryValidBrands[k], 10))
                    {
                        brandFound = true;
                        break;
                    }
                }
                
                if(!brandFound)
                    aryValidBrands.push(id);
            }
        }
    }
    
    return aryValidBrands;
}

function getValidColors()
{
    var aryValidColors = new Array();
    
    if(_groups.value.toLowerCase() == "all")
    {
        if(_brands.value.toLowerCase() == "all")
        {
            aryValidColors = _aryAllColors;
        }
        else if(_brands.value.length == 0)
        {
            //
        }
        else if(_brands.value.length > 0)
        {
            for(var i = 0; i < _aryAllGroups.length; i++)
            {
                var ix = _aryAllGroups[i].indexOf("|");            
                var id = _aryAllGroups[i].substr(ix + 1);
                
                for(var j = 0; j < _arySelectedBrands.length; j++)
                {
                    eval("var colors = colors_" + id + "_" + _arySelectedBrands[j]);
                    for(var k = 0; k < colors.length; k++)
                    {
                        var colorFound = false;
                        for(var m = 0; m < aryValidColors.length; m++)
                        {
                            if(colors[k].toLowerCase() == aryValidColors[m].toLowerCase())
                            {
                                colorFound = true;
                                break;
                            }
                        }
                        
                        if(!colorFound)
                            aryValidColors.push(colors[k]);
                    }
                }
            }
        }
    }
    else if(_groups.value.length == 0)
    {
        //
    }
    else if(_groups.value.length > 0)
    {
        if(_brands.value.toLowerCase() == "all")
        {
            for(var i = 0; i < _arySelectedGroups.length; i++)
            {                
                for(var j = 0; j < _aryAllBrands.length; j++)
                {
                    var ix = _aryAllBrands[j].indexOf("|");            
                    var id = _aryAllBrands[j].substr(ix + 1);
                
                    eval("var colors = colors_" + _arySelectedGroups[i] + "_" + id);
                    for(var k = 0; k < colors.length; k++)
                    {
                        var colorFound = false;
                        for(var m = 0; m < aryValidColors.length; m++)
                        {
                            if(colors[k].toLowerCase() == aryValidColors[m].toLowerCase())
                            {
                                colorFound = true;
                                break;
                            }
                        }
                        
                        if(!colorFound)
                            aryValidColors.push(colors[k]);
                    }
                }
            }
        }
        else if(_brands.value.length == 0)
        {
            //
        }
        else if(_brands.value.length > 0)
        {
            for(var i = 0; i < _arySelectedGroups.length; i++)
            {                
                for(var j = 0; j < _arySelectedBrands.length; j++)
                {
                    eval("var colors = colors_" + _arySelectedGroups[i] + "_" + _arySelectedBrands[j]);
                    for(var k = 0; k < colors.length; k++)
                    {
                        var colorFound = false;
                        for(var m = 0; m < aryValidColors.length; m++)
                        {
                            if(colors[k].toLowerCase() == aryValidColors[m].toLowerCase())
                            {
                                colorFound = true;
                                break;
                            }
                        }
                        
                        if(!colorFound)
                            aryValidColors.push(colors[k]);
                    }
                }
            }
        }
    }
    
    return aryValidColors;
}

function enableBrands(validBrands)
{
    document.getElementById("brand_all").disabled = true;
    
    for(var i = _aryAllBrands.length - 1; i >= 0; i--)
    {
        //set id and index
        var ix = _aryAllBrands[i].indexOf("|");            
        var id = _aryAllBrands[i].substr(ix + 1);
        
        document.getElementById("brand_" + id).disabled = true;
        
        for(var j = 0; j < validBrands.length; j++)
        {
            if(parseInt(id, 10) == parseInt(validBrands[j], 10))
            {
                document.getElementById("brand_" + id).disabled = false;
                document.getElementById("brand_all").disabled = false;
                break;
            }
        }
        
        if(document.getElementById("brand_" + id).disabled)
            document.getElementById("brand_" + id).checked = false;
            
        if(document.getElementById("brand_all").disabled)
            document.getElementById("brand_all").checked = false;
        
    }
}

function enableColors(validColors)
{
    document.getElementById("color_all").disabled = true;
    
    for(var i = _aryAllColors.length - 1; i >= 0; i--)
    {
        document.getElementById("color_" + _aryAllColors[i]).disabled = true;
        
        for(var j = 0; j < validColors.length; j++)
        {
            if(_aryAllColors[i].toLowerCase() == validColors[j].toLowerCase())
            {
                document.getElementById("color_" + _aryAllColors[i]).disabled = false;
                document.getElementById("color_all").disabled = false;
                break;
            }
        }
        
        if(document.getElementById("color_" + _aryAllColors[i]).disabled)
            document.getElementById("color_" + _aryAllColors[i]).checked = false;
            
        if(document.getElementById("color_all").disabled)
            document.getElementById("color_all").checked = false;
        
    }
}

function validateSelectedBrands(validBrands)
{
    if(validBrands.length == 0)
    {
        _brands.value = "";
    }
    else
    {
        if(_brands.value.toLowerCase() == "all")
        {
            //
        }
        else if(_brands.value.length == 0)
        {
            //
        }
        else
        {
            var aryNewSelectedBrands = new Array();
            for(var i = _arySelectedBrands.length - 1; i >= 0; i--)
            {
                for(var j = 0; j < validBrands.length; j++)
                {
                    if(parseInt(_arySelectedBrands[i], 10) == parseInt(validBrands[j], 10))
                    {
                        aryNewSelectedBrands.push(_arySelectedBrands[i]);
                        break;
                    }
                }
            }
            _arySelectedBrands = aryNewSelectedBrands;
            _brands.value = arrayToString(_arySelectedBrands);
        }
    }
}

function validateSelectedColors(validColors)
{
    if(validColors.length == 0)
    {
        _colors.value = "";
    }
    else
    {
        if(_colors.value.toLowerCase() == "all")
        {
            //
        }
        else if(_colors.value.length == 0)
        {
            //
        }
        else
        {
            var aryNewSelectedColors = new Array();
            for(var i = _arySelectedColors.length - 1; i >= 0; i--)
            {
                for(var j = 0; j < validColors.length; j++)
                {
                    if(_arySelectedColors[i].toLowerCase() == validColors[j].toLowerCase())
                    {
                        aryNewSelectedColors.push(_arySelectedColors[i]);
                        break;
                    }
                }
            }
            _arySelectedColors = aryNewSelectedColors;
            _colors.value = arrayToString(_arySelectedColors);
        }
    }
}

function checkGroups()
{
    if(_groups.value.toLowerCase() == "all")
    {
        document.getElementById("subgroup_all").checked = true;
        
        for(var i = 0; i < _aryAllGroups.length; i++)
        {
            var ix = _aryAllGroups[i].indexOf("|");            
            var id = _aryAllGroups[i].substr(ix + 1);
            
            document.getElementById("subgroup_" + id).checked = false;
        }
    }
    else if(_groups.value.length == 0)
    {
        //
    }
    else
    {
        document.getElementById("subgroup_all").checked = false;
        
        for(var i = 0; i < _arySelectedGroups.length; i++)
            document.getElementById("subgroup_" + _arySelectedGroups[i]).checked = true;
    }
}

function checkBrands()
{
    if(_brands.value.toLowerCase() == "all")
    {
        document.getElementById("brand_all").checked = true;
    
        for(var i = 0; i < _aryAllBrands.length; i++)
        {
            //set id and index
            var ix = _aryAllBrands[i].indexOf("|");            
            var id = _aryAllBrands[i].substr(ix + 1);
            
            document.getElementById("brand_" + id).checked = false;
        }
    }
    else if(_brands.value.length == 0)
    {
        document.getElementById("brand_all").checked = false;
    
        for(var i = 0; i < _aryAllBrands.length; i++)
        {
            //set id and index
            var ix = _aryAllBrands[i].indexOf("|");            
            var id = _aryAllBrands[i].substr(ix + 1);
            
            document.getElementById("brand_" + id).checked = false;
        }
    }
    else if(_brands.value.length > 0)
    {
        document.getElementById("brand_all").checked = false;
        
        for(var i = 0; i < _arySelectedBrands.length; i++)
            document.getElementById("brand_" + _arySelectedBrands[i]).checked = true;
    }
}

function checkColors()
{
    if(_colors.value.toLowerCase() == "all")
    {
        document.getElementById("color_all").checked = true;
    
        for(var i = 0; i < _aryAllColors.length; i++)
            document.getElementById("color_" + _aryAllColors[i]).checked = false;
    }
    else if(_colors.value.length == 0)
    {
        document.getElementById("color_all").checked = false;
    
        for(var i = 0; i < _aryAllColors.length; i++)
            document.getElementById("color_" + _aryAllColors[i]).checked = false;
    }
    else if(_colors.value.length > 0)
    {
        document.getElementById("color_all").checked = false;
        
        for(var i = 0; i < _arySelectedColors.length; i++)
            document.getElementById("color_" + _arySelectedColors[i]).checked = true;
    }
}

function checkFilter()
{
    if(_groups.value.length == 0)
        return false;
    if(_brands.value.length == 0)
        return false;
    if(_colors.value.length == 0)
        return false;
    
    return true;
}

function arrayToString(ary)
{
    var s = "" + ary;
    
    while(s.substring(0, 1) == ",")
        s = s.substring(1, s.length)
    
    return s;
}
