var filterNow=false;

function changeClass(element,className)
	{
	document.getElementById(element).className=className;
	}

function addToList(field,place)
	{
	if (document.getElementById(field).value=='')
		{
		document.getElementById(field).value='-';
		}
	document.getElementById(field).value=document.getElementById(field).value+place+'-';
	}

function removeFromList(field,place)
	{
	document.getElementById(field).value=document.getElementById(field).value.substring(0,document.getElementById(field).value.indexOf('-'+place+'-'))+document.getElementById(field).value.substring(document.getElementById(field).value.indexOf('-'+place+'-')+place.length+1,document.getElementById(field).value.length);
	}

/* 
actions are:
	0=off
	1=on
	2=invert (passed from district links)
	3=area off (passed from R() to A())
*/	
function D(district,area,region,action)
	{
	if (action==1 || (action==2 && document.getElementById('d').value.indexOf('-'+district+'-')==-1))
		{
		if (document.getElementById('d').value.indexOf('-'+district+'-')==-1)
			{
			addToList('d',district);
			}
		changeClass('d'+district,'on');
		}
	else if (action==0 || (action==2 && document.getElementById('d').value.indexOf('-'+district+'-')!=-1))
		{
		removeFromList('d',district);
		changeClass('d'+district,'');
		if (action==2)
			{ 
			if (document.getElementById('r').value.indexOf('-'+region+'-')!=-1)
				{
				R(region,0);
				}
			if (document.getElementById('a').value.indexOf('-'+area+'-')!=-1)
				{
				A(area,region,0);
				}
			}
		}
	if (!filterNow)
		{
		changeClass('filter','sub');
		changeClass('search','sub');
		filterNow=true;
		}
	}

function A(area,region,action)
	{
	if (action==1 || (action==2 && document.getElementById('a').value.indexOf('-'+area+'-')==-1))
		{
		if (document.getElementById('a').value.indexOf('-'+area+'-')==-1)
			{
			addToList('a',area);
			}
		changeClass('a'+area,'on');
		for(districtLoop=0; districtLoop<eval('districts_in_'+[area]).length; districtLoop++)
			{
			D(eval('districts_in_'+[area])[districtLoop],area,region,1);
			}
		/* if this area is 'expand subselected' change it to 'expand' and change the [+] image */
		if (eval('districts_in_'+[area]).length > 0)
			{
			if (document.getElementById('expand'+area+'a').className=='expand subselected')
				{
				document.getElementById('expand'+area+'a').className='expand';
				document.getElementById('plus'+area+'a').src='/img/open2.gif';
				}
			}
		}
	else if (action==0 || action==3 || (action==2 && document.getElementById('a').value.indexOf('-'+area+'-')!=-1))
		{
		removeFromList('a',area);
		changeClass('a'+area,'');
		if (action!=0 || action==3)
			{
			for(districtLoop=0; districtLoop<eval('districts_in_'+[area]).length; districtLoop++)
				{
				D(eval('districts_in_'+[area])[districtLoop],area,region,0);
				}
			}
		if (action==2)
			{ 
			if (document.getElementById('r').value.indexOf('-'+region+'-')!=-1)
				{
				R(region,0);
				}
			}
		if ((document.getElementById('expand'+area+'a')) && (document.getElementById('expand'+area+'a').className=='expand subselected'))
			{
			document.getElementById('expand'+area+'a').className='expand';
			}
		}
	if (!filterNow)
		{
		changeClass('filter','sub');
		changeClass('search','sub');
		filterNow=true;
		}
	}

function R(region,action)
	{
	if (action==2 && document.getElementById('r').value.indexOf('-'+region+'-')==-1)
		{
		if (document.getElementById('r').value.indexOf('-'+region+'-')==-1)
			{
			addToList('r',region);
			}
		changeClass('r'+region,'on');
		for(areaLoop=0; areaLoop<eval('areas_in_'+[region]).length; areaLoop++)
			{
			A(eval('areas_in_'+[region])[areaLoop],region,1);
			}
		/* expand regions when they are selected - but not if already open */
		if (document.getElementById('ul'+region+'r').style.display=='none')
			{
			expand(region,'');
			}
		}
	else if (action==0 || (action==2 && document.getElementById('r').value.indexOf('-'+region+'-')!=-1))
		{
		removeFromList('r',region);
		changeClass('r'+region,'');
		if (action==2)
			{
			for(areaLoop=0; areaLoop<eval('areas_in_'+[region]).length; areaLoop++)
				{
				A(eval('areas_in_'+[region])[areaLoop],region,3);
				}
			}
		if (document.getElementById('expand'+region+'r').className=='expand subselected')
			{
			document.getElementById('expand'+region+'r').className='expand';
			}
		}
	}

