//-----------------------------------------------------------------------------
// JavaScript code used by the property search input page.
// (c) Copyright 2008 Warners
//-----------------------------------------------------------------------------


//
// Where are the images for the map located?
//
var _imagedir				= "images/search_map/";


//
// Declare global variables for each area of the map.  A value
// of true means that area is selected, false means it's unselected.
//
var _edinburgh_citycentre	= false;
var _edinburgh_north		= false;
var _edinburgh_north_west	= false;
var _edinburgh_west			= false;
var _edinburgh_south_west	= false;
var _edinburgh_south		= false;
var _edinburgh_east			= false;

var _borders				= false;
var _east_fife				= false;
var _west_fife				= false;
var _east_lothian			= false;
var _west_lothian			= false;
var _midlothian				= false;
var _central_scotland		= false;
var _perthshire				= false;
var _dundee					= false;



//-----------------------------------------------------------------------------
// NAME		: Initialise()
// PURPOSE	: Initialises the search form once the page has loaded.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function Initialise()
{
	LoadSearch();
	RedrawMap();
}



//-----------------------------------------------------------------------------
// NAME		: ResetForm()
// PURPOSE	: Resets the search criteria form to its default values.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function ResetForm()
{
	document.getElementById( "map_search" ).reset();
	ClearAreas();
}



//-----------------------------------------------------------------------------
// NAME		: ToggleArea()
// PURPOSE	: Selects or deselects the specified area of the map.
// INPUTS	: area	- The name of the area to toggle (north, north west, west, etc.)
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function ToggleArea( area )
{
	switch ( area )
	{
		case "north":
			_edinburgh_north = !_edinburgh_north;
			break;

		case "north west":
			_edinburgh_north_west = !_edinburgh_north_west;
			break;

		case "west":
			_edinburgh_west = !_edinburgh_west;
			break;

		case "south west":
			_edinburgh_south_west = !_edinburgh_south_west;
			break;

		case "south":
			_edinburgh_south = !_edinburgh_south;
			break;

		case "east":
			_edinburgh_east = !_edinburgh_east;
			break;

		case "city centre":
			_edinburgh_citycentre = !_edinburgh_citycentre;
			break;

		case "borders":
			_borders = !_borders;
			break;

		case "east fife":
			_east_fife = !_east_fife;
			break;

		case "west fife":
			_west_fife = !_west_fife;
			break;

		case "east lothian":
			_east_lothian = !_east_lothian;
			break;

		case "west lothian":
			_west_lothian = !_west_lothian;
			break;

		case "midlothian":
			_midlothian = !_midlothian;
			break;
		
		case "central scotland":
			_central_scotland = !_central_scotland;
			break;

		case "perthshire":
			_perthshire = !_perthshire;
			break;
		
		case "dundee":
			_dundee = !_dundee;
			break;
	}
	RedrawMap();
}



//-----------------------------------------------------------------------------
// NAME		: CheckboxHit()
// PURPOSE	: Used to select areas which are displayed as checkboxes rather
//			  than as graphics on the map.  We need to keep the area variables
//			  used by this script in-sync with the controls on the form.
// INPUTS	: name		- The name of the area.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function CheckboxHit( name )
{
	switch ( name )
	{
		case "borders":
			_borders = document.getElementById( "map_borders" ).checked;
			break;
		case "east fife":
			_east_fife = document.getElementById( "map_east_fife" ).checked;
			break;
		case "west fife":
			_west_fife = document.getElementById( "map_west_fife" ).checked;
			break;
		case "east lothian":
			_east_lothian = document.getElementById( "map_east_lothian" ).checked;
			break;
		case "west lothian":
			_west_lothian = document.getElementById( "map_west_lothian" ).checked;
			break;
		case "midlothian":
			_midlothian = document.getElementById( "map_midlothian" ).checked;
			break;
		case "central scotland":
			_central_scotland = document.getElementById( "map_central_scotland" ).checked;
		case "perthshire":
			_perthshire = document.getElementById( "map_perthshire" ).checked;
		case "dundee":
			_dundee = document.getElementById( "map_dundee" ).checked;
	}
}



