﻿function PopulateMakes(controlSetName)
{
    var controlSet = GetControlSet(controlSetName);
    if ( document.getElementById(controlSet.makeId) == null ) return;

    var makeDropDown = document.getElementById(controlSet.makeId);
    selectedMakeIndex = makeDropDown.selectedIndex;

    if (selectedMakeIndex < 0)
        selectedMakeIndex = 0;

    makeDropDown.length = 0;
    
    
    var optionDefault = new Option("All Makes", "any");
    makeDropDown.options[0] = optionDefault;
    
    for(var i = 0; MakeArray[i] != null; i++) 
    {
        var option = new Option(MakeArray[i], MakeArray[i]);
        makeDropDown.options[i+1] = option;
    }

    if ( controlSet.selectedMakeName != '')
    {
        // This happens for postback
	    for(var i = 0; MakeArray[i] != null; i++) 
	    {
	        if (MakeArray[i].toLowerCase() == controlSet.selectedMakeName.toLowerCase() )
	        {
	            makeDropDown.value = MakeArray[i];
	            break;
	        }
	    }
    }
    else
    {
        // This happens at back button
        makeDropDown.selectedIndex = selectedMakeIndex;
    }
    
    MakeModelTrimActivate(controlSet.makeId, controlSet.modelId);
    
    if (controlSet.makeFilter != "")
    {
        $.FilterMakeDD(controlSet.makeFilter, controlSet.makeId);
    }
    
    PopulateModels(controlSet.setName);
}

function PopulateModels(controlSetName)
{
    var controlSet = GetControlSet(controlSetName);
    if ( document.getElementById(controlSet.makeId) == null ) return;
    if ( document.getElementById(controlSet.modelId) == null ) return;

    var makeDropDown = document.getElementById(controlSet.makeId);
    var modelDropDown = document.getElementById(controlSet.modelId);
    selectedModelIndex = modelDropDown.selectedIndex;

    if (selectedModelIndex < 0)
        selectedModelIndex = 0;

    modelDropDown.length = 0;

    var optionDefault = new Option("All Models", "any");
    modelDropDown.options[0] = optionDefault;

    var selectedMakeIndex = makeDropDown.options.selectedIndex;
    // if selected make is not same as index, get the right index
    if( MakeArray[selectedMakeIndex-1] != makeDropDown.options[selectedMakeIndex].value )
    {
        for( var i=0; i < MakeArray.length; i++ )
        {
            if( MakeArray[i] == makeDropDown.options[selectedMakeIndex].value )
            {
                // found make, so break;
                selectedMakeIndex = i + 1;
                break;
            }
        }
    }

    modelDropDown.options.length = 1;
    
    if ( selectedMakeIndex == 0 ) return;

    var currentModelArray = ModelArray[selectedMakeIndex-1]
	for(var i = 0; currentModelArray[i] != null; i++) 
	{
        var option = new Option(currentModelArray[i], currentModelArray[i]);
        modelDropDown.options[i+1] = option;
	}
    
    if ( controlSet.selectedModelName != '')
    {
        // This happens for postback
	    for(var i = 0; currentModelArray[i] != null; i++) 
	    {
	        if (currentModelArray[i].toLowerCase() == controlSet.selectedModelName.toLowerCase() )
	        {
	            modelDropDown.value = currentModelArray[i];
	            break;
	        }
	    }
    }
    else
    {
        // This happens at back button
        modelDropDown.selectedIndex = selectedModelIndex;
    }
 
    var cpoDropDown = document.getElementById(controlSet.cpoId);
    var prevCheckStatus;
   
    if (controlSet.makeFilter != "")
    {
        $.FilterModelDD(controlSet.makeFilter, controlSet.modelId, $("#"+controlSet.makeId).val());
    }
    
    //Filter CPO Models
    if (cpoDropDown != null) {
        if (cpoDropDown.checked) {
            ModelDDRemoveNonCPO(controlSet.makeId, controlSet.modelId);
        }
    }
} //PopulateModels


// Private methods that should NOT be called from your web page...

function InitializeControlSets()
{
	for (i = 0; i < controlSets.length; i++)
	{
		PopulateMakes(controlSets[i].setName);
	}
}

function RegisterControlSet(setName, makeControlId, modelControlId, selectedMake, selectedModel, cpoControlId, makeFilterType)
{
	controlSet = GetControlSet(setName);
	if (controlSet == null)
	{
		// Create a new registry...
		controlSet = CreateNewControlSet(setName);
	}
	
	controlSet.makeId = makeControlId;
	controlSet.modelId = modelControlId;
	controlSet.selectedMakeName = selectedMake;
	controlSet.selectedModelName = selectedModel;
	controlSet.cpoId = cpoControlId;
	controlSet.makeFilter = makeFilterType;
}

function GetControlSet(setName)
{
	for (i = 0; i < controlSets.length; i++)
	{
		if (controlSets[i] != null)
		{
			if (controlSets[i].setName == setName)
			{
				return (controlSets[i]);
			}
		}
	}
	
	return (null);
}

function CreateNewControlSet(setName)
{
	controlSet = new Object;
	controlSet.setName = setName;
	controlSets[controlSets.length] = controlSet;
	return (controlSet);
}
