//
// Functions for the focus search
//
var focus_divs = new Array;

function focus_onLoad()
{
	// Find all the hide/show layers associated with this search
	// and store them in a global variable
	var divs = getElementsByClassName('ServFocusSearchLayer', 'div');
	for (var i = 0; i < divs.length; i++) {
		focus_divs[i] = divs[i].id;
	}

	// Load the initial select
	focus_loadOptions('ServFocus1', focus_loader());

	document.getElementById('ServFocusGender').selectedIndex = 0;
}

function focus_onChange(ctl, nextCtlId)
{
	var e = ctl.options[ctl.selectedIndex];

	// hide all the hide/show layers after this one
	for (var i = 0; i < focus_divs.length; i++) {
		if (focus_divs[i] == ctl.id + '_Layer') {
			while (++i < focus_divs.length) {
				hideShowLayer(focus_divs[i], 'HIDE');
			}
		}
	}
	// Need to reset the gender 'cos if it's not visible we don't want
	// anything, including the first option, sent through.
	_focus_resetGender();

	if (e.sub) {
		if (e.sub == 'gender') {
			if (e.value) {
				hideShowLayer('ServGender', 'SHOW');
				_focus_setGender();
				// hideShowLayer('ServFocusLanguage', 'SHOW');
			}
		}
		else if (nextCtlId) {
			focus_loadOptions(nextCtlId, e.sub);
			if (e.value) {
				hideShowLayer(nextCtlId + '_Layer', 'SHOW');
			}
		}
	}
}

function focus_onSubmit(form)
{
	var e = document.getElementById('ServFocus1');
	if (e.value == '' || e.value == null) {
		alert("Please select a Service Area");
		return false;
	}
	if (form.rn.value == 'Enter Suburb or Postcode')
	{
		alert("Please enter a Suburb or Postcode");
		form.rn.focus();
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////
// Load a select box with the appropriate options
// and set the label on the box
//
function focus_loadOptions(id, sub)
{
	var e = document.getElementById(id);

	// Load the select options
	e.options.length = 0;
	for (var i = 0; i < sub.options.length; i++) {
		e.options[i] = sub.options[i];
	}
	e.selectedIndex = 0;

	// Set the title
	if (sub.title) {
		var label = document.getElementById(id + '_Label');
		if (label) {
			label.innerHTML = sub.title + ':';
		}
	}
}

//////////////////////////////////////////////////////////////////////
// Helper routine, used from the generated javascript
//
function _focus_setGender()
{
	var e = document.getElementById('ServFocusGender');
	e.options.length = 0;
	e.options[0] = new Option('Either Gender', 'x');
	e.options[1] = new Option(       'Female', 'f');
	e.options[2] = new Option(         'Male', 'm');
}

function _focus_resetGender()
{
	var e = document.getElementById('ServFocusGender');
	e.options.length = 0;
}

//////////////////////////////////////////////////////////////////////
// Helper routine, used from the generated javascript
//
function focus_option(sub, text)
{
	var val = focus_option.arguments.length > 2 ? focus_option.arguments[2] : text;
	var o = new Option(text, val);
	if (sub) {
		o.sub = sub;
	}
	return o;
}