//-----------------------------------------------------------------------------
// NAME		: RedrawMap()
// PURPOSE	: Updates the map.   The map is created by overlaying multiple
//			  images on top of one another.   This functon generates the HTML
//			  and then inserts it into the page.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function RedrawMap()
{
	var overlay = document.getElementsByName( "map_overlays" )[0];
	var html = "";
	
	if ( _edinburgh_citycentre )	{ html += "<img class='map' src='" + _imagedir + "city centre.gif' style='z-index:1;' usemap='#map' />"; }
	if ( _edinburgh_north )			{ html += "<img class='map' src='" + _imagedir + "edinburgh north.gif' style='z-index:2;' usemap='#map' />"; }
	if ( _edinburgh_north_west )	{ html += "<img class='map' src='" + _imagedir + "edinburgh north west.gif' style='z-index:3;' usemap='#map' />"; }
	if ( _edinburgh_west )			{ html += "<img class='map' src='" + _imagedir + "edinburgh west.gif' style='z-index:4;' usemap='#map' />"; }
	if ( _edinburgh_south_west )	{ html += "<img class='map' src='" + _imagedir + "edinburgh south west.gif' style='z-index:5;' usemap='#map' />"; }
	if ( _edinburgh_south )			{ html += "<img class='map' src='" + _imagedir + "edinburgh south.gif' style='z-index:6;' usemap='#map' />"; }
	if ( _edinburgh_east )			{ html += "<img class='map' src='" + _imagedir + "edinburgh east.gif' style='z-index:7;' usemap='#map' />"; }

	html += "<img class='map' src='" + _imagedir + "text overlay.gif' style='z-index:8;' usemap='#map' />";

	overlay.innerHTML = html;
	
	document.getElementById( "map_borders" ).checked = _borders;
	document.getElementById( "map_east_fife" ).checked = _east_fife;
	document.getElementById( "map_west_fife" ).checked = _west_fife;
	document.getElementById( "map_east_lothian" ).checked = _east_lothian;
	document.getElementById( "map_west_lothian" ).checked = _west_lothian;
	document.getElementById( "map_midlothian" ).checked = _midlothian;
	document.getElementById( "map_central_scotland" ).checked = _central_scotland;
	document.getElementById( "map_perthshire" ).checked = _perthshire;
	document.getElementById( "map_dundee" ).checked = _dundee;
}



//-----------------------------------------------------------------------------
// NAME		: ClearAreas()
// PURPOSE	: Deselects every area and redraws the map.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function ClearAreas()
{
	SetAllAreas( false );
	RedrawMap();
}



//-----------------------------------------------------------------------------
// NAME		: SelectAllAreas()
// PURPOSE	: Selects every area and redraws the map.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function SelectAllAreas()
{
	SetAllAreas( true );
	RedrawMap();
}



//-----------------------------------------------------------------------------
// NAME		: SetAllAreas()
// PURPOSE	: Either selects or deselects every area on the map.  The map
//			  will not be redrawn.
// INPUTS	: value		- True to select all areas, false to unselect them.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function SetAllAreas( value )
{
	_edinburgh_citycentre	= value;
	_edinburgh_north		= value;
	_edinburgh_north_west	= value;
	_edinburgh_west			= value;
	_edinburgh_south_west	= value;
	_edinburgh_south		= value;
	_edinburgh_east			= value;
	
	_borders				= value;
	_east_fife				= value;
	_west_fife				= value;
	_east_lothian			= value;
	_west_lothian			= value;
	_midlothian				= value;
	_central_scotland		= value;
	_perthshire				= value;
	_dundee					= value;
}



//-----------------------------------------------------------------------------
// NAME		: GetAreaCodes()
// PURPOSE	: Every area on the map has a unique number in our database.   This
//			  function looks at which areas have been selected on the list and
//			  builds a comma-delimited list of the database record ID numbers.
// INPUTS	: None.
// RETURNS	: A comma-delimited list of area IDs.
//-----------------------------------------------------------------------------
function GetAreaCodes()
{
	var area_ids = "";

	//
	// Assign the database IDs for each area.
	//
	if ( _edinburgh_citycentre )	{ area_ids = area_ids + " 2"; }
	if ( _edinburgh_north )			{ area_ids = area_ids + " 4"; }
	if ( _edinburgh_north_west )	{ area_ids = area_ids + " 5"; }	
	if ( _edinburgh_west )			{ area_ids = area_ids + " 8"; }
	if ( _edinburgh_south_west )	{ area_ids = area_ids + " 7"; }
	if ( _edinburgh_south )			{ area_ids = area_ids + " 6"; }
	if ( _edinburgh_east )			{ area_ids = area_ids + " 3"; }

	if ( _borders )					{ area_ids = area_ids + " 12"; }
	if ( _east_fife )				{ area_ids = area_ids + " 10"; }
	if ( _west_fife )				{ area_ids = area_ids + " 11"; }
	if ( _east_lothian )			{ area_ids = area_ids + " 1"; }
	if ( _west_lothian )			{ area_ids = area_ids + " 13"; }
	if ( _midlothian )				{ area_ids = area_ids + " 9"; }
	if ( _central_scotland )		{ area_ids = area_ids + " 14"; }
	if ( _perthshire )				{ area_ids = area_ids + " 15"; }
	if ( _dundee )					{ area_ids = area_ids + " 16"; }
	
	//
	// Chop off the first comma (if it's present).
	//
	if ( area_ids.length >= 2 )
	{
		area_ids = area_ids.substring( 1 );
	}
	
	return area_ids;
}



