//
// SUBURB REGION LOOKUP
// ============================================================================
// Gets the suburbs regions from a JSON object
// ----------------------------------------------------------------------------
// Requires:	Prototype Library
// Author:		Josiah Truasheim
// Copyright:	NetOptions Pty Ltd
//

// Pointer function, activates SuburbRegion functions
function update_suburbs_by_state(State){
	update_suburbs(State);
}
function update_suburbs_by_region(State,Region){
	update_suburbs(State,Region);
}
function update_suburbs_by_sub_region(State,Region,SubRegion){
	update_suburbs(State,Region,SubRegion);
}
function clear_suburbs()
{
}
function update_suburbs(State, Region, SubRegion)
{
	var URL = '?Action=option_suburbs';
	//var URL = '?Action=json_suburbs';
	if(typeof(State) != "undefined")
	{
		URL += "&State="+escape(State);
		$('State').value = State;
	}
	if(typeof(Region) != "undefined")
	{
		URL += "&Region="+escape(Region);
	}
	if(typeof(SubRegion) != "undefined")
	{
		URL += "&SubRegion="+escape(SubRegion);
	}
	new Ajax.Updater($('SuburbsDiv'), URL, {asynchronous:true, evalScripts:true, onSuccess:load_options});

	// Hide the main form area as well
		HideHomeFormFields(State);
	// Setup the AJAX request for JSON
//	new Ajax.Request( URL, {
//			method: 'get',
//			onSuccess: json_load_options 
//	});
}

function HideHomeFormFields(State)
{
	var flashContent = document.getElementById("flash_map_search_form_content");
	var standardContent = document.getElementById("standard_search_form_content");
//	if(ele.style.display == "block") {
	if(typeof(State) != "undefined")
	{
		flashContent.style.display = "block";
		standardContent.style.display = "none";
	}
	else
	{
		flashContent.style.display = "none";
		standardContent.style.display = "block";
	}
//		text.innerHTML = "show";
//	}
//	else {
//		ele.style.display = "block";
//		text.innerHTML = "hide";
//	}

}

function load_options(Response)
{

}


function json_load_options(Response)
{
		eval(Response.responseText); // Parse JSON data

		// Reset Suburb Indexes
		SuburbIndex = new Object();
		var Options = '';
		while($('Suburbs').length)
		{
//				$('Suburbs').remove(0);
		}
		if( ! $('Suburbs').visible() )
		{
				$('Suburbs').show();
				$('Suburbs').parentNode.removeChild($('Suburbs').previousSibling);
		}
		for(i in suburbs)
		{
				var Option = document.createElement( 'option' );
				Option.appendChild( document.createTextNode( suburbs[i] ) );
				Option.setAttribute( 'value', suburbs[i] );
				$('Suburbs').appendChild(Option);

				// Index the suburbs for faster searching
				SuburbIndex[suburbs[i].toUpperCase().charAt(0)] = i;					
//				alert('Suburbs: '+suburbs[i].toUpperCase().charAt(0));
				alert(i);
				break;
		}
		
		if(Data.Complete != 'true')
		{
			update_suburbs(Data.State,Data.Region, Data.SubRegion, Data.Loaded);
		}
}
