﻿function VehicleSpecification()
{
	this.ddlFuelType = null;
	this.ddlMake = null;
	this.ddlModel = null;
	this.ddlEngineSize = null;
	this.ddlYear = null;
	this.ddlBodyType = null;

	this.lblFuelType = null;
	this.lblMake = null;
	this.lblModel = null;
	this.lblEngineSize = null;
	this.lblYear = null;
	this.lblBodyType = null;

	this.searchButton = null;
	this.resultsPanel = null;
	this.resultsContainer = null;
	this.panelSpecificationUnlocked = null;
	this.panelSpecificationLocked = null;

	this.searchSuccessCallback = null;
	this.failureSuccessCallback = null;

	this.searchNotEnoughInformationCallback = null;
}

/// <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>
VehicleSpecification.prototype.updateFilterDropDownLists = function(type, success)
{
	var order =
	[
		['Fuel Type', this.ddlFuelType],
		['Make', this.ddlMake],
		['Model', this.ddlModel],
		['Engine Size', this.ddlEngineSize],
		['Year', this.ddlYear],
		['Body Type', this.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>
VehicleSpecification.prototype.populateDropDownList = function (dropdownlist, items, defaultItemText, defaultItemValue)
{
	dropdownlist.options.length = 0;
	var itemsIndex = 0;
	if (defaultItemText !== null)
	{
		//console.log(defaultItemText);
		itemsIndex = 1;
		dropdownlist.options[0] = new Option(defaultItemText, defaultItemValue);
		dropdownlist.disabled = true;
	}
	if (items != null)
	{
		for (var index = 0; index < items.length; index++)
		{
			dropdownlist.options[index + itemsIndex] = new Option(items[index].DisplayText, items[index].Value);
		}
	}
};

VehicleSpecification.prototype.GetMakeList = function ()
{
	var me = this;
	var valid = true;
	var intFuelTypeID = 0;

	valid &= isNumeric(this.ddlFuelType.value);
	if (valid)
	{
		intFuelTypeID = this.ddlFuelType.value;
		this.populateDropDownList(this.ddlMake, null, 'Loading...', '');
		this.searchButton.disabled = true;
		OLAWebAJAX.GetMakeList(intFuelTypeID, bind(this, this.On_GetMakeList_Success), bind(this, this.On_GetMakeList_Failed));
	}
	else
	{
		this.updateFilterDropDownLists('Make', false);
	}
};
VehicleSpecification.prototype.On_GetMakeList_Success = function(results)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlMake, results, 'Manufacturer...', '');
	this.updateFilterDropDownLists('Make', true);
}
VehicleSpecification.prototype.On_GetMakeList_Failed = function(error)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlMake, null, 'Manufacturer...', '');
	this.updateFilterDropDownLists('Make', false);
}

VehicleSpecification.prototype.GetModelList = function()
{
	var valid = true;
	var intFuelTypeID = 0;
	var intMakeID = 0;
	
	valid &= isNumeric(this.ddlFuelType.value);
	valid &= isNumeric(this.ddlMake.value);
	
	if (valid)
	{
		intFuelTypeID = parseInt(this.ddlFuelType.value);
		intMakeID = parseInt(this.ddlMake.value);
		this.populateDropDownList(this.ddlModel, null, 'Loading...', '');
		this.searchButton.disabled = true;
		OLAWebAJAX.GetModelList(intFuelTypeID, intMakeID, bind(this, this.On_GetModelList_Success), bind(this, this.On_GetModelList_Failed));
	}
	else
	{
		this.updateFilterDropDownLists('Model', false);
	}
}
VehicleSpecification.prototype.On_GetModelList_Success = function (results)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlModel, results, 'Model...', '');
	this.updateFilterDropDownLists('Model', true);
}
VehicleSpecification.prototype.On_GetModelList_Failed = function(error)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlModel, null, 'Model...', '');
	this.updateFilterDropDownLists('Model', false);
}


