
function redrawNarrowDropdowns() {
	var e_brand 		= document.getElementById('narrow_brand');
	var e_category 		= document.getElementById('narrow_category');
	var brand_idx 		= e_brand.selectedIndex;
	var category_idx 	= e_category.selectedIndex;	
	var brand_id 		= e_brand.options[brand_idx].value;
	var category_id 	= e_category.options[category_idx].value;
	
	initNarrowDropdowns(brand_id,category_id);	
}

function initNarrowDropdowns(b,c) {
	createNarrowCategoryDropdown(b,c);
	createNarrowBrandDropdown(b,c);
}

function isCatInBrand(c,b) {
	if (c == undefined || c == 0 || b == undefined || b == 0)
		return true;
	
	for( var i=0;i<narrowData.d.length;i++ ) {
		if (narrowData.d[i].bi == b) {
			for (j=0;j<narrowData.d[i].bcs.length;j++) {
				if (narrowData.d[i].bcs[j] == c)
					return true;	
			}
		}	
	}
	return false;
}

function createNarrowCategoryDropdown(b,c) {
	var e = document.getElementById('narrow_category_select');
	var o =  '<select id="narrow_category" name="cid" class="form_select narrow_results_select" onchange="redrawNarrowDropdowns(this)">';
	o += '<option value="0">All Categories</option>';
	
	for( i=0;i<narrowData.c.length;i++ ) {
		var id = narrowData.c[i].i;
		var name = narrowData.c[i].n; 
		var parent_id = (narrowData.c[i].p != "undefined") ? narrowData.c[i].p : 0; 
		var selected = (id == c) ? 'selected ' : '';
		var subcat = (parent_id) ? '&nbsp; &nbsp; &nbsp ' : ''; 
		var disabled = (isCatInBrand(id,b)) ? '' : 'class="disabled" ';
		
		o += '<option '+disabled+selected+'value="'+id+'">'+subcat+name+'</option>';			
	}
	
	o += '</select>';								
	e.innerHTML = o;
}

function createNarrowBrandDropdown(b,c) {
	var e = document.getElementById('narrow_brand_select');
	var o =  '<select id="narrow_brand" name="bid" class="form_select narrow_results_select" onchange="redrawNarrowDropdowns(this)">';
	o += '<option value="0">All Brands</option>';
	
	for( i=0;i<narrowData.b.length;i++ ) {
		var id = narrowData.b[i].i;
		var name = narrowData.b[i].n; 
		var selected = (id == b) ? 'selected ' : '';
		var disabled = (isCatInBrand(c,id)) ? '' : 'class="disabled" ';
		o += '<option '+disabled+selected+'value="'+id+'">'+name+'</option>';
	}
	
	o += '</select>';								
	e.innerHTML = o;
}

function colorSelectChanged(me) {
	var val = me.options[me.selectedIndex].value;
	if (val != "null")
		window.location = val;
}