

Array.prototype.indexOf = function(value) {
	for( var i = 0; i < this.length; i++ ) {
		if( this[ i ] == value ) {
			return i;
		}
	}
	return -1;
}







function setCities() {
	var f = document.forms[0];
	var provinces = f.province;
	var pre_cities = document.getElementById("pre_cities");
	var city = f.city;
	
	clearCities();
	
	var j = 0;
	if(provinces.value == "") {
		cities.sort(cityCompare);
	}
	for( var i = 0; i < cities.length; i++ ) {
		
		if(provinces.value == "" || provinces.value == cities[i].province ) {
			pre_cities.options[pre_cities.options.length] = createOption(cities[i].city);
		}
	}
	
	provinces.onchange = function() {
		setCities();
	}
}
 
function setCitySelector() {
	var f = document.forms[0];
	
	var pre_cities = document.getElementById("pre_cities");
	pre_cities.onclick = function(){
		if(this.selectedIndex > -1) {
			preSelectOption(this.options[this.selectedIndex]);
		}
	};
	
	var cities = document.getElementById("cities");
	cities.onclick = function(){

		if(this.selectedIndex > -1) {
			deselectOption();
		}
	};
	
	f.onsubmit = function() {
		
		for(i = 0 ; i < f.cities.options.length ; i++ ) {
			f.cities.options[i].selected = true;
		}
	}
}

function cityCompare(a,b) {
	if(a.city < b.city) {
		return -1;
	} else if(a.city > b.city) {
		return 1;
	} else {
		return 0;
	}
}

function makeCityField() {
		var f = document.forms[0];
		if( f.cities.options.length == 0 && f.province.selectedIndex  > 0) 
			{alert("Selecteer minimaal 1 plaats");}
		else
			{
			for(i = 0 ; i < f.cities.options.length ; i++ ) {
				if (i == 0)
					f.city.value = f.city.value + f.cities.options[i].value  ;
				else
					f.city.value = f.city.value +  "," + f.cities.options[i].value ;
			 
			}
		f.submit();
		}
	 
}
function selectAllCities() {
	c = document.getElementById('cities');
	
	for(i =0;i< c.options.length;i++) {
		c.options[i].selected=true;
	}
	 
}

function deselectOption(option) {
	//var city = document.forms[0].city;
	var cities = document.getElementById("cities");
	var value = cities.options[cities.selectedIndex].value;
	//var values = city.value.split(",");
	//var index = values.indexOf(value);
	//values.splice(index,1);
	//city.value = values.toString();
	cities.options[cities.selectedIndex] = null;

	
	
}

function preSelectOption(option) {
	var cities = document.getElementById("cities");
	//var city = document.forms[0].city;
	for( var i = 0; i < cities.options.length; i++ ) {
		if(cities.options[i].value == option.value ) {
			return false;
		}
	}
	var opt = createOption(option.value,option.value);
	cities.options[cities.options.length] = opt;
		
	
	
	return true;
}

function clearCities() {
	var f = document.forms[0];
	var pre_cities = f.pre_cities;
	while( pre_cities.options.length > 0 ) {
		pre_cities.options[ 0 ] = null;
	}
	f.city.value = "";

}

function createOption(value,display) {
	if(!display) {
		var display = value;
	}
	var opt = new Option(display,value);
	return opt;
}

function setPriceSelectors() {
	var from = document.getElementById("priceFrom");
	var to = document.getElementById("priceTo");
	
	from.options.length = 0;
	to.options.length = 0;
	
	var a = from.options[from.options.length] = createOption(0,"€ 0");
	a.selected = true;
	for( var i = 25; i < 1001; i += 25 ) {
		var display = " €      " + i + ".000";
		if( i == 1000 ) {
			display = "€ 1.000.000";
		}  
		var value = i * 1000;
		
		from.options[from.options.length] = createOption(value,display);
		to.options[to.options.length] = createOption(value,display);
	}
	var z = to.options[to.options.length] = createOption("","Geen maximum");
	z.selected = true;
}

