$(document).ready(function()
{
    $('select#vehicleMake').change(function()
    {
        resetModel();
        resetSubModel();
        resetEngine();
        resetYear();
        resetVehicle();
        $('div#vehicleSubModelBox').css('display', 'none');
        $('div#vehicleVehicleBox').css('display', 'none');
        var strMake = $(this).val();
        $.post(
           '/ajax.php?x=getVehicleOptions',
           {
               option : 'models',
               make : strMake
           },
           function(data)
           {
               $('select#vehicleModel').html(data);
               $('select#vehicleModel').removeAttr('disabled');           
           }             
        )
    });

    $('select#vehicleModel').change(function()
    {
        resetSubModel();
        resetEngine();
        resetYear();
        resetVehicle();
        var strMake = $('#vehicleMake').val();
        var strModel = $(this).val();
        $.post(
            '/ajax.php?x=getVehicleOptions',
            {
                option : 'hasSubModels',
                make : strMake,
                model : strModel
            },
            function(data)
            {
                if (data == 'true')
                {
                    $.post(
                        '/ajax.php?x=getVehicleOptions',
                        {
                            option : 'subModels',
                            make : strMake,
                            model : strModel
                        },
                        function(data)
                        {
                            $('select#vehicleSubModel').html(data);
                            $('div#vehicleSubModelBox').css('display', 'block');
                            $('select#vehicleSubModel').removeAttr('disabled');
                        }
                    );
                }
                else
                {
                    $('select#vehicleSubModel').css('display', 'none');
                    $.post(
                        '/ajax.php?x=getVehicleOptions',
                        {
                            option : 'engines',
                            make : strMake,
                            model : strModel
                        },
                        function(data)
                        {
                            $('select#vehicleEngine').html(data);
                            $('select#vehicleEngine').removeAttr('disabled');
                        }
                    );
                }
            }
        );
    });

    $('select#vehicleSubModel').change(function()
    {
        resetEngine();
        resetYear();
        resetVehicle();
        var strMake = $('#vehicleMake').val();
        var strModel = $('#vehicleModel').val();
        var strSubModel = $(this).val();
        $.post(
           '/ajax.php?x=getVehicleOptions',
           {
               option : 'engines',
               make : strMake,
               model : strModel,
               submodel : strSubModel
           },
           function(data)
           {
               $('select#vehicleEngine').html(data);
               $('select#vehicleEngine').removeAttr('disabled');
           }
        )
    });

    $('select#vehicleEngine').change(function()
    {
        resetVehicle();
        var strMake = $('#vehicleMake').val();
        var strModel = $('#vehicleModel').val();
        var strEngine = $(this).val();
        $.post(
           '/ajax.php?x=getVehicleOptions',
           {
               option : 'years',
               make : strMake,
               model : strModel,
               submodel : getSubModelValue(),
               engine : strEngine
           },
           function(data)
           {
               $('select#vehicleYear').html(data);
               $('select#vehicleYear').removeAttr('disabled');
           }
        )
    });

    $('select#vehicleYear').change(function()
    {
        var strMake = $('#vehicleMake').val();
        var strModel = $('#vehicleModel').val();
        var strEngine = $('select#vehicleEngine').val();
        var strYear = $(this).val();
        $.post(
           '/ajax.php?x=getVehicleOptions',
           {
               option : 'vehicles',
               make : strMake,
               model : strModel,
               submodel : getSubModelValue(),
               engine : strEngine,
               year : strYear
           },
           function(data)
           {
               $('select#vehicleVehicle').html(data);
               $('div#vehicleVehicleBox').css('display', 'block');
               $('select#vehicleVehicle').removeAttr('disabled');
           }
        )
    });

    $('button#selectVehicle').click(function()
    {
        var strId = $('select#vehicleVehicle').val();
        var strYear = $('select#vehicleYear').val();

        if (strId != "_blank_" && strYear != "")
    	{
	        $.post(
	            '/ajax.php?x=setVehicle',
	            {
	                id : strId,
	                year : strYear
	            },
	            function(data)
	            {
	                window.location.reload();
	            }
	        );
    
    	}
        
        return false;
    });

    $('select#vehicleVehicle').change(function()
    {
        var strId = $('select#vehicleVehicle').val();
        var strYear = $('select#vehicleYear').val();
        $.post(
            '/ajax.php?x=setVehicle',
            {
                id : strId,
                year : strYear
            },
            function(data)
            {
             //   window.location.reload();
            }
        )
        return false;
    });

    $('span#vrmLookup form').submit(function(){ vrmLookup() });
});

