CndCompany.include_once(CndCompany.scriptPath + 'prototype/prototype.js');

var cndCatalog_WebFilter = null;
function CndCatalog_WebFilter() {

	this.ajax = new CndCatalog_AjaxWebFilter();
	this.ajax.webFilter=this;
	this.ajax.onSetFilterValue=function(transport, json, outerClass) {
		
		var toFill = Array();
		var tmp = null;
		var name = null;
		var value = null;
		var countUncompleted = 0;
		for (var i=outerClass.webFilter.filterSelection.length-1; i>=0; i--) {
			tmp = outerClass.webFilter.form.elements["productFilter[" + outerClass.webFilter.filterSelection[i] + "]"];
			if (tmp != outerClass.srcElem) {
				tmp.disabled=true;
				toFill.push(tmp);
			}
		}
		
		if (toFill.length > 0) {
			for (var i=toFill.length-1; i>=0; i--) {
				var name = toFill[i].name.replace(/\w*\[(\w*)\]/, '$1');
				
				var ajax = new CndCatalog_AjaxWebFilter();
				ajax.selectElem = toFill[i];
				ajax.onGetFilterValuesByFilterName=function(transport, json, outerClass) {

					var values = eval("(" + transport.responseText + ")");
					
					var selected = outerClass.selectElem.options[outerClass.selectElem.selectedIndex].value;
					for (var j=outerClass.selectElem.options.length-1; j>0; j--) {
						outerClass.selectElem.remove(j);
					}
					
					for (a in values) {
						var tmp = "" + values[a];
						var option = null;
//						if (true || !tmp.match(/function|reverse\s?\(/)) {
							option = document.createElement("option");
							option.value = a;
							option.text = tmp;
							if (a == selected) {
								option.selected = true;
							}
							if (navigator.appName.match(/Microsoft/)) {
								outerClass.selectElem.add(option);
							} else {
								outerClass.selectElem.add(option, null);
							}
//						}
					}
					
					outerClass.selectElem.disabled=false;
				}
				ajax.getFilterValuesByFilterName(name, 'async');
			}
		}
		
		if (countUncompleted > 0) {
			outerClass.webFilter.perfomDeCompletition();
		} else {
			outerClass.webFilter.perfomCompletition();
		}

		outerClass.webFilter.stopProcessing();
	};
	
	this.form = null;
	this.filterSelection = null;
	this.filterValues = null;
	this.seriesProduct = null;
	this.categoryId = 0;
	
	this.onSelectionChange = function(evt) {
		this.startProcessing();

		var src = (evt.target) ? evt.target : evt.srcElement;
		var srcName = src.name.replace(/\w*\[(\w*)\]/, '$1');
		var srcValue = src.options[src.selectedIndex].value;
		this.ajax.srcElem=src;
		this.ajax.setFilterValue(srcName, srcValue, 'async');
	}
	
	this.setCategory = function(categoryId) {
		this.categoryId = categoryId;
	}

}

	

CndCatalog_WebFilter.getInstance = function () {
	if (!cndCatalog_WebFilter) {
		cndCatalog_WebFilter = new CndCatalog_WebFilter();
	}
	return cndCatalog_WebFilter;
}

CndCatalog_WebFilter.registerListener = function(form, filterSelection) {
	var inst = CndCatalog_WebFilter.getInstance();
	inst.form = form;
	inst.filterSelection = filterSelection;
		
	var tmp = null;
	for (var i=inst.filterSelection.length-1; i>=0; i--) {
		tmp = form.elements["productFilter[" + inst.filterSelection[i] + "]"];
		tmp.onchange=function(event){var evt = (event) ? event : window.event;CndCatalog_WebFilter.getInstance().onSelectionChange(evt);}
	}
}

CndCatalog_WebFilter.prototype.startProcessing = function(){};
CndCatalog_WebFilter.prototype.stopProcessing = function(){};
CndCatalog_WebFilter.prototype.perfomCompletition = function(){};
CndCatalog_WebFilter.prototype.perfomDeCompletition = function(){};