function setRentPriceSelectors() {
	var from = document.getElementById("priceFrom");
	var to = document.getElementById("priceTo");
	
	from.options.length = 0;
	to.options.length = 0;
	
	var a = from.options[from.options.length] = createOption(0,"€ 0 per maand");
	a.selected = true;
	var i = 100;
	while( i < 7000 ) {
		var display = " €   " + i + " per maand";
		from.options[from.options.length] = createOption(i,display);
		to.options[to.options.length] = createOption(i,display);
		if( i < 1000 ) {
			i += 100;
		} else if( i < 2000 ) {
			i += 250;
		} else {
			i += 1000;
		}
	}
	var z = to.options[to.options.length] = createOption("","Geen maximum");
	z.selected = true;
}
function setRentPriceSelectorsBog() {
	var from = document.getElementById("priceFrom");
	var to = document.getElementById("priceTo");
	
	from.options.length = 0;
	to.options.length = 0;
	
	var a = from.options[from.options.length] = createOption(0,"€ 0 per maand");
	a.selected = true;
	var i = 500;

	while( i < 10001 ) {
		var display = " €   " + i + " per maand";
		from.options[from.options.length] = createOption(i,display);
		to.options[to.options.length] = createOption(i,display);
		if( i < 2000 ) {
			i += 250;
		} else if( i < 5000 ) {
			i += 500;
		
		} else {
			i += 2500;
		}
	}
	var z = to.options[to.options.length] = createOption("","Geen maximum");
	z.selected = true;
}




function setSurfaceSelectors() {
	var from = document.getElementById("surfacefrom");
	var to = document.getElementById("surfaceto");
	
	
	var a = from.options[from.options.length] = createOption(0,"0 m²");
	a.selected = true;
	var i = 100;
	while( i < 10001 ) {
		var display = String(i).replace(/(.+)([0-9]{3})$/,"$1.$2") + " m²";
		
		from.options[from.options.length] = createOption(i,display);
		to.options[to.options.length] = createOption(i,display);
		if( i == 100 || i == 1000) {
			i *= 2.5;
		} else {
			i *= 2;
		}
	}
	var z = to.options[to.options.length] = createOption("","Geen maximum");
	z.selected = true;
}
function setOfficeSurfaceSelectors() {
	var from = document.getElementById("officesurfacefrom");
	var to = document.getElementById("officesurfaceto");
	
	
	var a = from.options[from.options.length] = createOption(0,"0 m²");
	a.selected = true;
	
	to.options[to.options.length] = createOption(-1,"0 m²");
	
	var i = 100;
	while( i < 10001 ) {
		var display = String(i).replace(/(.+)([0-9]{3})$/,"$1.$2") + " m²";
		
		from.options[from.options.length] = createOption(i,display);
		to.options[to.options.length] = createOption(i,display);
		if( i == 100 || i == 1000) {
			i *= 2.5;
		} else {
			i *= 2;
		}
	}
	var z = to.options[to.options.length] = createOption("99999999999","Geen maximum");
	z.selected = true;
}
function setRoomsSelector() {
	var from = document.getElementById("roomsFrom");
	var to = document.getElementById("roomsTo");
	
	
	var a = from.options[from.options.lenght] = createOption(0);
	a.selected = true;
	for( var i = 0; i < 11; i++ ) {
		from.options[from.options.length] = createOption(i);
		to.options[to.options.length] = createOption(i);	
	}
	var z = to.options[to.options.length] = createOption("","Geen maximum");
	z.selected = true;
}

