﻿/// <summary>
/// Updates the filter drop downs, setting them to enabled or disabled and resetting the
/// selection as relevant.
/// </summary>
/// <param name="type">The type of filter that was most recently updated.</param>
/// <param name="success">
/// Whether the filter specified in the type parameter has been populated successfully, allowing the next
/// filter in sequence to become enabled.
/// </param>
/// <returns>No relevant return value.</param>
function updateFilterDropDownLists(type, success)
{
    var order =
    [
        ['Fuel Type', 'ddlFuelType'],
        ['Make', 'ddlMake'],
        ['Model', 'ddlModel'],
        ['Engine Size', 'ddlEngineSize'],
        ['Year', 'ddlYear'],
        ['Body Type', 'ddlBodyType']
    ];

    var index = 0;
    for (index = 0; index < order.length; index++)
    {
        if (order[index][0] == type)
        {
            break;
        }
    }

    if (success)
    {
        index++;
    }

    if (index > order.length)
    {
        index = order.length;
    }

    for (var i = 0; i < order.length; i++)
    {
        if (i < index)
        {
            $(order[i][1]).disabled = false;
        }
        else
        {
            $(order[i][1]).disabled = true;
            $(order[i][1]).selectedIndex = 0;
        }
    }
}

/// <summary>
/// Populates a dropdown list with the specifies items, and adding the specified default item to the start.
/// selection as relevant.
/// </summary>
/// <param name="dropdownlist">A reference to the <select> tag to populate.</param>
/// <param name="items">An array of BasicListItems containing the items to be populated.</param>
/// <param name="defaultItemText">The default item's display text. If null, no default item is added.</param>
/// <param name="defaultItemValue">The default item's value.</param>
/// <returns>No relevant return value.</param>
function populateDropDownList(dropdownlist, items, defaultItemText, defaultItemValue)
{
    dropdownlist.options.length = 0;
    var itemsIndex = 0;
    if (defaultItemText !== null)
    {
        itemsIndex = 1;
        dropdownlist.options[0] = new Option(defaultItemText, defaultItemValue);
    }
    if (items != null)
    {
        for (var index = 0; index < items.length; index++)
        {
            dropdownlist.options[index + itemsIndex] = new Option(items[index].DisplayText, items[index].Value);
        }
    }
}

function GetMakeList()
{
    var valid = true;
    var intFuelTypeID = 0;

    valid &= isNumeric($('ddlFuelType').value);
    if (valid)
    {
        intFuelTypeID = $('ddlFuelType').value;
        populateDropDownList($('ddlMake'), null, 'Loading...', '');
        OLAWebAJAX.GetMakeList(intFuelTypeID, On_GetMakeList_Success, On_GetMakeList_Failed);
    }
    else
    {
        updateFilterDropDownLists('Make', false);
    }
}
function On_GetMakeList_Success(results)
{
    populateDropDownList($('ddlMake'), results, 'Manufacturer...', '');
    updateFilterDropDownLists('Make', true);
}
function On_GetMakeList_Failed(error)
{
    populateDropDownList($('ddlMake'), null, 'Manufacturer...', '');
    updateFilterDropDownLists('Make', false);
}


function GetModelList()
{
    var valid = true;
    var intFuelTypeID = 0;
    var intMakeID = 0;
    
    valid &= isNumeric($('ddlFuelType').value);
    valid &= isNumeric($('ddlMake').value);
    
    if (valid)
    {
        intFuelTypeID = parseInt($('ddlFuelType').value);
        intMakeID = parseInt($('ddlMake').value);
        populateDropDownList($('ddlModel'), null, 'Loading...', '');
        OLAWebAJAX.GetModelList(intFuelTypeID, intMakeID, On_GetModelList_Success, On_GetModelList_Failed);
    }
    else
    {
        updateFilterDropDownLists('Model', false);
    }
}
function On_GetModelList_Success(results)
{
    populateDropDownList($('ddlModel'), results, 'Model...', '');
    updateFilterDropDownLists('Model', true);
}
function On_GetModelList_Failed(error)
{
    populateDropDownList($('ddlModel'), null, 'Model...', '');
    updateFilterDropDownLists('Model', false);
}


