
function filterlist(selectobj) {

	this.arrayFroelist = new Array(IntAantalFroePos);
	this.selectobj = selectobj;

	//INIT ######################################################
		this.init = function() {
			//kopie van de volledige systemenlijst
			this.optionscopy = new Array(); 
			if (this.selectobj && this.selectobj.options){
				for (var i=0; i < this.selectobj.options.length; i++){
					this.optionscopy[i] = new Option();
					this.optionscopy[i].text = selectobj.options[i].text;
					this.optionscopy[i].value = selectobj.options[i].value;
					this.optionscopy[i].setAttribute("froecode",selectobj.options[i].getAttribute('froecode'));
					this.optionscopy[i].setAttribute("sysid",selectobj.options[i].getAttribute('sysid'));
					this.optionscopy[i].setAttribute("sysnaam",selectobj.options[i].getAttribute('sysnaam'));
					this.optionscopy[i].setAttribute("pid",selectobj.options[i].getAttribute('pid'));
					this.optionscopy[i].setAttribute("pnaam",selectobj.options[i].getAttribute('pnaam'));
					
					//**********************************************************************************************************************************************************************
					this.optionscopy[i].setAttribute('attr_surl',selectobj.options[i].getAttribute('attr_surl'));
					this.optionscopy[i].setAttribute('attr_sintro',selectobj.options[i].getAttribute('attr_sintro'));
					this.optionscopy[i].setAttribute('attr_rpurl',selectobj.options[i].getAttribute('attr_rpurl'));
				}
			}

			//kopier alle optielijstjes ( afhankelijk van het aantal froe-posities
			this.OptionArray = new Array();
				for (var y=0; y<IntAantalFroePos; y++){
				
					//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
					if(document.getElementById("froepos_"+(y+1))){
						ObjfrmSelect = document.getElementById("froepos_"+(y+1))
						IntOptionsLen = ObjfrmSelect.options.length
						this.OptionArray[y] = new Array(IntOptionsLen);
							for (var z=0; z<IntOptionsLen; z++){
								this.OptionArray[y][z] = new Option();
								this.OptionArray[y][z].text = ObjfrmSelect.options[z].text;
								this.OptionArray[y][z].value = ObjfrmSelect.options[z].value;
								
							//**********************************************************************************************************************************************************************
							}
					}
				}
			this.set()
		}
	//END INIT --------------------------------------------------

	//SET ######################################################
		this.set = function() {
			//variabelen
			var loop=0, index=0, regexp, e;
			if(!this.preselectfroe){
				this.preselectfroe = "";
			}
			this.countprod = 0;
			this.countsys = 0;
			this.doubles = "";
			this.listprod = "";			
			this.listsys = "";
			this.emptyoption = '';
			
			for (var x=0; x<IntAantalFroeDigits; x++){
				this.emptyoption = this.emptyoption+"~"
			}
			
			//froelijst leegmaken, maak van iedere positie een #-je
			for (var y=0; y<IntAantalFroePos; y++){
				this.arrayFroelist[y] = "#";
			}
		
			//resulatenlijst leeg kicken
			this.selectobj.options.length = 0;
	
			//DE FROESELECTIE start/selectie en VRIJE INVOER
			if(START_Search!=""){
				document.getElementById('search_selectie').value = START_Search;
				START_Search = "";
			}
			if(START_Froe!=""){
				strFroeRequest = START_Froe;
				START_Froe = "";
			}
			else{
				//Wat is de Froe-code (op basis van alle optielijstjes) en maak het RegExp-patroon. Notatie "/|...\\|...\\|...|/"
				var strFroeRequest = '|';
				for (var i=1; i<IntAantalFroePos+1; i++){
					//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
					if(document.getElementById("froepos_"+i)){
						var antwmodelfunctie = document.getElementById("froepos_"+i).getAttribute('functie')

						if(antwmodelfunctie==''){
							//er hoeft geen funtie berekening, dus deze froecode gebruiken
							strFroeRequest = strFroeRequest + document.getElementById("froepos_"+i).value;}
						else{
							//afhankelijk van de functie, moet er een range van froecodes gebruikt worden.
							//iedere waarde kan vooraf worden gegevaan door 1 of meerder nullen (10 max)
							
							//****************************************************************************************************************
							if(antwmodelfunctie=='<'){
								strFroeRequest = strFroeRequest +'(0{0,10}[1-'+parseInt(document.getElementById("froepos_"+i).value)+'])';
							}

							if(antwmodelfunctie=='<='){
								strFroeRequest = strFroeRequest +'(0{0,10}[1-'+parseInt(document.getElementById("froepos_"+i).value)+'])';
							}
							
							if(antwmodelfunctie=='>'){
								if(isNaN(document.getElementById("froepos_"+i).value)){
									strFroeRequest = strFroeRequest +'(0{0,10}[0-9])';
								}
								else{
									strFroeRequest = strFroeRequest +'(0{0,10}['+parseInt(document.getElementById("froepos_"+i).value)+'-9])';
									
								}
							}

							if(antwmodelfunctie=='>='){
								if(isNaN(document.getElementById("froepos_"+i).value)){
									strFroeRequest = strFroeRequest +'(0{0,10}[0-9])';
								}
								else{
									tmp = document.getElementById("froepos_"+i).value
									tmp.replace(new RegExp("\A0{0,10}/g/i"), ""); 

									strFroeRequest = strFroeRequest +'(0{0,10}['+tmp+'-9])';
									//alert('froepos_'+i+' custom #'+document.getElementById("froepos_"+i).value+' #'+parseInt(document.getElementById("froepos_"+i).value));
								}
							}
							//****************************************************************************************************************
						
						}
					}
					else{
						strFroeRequest = strFroeRequest + this.emptyoption;
					}
					//seperaties/einde froecode
					if(i==IntAantalFroePos){strFroeRequest = strFroeRequest + '|';}
					else{strFroeRequest = strFroeRequest + '\\|';}
				}
			}
			strFroeRequest = "/"+strFroeRequest+"/";
			
			//('~' vervangen door '.') t.b.v. lege locaties binnen de expressie
			strFroeRequest = strFroeRequest.replace(/~/g,".");
			RegExFroe = new RegExp(strFroeRequest);
			
			//reguliere expressie aanzetten, voor het controleren van het vrije-zoeken-veld
			searchpattern = document.getElementById('search_selectie').value;
			try{regexp = new RegExp(searchpattern, 'i');}
			catch(e){return;}

			//doorloop de gehele systemenlijst
			for (loop=0; loop < this.optionscopy.length; loop++) {
				var option = this.optionscopy[loop];
				
				//test de vrije invoer met deze optie...(alleen getest op de textvalue, (nog) niet op attributen)
				//if(regexp.test(option.text)){
				//Zoeken door:
				if(regexp.test(option.text+option.getAttribute('attr_sintro')+option.getAttribute('attr_sextra'))){

					//voldoet de huidige optie aan de froecode. vb: regexp.test(option.text)) || (this.match_value && regexp.test(option.value)
					if(RegExFroe.test(option.getAttribute('froecode'))){
						
						
						//Systemenlijst samenstellen
						this.makelist('listsys',option);
						

							if(this.doubles.indexOf("|"+option.getAttribute('pid')+"|")==-1){
							
								
							
								//Productenlijst samenstellen
								//this.makelist('listprod',option.getAttribute('pnaam'),option.getAttribute('attr_rpurl'));
								this.makelist('listprod',option);
								
								this.selectobj.options[index] = new Option(option.text, option.value, false);
								this.selectobj.options[index].setAttribute("froecode",option.getAttribute('froecode'));
								this.selectobj.options[index].setAttribute("sysid",option.getAttribute('sysid'));
								this.selectobj.options[index].setAttribute("sysnaam",option.getAttribute('sysnaam'));
								this.selectobj.options[index].setAttribute("pid",option.getAttribute('pid'));
								this.selectobj.options[index].setAttribute("pnaam",option.getAttribute('pnaam'));
								
								
							//**********************************************************************************************************************************************************************
							this.selectobj.options[index].setAttribute('attr_surl',option.getAttribute('attr_surl'));
							this.selectobj.options[index].setAttribute('attr_sintro',option.getAttribute('attr_sintro'));
							this.selectobj.options[index].setAttribute('attr_rpurl',option.getAttribute('attr_rpurl'));
	
								index = index+1
								this.doubles = this.doubles+"|"+option.getAttribute('pid')+"|";
								this.countprod = this.countprod+1;
								
								
							}
							
							//systemen tellen
							this.countsys = this.countsys+1;
							
						
							//vul de froelist-array, voor ieder positie is er een lijst met gematch-de codes (houd daarbij rekening met de "|"-scheidingstekens
							strAddFroe = option.getAttribute('froecode')
							for (var y=0; y<IntAantalFroePos; y++){
								startpos = y*(IntAantalFroeDigits+1)+2
								this.arrayFroelist[y] = this.arrayFroelist[y]+strAddFroe.substring((startpos-1),(startpos-1)+IntAantalFroeDigits)+"#"
							}
					}
				}
			}
			this.filteroptions();
			this.showlist();
			
			//Trespa
			this.Paneeluitvoering();

			this.docount();
		}
	//END SET--------------------------------------------------
	
	// Trespa Paneeluitvoering +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.Paneeluitvoering = function(){
		currentPaneeluitvoering = document.frmUtopisSelector.froepos_5.options[document.frmUtopisSelector.froepos_5.selectedIndex].value;
		if(currentPaneeluitvoering==001&&document.getElementById('checkpaneeluitvoering').innerHTML==''){
			
			var tekst = confirm("U heeft bij paneeluitvoering gekozen voor \"Standaard\". \nVolgens het bouwbesluit dient u met het oog op de brandvoorschriften in bepaalde gevallen te kiezen voor de FR-kwaliteit (FR staat voor Fire-Retardant). In het volgende stroomschema bieden wij u de mogelijkheid uw keuze te controleren.")
				if (tekst){
					dolayer('Info','InfoSluit');
					document.getElementById('checkpaneeluitvoering').innerHTML='001';
					this.resetoption('froepos_5');
				}
				else{
					document.getElementById('checkpaneeluitvoering').innerHTML='001';
					this.resetoption('froepos_5');
					
					//alert("geen keuze")
				}

		
			
			
			
		}
	}

	//RESET FUNCTIES ######################################################
	this.reset = function() {
		this.preselectfroe=''; //de pre-selectie weghalen
		document.getElementById('search_selectie').value = '';//text-zoeken weghalen
		
		//lijstjes leegkicken en nieuwe maken o.b.v. volledige lijst
		for (var y=0; y<IntAantalFroePos; y++){
			//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
			if(document.getElementById("froepos_"+(y+1))){
				ObjfrmSelect = document.getElementById("froepos_"+(y+1))
				ObjfrmSelect.options.length = 0;
					IntPossibleLen = this.OptionArray[y].length;
					index = 0;
					for (var z=0; z<IntPossibleLen; z++){
						ObjfrmSelect.options[index] = new Option(this.OptionArray[y][z].text, this.OptionArray[y][z].value, false);
						
					//**********************************************************************************************************************************************************************
						index = index+1;
					}
				ObjfrmSelect.options[0].selected = true;
			}
		}
		//Trespa
		document.getElementById('checkpaneeluitvoering').innerHTML = '';
		
		this.set()
	}

	this.resetoption = function(objid) {
		document.getElementById(objid).options[0].selected = true;
		this.setselobj()
		this.set()
	}

	this.resetinput = function() {
		document.getElementById('search_selectie').value = '';
		this.set()
	}
	//END RESET FUNCTIES--------------------------------------------------
	
	//SETSELOBJ ######################################################
	this.setselobj = function(obj){
		this.preselectfroe = ""
		for (var y=1; y<IntAantalFroePos+1; y++){
			//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
			if(document.getElementById("froepos_"+y)){
				ObjfrmSelect = document.getElementById("froepos_"+y)
				if(!ObjfrmSelect.options[0].selected){
					this.preselectfroe = this.preselectfroe+"froepos_"+y+","+ObjfrmSelect.value+"#";
				}
			}
		}
	}
	//END SETSELOBJ --------------------------------------------------
	
	//FILTEROPTIONS ######################################################
		this.filteroptions = function(){
			for (var y=0; y<IntAantalFroePos; y++){
				
				//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
				if(document.getElementById("froepos_"+(y+1))){

					//optielijsten leegmaken
					ObjfrmSelect = document.getElementById("froepos_"+(y+1))
					ObjfrmSelect.options.length = 0;
					
						//hoeveel opties zijn er in totaal per lijst?
						IntPossibleLen = this.OptionArray[y].length
						
						//er moet altijd een 'lege' optie bovenaan
						index = 0
	
						ObjfrmSelect.options[index] = new Option("", this.emptyoption, false);
						index = 1
						
						//welke opties voldoen nog aan de selectie/froe-array?
						for (var z=0; z<IntPossibleLen; z++){
							var myReg = new RegExp("#"+this.OptionArray[y][z].value+"#")
							var regfilter = myReg.exec(this.arrayFroelist[y])
							if (regfilter) {
								ObjfrmSelect.options[index] = new Option(this.OptionArray[y][z].text, this.OptionArray[y][z].value, false);
						
							//**********************************************************************************************************************************************************************
								index = index+1
							}
						}
				}
			}

			//als er maar 2 opties zijn (waarvan 1 leeg) dan altijd de tekst-optie selcteren, nummer 2 dat is.
			for (var y=0; y<IntAantalFroePos; y++){
				//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
				if(document.getElementById("froepos_"+(y+1))){
					ObjfrmSelect = document.getElementById("froepos_"+(y+1))
					if(ObjfrmSelect.options.length==2){
						ObjfrmSelect.options[1].selected = true;
					}
				}
			}

			//maak de voorselecties
			if(!this.preselectfroe==''){
				//alert('maak voorselecties='+this.preselectfroe);
				var preselectallarray = this.preselectfroe.split("#")
				for (var z=0; z<preselectallarray.length; z++){
					if(preselectallarray[z]!=''){
						var preselectthisarray = preselectallarray[z].split(",")
						//welke optie heeft dezelfde froecode als de geselecteerde
						for (var i=0; i < document.getElementById(preselectthisarray[0]).options.length; i++){
							if(preselectthisarray[1]==document.getElementById(preselectthisarray[0]).options[i].value){
								document.getElementById(preselectthisarray[0]).options[i].selected = true;
							}
						}	
					}
				}
			}
			else{
				for (var i=1; i<IntAantalFroePos+1; i++){
					//een positie in de Froecode hoeft niet perse te betekenen dat er ook een vraag is. Mag ook gewoon leeg zijn
					if(document.getElementById("froepos_"+i)){
						if(document.getElementById("froepos_"+i).options.length==2){
							//als er ZEKER nog maar 1 optie over is selecteer deze dan
							if(ObjfrmSelect.options[1]){
								ObjfrmSelect.options[1].selected = true;
							}
						}
						else{
							//selecteer de null(eerste)-optie
							document.getElementById("froepos_"+i).options[0].selected = true
						}
					}
				}
			}
		}
	//END showlist --------------------------------------------------
	
	this.code_afbeelding = function(froeposid,layerstatus){
		var ObjfrmSelect = document.getElementById('froepos_'+froeposid)
		var strCodeAfb = '';
		strCodeAfb = strCodeAfb+'<a href="#" onclick=fabcode_s1.custom_layer("hidden")>sluiten</a>';
		for (var y=1; y<ObjfrmSelect.options.length; y++){
			strCodeAfb = strCodeAfb+'<br><img  onclick=fabcode_s1.distantselect("'+froeposid+'","'+ObjfrmSelect.options[y].value+'","code_afbeelding"); onmouseover="imageover(this);" id="code_afbeelding_'+froeposid+'_'+y+'" src="test/'+ObjfrmSelect.options[y].getAttribute('attr_aafb')+'.jpg" class="image_off" width="50" height="50" vspace=5 alt="'+ObjfrmSelect.options[y].text+'">';
		}
		document.getElementById('custom_layer').innerHTML = strCodeAfb;
		
		if(layerstatus!=''||layerstatus!='undefined'){
			alert('layerstatus aanpassen='+layerstatus);
			this.custom_layer(layerstatus);
		}
	}
	
	this.custom_layer = function(layerstatus){
		document.getElementById('custom_layer').style.visibility = layerstatus;
	}
		
	this.distantselect = function(froeposid,froecode,objparser){
		var ObjfrmSelect = document.getElementById('froepos_'+froeposid)
		for (var y=1; y<ObjfrmSelect.options.length; y++){
				if(ObjfrmSelect.options[y].value==froecode){
					ObjfrmSelect.options[y].selected = true;
				}
		}
		//voor verschillende selecteermethoden kunnen verschillende vervolgacties nodig zijn
		if(objparser=='code_afbeelding'){
			//SET aanroepen
			// document.getElementById('code_afbeelding_'+froeposid+'_2').className = 'code_afb_img_on';
			this.set()
			this.code_afbeelding(froeposid);
		}
	}
	
	//DOCOUNT ######################################################
  	this.docount = function(){
		if(document.getElementById('CountProducten_extra')){document.getElementById('CountProducten_extra').innerHTML = this.countprod;}
		if(document.getElementById('CountSystemen_extra')){document.getElementById('CountSystemen_extra').innerHTML = this.countsys;}
  	}
	//END DOCOUNT --------------------------------------------------
	
	//CUSTOMIZE PREVIEW/LIST ######################################################
	this.showlist = function(){
		document.getElementById('resultlist_producten').innerHTML = this.listprod;
		document.getElementById('resultlist_systemen').innerHTML = this.listsys;
	}
	//END CUSTOMIZE PREVIEW/LIST --------------------------------------------------
	
	this.makelist = function(listname,obj){
		if(listname=='listsys'){
			strtemp = formatsys;
			strtemp = strtemp.replace('[froecode]',obj.getAttribute('froecode'))
strtemp = strtemp.replace('[sysid]',obj.getAttribute('sysid'))
strtemp = strtemp.replace('[sysnaam]',obj.getAttribute('sysnaam'))
strtemp = strtemp.replace('[attr_sintro]',obj.getAttribute('attr_sintro'))
strtemp = strtemp.replace('[pid]',obj.getAttribute('pid'))
strtemp = strtemp.replace('[pnaam]',obj.getAttribute('pnaam'))
strtemp = strtemp.replace('[attr_rpurl]',obj.getAttribute('attr_rpurl'))
strtemp = strtemp.replace('[attr_surl]',obj.getAttribute('attr_surl'))
strtemp = strtemp.replace('[attr_sintro]',obj.getAttribute('attr_sintro'))

			this.listsys = this.listsys+strtemp+'<br>';
		}
		if(listname=='listprod'){
			strtemp = formatprod;
			strtemp = strtemp.replace('[froecode]',obj.getAttribute('froecode'))
strtemp = strtemp.replace('[sysid]',obj.getAttribute('sysid'))
strtemp = strtemp.replace('[sysnaam]',obj.getAttribute('sysnaam'))
strtemp = strtemp.replace('[attr_sintro]',obj.getAttribute('attr_sintro'))
strtemp = strtemp.replace('[pid]',obj.getAttribute('pid'))
strtemp = strtemp.replace('[pnaam]',obj.getAttribute('pnaam'))
strtemp = strtemp.replace('[attr_rpurl]',obj.getAttribute('attr_rpurl'))
strtemp = strtemp.replace('[attr_surl]',obj.getAttribute('attr_surl'))
strtemp = strtemp.replace('[attr_sintro]',obj.getAttribute('attr_sintro'))

			this.listprod = this.listprod+strtemp+'<br>';
		}
	}
	this.init();
	this.docount()
}

function resultswitchto(listid){
	document.getElementById('resultlist_producten').style.visibility = 'hidden';
	document.getElementById('resultlist_systemen').style.visibility = 'hidden';
	document.getElementById('resultlist_'+listid).style.visibility = 'visible';
}

function imageover(obj){
	obj.className = 'image_on';
}
function imageout(obj){
	obj.className = 'image_off';
}

function popup(strpage){
	popupwindow=window.open(strpage,'popup','toolbar=0,location=0,scrollbars=1,status=0,menubar=0,resizable=1,width=720,height=600,left='+(screen.width-720)/2+',top='+(screen.height-600)/2+'');
}

var formatsys = "<a href='http://www.bestekschrijven.nl/cws/c5launch.dll/ipsnet.exe.0?refr=website?dirn=Trespa?skel=Trespa?[attr_surl]' target='_blank'>[attr_sintro]</a><br>";
var formatprod = "<a href='http://www.utopis.net/modelbak_nl/[attr_rpurl]/01' target='_blank'>[pnaam]</a>";

var START_Froe = ""
var START_Search = "";

var IntAantalFroeDigits = 3;
var IntAantalFroePos = 5;
var fabcode_s1 = new filterlist(document.frmUtopisSelector.froe_selection);