function setSubTypeSelector(){
	var subTypeRow = document.getElementById('subTypeRow');
	var subTypeLabelHolder = document.getElementById('subTypeLabel'); 
	var subTypeSelector = document.getElementById('subtype');
	var mainType = document.getElementById('main_type').options[document.getElementById('main_type').selectedIndex].value;

	
	switch(mainType) {
		case "":
			subTypeRow.style.visibility = 'hidden';
			subTypeSelector.options.length = 0;
			subTypeSelector.options[subTypeSelector.options.length] = createOption("","");
			subTypeLabelHolder.innerHTML = 'Soort woning:';
			break;
		case "1":
			subTypeSelector.options.length = 0;
			subTypeSelector.options[subTypeSelector.options.length] = createOption("1","Alle Woonhuizen");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("10001","Vrijstaande woning");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("10002","Verspringende woning");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("10003","Helft van dubbel");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("10004","Tussenwoning");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("10005","Hoekwoning");
			subTypeRow.style.visibility = 'visible';
			subTypeLabelHolder.innerHTML = 'Soort woning:';
			break;
		case "2":
			subTypeSelector.options.length = 0;
			subTypeSelector.options[subTypeSelector.options.length] = createOption("2","Alle appartementen");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("202","Appartement");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("201","Bovenwoning");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("203","Benedenwoning");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("204","Maisonette");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("205","Galerijflat");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("206","Portiekflat");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("207","Beneden en bovenwoning");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("208","Verzorgingsflat");
			subTypeSelector.options[subTypeSelector.options.length] = createOption("209","Penthouse");
			subTypeRow.style.visibility = 'visible';
			subTypeLabelHolder.innerHTML = 'Soort appartement:';
			break;
		case "3":
			subTypeSelector.options.length = 0;
			subTypeSelector.options[subTypeSelector.options.length] = createOption("3","Bouwgrond");
			break;
		case "4":
			subTypeSelector.options.length = 0;
			subTypeSelector.options[subTypeSelector.options.length] = createOption("4","Garage");
			break;			
	}
	setType();
}

function setType(  ) {
	var t 		= document.getElementById('type');
	var st 		= document.getElementById('subtype');
	t.value = '';
	if(st) {
		if(st.options.length) {
			t.value = st.options[st.selectedIndex].value;
		}
	}
}

function toggleSearch()
{
	var toggle = document.getElementById("toggle");
	var searchType = toggle.getAttribute("searchType");
	if(searchType == "advanced") {
		var rs = document.getElementsByTagName("TR");
		for( var i = 0; i < rs.length; i++) {
			var r = rs[i];
			if(r.className == "Advanced" ) {
				try {
					r.style.display = "table-row";
				} catch(e) {
					r.style.display = "block";
				}
			}
		}
		toggle.innerHTML = "Eenvoudig zoeken";
		toggle.setAttribute("searchType","simple");
	} else {
		var rs = document.getElementsByTagName("TR");
		for( var i = 0; i < rs.length; i++) {
			var r = rs[i];
			if(r.className == "Advanced" ) {
				r.style.display = "none";
			}
		}
		toggle.innerHTML = "Uitgebreid zoeken";
		toggle.setAttribute("searchType", "advanced");
	}
}

function getResultIndex() {
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			index = eval(xmlHttp.responseText);
        	index.pop();
        	//calculateResults();
        }
	}
	xmlHttp.open("GET","resultIndex.cfm",true);
	xmlHttp.send(null);
 }
 
 function calculateResults() {
 	if(!index) {
 		return;
 	}
 	var count = document.getElementById("resultCount");
 	var nrOfObjects = index.length;
 	var f = document.forms[0];
 	var provinceQuery = f.province.value;
 	var cityQuery = f.city.value;
 	var buildTypeQuery = f.buildType.value;
 	var typeQuery = f.type.value;
 	var priceFromQuery = f.priceFrom.value;
 	var priceToQuery = f.priceTo.value;
 	var roomsFromQuery = f.roomsTo.value;
 	var roomsToQuery = f.roomsTo.value;
 	
 	
 	/*
 	for( var i = 0; i < index.length; i++ ) {
 		if(provinceQuery == "" || provinceQuery == index[i].province) {
 			nrOfObjects++;
 		}
 	}
 	alert(provinceQuery);
 	alert(cityQuery);
 	alert(buildTypeQuery);
 	alert(typeQuery);
 	alert(priceFromQuery);
 	alert(priceToQuery);
 	alert(roomsFromQuery);
 	alert(roomsToQuery);
 	*/
 	if(nrOfObjects == 1 ) {
 		count.innerHTML = nrOfObjects + " object gevonden";
 	} else {
 		count.innerHTML = nrOfObjects + " objecten gevonden";
 	}
 }
 
 function setResultCountCalculator() {
 	var s = document.getElementsByTagName("SELECT");
 	for( var i = 0; i < s.length; i++ ) {
 		//s[i].onchange = calculateResults;
 	}
 }
