
var _refineSearch = "Please refine your search criteria.";

var _changedElement = null;

//<![CDATA[

// displays the number of results in an absolute position div.
function DisplayResultsDiv(numberOfProperties)
{
	try
	{
		var resultsDiv = document.getElementById('results');
		
		if (resultsDiv != null)
		{
			var infoDiv = document.getElementById('information');
			var numberOfResults = document.getElementById('numberOfResults');

			numberOfResults.innerHTML = numberOfProperties;
			
			resultsDiv.style.display = "block";
			
			if (numberOfProperties == 0)
			{
				document.getElementById('ctl00_ctl00_Content_Content_SearchButton').disabled = true;
				infoDiv.innerHTML = _refineSearch;
				infoDiv.style.display = "block";
			}
			else
			{
				document.getElementById('ctl00_ctl00_Content_Content_SearchButton').disabled = false;
				infoDiv.style.display = "none";
			}
		}
	}
	catch (e)
	{
		alert("Error: " + e.message);
	}
}

function CountResults(elem)
{
    try
    {
	    var resultsDiv = document.getElementById('resultsDiv');

	    // Only count properties if we have the results div on the page.
	    if (resultsDiv != null)
	    {
	      PositionResults(elem);
		    resultsDiv.style.visibility = "visible";
		    LoadingStart();
		
		    _changedElement = elem;
		    AjaxMethods.NumberOfProperties(CountProperties_CallBack);
	    }
	}
	catch (e)
	{
		alert("Error: " + e.message);
	}
}

function PositionResults(elem)
{
    try
    {
	    var resultsDiv = document.getElementById('resultsDiv');
	    resultsDiv.style.top = calculateOffsetTop(elem) - 332 + "px";
	    //resultsDiv.style.left = calculateOffsetLeft(elem) - 185 + elem.offsetWidth + "px";
	    resultsDiv.style.left = 350 + "px";
	}
	catch (e)
	{
	}
}

function CountProperties_CallBack(response)
{
    try
    {
	    var numberOfProperties = response.value;
	
	    if (response.error)
	    {
		    alert(response.error);
	    }
	
	    LoadingStop();
	
	    DisplayResultsDiv(numberOfProperties);	
	}
	catch (e)
	{
		alert("Error: " + e.message);
	}
}

//]]>

function ChangePropertyType(elem)
{
	LoadingStart();

	_changedElement = elem;
	var propertyType = elem.options[elem.selectedIndex].value;
	
	AjaxMethods.SelectPropertyType(propertyType, Count_CallBack);	
}

function ChangeMinimumPrice(elem)
{
	LoadingStart();

	// Show the change next to maximum price!!
	_changedElement = document.getElementById('ctl00_ctl00_Content_Content_MaximumPrice');
	var val = elem.options[elem.selectedIndex].value;
		
	AjaxMethods.MinimumPrice(val, Count_CallBack);	
}

function ChangeMaximumPrice(elem)
{
	LoadingStart();

	_changedElement = elem;
	var val = elem.options[elem.selectedIndex].value;
	
	AjaxMethods.MaximumPrice(val, Count_CallBack);	
}

function ChangeMinimumBedrooms(elem)
{
	LoadingStart();

	// Show the change next to maximum bedrooms!!
	_changedElement = document.getElementById('ctl00_ctl00_Content_Content_MaximumBedrooms');
	var val = elem.options[elem.selectedIndex].value;
	
	AjaxMethods.MinimumBedrooms(val, Count_CallBack);	
}

function ChangeMaximumBedrooms(elem)
{
	LoadingStart();
	
	_changedElement = elem;
	var val = elem.options[elem.selectedIndex].value;
	
	AjaxMethods.MaximumBedrooms(val, Count_CallBack);	
}

function ChangeRecent(elem)
{
	LoadingStart();
	
	_changedElement = elem;
	var val = elem.options[elem.selectedIndex].value;
	
	AjaxMethods.CreationDate(val, Count_CallBack);	
}


function Count_CallBack(response)
{
	if (response.error)
	{
		alert(response.error);
	}
	CountResults(_changedElement);
}

// 

function LoadSearchPage()
{
	// setup the smart area tag.
	window.setTimeout("SetupSmartArea(document.getElementById('areaSearch'), document.getElementById('aspnetForm'));", 50);

    window.onresize = resizeHandler;

	// Get the number of results, and attach it to Area Search which is set as default value.
	window.setTimeout("CountResults(document.getElementById('areaSearch'));", 100);
	
	window.setTimeout("AjaxMethods.GetCachedAreas('ctl00_ctl00_Content_Content_Towns_t', 3, false, GetAreasForDropDown_CallBack);", 200);
}

// START Loading functions
loading = false;
function Loading(dots)
{
	var s = ".";
	switch (dots)
	{
		case 1:
			dots = 2;
			break;
		case 2:
			s = ". ."
			dots = 3;
			break;
		default:
			s = ". . ."
			dots = 1;
			break;
	}
	
	document.getElementById('ellipsis').innerHTML = s;
	
	if (loading == true)
	{
		window.setTimeout('Loading(' + dots + ');', 500);	
	}
}

function LoadingStart()
{
	if (loading == false && document.getElementById('results') != null)
	{
		loading = true;
		document.getElementById('results').style.display = "none";
		document.getElementById('information').style.display = "none";
		document.getElementById('loading').style.display = "block";
		Loading(3);	
	}
}


function LoadingStop()
{
	document.getElementById('loading').style.display = "none";
	document.getElementById('results').style.display = "block";
	loading = false;
}

// END Loading functions

function resizeHandler()
{
    try
    {
	    setCompleteDivSize();
	    PositionResults(_changedElement);
	}
	catch (e)
	{
	    alert("Error: " + e.message);
	}
}


