

/*-- constructor --*/
function filterSimulation(appliedPage,catLevel_1,catLevel_2,textSearch){

	/*# Instance variables #*/
	//Objects containing all required data, set up in filterSimulationInit()
	this.appliedPage		 = appliedPage;
	this.catLevel_1			 = catLevel_1;
	this.catLevel_2			 = catLevel_2;
	this.textSearch			 = textSearch;
	
	this.request			 = false;
	this.defOptionHTML		 = '<option value="0">(Vælg)</option>';
	
	/*# Methods #*/
	this.loadSelectUnfiltered = loadSelectUnfiltered;
	this.reduceCatLevel2	 = reduceCatLevel2;
	this.filter				 = filter;
	this.getRequestedVars	 = getRequestedVars;
	

}


//Loads default select for a category
function loadSelectUnfiltered( catLevelObj ){

	var xmlHttp = getHTTPObject();
	
	catLevelObj.loadStatus = "uninitialized";
	
	// Build the URL to connect to
	var url = catLevelObj.optionPage;
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	var tempObj = this;
	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = function(){
		
		/*
	   readyState
	   1. 0 = uninitialized
	   2. 1 = loading
	   3. 2 = loaded
	   4. 3 = interactive
	   5. 4 = complete
		*/
		if (xmlHttp.readyState == 1)
			catLevelObj.loadStatus = "loading";
		
		
		if (xmlHttp.readyState == 4) {
			var response = xmlHttp.responseText;
			//catLevelObj.selectEl.innerHTML = tempObj.defOptionHTML + response;//IE bug
			select_innerHTML( catLevelObj.selectEl , tempObj.defOptionHTML + response );
			catLevelObj.loadStatus = "complete";
			setSelect( catLevelObj.selectEl , catLevelObj.request );
		}
		
	};
	
	// Send the request
	xmlHttp.send(null);

}



function reduceCatLevel2(){

	this.catLevel_2.loadStatus = "uninitialized";

	var reqCatId = this.catLevel_1.selectEl.value;
	
	// Build the URL to connect to
	var url = this.catLevel_2.filteredOptionPage + "?" + this.catLevel_2.filteredOptionPageQueryVar + "=" + escape(reqCatId);
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	var tempObj = this;
	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = function(){
		
		/*
	   readyState
	   1. 0 = uninitialized
	   2. 1 = loading
	   3. 2 = loaded
	   4. 3 = interactive
	   5. 4 = complete
		*/
		if (xmlHttp.readyState == 1)
			tempObj.catLevel_2.loadStatus = "loading";
			
			
		if (xmlHttp.readyState == 4) {
			var response = xmlHttp.responseText;
			//tempObj.catLevel_2.selectEl.innerHTML = tempObj.defOptionHTML + response;//IE bug
			select_innerHTML( tempObj.catLevel_2.selectEl , tempObj.defOptionHTML + response );
			tempObj.catLevel_2.loadStatus = "complete";
		}
		
	};
	
	// Send the request
	xmlHttp.send(null);


}



function getRequestedVars(){

	this.catLevel_1.request = getQueryVariable( "filtercontrol" + this.appliedPage.pageID + this.catLevel_1.filterControl );
	this.catLevel_2.request = getQueryVariable( "filtercontrol" + this.appliedPage.pageID + this.catLevel_2.filterControl );
	this.textSearch.request = getQueryVariable( "searchword" + this.appliedPage.pageID );
	
	if( this.textSearch.request )
		document.getElementById( this.textSearch.inputEl.id ).value = this.textSearch.request;
	
	
}




function filter(){

	this.catLevel_1.request = this.catLevel_1.selectEl.value;
	this.catLevel_2.request = this.catLevel_2.selectEl.value;
	this.textSearch.request = this.textSearch.inputEl.value;
	
	var querystring	  = "doshow" + this.appliedPage.pageID + "=1";
	querystring		 += "&filtercontrol" + this.appliedPage.pageID + this.catLevel_1.filterControl + "=" + this.catLevel_1.request;
	querystring		 += "&filtercontrol" + this.appliedPage.pageID + this.catLevel_2.filterControl + "=" + this.catLevel_2.request;
	querystring		 += "&searchword" + this.appliedPage.pageID + "=" + escape(this.textSearch.request);
	
	var page = this.appliedPage.pageName + "?" + querystring;
	
	document.location.href = page;

}