function expand(region,area)
	{
	if (area!='')
		{
		if (document.getElementById('ul'+area+'a').style.display!='none')
			{
			document.getElementById('ul'+area+'a').style.display='none';
			document.getElementById('plus'+area+'a').src='/img/open2.gif';
			document.getElementById('plus'+area+'a').alt='expand';
			document.getElementById('expand'+area+'a').title='expand';
			/* it is minimised, so should it have the 'sub' class applied? 
				step 1
					get array of all Districts in this Area
				step 2
					loop through this array checking if any of them exist in the district field (id=d)
				step 3
					if they don't, and the 'sub' class is already applied, remove it 
					if they do, and the 'sub' class is not already applied, apply it now
			*/
			var foundOne=false;
			if (document.getElementById('a').value.indexOf('-'+area+'-')!=-1)
				{
				foundOne=true;
				}
			else
				{
				for(districtLoop=0; districtLoop<eval('districts_in_'+[area]).length; districtLoop++)
					{
					if (document.getElementById('d').value.indexOf('-'+eval('districts_in_'+[area])[districtLoop]+'-')!=-1)
						{
						foundOne=true;
						break;
						}
					}
				}
			if ((foundOne) && (document.getElementById('expand'+area+'a').className=='expand') && (document.getElementById('r'+region).className!='on') && (document.getElementById('a'+area).className!='on'))
				{
				document.getElementById('expand'+area+'a').className='expand subselected';
				document.getElementById('plus'+area+'a').src='/img/open2_sub.gif';
				}
			else if ((!foundOne) && (document.getElementById('expand'+area+'a').className=='expand subselected'))
				{
				document.getElementById('expand'+area+'a').className='expand';
				}
			}
		else
			{
			document.getElementById('ul'+area+'a').style.display='block';
			document.getElementById('plus'+area+'a').src='/img/close2.gif';
			document.getElementById('plus'+area+'a').alt='collapse';
			document.getElementById('expand'+area+'a').title='collapse';
			/* it is open, so remove the the 'sub' class if it is currently applied */
			if (document.getElementById('expand'+area+'a').className=='expand subselected')
				{
				document.getElementById('expand'+area+'a').className='expand';
				}
			}
		}
	else
		{
		if (document.getElementById('ul'+region+'r').style.display!='none')
			{
			document.getElementById('ul'+region+'r').style.display='none';
			document.getElementById('plus'+region+'r').src='/img/open.gif';
			document.getElementById('plus'+region+'r').alt='expand';
			document.getElementById('expand'+region+'r').title='expand';
			/* it is minimised, so should it have the 'sub' class applied? 
				step 1
					get array of all Districts in this Area
				step 2
					loop through this array checking if any of them exist in the district field (id=d)
					(step 2b - also loop through all districts within each area)
				step 3
					if they don't, and the 'sub' class is already applied, remove it 
					if they do, and the 'sub' class is not already applied, apply it now
			*/

			var foundOne=false;
			if (document.getElementById('r').value.indexOf('-'+region+'-')!=-1)
				{
				foundOne=true;
				}
			else
				{
				for(areaLoop=0; areaLoop<eval('areas_in_'+[region]).length; areaLoop++)
					{
					if (document.getElementById('a').value.indexOf('-'+eval('areas_in_'+[region])[areaLoop]+'-')!=-1)
						{
						foundOne=true;
						break;
						}
					for(districtLoop=0; districtLoop<eval('districts_in_'+[eval('areas_in_'+[region])[areaLoop]]).length; districtLoop++)
						{
						if (document.getElementById('d').value.indexOf('-'+eval('districts_in_'+[eval('areas_in_'+[region])[areaLoop]])[districtLoop]+'-')!=-1)
							{
							foundOne=true;
							break;
							}
						}
					if (foundOne)
						{
						break;
						}
					}
				}
			if ((foundOne) && (document.getElementById('expand'+region+'r').className=='expand') && (document.getElementById('r'+region).className!='on'))
				{
				document.getElementById('expand'+region+'r').className='expand subselected';
				document.getElementById('plus'+region+'r').src='/img/open_sub.gif';
				}
			else if ((!foundOne) && (document.getElementById('expand'+region+'r').className=='expand subselected'))
				{
				document.getElementById('expand'+region+'r').className='expand';
				}
			}
		else
			{
			document.getElementById('ul'+region+'r').style.display='block';
			document.getElementById('plus'+region+'r').src='/img/close.gif';
			document.getElementById('plus'+region+'r').alt='collapse';
			document.getElementById('expand'+region+'r').title='collapse';
			/* it is open, so remove the the 'sub' class if it is currently applied */
			if (document.getElementById('expand'+region+'r').className=='expand subselected')
				{
				document.getElementById('expand'+region+'r').className='expand';
				}
			}
		}
	}

