// JavaScript Document


var productYearObj = null;

//called when the page is ready
$(document).ready(function() {
	//getCatalogs(0);
	
	//set onChange event for the vehicle pull-down.
	$("#vehicleId").change(function() {
		var selectedVehicleId = $(this).val();
		getCatalogYears(selectedVehicleId);						
	});
	
	//set onFocus event for the keyword text field.
	$("#keyword").focus(function() {
		$(this).val("");						 
	});
	
	//set onFocus event for the part number text field.
	$("#partNumber").focus(function() {
		$(this).val("");					  
	});
	
	$("#vehicleSearchForm").validate({
		
		submitHandler: function(form) {
			var keywordValue = form.keyword.value;
			var partNumberValue = form.partNumber.value;
			var selectedCatalog = $("#vehicleId").val();
			var selectedCatalogYear = $("#vehicleYearId").val();
			
			if(keywordValue.length == 0 && partNumberValue.length == 0) {
				alert("Please enter a Keyword or a Part Number");	
			}
			
			else if(keywordValue.toUpperCase() == "ENTER KEYWORD" && partNumberValue.toUpperCase() == "ENTER PART NUMBER") {
				alert("Please enter a Keyword or a Part Number");
			}
			
			else if(keywordValue.toUpperCase() != "ENTER KEYWORD" && keywordValue.length > 0) {
				window.location = "/controller.cfm?type=product&action=keywordSearch&keyword=" + keywordValue + "&productSearchCatalogId=" + selectedCatalog + "&productYearId=" + selectedCatalogYear;	
			}
			
			else if(partNumberValue.toUpperCase() != "ENTER PART NUMBER" && partNumberValue.length > 0) {
				window.location = "/controller.cfm?type=product&action=productNumberSearch&partNumber=" + partNumberValue + "&productSearchCatalogId=" + selectedCatalog + "&productYearId=" + selectedCatalogYear;	
			}									
		}
		
	});
});


function getCatalogs(catalogId) {
	$.post(
		"/object/Vehicle.cfc",
		{
			method: "getVehicleCatalogs",
			returnFormat: "json"
		},
		function(response){
			var catalogData = response.DATA;
			
			//cache the catalog pull-down.
			var catalogPulldown = $("#vehicleId");
			
			//remove all pull-down values.
			catalogPulldown.children().remove();
			
			//set default value.
			catalogPulldown.append('<option value="0">All Models</option>');
			
			//loop over the catalogs and set the pull-down options.
			$.each(catalogData, function(index, optionData) {
				var responseCatalogId = optionData[0];
				var catalogName = optionData[1];
				
				if(responseCatalogId == catalogId) {
					catalogPulldown.append('<option value="' + responseCatalogId + '" selected="selected">' + catalogName + '</option>');	
				}
				
				else {
					catalogPulldown.append('<option value="' + responseCatalogId + '">' + catalogName + '</option>');	
				}
			});			
		},
		"json"
	);	
}



function getCatalogYears(catalogId, catalogYearId) {
	$.post(
		"/object/Vehicle.cfc",
		{
			method: "getAllVehicleYearsTemp",
			returnFormat: "json"			
		},
		function(response) {
			$.each(response, function(index, optionData) {							
				var responseCatalogId = optionData['catalogId'];
				
				if(responseCatalogId == catalogId) {
					productYearObj = optionData['years'];	
				}								
			});
			
			loadCatalogYears(catalogYearId);
		},
		"json"
	);	
}

function loadCatalogYears(catalogYearId){
	var productYearPulldown = $("#vehicleYearId");
	
	//remove all pull-down values.
	productYearPulldown.children().remove();
	
	//variable to store the product years array.
	var productYearArray = [];
	
	//loop over the object and create a year array.
	$.each(productYearObj, function(index, productYearData) {
		productYearArray.push({
			yearId: productYearData.yearId,
			year: productYearData.year
		});
	});
	
	//sort by year.
	productYearArray.sort(function(a,b){
		return a.year - b.year;							   
	});
			
	//add default value.
	productYearPulldown.append('<option value="0">All Years</option>');
	
	//add each value to the year pull-down.
	$.each(productYearArray, function(index, productYearData) {
		if(catalogYearId == productYearData.yearId) {
			productYearPulldown.append('<option value="' + productYearData.yearId + '" selected="selected">' + productYearData.year + '</option>');	
		}
		
		else {
			productYearPulldown.append('<option value="' + productYearData.yearId + '">' + productYearData.year + '</option>');	
		}
	});	
}