

var _areaDropDownCache = new Object(); 
var _changedDropDown = null;

// This function uses AJAX to get areas back from the database that match the
// search criteria.
function GetAreasForDropwDown(elem, childElem, parentArea)
{	
	ClearAreaGroup();
	ClearSmartArea();
	
	_changedDropDown = elem;

	if (parentArea.length > 0)
	{
		var getSubAreas = false;
		if (_getSubAreas[elem.id])
		{
			getSubAreas = _getSubAreas[elem.id];
		}
			
		EmptyDropDown(childElem);
		
		ResetChildrenDropDowns(childElem);
		
		// mark the drop down for loading
		var offset = childElem.options.length;
		childElem.options[offset] = new Option(_loadingAreaDropDown, "l");
		childElem.selectedIndex = offset;
		childElem.disabled = true;
			
		
		if (_areaDropDownCache[parentArea])
		{
			PopulateAreaDropDown(_areaDropDownCache[parentArea]);

			AjaxMethods.SelectAreaForDropDown(parentArea, _levels[childElem.id] - 1, SelectArea_CallBack);
		}
		else
		{
			AjaxMethods.GetAreasForDropDown(childElem.id, parentArea, _levels[childElem.id], GetSubAreas(childElem), GetAreasForDropDown_CallBack);
		}
		
	}
	else
	{
		// clear area for this level.
		AjaxMethods.SelectAreaForDropDown("", _levels[elem.id], SelectArea_CallBack);
		ResetChildrenDropDowns(elem);
	}
}

function GetSubAreas(elem)
{
 	var getSubAreas = false;
	if (_getSubAreas[elem.id])
	{
		getSubAreas = _getSubAreas[elem.id];
	}
	return getSubAreas;
}

// recursively resets children elements
function ResetChildrenDropDowns(elem)
{
	if (_childClientIds[elem.id] != '')
	{
		childElem = document.getElementById(_childClientIds[elem.id]);
		if (childElem)
		{
			EmptyDropDown(childElem);
			childElem.disabled = false;
			ResetChildrenDropDowns(childElem);
		}
	}
}

// loads a child drop down.
function LoadChildDropDown(elem, parentArea)
{
	if (parentArea.length > 0)
	{
		if (_childClientIds[elem.id] != '')
		{
			childElem = document.getElementById(_childClientIds[elem.id]);
			
			if (childElem)
			{
				AjaxMethods.GetAreasForDropDown(childElem.id, parentArea, _levels[childElem.id], GetSubAreas(childElem), GetAreasForDropDown_CallBack);
				childElem.disabled = false;
			}
		}
	}
}

// function does not return anything, but must count the areas.
function SelectArea_CallBack(response)
{
	CountResults(_changedDropDown);
}

function GetAreasForDropDown_CallBack(response)
{
	var areaCol = response.value;
	
	if (response.error)
	{
		alert(response.error);
	}
	
	// check to see if we have returned areas.
	if (areaCol != null)
	{
		cacheDropDownResults(areaCol.PI, areaCol);
		
		// if we get the results on loading the page, we have not actually changed it
		// hence check for null occurance.
		if (_changedDropDown != null)
		{
			CountResults(_changedDropDown);
		}
			
		PopulateAreaDropDown(areaCol);
	}
}

function PopulateAreaDropDown(areaCol)
{
	var elem = document.getElementById(areaCol.CI);
	
	EmptyDropDown(elem);
	
	var offset = elem.options.length;
	if (areaCol.Ars && areaCol.Ars.length > 0)
	{
		elem.disabled = false;
		var areas = areaCol.Ars;
		
		for(var i = 0; i < areas.length; i++)
		{
			elem.options[i + offset] = new Option(areas[i].N, areas[i].Id);
			
			// if area is selected (from cache)
			if (areas[i].S == true)
			{
				elem.options[i + offset].selected = true;
			}
		}
	}
	else
	{
		elem.options[offset] = new Option(_noFurtherAreas, "e");
		elem.selectedIndex = offset;
		elem.disabled = true;
	}
}


// Empties as long as the value is not blank (please select option)
function EmptyDropDown(elem)
{
	var val = '';
	// Delete them from original
	for (var i = (elem.options.length-1); i >= 0; i--)
	{
		val = elem.options[i].value;
		if (val.length > 0)
		{
			elem.options[i] = null;
		}
	}
	
	elem.selectedIndex = 0;
}

// Cache drop down results on the client.
function cacheDropDownResults(parentArea, areaCol)
{
	_areaDropDownCache[parentArea]= areaCol;
}


function SelectArea(elem)
{
	ClearSmartArea();
	ClearAreaGroup();
	
	_changedDropDown = elem;
	var level = _levels[elem.id];
	var areaID = elem.options[elem.selectedIndex].value;
	
	AjaxMethods.SelectAreaForDropDown(areaID, level, SelectArea_CallBack);	
}


function SelectAreaGroup(elem)
{
	ClearSmartArea();
	ClearAreaDropDowns();
	
	_changedDropDown = elem;
	
	AjaxMethods.SelectAreaGroup(elem.options[elem.selectedIndex].value, SelectArea_CallBack);	
}


function ClearAreaDropDowns()
{
	var dropDown = document.getElementById('ctl00_ctl00_Content_Content_Provinces_p');
	ClearDropDown(dropDown);
	ResetChildrenDropDowns(dropDown);
}

function ClearAreaGroup()
{
	var dropDown = document.getElementById('ctl00_ctl00_Content_Content_AreaGroup_ag');
	ClearDropDown(dropDown);
}

function ClearDropDown(dropDown)
{
    if (dropDown.selectedIndex != 0)
	{
	    // make sure the onchange of the drop down is not called
	    var onChangeOld = dropDown.onChange;
	    dropDown.onChange = '';
	    dropDown.selectedIndex = 0;
	    dropDown.onChange = onChangeOld;
	}
}

function ClearSmartArea()
{
	if (_inputField != null && _inputField.value != '')
	{
		_inputField.value = '';
	}
}