function tidyDelims(element)
	{
	if (document.getElementById(element).value.charAt(0)=='-')
		{
		document.getElementById(element).value=document.getElementById(element).value.substring(1,document.getElementById(element).value.length);
		}
	if (document.getElementById(element).value.charAt(document.getElementById(element).value.length-1)=='-')
		{
		document.getElementById(element).value=document.getElementById(element).value.substring(0,document.getElementById(element).value.length-1);
		}
	}
	
function tidyUp()
	{
	/* 	remove sub-areas and sub-districts from any selected regions */
	for(regionLoop=0; regionLoop<document.getElementById('r').value.split('-').length; regionLoop++)
		{
		if (document.getElementById('r').value.split('-')[regionLoop].length)
			{
			for(areaLoop=0; areaLoop<eval('areas_in_'+document.getElementById('r').value.split('-')[regionLoop]).length; areaLoop++)
				{
				if (document.getElementById('a').value.indexOf('-'+eval('areas_in_'+document.getElementById('r').value.split('-')[regionLoop])[areaLoop]+'-')!=-1)
					{
					removeFromList('a',eval('areas_in_'+document.getElementById('r').value.split('-')[regionLoop])[areaLoop])
					}
				for(districtLoop=0; districtLoop<eval('districts_in_'+[eval('areas_in_'+document.getElementById('r').value.split('-')[regionLoop])[areaLoop]]).length; districtLoop++)
					{
					if (document.getElementById('d').value.indexOf('-'+eval('districts_in_'+[eval('areas_in_'+document.getElementById('r').value.split('-')[regionLoop])[areaLoop]])[districtLoop]+'-')!=-1)
						{
						removeFromList('d',eval('districts_in_'+[eval('areas_in_'+document.getElementById('r').value.split('-')[regionLoop])[areaLoop]])[districtLoop]);
						}
					}
				}
			}
		}
	/* 	remove sub-districts from any selected areas */
	for(areaLoop=0; areaLoop<document.getElementById('a').value.split('-').length; areaLoop++)
		{
		if (document.getElementById('a').value.split('-')[areaLoop].length)
			{
			for(districtLoop=0; districtLoop<eval('districts_in_'+document.getElementById('a').value.split('-')[areaLoop]).length; districtLoop++)
				{
				if (document.getElementById('d').value.indexOf('-'+eval('districts_in_'+document.getElementById('a').value.split('-')[areaLoop])[districtLoop]+'-')!=-1)
					{
					removeFromList('d',eval('districts_in_'+document.getElementById('a').value.split('-')[areaLoop])[districtLoop])
					}
				}
			}
		}		
	tidyDelims('r');
	tidyDelims('a');
	tidyDelims('d');
	/* remove non-numeric charcters from price fields */
	document.getElementById('minPrice').value=document.getElementById('minPrice').value.replace(/[^0-9.]/g,'');
	document.getElementById('maxPrice').value=document.getElementById('maxPrice').value.replace(/[^0-9.]/g,'');
	}