var dwrdebug = false;

var advancedSearchInitialized = false;
function initializeAdvancedSearch(selectedProductTypeId, selectedBrandId, selectedType) {
	if(!advancedSearchInitialized) {		
		$("#loading-producttypes-img").show();
		getProductTypesAjax(selectedBrandId, selectedProductTypeId);
		$("#loading-brands-img").show();		
		getBrandsAjax(selectedProductTypeId, selectedBrandId);
		
		getTypesAjax(selectedProductTypeId, selectedBrandId, selectedType);
		advancedSearchInitialized = true;
	}
}

function getBrands(prodtype, brand) {
	
	var selectedBrand = "";
	if (brand != null && brand.selectedIndex > 0) {
		selectedBrand = brand.options[brand.selectedIndex].value;
	}
	if (prodtype != null) {
		$("#loading-brands-img").show();
		var value = prodtype.options[prodtype.selectedIndex].value;		
		getBrandsAjax(value, selectedBrand);		
	}
}

function getBrandsAjax(productTypeId, selectedBrandId) {

	searchController.getBrandsForType(productTypeId, {
		callback:function(search) {
			dwr.util.removeAllOptions("brand");
			dwr.util.addOptions("brand", search, "value", "name" );
			
			if (selectedBrandId != "") {
				var newbrand = document.getElementById("brand");
				var i = 0;
				for (i = 0; i < newbrand.options.length; i++) {
					if (i % 10 == 0) {output = "";}
					if (newbrand.options[i].value == selectedBrandId) {
						newbrand.options[i].selected= true;
						break;
					}
				}
			}			
			$("#loading-brands-img").hide();
			
		},
  		timeout:5000,
  		errorHandler:function(message) {
  			$("#loading-brands-img").hide();
  			 if(dwrdebug) alert("Oops: " + message); 
  			
	 	}
	});
}

function getProductTypes(brand, producttype) {	
	
	var selectedProducttype = "";
	if (producttype != null && producttype.selectedIndex > 0) {
		selectedProducttype = producttype.options[producttype.selectedIndex].value;
	}
	if (brand != null) {
		var value = brand.options[brand.selectedIndex].value;
		$("#loading-producttypes-img").show();
		getProductTypesAjax(value, selectedProducttype);
	}
}

function getProductTypesAjax(brandId, selectedProductTypeId) {
	
	searchController.getProductTypesForBrand(brandId, {
		callback:function(search) {
			dwr.util.removeAllOptions("producttype");
			dwr.util.addOptions("producttype", search, "value", "name" );
			
			if (selectedProductTypeId != "") {
				var newproducttype = document.getElementById("producttype");
				var i = 0;
				for (i = 0; i < newproducttype.options.length; i++) {
					if (i % 10 == 0) {output = "";}
					if (newproducttype.options[i].value == selectedProductTypeId) {
						newproducttype.options[i].selected= true;
						break;
					}
				}
			}			
			$("#loading-producttypes-img").hide();
		},
  		timeout:5000,
  		errorHandler:function(message) { 
  			$("#loading-producttypes-img").hide();
  			if(dwrdebug) alert("Oops: " + message); 
  		
		}
	});
}

function getTypes(prodtype, brand) {	
	
	if (prodtype != null && prodtype.selectedIndex > 0 && brand != null && brand.selectedIndex > 0) {		
		var value = prodtype.options[prodtype.selectedIndex].value;
		var value2 = brand.options[brand.selectedIndex].value;		
		$("#loading-types-img").show();	
		getTypesAjax(value, value2, "");
	} else {
		document.forms['searchform'].elements['type'].disabled=true;	
	}
}

function getTypesAjax(productTypeId, brandId, selectedType) {
	
	if(productTypeId != null && productTypeId.length > 0 && brandId != null && brandId.length > 0) {
		searchController.getTypesForTypeAndBrand(productTypeId, brandId, {
			callback:function(search) {
				dwr.util.removeAllOptions("type");
				dwr.util.addOptions("type", search, "value", "name" );
				
				if(selectedType != "") {
					var newtype = document.getElementById("type");
					var i = 0;
					for (i = 0;i < newtype.options.length; i++) {
						if(i % 10 == 0) {output = "";}
						if(newtype.options[i].value == selectedType) {
							newtype.options[i].selected = true;
							break;
						}
					}
				}							
				document.forms['searchform'].elements['type'].disabled=false;
				$("#loading-types-img").hide();
			},
	  		timeout:5000,
	  		errorHandler:function(message) { 
	  			$("#loading-types-img").hide();
	  			if(dwrdebug) alert("Oops: " + message); 
	  			
  			}
		});
	}
	$("#loading-types-img").hide();			
}