function GetEngineSizeList()
{
    var valid = true;
    var intFuelTypeID = 0;
    var intMakeID = 0;
    var intModelID = 0;

    valid &= isNumeric($('ddlFuelType').value);
    valid &= isNumeric($('ddlMake').value);
    valid &= isNumeric($('ddlModel').value);

    if (valid)
    {
        intFuelTypeID = parseInt($('ddlFuelType').value);
        intMakeID = parseInt($('ddlMake').value);
        intModelID = parseInt($('ddlModel').value);
        populateDropDownList($('ddlEngineSize'), null, 'Loading...', '');
        OLAWebAJAX.GetEngineSizeList(intFuelTypeID, intMakeID, intModelID, On_GetEngineSizeList_Success, On_GetEngineSizeList_Failed);
    }
    else
    {
        updateFilterDropDownLists('Engine Size', false);
    }
}
function On_GetEngineSizeList_Success(results)
{
    populateDropDownList($('ddlEngineSize'), results, 'Engine Size...', '0');
    updateFilterDropDownLists('Engine Size', true);
}
function On_GetEngineSizeList_Failed(error)
{
    populateDropDownList($('ddlEngineSize'), null, 'Engine Size...', '0');
    updateFilterDropDownLists('Engine Size', false);
}


function GetYearList()
{
    var valid = true;
    var intFuelTypeID = 0;
    var intMakeID = 0;
    var intModelID = 0;
    var decEngineSize = 0;

    valid &= isNumeric($('ddlFuelType').value);
    valid &= isNumeric($('ddlMake').value);
    valid &= isNumeric($('ddlModel').value);
    valid &= isNumeric($('ddlEngineSize').value);
    
    if (valid)
    {
        intFuelTypeID = parseInt($('ddlFuelType').value);
        intMakeID = parseInt($('ddlMake').value);
        intModelID = parseInt($('ddlModel').value);
        decEngineSize = parseFloat($('ddlEngineSize').value);
        populateDropDownList($('ddlYear'), null, 'Loading...', '');
        OLAWebAJAX.GetYearList(intFuelTypeID, intMakeID, intModelID, decEngineSize, On_GetYearList_Success, On_GetYearList_Failed);
    }
    else
    {
        updateFilterDropDownLists('Year', false);
    }
}
function On_GetYearList_Success(results)
{
    populateDropDownList($('ddlYear'), results, 'Year...', '0');
    updateFilterDropDownLists('Year', true);
}
function On_GetYearList_Failed(error)
{
    populateDropDownList($('ddlYear'), results, 'Year...', '0');
    updateFilterDropDownLists('Year', false);
}


function GetBodyTypeList()
{
    var valid = true;
    var intFuelTypeID = 0;
    var intMakeID = 0;
    var intModelID = 0;
    var decEngineSize = 0;
    var intYear = 0;

    valid &= isNumeric($('ddlFuelType').value);
    valid &= isNumeric($('ddlMake').value);
    valid &= isNumeric($('ddlModel').value);
    valid &= isNumeric($('ddlEngineSize').value);
    valid &= isNumeric($('ddlYear').value);

    if (valid)
    {
        intFuelTypeID = parseInt($('ddlFuelType').value);
        intMakeID = parseInt($('ddlMake').value);
        intModelID = parseInt($('ddlModel').value);
        decEngineSize = parseFloat($('ddlEngineSize').value);
        intYear = parseInt($('ddlYear').value);
        populateDropDownList($('ddlBodyType'), null, 'Loading...', '');
        OLAWebAJAX.GetBodyTypeList(intFuelTypeID, intMakeID, intModelID, decEngineSize, intYear, On_GetBodyTypeList_Success, On_GetBodyTypeList_Failed);
    }
    else
    {
        updateFilterDropDownLists('Body Type', false);
    }
}
function On_GetBodyTypeList_Success(results)
{
    populateDropDownList($('ddlBodyType'), results, 'Body Type...', '0');
    updateFilterDropDownLists('Body Type', true);
}
function On_GetBodyTypeList_Failed(error)
{
    populateDropDownList($('ddlBodyType'), null, 'Body Type...', '0');
    updateFilterDropDownLists('Body Type', false);
}