//-----------------------------------------------------------------------------
// NAME		: ValidateForm()
// PURPOSE	: Checks that the form is complete before submitting it.
// INPUTS	: None.
// RETURNS	: True if the form is complete, false if not.
//-----------------------------------------------------------------------------
function ValidateForm()
{
	var is_valid = true;
	var area_ids = document.getElementById( "area_ids" );
	var street = document.getElementById( "street" );

	if ( area_ids != null )
	{
		area_ids.value = GetAreaCodes();
		//
		// The following code is no longer required because if no area ID is
		// passed into the search page, it will automatically assume all areas.
		//
		//if ( area_ids.value == "" && street.value == "" )
		//{
		//		SelectAllAreas();
		//		area_ids.value = GetAreaCodes();
		//}
	}
	
	SaveSearch();
	return is_valid;
}




//-----------------------------------------------------------------------------
// NAME		: LoadSearch()
// PURPOSE	: Reads the contents of the cookie containing the users last search
//			  so the search criteria can be re-populated.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function LoadSearch()
{
	//
	// Load the map settings.
	// NOTE: The cookie will return the text "true" or "false", so I've used a comparison
	//		 in order to convert the string value into a boolean.
	//
	_edinburgh_citycentre	= ( readCookie( "map_edinburgh_citycentre", false ) == "true" );
	_edinburgh_north		= ( readCookie( "map_edinburgh_north", false ) == "true" );
	_edinburgh_north_west	= ( readCookie( "map_edinburgh_north_west", false ) == "true" );
	_edinburgh_west			= ( readCookie( "map_edinburgh_west", false ) == "true" );
	_edinburgh_south_west	= ( readCookie( "map_edinburgh_south_west", false ) == "true" );
	_edinburgh_south		= ( readCookie( "map_edinburgh_south", false ) == "true" );
	_edinburgh_east			= ( readCookie( "map_edinburgh_east", false ) == "true" );
	_borders				= ( readCookie( "map_borders", false ) == "true" );
	_east_fife				= ( readCookie( "map_east_fife", false ) == "true" );
	_west_fife				= ( readCookie( "map_west_fife", false ) == "true" );
	_east_lothian			= ( readCookie( "map_east_lothian", false ) == "true" );
	_west_lothian			= ( readCookie( "map_west_lothian", false ) == "true" );
	_midlothian				= ( readCookie( "map_midlothian", false ) == "true" );
	_central_scotland		= ( readCookie( "map_central_scotland", false ) == "true" );
	_perthshire				= ( readCookie( "map_perthshire", false ) == "true" );
	_dundee					= ( readCookie( "map_dundee", false ) == "true" );
	
	//
	// Load the other form criteria.
	//
	document.getElementById( "property_type" ).value = readCookie( "property_type", "" );
	document.getElementById( "price_min" ).value = readCookie( "price_min", 0 );
	document.getElementById( "price_max" ).value = readCookie( "price_max", 9999999 );
	document.getElementById( "min_bedrooms" ).value = readCookie( "min_bedrooms", 1 );
	document.getElementById( "has_garden" ).value = readCookie( "has_garden", "" );
	document.getElementById( "has_garage" ).value = readCookie( "has_garage", "" );
	document.getElementById( "street" ).value = readCookie( "street", "" );
}



//-----------------------------------------------------------------------------
// NAME		: SaveSearch()
// PURPOSE	: Saves the users search criteria to a cookie so it can be retrieved later.
// INPUTS	: None.
// RETURNS	: Nothing.
//-----------------------------------------------------------------------------
function SaveSearch()
{
	//
	// Save the map settings.
	//
	createCookie( "map_edinburgh_citycentre", _edinburgh_citycentre, "" );
	createCookie( "map_edinburgh_north", _edinburgh_north, "" );
	createCookie( "map_edinburgh_north_west", _edinburgh_north_west, "" );
	createCookie( "map_edinburgh_west", _edinburgh_west, "" );
	createCookie( "map_edinburgh_south_west", _edinburgh_south_west, "" );
	createCookie( "map_edinburgh_south", _edinburgh_south, "" );
	createCookie( "map_edinburgh_east", _edinburgh_east, "" );
	createCookie( "map_borders", _borders, "" );
	createCookie( "map_east_fife", _east_fife, "" );
	createCookie( "map_west_fife", _west_fife, "" );
	createCookie( "map_east_lothian", _east_lothian, "" );
	createCookie( "map_west_lothian", _west_lothian, "" );
	createCookie( "map_midlothian", _midlothian, "" );
	createCookie( "map_central_scotland", _central_scotland, "" );
	createCookie( "map_perthshire", _perthshire, "" );
	createCookie( "map_dundee", _dundee, "" );
	
	//
	// Save the other form criteria.
	//
	createCookie( "property_type", document.getElementById( "property_type" ).value, "" );
	createCookie( "price_min", document.getElementById( "price_min" ).value, "" );
	createCookie( "price_max", document.getElementById( "price_max" ).value, "" );
	createCookie( "min_bedrooms", document.getElementById( "min_bedrooms" ).value, "" );
	createCookie( "has_garden", document.getElementById( "has_garden" ).value, "" );
	createCookie( "has_garage", document.getElementById( "has_garage" ).value, "" );
	createCookie( "street", document.getElementById( "street" ).value, "" );
}