// detailsearch.js by frank thx to QuirksMode
window.onload = init;

function init()
{
	optionTest = true;
	if (!document.getElementById)
		{ 
		optionTest = false;
		return;
		}

	initeventhandlers();		
} //end init

function initeventhandlers ()
{
	if (!optionTest) return;

	document.forms[0].country.onclick = countryselected;
	document.forms[0].country.onchange  = countryselected;
	document.forms[0].area.onclick = areaselected;
	document.forms[0].area.onchange  = areaselected;
	
	resetselects ();
} // end initeventhandlers

function resetselects ()
{
	if (!optionTest) return;

	var queryvars = document.URL.split("?");
	if (!queryvars[1]) return;
	queryvars = queryvars[1].split("&");
	
	if ((qvc = queryvars[0].substr(8))!=0)
		{for (i = 0; i < document.forms[0].country.length; ++i) if (document.forms[0].country.options[i].value == qvc){ document.forms[0].country.selectedIndex = i;countryselected();break;}}
	if ((qva = queryvars[1].substr(5))!=0)
		{for (i = 0; i < document.forms[0].area.length; ++i) if (document.forms[0].area.options[i].value == qva){document.forms[0].area.selectedIndex = i;areaselected();break;}}
	if ((qvh = queryvars[2].substr(8))!=100778)
		{for (i = 0; i < document.forms[0].housing.length; ++i) if (document.forms[0].housing.options[i].value == qvh){document.forms[0].housing.selectedIndex = i;break;}}
	if ((qvp = queryvars[3].substr(6))!="") {document.getElementById("price").value = qvp;}	
	if ((qvd = queryvars[4].substr(5))!="") {document.getElementById("date").value = qvd;}
	if ((qvt = queryvars[5].substr(10))!=14)
		{for (i = 0; i < document.forms[0].tolerance.length; ++i) if (document.forms[0].tolerance.options[i].value == qvt){document.forms[0].tolerance.selectedIndex = i;break;}}
	
} // end initeventhandlers

function countryselected()
{
	if (!optionTest) return;
	if (document.forms[0].country.selectedIndex < 0) return;
		
	var countryindex = document.forms[0].country.value;

	document.forms[0].area.options.length = 0;
	for(i=0;i<areas[countryindex].length;i+=2)
	{
		if (!(areas[countryindex][i] == 7)) document.forms[0].area.options[i/2] = new Option(areas[countryindex][i],areas[countryindex][i+1]);
	}

	document.forms[0].housing.options.length = 0;//clear old housings
	for(i=0;i<housings[countryindex].length;i+=2)	//refill with all housingtypes in this country
	{
		if (!(housings[countryindex][i] == 230878)) document.forms[0].housing.options[i/2] = new Option(housings[countryindex][i],housings[countryindex][i+1]);
	}

} // end countryselected

function areaselected()
{
	if (!optionTest) return;
	if (document.forms[0].country.selectedIndex < 0) return;
	if (document.forms[0].area.selectedIndex < 0) return;
	
	var housinglist = housings[document.forms[0].area.value];

	document.forms[0].housing.options.length = 0;
	for(i=0;i<housinglist.length;i+=2)
	{
		if (!(housinglist[i] == 230878)) document.forms[0].housing.options[i/2] = new Option(housinglist[i],housinglist[i+1]);
	}

} //end areaselected