VehicleSpecification.prototype.GetEngineSizeList = function()
{
	var valid = true;
	var intFuelTypeID = 0;
	var intMakeID = 0;
	var intModelID = 0;

	valid &= isNumeric(this.ddlFuelType.value);
	valid &= isNumeric(this.ddlMake.value);
	valid &= isNumeric(this.ddlModel.value);

	if (valid)
	{
		intFuelTypeID = parseInt(this.ddlFuelType.value);
		intMakeID = parseInt(this.ddlMake.value);
		intModelID = parseInt(this.ddlModel.value);
		this.populateDropDownList(this.ddlEngineSize, null, 'Loading...', '');
		this.searchButton.disabled = true;
		OLAWebAJAX.GetEngineSizeList(intFuelTypeID, intMakeID, intModelID, bind(this, this.On_GetEngineSizeList_Success), bind(this, this.On_GetEngineSizeList_Failed));
	}
	else
	{
		this.updateFilterDropDownLists('Engine Size', false);
	}
}
VehicleSpecification.prototype.On_GetEngineSizeList_Success = function(results)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlEngineSize, results, 'Engine Size...', '0');
	this.updateFilterDropDownLists('Engine Size', true);
}
VehicleSpecification.prototype.On_GetEngineSizeList_Failed = function(error)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlEngineSize, null, 'Engine Size...', '0');
	this.updateFilterDropDownLists('Engine Size', false);
}


VehicleSpecification.prototype.GetYearList = function()
{
	var valid = true;
	var intFuelTypeID = 0;
	var intMakeID = 0;
	var intModelID = 0;
	var decEngineSize = 0;

	valid &= isNumeric(this.ddlFuelType.value);
	valid &= isNumeric(this.ddlMake.value);
	valid &= isNumeric(this.ddlModel.value);
	valid &= isNumeric(this.ddlEngineSize.value);
	
	if (valid)
	{
		intFuelTypeID = parseInt(this.ddlFuelType.value);
		intMakeID = parseInt(this.ddlMake.value);
		intModelID = parseInt(this.ddlModel.value);
		decEngineSize = parseFloat(this.ddlEngineSize.value);
		this.populateDropDownList(this.ddlYear, null, 'Loading...', '');
		this.searchButton.disabled = true;
		OLAWebAJAX.GetYearList(intFuelTypeID, intMakeID, intModelID, decEngineSize, bind(this, this.On_GetYearList_Success), bind(this, this.On_GetYearList_Failed));
	}
	else
	{
		this.updateFilterDropDownLists('Year', false);
	}
}
VehicleSpecification.prototype.On_GetYearList_Success = function (results)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlYear, results, 'Year...', '0');
	this.updateFilterDropDownLists('Year', true);
}
VehicleSpecification.prototype.On_GetYearList_Failed = function(error)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlYear, results, 'Year...', '0');
	this.updateFilterDropDownLists('Year', false);
}


VehicleSpecification.prototype.GetBodyTypeList = function()
{
	var valid = true;
	var intFuelTypeID = 0;
	var intMakeID = 0;
	var intModelID = 0;
	var decEngineSize = 0;
	var intYear = 0;

	valid &= isNumeric(this.ddlFuelType.value);
	valid &= isNumeric(this.ddlMake.value);
	valid &= isNumeric(this.ddlModel.value);
	valid &= isNumeric(this.ddlEngineSize.value);
	valid &= isNumeric(this.ddlYear.value);

	if (valid)
	{
		intFuelTypeID = parseInt(this.ddlFuelType.value);
		intMakeID = parseInt(this.ddlMake.value);
		intModelID = parseInt(this.ddlModel.value);
		decEngineSize = parseFloat(this.ddlEngineSize.value);
		intYear = parseInt(this.ddlYear.value);
		this.populateDropDownList(this.ddlBodyType, null, 'Loading...', '');
		this.searchButton.disabled = true;
		OLAWebAJAX.GetBodyTypeList(intFuelTypeID, intMakeID, intModelID, decEngineSize, intYear, bind(this, this.On_GetBodyTypeList_Success), bind(this, this.On_GetBodyTypeList_Failed));
	}
	else
	{
		this.updateFilterDropDownLists('Body Type', false);
	}
}
VehicleSpecification.prototype.On_GetBodyTypeList_Success = function(results)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlBodyType, results, 'Body Type...', '0');
	this.updateFilterDropDownLists('Body Type', true);
}
VehicleSpecification.prototype.On_GetBodyTypeList_Failed = function(error)
{
	this.searchButton.disabled = false;
	this.populateDropDownList(this.ddlBodyType, null, 'Body Type...', '0');
	this.updateFilterDropDownLists('Body Type', false);
}

