
var index = {
	
	init: function() {
		var categoriesBox = document.getElementById('categoriesBox');
		categoriesBox.selectedIndex = 0;
		var seriesBox = document.getElementById('seriesBox');
		var butQuickFind = document.getElementById('butQuickFind');
		YAHOO.util.Event.addListener("categoriesBox", "change", index.categoryChange);
		YAHOO.util.Event.addListener("quickFind", "submit", index.goToProductPage);
		index.toggleFields();
	},
	
	categoryChange: function(e) {
		var categoriesBox = document.getElementById('categoriesBox');
		var seriesBox = document.getElementById('seriesBox');
		var categoryId = categoriesBox.options[categoriesBox.selectedIndex].value;
		if(categoryId) {
			index.populateSeries(categoryId);
		}
		index.toggleFields();
	},
	
	populateSeries: function(categoryId) {
		var categoriesBox = document.getElementById('categoriesBox');
		var seriesBox = document.getElementById('seriesBox');
		var newOptions = getSeriesByCategoryId(categoryId);
		if(newOptions) {
			seriesBox.options.length = 0;
			seriesBox.options[0] = new Option('Click "Go!" or select series...', '');
			for(var i = 0; i < newOptions.length; ++i) {
				seriesBox.options[i+1] = new Option(newOptions[i].seriesName, newOptions[i].seriesId);
			}
		}	
		index.toggleFields();
	},
	
	toggleFields: function() {
		var categoriesBox = document.getElementById('categoriesBox');
		var seriesBox = document.getElementById('seriesBox');
		var butQuickFind = document.getElementById('butQuickFind');
		seriesBox.disabled = categoriesBox.selectedIndex <= 0;
		butQuickFind.disabled = categoriesBox.selectedIndex <= 0;
	},

	goToProductPage: function(e) {
		YAHOO.util.Event.preventDefault(e);
		var categoriesBox = document.getElementById('categoriesBox');
		var seriesBox = document.getElementById('seriesBox');
		var cat = getCategoryByCategoryId(categoriesBox.options[categoriesBox.selectedIndex].value);
		var prodUrl = '';
		var catUrl = '';
		if(cat.divisionId == 2) {
			catUrl = 'sunx-sensors/';
		}
		else if(cat.divisionId == 3) {
			catUrl = 'sunx-laser-markers/';
		}
		if(seriesBox.selectedIndex > 0) {
			var series = getSeriesBySeriesId(seriesBox.options[seriesBox.selectedIndex].value);
			prodUrl = '/acsd/' + catUrl + cat.categoryShortName + '/' + series.seriesShortName + '/';
		}
		else {
			prodUrl = '/acsd/' + catUrl + cat.categoryShortName + '/';	
		}
		document.location.href = prodUrl;
	}
};
YAHOO.util.Event.addListener(window, "load", index.init);