var ajaxObjects = new Array();

function filterData(name,path){
	var serverSideFile = path + "/include/request.php";
	var ajaxIndex = ajaxObjects.length;
	var item = document.getElementById(name).options[document.getElementById(name).selectedIndex].value;
	
	if(name == "category")
		item = item + "-" + document.getElementById("manufacturer").options[document.getElementById("manufacturer").selectedIndex].value;

	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile;
	ajaxObjects[ajaxIndex].setVar('item',item);
	ajaxObjects[ajaxIndex].setVar('command',name);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showFilterResults(name,ajaxIndex); };
	ajaxObjects[ajaxIndex].runAJAX();	
}

function showFilterResults(name,ajaxIndex){ 
	
	if(name == "category")
		var requestSub = ajaxObjects[ajaxIndex].response.split("###");
	else{
		var requestArr = ajaxObjects[ajaxIndex].response.split("%%%");
		var requestSub = requestArr[1].split("###");
		var requestCat = requestArr[0].split("###");

		var objCategory = document.getElementById("category");		
		removeOption(objCategory);
	
		objCategory.options[0] = new Option("- Select one -","0");
		if(requestCat != 0){
			if(requestCat.length > 0){
				for(var i = 0; i < requestCat.length; i++){
					var optionsArr = requestCat[i].split("|||");
					objCategory.options[i + 1] = new Option(optionsArr[1],optionsArr[0]);
				}
			}
		}
	}

	var objSubCategory = document.getElementById("subcategory");		
	removeOption(objSubCategory);
	
	objSubCategory.options[0] = new Option("- Select one -","0");
	if(requestSub != 0){
		if(requestSub.length > 0){
			for(var i = 0; i < requestSub.length; i++){
				var optionsArr = requestSub[i].split("|||");
				objSubCategory.options[i + 1] = new Option(optionsArr[1],optionsArr[0]);
			}
		}
	}
	
	ajaxObjects[ajaxIndex] = false;
}

function removeOption(obj)
{
	for (var i = obj.length; i > 0; i--) {
		obj.remove(i);
	}
}