// JavaScript For Buy - Property Search - Property Location
// JavaScript For Buy - Recent Sales Search - Property Location
// JavaScript For Rent - Rental Property Search - Property Location

// property location - select multiple suburbs from the drop down menu... all values will be displayed at the input field

function favSubLocation () 
{
	if (!document.getElementById) return false;
	
	var input = document.getElementById('locationList'); // for input field
	
	var select = document.getElementById('locationDropList'); // for drop down menu
	
	input.focus();
	
	var suburb = select.options[select.selectedIndex].text;
	
	// when user selected the "show all suburbs" -- keep looping
	if (select.selectedIndex == 0) 
	{
		input.value = '';
		return true;
	}
	
	// split up a string from the user's multiple suburb selections
	var textSuburbs = input.value.split(/\s*,\s*/);
	
	if (!textSuburbs) return false;
	
	// pattern matches in strings
	var pattern = new RegExp('\s*' + suburb + '\s*$');
	
	for (var i = 0; i < textSuburbs.length; i++)
	{
		if (pattern.exec(textSuburbs[i])) return false;
	}
	
	// adding a new suburb and separated by a comma
	var newvalue = input.value.replace(/(^|,)([^,]*)$/, "$1 " + suburb + ', ');
	
	input.value = newvalue;
	
	return false;
}
