
var optionsloaded_h = false;
var option_ids_h;
var option_values_h;
var option_texts_h;

var oSelect1h;
var oSelect2h;

function Select1h_onload() // could be tied to page load
{
	// do once only
	if (!optionsloaded_h)
	{
		assignValsToArray_h();
			
		
		// if no select1 item is selected, select the first item
		// call Select1h_onchange()
		
		if ( optionsloaded_h || (oSelect1h.selectedIndex != 0) )
		{
			Select1h_onchange( true );
		} else
			loadAllDistricts_h();
			
		// set optionsloaded_h = true
		optionsloaded_h = true;
	}
}

function Select1h_onchange( loadedFromBody )
{

	// load appropriate options into oSelect2h
	// get the oSelected group
	var groupname = oSelect1h.options[oSelect1h.selectedIndex].id;	
	
	if (groupname != null)
	{
		// add items to the list
		if (oSelect1h.selectedIndex == 0)
		{
			loadAllDistricts_h();		
		}
		else
		{
			loadDistForRegion_h( groupname, loadedFromBody );
		}
	}
}


function assignValsToArray_h() {
	// assign brower compat
	oSelect1h = document.getElementById("Select1h");
	oSelect2h = document.getElementById("Select2h");
	
	// copy all the options to an array for future use
	option_ids_h = new Array();
	option_values_h = new Array();
	option_texts_h = new Array();
	
	for (i = 0; i < oSelect2h.length; i++)
	{
		option_ids_h[i] = oSelect2h.options[i].id;
		option_values_h[i] = oSelect2h.options[i].value;
		option_texts_h[i] = oSelect2h.options[i].text;
	}
}

function loadAllDistricts_h() {
	var oSelect2hSelected = oSelect2h.selectedIndex;
	var selectedVal = oSelect2h.options[oSelect2hSelected].value;
	
	clearItems_h();
	//var previousElement = "";
	
	// add all items to the second select excluding All (first item).
	var counter = 1;
	for (i = 1; i < option_ids_h.length; i++)
	{
		// create a new option element
		var oOption = document.createElement("option");
		// assign original values to new element
		oOption.id = option_ids_h[i];
		oOption.text = option_texts_h[i];
		oOption.value = option_values_h[i];
		// add new element to select2 collection
		var isNotInList = true;
		 
		for( x=0; x<oSelect2h.length; x++ ) {
			if( oOption.text == oSelect2h[x].text ) {
				isNotInList = false;
				break;
			}
		}
		
		if( isNotInList ) {	
			oSelect2h[counter++] = oOption;
			//previousElement = oOption.text;
			
			if( oOption.value == selectedVal )	//set the selected index
				oSelect2h.selectedIndex = counter - 1;
		}
	}
	
	
}


function loadDistForRegion_h( groupname, loadedFromBody ) {
//var oSelect2hSelected = oSelect2h.selectedItem.value;
	var oSelect2hSelected = oSelect2h.selectedIndex;
	var selectedVal = oSelect2h.options[oSelect2hSelected].value;
	
	clearItems_h();
	var counter = 1;
	var selectedInd = 0;
	for( i = 0; i < option_ids_h.length; i++ )
	{
		// add items where first groupid part of ID (groupid_optionid) == groupname		
		// get the group name
	
		if ( option_ids_h[i].substr(0,option_ids_h[i].indexOf("_")) == groupname )
		{
			// create a new option element
			var oOption = document.createElement("option");
			// assign original values to new element
			
			oOption.id = option_ids_h[i];
			oOption.text = option_texts_h[i];
			oOption.value = option_values_h[i];
			
			// add new element to select2 collection
			oSelect2h[counter++] = oOption;
			
			if( oOption.value == selectedVal )	//set the selected index
				oSelect2h.selectedIndex = counter - 1;
		}
		
	}
}

function clearItems_h() {
	// clear the current item list
	for (i = oSelect2h.length - 1; i >= 0; i--)
	{
		if( oSelect2h.options[i].value != "-9999" ) {
			oSelect2h.remove(i);
		}
	}
}