function PerformSearch()
{
    var valid = true;
    var intFuelTypeID = 0;
    var intMakeID = 0;
    var intModelID = 0;
    var decEngineSize = 0;
    var intYear = 0;
    var intBodyTypeID = 0;

    valid &= isNumeric($('ddlFuelType').value);
    valid &= isNumeric($('ddlMake').value);
    valid &= isNumeric($('ddlModel').value);
    valid &= isNumeric($('ddlEngineSize').value);
    valid &= isNumeric($('ddlYear').value);
    valid &= isNumeric($('ddlBodyType').value);

    if (valid)
    {
        intFuelTypeID = parseInt($('ddlFuelType').value);
        intMakeID = parseInt($('ddlMake').value);
        intModelID = parseInt($('ddlModel').value);
        decEngineSize = parseFloat($('ddlEngineSize').value);
        intYear = parseInt($('ddlYear').value);
        intBodyTypeID = parseInt($('ddlBodyType').value);

        var panel = $('search_results');
        var container = $('search_results_content');
        container.innerHTML = '<em>Loading...</em>';
        panel.style.display = '';

        OLAWebAJAX.GetVehicleSearchList(intFuelTypeID, intMakeID, intModelID, decEngineSize, intYear, intBodyTypeID, On_GetVehicleSearchList_Success, On_GetVehicleSearchList_Failed);
    }
    else
    {
        alert('Please select a minimum of fuel type, make and model.');
    }
}

function On_GetVehicleSearchList_Success(results)
{
    var panel = $('search_results');
    var container = $('search_results_content');
    container.innerHTML = '';
    var html = '';
    if (results !== null)
    {
        html = '<table id="search_results" cellspacing="0">';
        for (var groupIndex = 0; groupIndex < results.CategoryGroups.length; groupIndex++)
        {
            var categoryGroup = results.CategoryGroups[groupIndex];
            html += '<tr><td colspan="2"></td></tr><tr class="category_group"><td colspan="2">' + categoryGroup.CategoryGroupName + '</td></tr>';
            for (var categoryIndex = 0; categoryIndex < categoryGroup.Categories.length; categoryIndex++)
            {
                var alternate = (categoryIndex % 2) ? 'alternate' : '';
                var category = categoryGroup.Categories[categoryIndex];
                html += '<tr class="' + alternate + '"><td class="search_result_count">(' + category.Count + ')</td><td><a href="Products.aspx?CategoryID=' + category.CategoryID + '">' + category.CategoryName + '</a></td>';
            }
        }
        html += '</table>';
    }
    else
    {
        html = '';
    }
    if (html == '')
    {
        html = '<em>No results.</em>';
    }
    else
    {
        // Lock the specification
        $('pnlSpecificationUnlocked').style.display = 'none';
        $('pnlSpecificationLocked').style.display = '';
        
        $('lblVSFuelType').innerHTML = $('ddlFuelType').options[$('ddlFuelType').selectedIndex].text;
        $('lblVSMake').innerHTML = $('ddlMake').options[$('ddlMake').selectedIndex].text;
        $('lblVSModel').innerHTML = $('ddlModel').options[$('ddlModel').selectedIndex].text;
        $('lblVSEngineSize').innerHTML = $('ddlEngineSize').options[$('ddlEngineSize').selectedIndex].text;
        $('lblVSYear').innerHTML = $('ddlYear').options[$('ddlYear').selectedIndex].text;
        $('lblVSBodyType').innerHTML = $('ddlBodyType').options[$('ddlBodyType').selectedIndex].text;

        if ($('lblVSEngineSize').innerHTML == 'Engine Size...')
        {
            $('lblVSEngineSize').innerHTML = 'Any';
        }
        if ($('lblVSYear').innerHTML == 'Year...')
        {
            $('lblVSYear').innerHTML = 'Any';
        }
        if ($('lblVSBodyType').innerHTML == 'Body Type...')
        {
            $('lblVSBodyType').innerHTML = 'Any';
        }
    }
    container.innerHTML = html;
    panel.style.display = '';
}

function On_GetVehicleSearchList_Failed(error)
{
    var panel = $('search_results');
    var container = $('search_results_content');
    container.innerHTML = error;
    panel.style.display = '';
}

function watermark(el, focus)
{
    try
    {
        if (focus)
        {
            el.className = '';
        }
        else
        {
            if (el.value != '')
            {
                el.className = '';
            }
            else
            {
                el.className = 'watermark';
                el.value = '';
            }
        }
    }
    catch (e)
    {
    }
}

function updateTitleSelection(el)
{
    var otherTextBox = $('txtTitle');
    switch (el.value)
    {
        case "Other":
            {
                otherTextBox.style.display = '';
            }
            break;
        default:
            {
                otherTextBox.style.display = 'none';
            }
            break;
    }
}