var brandSelect = document.getElementById('brandSelect');
var serieSelect = document.getElementById('serieSelect');
var modelSelect = document.getElementById('modelSelect');

var currentSelection = 'brand';

function fillSelect() {

	brandSelect = document.getElementById('brandSelect');
	serieSelect = document.getElementById('serieSelect');
	modelSelect = document.getElementById('modelSelect');
	
	var selectToFill = null;
	if (currentSelection == 'brand') {
		selectToFill = brandSelect;
		
	} else if (currentSelection == 'serie') {
		selectToFill = serieSelect;
		
	} else if (currentSelection == 'model') {
		selectToFill = modelSelect;
	
	}
	
	if (selectToFill != null) {
		for (var i=0; i < optionsToFill.length; i++) {
			var catId = optionsToFill[i][0] + '-' + optionsToFill[i][2];
			var catName = optionsToFill[i][1];
			var option = new Option(catName, catId);
			selectToFill.options[selectToFill.options.length] = option;
		}
	}
}
function changeBrand() {
	serieSelect.options.length = 1;
	modelSelect.options.length = 1;
	if (brandSelect.selectedIndex > 0) {
		currentSelection = 'serie';
		var url = 'http://72.167.33.136/inkjetCA/loadLevel.jsp?catId=' + brandSelect.value.split("-")[0];
		callScript(url);
	}
}
function changeSerie() {
	modelSelect.options.length = 1;
	if (serieSelect.selectedIndex > 0) {
		currentSelection = 'model';
		var url = 'http://72.167.33.136/inkjetCA/loadLevel.jsp?catId=' + serieSelect.value.split("-")[0];
		callScript(url);
	}
}
function callScript(url) {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild(script);
}	
function goFast() { 
	if (! processLevel('modelSelect')) { 
       	if (! processLevel('serieSelect')) { 
           	if (! processLevel('brandSelect')) {
               	alert("You must set a brand, serie or model");
			}
		}
	}
}
function processLevel(selectId) {
	if (document.getElementById(selectId).selectedIndex > 0) {
    	var categoryId = document.getElementById(selectId).value.split("-")[1];
		if (categoryId.indexOf('-') != -1) { 
			categoryId = categoryId.substring(categoryId.indexOf('-')+1, categoryId.length);
		}
        document.location = "http://www.inkjetsuperstore.ca/s.nl/category."+categoryId+"/.f";
        return true;
	}
	return false;
}
