var PlatformType =
{
    All : "0",
    Desktop : "1",
    Notebook : "2"
}

var SelectionManager = 
{
    Platforms : new Object(),

    AddPlatform : function( platformType, procContainer )
    {
        this.Platforms[platformType] = procContainer;
    },

    GetPlatform : function( platformType )
    {
        if( platformType == PlatformType.Desktop )
        {
            return this.Platforms[PlatformType.Desktop];
        }
        else if( platformType == PlatformType.Notebook )
        {
            return this.Platforms[PlatformType.Notebook];
        }
        else
        {
            var compositeList =
            {
                IsComposite : true,
                DesktopProcessors : this.Platforms[PlatformType.Desktop],
                NotebookProcessors : this.Platforms[PlatformType.Notebook]
            }
            return compositeList;
        }
    },

    AssignPlatformProcessors : function( platformType )
    {
        var processorSelector = document.getElementById( "processorSelect" );
        
        //-- Clear out the current selection
        if( processorSelector.options.length > 1 )
        {
            var firstItem = processorSelector.options[0];
            processorSelector.options.length = null;
            processorSelector.options[0] = firstItem;
        }
        
        var platformProcs = this.GetPlatform( platformType );
        
        if( platformProcs.IsComposite )
        {
            this.CopyAttributes( processorSelector, platformProcs.DesktopProcessors );
            this.CopyAttributes( processorSelector, platformProcs.NotebookProcessors );
        }
        else
        {
            this.CopyAttributes( processorSelector, platformProcs );
        }
    },

    CopyAttributes : function( targetSelector, srcCollection )
    {
        var procRef;
        var idx = targetSelector.length;
        
        for( var procKey in srcCollection.Processors )
        {
            procRef = srcCollection.Processors[procKey];
            targetSelector.options.length++;
            targetSelector.options[idx].value = procRef.ID;
            targetSelector.options[idx].text = procRef.Description;
            idx++;
        }
    }
}

//-- Event delegates
window.onload = function()
{
    SelectionManager.AddPlatform( PlatformType.Desktop, eval( {"Processors":[{"ID":"3","Description":"AMD Athlon X2 Dual-Core"},{"ID":"1","Description":"AMD Athlon"},{"ID":"4","Description":"AMD Sempron"}]} ) );
    SelectionManager.AddPlatform( PlatformType.Notebook, eval( {"Processors":[{"ID":"12","Description":"AMD Turion 64 X2 Mobile Technologie"},{"ID":"7","Description":"AMD Turion 64 Mobile Technologie"},{"ID":"6","Description":"Mobile AMD Sempron"}]} ) );
    SelectionManager.AssignPlatformProcessors( PlatformType.All );
}

function Search()
{
    var selPlatform = document.getElementById( "platformSelect" );    
    var selProcs = document.getElementById( "processorSelect" );

    var indexPlatforms = selPlatform.options[selPlatform.selectedIndex].value;
    var indexProcs = selProcs.options[selProcs.selectedIndex].value;    
    var query = '/de-de/';
    var tracker = 'sid=qsearch';
    
    if(indexPlatforms > 0 && indexProcs < 1 )
    {
        //if Platform is the only one selected
        query = query + 'platforms/' + indexPlatforms + '.aspx' + '?' + tracker;
    }
    else if(indexPlatforms < 1 && indexProcs > 0 )
    {
        //if Processor is the only one selected
        query = query + 'products/' + indexProcs + '.aspx' + '?' + tracker;
    }    
    else if(indexPlatforms > 0 && indexProcs > 0 )
    {
        //if Platform and Processor are selected
        query = query + 'products/' + indexProcs + '/' + indexPlatforms + '.aspx' + '?' + tracker;
    }    
    else
    {
        //Nothing selected
        alert('You must select an option')
        return;
    }
    window.location = query;
}

function Reset()
{
    var selPlatform = document.getElementById( "platformSelect" );
    selPlatform.selectedIndex = 0;
    SelectionManager.AssignPlatformProcessors( PlatformType.All );
}