VehicleSpecification.prototype.PerformSearch = function ()
{
	try
	{
		var valid = true;
		var intFuelTypeID = 0;
		var intMakeID = 0;
		var intModelID = 0;
		var decEngineSize = 0;
		var intYear = 0;
		var intBodyTypeID = 0;

		valid &= isNumeric(this.ddlFuelType.value);
		valid &= isNumeric(this.ddlMake.value);
		valid &= isNumeric(this.ddlModel.value);
		valid &= isNumeric(this.ddlEngineSize.value);
		valid &= isNumeric(this.ddlYear.value);
		valid &= isNumeric(this.ddlBodyType.value);

		if (valid)
		{
			intFuelTypeID = parseInt(this.ddlFuelType.value);
			intMakeID = parseInt(this.ddlMake.value);
			intModelID = parseInt(this.ddlModel.value);
			decEngineSize = parseFloat(this.ddlEngineSize.value);
			intYear = parseInt(this.ddlYear.value);
			intBodyTypeID = parseInt(this.ddlBodyType.value);
			//console.log(this);
			this.resultsContainer.innerHTML = '<em>Loading...</em>';
			this.resultsPanel.style.display = '';

			this.searchButton.disabled = true;
			OLAWebAJAX.GetVehicleSearchList(intFuelTypeID, intMakeID, intModelID, decEngineSize, intYear, intBodyTypeID, bind(this, this.searchSuccessCallback), bind(this, this.searchFailureCallback));
		}
		else
		{
			if (this.searchNotEnoughInformationCallback != null)
			{
				this.searchNotEnoughInformationCallback();
			}
			else
			{
				alert('Please select a minimum of fuel type, make and model.');
			}
		}
	}
	catch (e)
	{
	}
	finally
	{
		return false;
	}
}

VehicleSpecification.prototype.On_GetVehicleSearchList_Success = function(results)
{
	var html = '';

	this.searchButton.disabled = false;
	this.resultsContainer.innerHTML = '';

	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
		this.panelSpecificationUnlocked.style.display = 'none';
		this.panelSpecificationLocked.style.display = '';
		
		this.lblFuelType.innerHTML = this.ddlFuelType.options[this.ddlFuelType.selectedIndex].text;
		this.lblMake.innerHTML = this.ddlMake.options[this.ddlMake.selectedIndex].text;
		this.lblModel.innerHTML = this.ddlModel.options[this.ddlModel.selectedIndex].text;
		this.lblEngineSize.innerHTML = this.ddlEngineSize.options[this.ddlEngineSize.selectedIndex].text;
		this.lblYear.innerHTML = this.ddlYear.options[this.ddlYear.selectedIndex].text;
		this.lblBodyType.innerHTML = this.ddlBodyType.options[this.ddlBodyType.selectedIndex].text;

		if (this.lblEngineSize.innerHTML == 'Engine Size...')
		{
			this.lblEngineSize.innerHTML = 'Any';
		}
		if (this.lblYear.innerHTML == 'Year...')
		{
			this.lblYear.innerHTML = 'Any';
		}
		if (this.lblBodyType.innerHTML == 'Body Type...')
		{
			this.lblBodyType.innerHTML = 'Any';
		}
	}
	this.resultsContainer.innerHTML = html;
	this.resultsPanel.style.display = '';
}

VehicleSpecification.prototype.On_GetVehicleSearchList_Failed = function(error)
{
	this.searchButton.disabled = false;
	this.resultsContainer.innerHTML = error;
	this.resultsPanel.style.display = '';
}

