function getCategories(listBox) {
	var len = listBox.length;
	var once_passed = false;
	var i = 0;
	var categories = "";
	for (i = 0; i < len; i++) {
		if (listBox[i].selected) {
		if(once_passed){categories += "+";}
		categories += listBox[i].value;// + "\n";
		once_passed=true;
		}
	}
	return categories;
}

function ClearList(list){
    var is_selected = false;
    if (list){
        for (var i = 0; i < list.options.length; i++) {
            if (list.options[i].selected){
                is_selected=true;
                list.selectedIndex =-1;
                break;
            }
        }
    }
    return is_selected;
}

function applyFilter(){
	if(document.filter.SEO.value == 0){
		var prefix = "&";
	}else{
		var prefix = "?";
	}
	var strip = prefix + "filter=";
	var url = "";
	var filter = "";
	var temp = false;

	var features = getCategories(document.filter.features);
	var manufacturers = getCategories(document.filter.manufacturers);
	var style = getCategories(document.filter.style);

	if(features != ""){
		filter += features;}
	if(manufacturers != "" && filter != ""){
		filter += "+" + manufacturers;
	}else{
		filter += manufacturers;}
	if(style != "" && filter != ""){
		filter += "+" + style;
	}else{
		filter += style;}
	//alert("Filter: "+filter);	
	start = location.href.indexOf(strip);
	if((location.href.indexOf("&",location.href.indexOf(strip)+1)) >1 && (location.href.indexOf(strip) != '-1')){
		end = location.href.indexOf("&",location.href.indexOf(strip)+1);
		//alert("end"+end);
	}else{
		end =location.href.length;
		if(start == -1 && document.filter.SEO.value != 0){
			start = location.href.indexOf("?");
			temp = true;
		}
	}
	
	
	//alert(start+"  -  "+end+" - "+location.href.length);
	var orginal_filter = location.href.substring(start,end);
	if(filter != "" && orginal_filter != ''){
		if(start == '-1'){
			url = orginal_filter + strip+filter;
			//alert(url);
			if(location.href.length > end){
				url += location.href.substring(end,location.href.length);
				/*if(document.filter.SEO.value != 0){
				t=location.href.substring(end,location.href.length);
				t=t.replace(prefix,"&");
				url = url.replace(location.href.substring(end,location.href.length),t);
			}*/
			}
		}else{
			if(document.filter.SEO.value != 0 && temp){
				url = location.href.substring(0,start);
				url += strip+filter;
				url += orginal_filter.replace(prefix,"&");
				//alert(url);
				//alert("Orginal filter: "+orginal_filter);	
			}else{
				url = location.href.replace(orginal_filter,strip+filter);
			}
		}
	}else{
		url = location.href.replace(orginal_filter,"");
		if(document.filter.SEO.value != 0){
			url = url.replace("&",prefix);
		}
	}
	if(document.filter.SEO.value == 1){
		//alert(url.replace(/index\d.html/, ""));
		url = url.replace(/index\d.html/, "");
	}else if(document.filter.SEO.value == 0){
		//alert(url.replace(/&page=\d/, ""));
		url = url.replace(/&page=\d/, "");
	}
	self.location=url;
}

function clearFilter(){
    var isFeaturesSelected = ClearList(document.filter.features);
    var isManufacturersSelected = ClearList(document.filter.manufacturers);
    var isStyleSelected = ClearList(document.filter.style);
    if((isFeaturesSelected || isManufacturersSelected || isStyleSelected) && (location.href.indexOf("filter") >1)){
	applyFilter();
    }
}