function vrmLookup()
{
    var strReg = $('input#vehicleReg').val();
    $('span#vrmLookup').html('<div id="vehicleNoSelect">Finding your vehicle...</div>');
    $.post(
        '/ajax.php?x=vrmLookup',
        {
            reg : strReg
        },
        function(data)
        {
        	if (data == 'error')
        	{
        		alert("Sorry, we have been unable to retrieve vehicle details for " + strReg);        	
        	}
          	window.location.reload();
            
        }
    );
    return false;
}

function deleteVehicle()
{
    $.post(
        '/ajax.php?x=deleteVehicle',
        {
        },
        function(data)
        {
            window.location.reload();
        }
    );
    return false;
}

function getSubModelValue()
{
    if ($('div#vehicleSubModelBox').css('display') != 'none')
    {
        return $('select#vehicleSubModel').val();
    }
    else
    {
        return '';
    }
}

function resetModel()
{
    $('select#vehicleModel').html('<option value="_blank_">* Please select *</option>');
    $('select#vehicleModel').attr('disabled', 'disabled');
}

function resetSubModel()
{
    $('select#vehicleSubModel').html('<option value="_blank_">* Please select *</option>');
    $('select#vehicleSubModel').attr('disabled', 'disabled');
    $('div#vehicleSubModelBox').css('display', 'none');
}

function resetEngine()
{
    $('select#vehicleEngine').html('<option value="_blank_">* Please select *</option>');
    $('select#vehicleEngine').attr('disabled', 'disabled');
}

function resetYear()
{
    $('select#vehicleYear').html('<option value="_blank_">* Please select *</option>');
    $('select#vehicleYear').attr('disabled', 'disabled');
}

function resetVehicle()
{
    $('select#vehicleVehicle').html('<option value="_blank_">* Please select *</option>');
    $('select#vehicleVehicle').attr('disabled', 'disabled');
    if ($('input#vehicleMake').attr('type') != 'hidden')
    {
        $('div#vehicleVehicleBox').css('display', 'none');
    }
}

function filterResults(pageNo)
{
    $('div#filterResults').html('<div class="loading"/>');
    $.post(
       '/ajax.php?x=filterResults',
       {
           cat : $('#seoCategory').val(),
           make : $('#vehicleMake').val(),
           model : $('#vehicleModel').val(),
           submodel : getSubModelValue(),
           engine : $('select#vehicleEngine').val(),
           year : $('select#vehicleYear').val(),
           vehicle : $('select#vehicleVehicle').val(),
           page : pageNo
       },
       function(data)
       {
           $('div#filterResults').html(data);
       }
    )
}

function filterResultsCatalogue(pageNo)
{
    $('div#filterResults').html('<div class="loading"/>');
    $.post(
       '/ajax.php?x=filterResultsCatalogue',
       {
           cat : $('#seoCategory').val(),
           make : $('#vehicleMake').val(),
           model : $('#vehicleModel').val(),
           submodel : getSubModelValue(),
           engine : $('select#vehicleEngine').val(),
           year : $('select#vehicleYear').val(),
           vehicle : $('select#vehicleVehicle').val(),
           page : pageNo
       },
       function(data)
       { 
    	   document.title =  $('#vehicleMake').val() + " " + $('#seoCategoryDesc').val() + " - Trade Portal - Comline Auto Parts";
           $('div#filterResults').html(data);
       }
    )
}

function setPageSize(iPageSize)
{
    $.post(
        '/ajax.php?x=setPageSize',
        {
            pageSize : iPageSize
        },
        function(data)
        {
            window.location.reload();
        }
    )
}
