var regionSelector; // region listbox
var regionsSelected; // hidden field - stores selected regions
var stateSelector; // state listbox
var stateSelectedCount = 0;
var selectedStates; // array of selected states

function stateChanged(stateSelector_cid, regionSelector_cid){
	regionSelector = document.getElementById(regionSelector_cid);
	stateSelector = document.getElementById(stateSelector_cid);			
	selectedStates = new Array();
	
	//reset the regions selected.
	regionSelector.options.length = 0;		
	
	if (stateSelector.selectedIndex >= 0){
		//an item has been selected
		for (i=0; i < stateSelector.options.length; i++){
			if (stateSelector.options[i].selected)	{
				selectedStates.push(stateSelector.options[i].value);
			}
		}		
		
		//stateData	is an array builtup w/in packet_search.ascx.vb
		addRegions(stateData);
		
		dividerOption = new Option('- - - - - - - - - - - - - - - - - - - - - - - - - - - -','_');	
		regionSelector.options[regionSelector.options.length] = dividerOption;
		
		//regions is an array builtup w/in packet_search.ascx.vb
		addRegions(regions);			
	}
	else {
		alert('You must select at least one item from the State list');
		stateSelector.selectedIndex = 0;
		stateChanged(stateSelector_cid, regionSelector_cid);
	}
}

/*
	adds regions to the regionSelector
	called by stateChanged
 */
function addRegions(data){
	for (i=0; i < selectedStates.length; i++){	
		for(j = 0; j < data.length; j++){
			// for each item in the region array			
			if(data[j][0] == selectedStates[i])	{
				// data relates to selectedState
				// create option and add it to the regionSelector listBox				
				option = new Option(data[j][2],data[j][1]);
				
				regionSelector.options[regionSelector.options.length] = option;
				
				//regionSelector.options.add(option, regionSelector.options.length+1);
				
				if (data[j][2].indexOf("Regions") > 0 || data[j][2].indexOf("Data") > 0){
					option.selected = true;
				}								
			}
		}
	}			
}

function saveRegions(regionParentsSelected_cid, regionsSelected_cid, endDate_cid, startDate_cid, regions_cid, topics_cid, types_cid){		

	regionParentsSelected = document.getElementById(regionParentsSelected_cid);
	regionParentsSelected.value = "";
	
	regionsSelected = document.getElementById(regionsSelected_cid);
	regionsSelected.value = ",";
			
	if (endDate_cid!==''){
		endDate = document.getElementById(endDate_cid);
		endDate.disabled = false;
	}
	
	if (startDate_cid !== ''){
		startDate = document.getElementById(startDate_cid);
		startDate.disabled = false;		
	}	
	
	if (regions_cid !== ''){
		regions = document.getElementById(regions_cid);
		regions.disabled = false;		
	}
	
	if (topics_cid !== ''){
		topics = document.getElementById(topics_cid);
		if (topics.options[0].selected){
			for (i=1; i < topics.length; i++){
				topics.options[i].selected = false;
			
			}
		
		}
		
	}
	
	if (types_cid !== ''){
		types = document.getElementById(types_cid);
		if (types.options[0].selected){
			for(i=1; i< types.length; i++){
				types.options[i].selected = false;
			}		
		}
	}
			
	for (i=0; i < regionSelector.options.length; i++){
		if ((regionSelector.options[i].selected) && (regionSelector.options[i].value != '_') && (regionsSelected.value.indexOf("," + regionSelector.options[i].value + ",") < 0 )){
			regionsSelected.value += regionSelector.options[i].value + ",";	
		}			
	}
	
	if (regionsSelected.value.charAt(regionsSelected.value.length-1)== ",")	{
		regionsSelected.value = regionsSelected.value.substring(1,regionsSelected.value.length-1);
	}	
	

}

/*************
RE - SELECT LISTS
*****************/

function reSelectItems(listBox_cid, items){
	listBox = document.getElementById(listBox_cid);	
	
	var itemsList = items.split(",");
	var itemsSelected = 0;
		
	for (i=0; i < listBox.options.length; i++){				
		listBox.options[i].selected = false;
		
		for (j=0; j < itemsList.length; j++){			
			if(itemsList[j]==listBox.options[i].value){			
				listBox.options[i].selected = true;
				itemsSelected++;
			}
		}
	}
}

function reSelectRegions(listBox_cid, stateListBox_cid, items){
	//(eg) items = '22,21,41,16,134,61,51,51' 
	//at this point, the regions listbox has been populated with regions  (options) re the state selection
	//eg: if qld > gold coast has been selected, then all qld regions will be loaded in.
	
	//2 default state options are added to the regions listbox.
	//	- the 'state data' and 'all region data' options have been selected by default.	
	var STATES_COUNT = 8;
	var STATES_SELECTED_COUNT = 0;
	
	// vars used to check if all regions of a state have been selected
	var AUS_SELECTED_COUNT = 0;
	var ACT_SELECTED_COUNT = 0;
	var NSW_SELECTED_COUNT = 0;
	var NT_SELECTED_COUNT = 0;
	var QLD_SELECTED_COUNT = 0;
	var SA_SELECTED_COUNT = 0;	
	var TAS_SELECTED_COUNT = 0;	
	var VIC_SELECTED_COUNT = 0;
	var WA_SELECTED_COUNT = 0;
	
	// the listboxes
	listBox = document.getElementById(listBox_cid);	
	stateListBox = document.getElementById(stateListBox_cid);
	
	// array of selected items
	itemArray = items.split(",");

	// string which will contain selected 'state data' id's
	stateDataList ="";
	
	//increment state count vars
	for (j=0; j < itemArray.length;j++){		
		for (i=0; i < listBox.length; i++){								
			if (itemArray[j] > 9){
				if(itemArray[j] == listBox.options[i].value){
					switch(listBox.options[i].text.substring(0, 3)){
						case "ACT": ACT_SELECTED_COUNT++; 	break;
						case "NSW": NSW_SELECTED_COUNT++;	break;
						case "NT ":	NT_SELECTED_COUNT++;	break;
						case "QLD":	QLD_SELECTED_COUNT++;	break;
						case "SA ": SA_SELECTED_COUNT++;	break;
						case "TAS": TAS_SELECTED_COUNT++;	break;
						case "VIC":	VIC_SELECTED_COUNT++;	break;
						case "WA ": WA_SELECTED_COUNT++;	break;					
					} // switch					
				break;
				}			
			} else {
				// item is a state data option
				if (itemArray[j] > 0){
					STATES_SELECTED_COUNT++;
				}
				
				stateDataList += itemArray[j] + ",";			
				break;
			}
		} // for
	} // for
	
	if ((STATES_SELECTED_COUNT + ACT_SELECTED_COUNT + NSW_SELECTED_COUNT + NT_SELECTED_COUNT + QLD_SELECTED_COUNT +  SA_SELECTED_COUNT +  TAS_SELECTED_COUNT + VIC_SELECTED_COUNT + WA_SELECTED_COUNT) == AUS_REGION_COUNT){
		// All regions
		for (i=0; i < stateListBox.length; i++){			
			//if (stateDataList.indexOf(stateListBox.options[i].value) >= 0){
			//	stateListBox.options[i].selected = true;
			//} else {
				stateListBox.options[i].selected = false;
			//}			
		}
		
		stateListBox.options[0].selected = true;					
		stateChanged(stateListBox_cid, listBox_cid);

		// for each 'State Data' option 
		for (i=0; i < selectedStates.length*2+1; i++){
			if (listBox.options[i].value.length > 1){
				listBox.options[i].selected = false;
			} else {
				if (stateDataList.indexOf(listBox.options[i].value) < 0){
					listBox.options[i].selected = false;
				}else{
					listBox.options[i].selected = true;
				}				
			}
		}
		
		// force selection of 'Australia - All Regions' option 		
		listBox.options[1].selected = true;		
		
	}else{
			
		if (ACT_REGION_COUNT == ACT_SELECTED_COUNT){ selectAllStateRegions(listBox, "ACT");	}
		else{ selectIndividualRegions(listBox, items, itemArray, "ACT");   }
				
		if (NSW_REGION_COUNT == NSW_SELECTED_COUNT){selectAllStateRegions(listBox, "NSW");}
		else {selectIndividualRegions(listBox, items, itemArray, "NSW");	}
		
		if (NT_REGION_COUNT == NT_SELECTED_COUNT){selectAllStateRegions(listBox, "NT");	}
		else {selectIndividualRegions(listBox, items, itemArray, "NT ");	}
		
		if (QLD_REGION_COUNT == QLD_SELECTED_COUNT){selectAllStateRegions(listBox, "QLD");	}
		else {selectIndividualRegions(listBox, items, itemArray, "QLD");	}

		if (SA_REGION_COUNT == SA_SELECTED_COUNT){selectAllStateRegions(listBox, "SA");	}
		else {selectIndividualRegions(listBox, items, itemArray, "SA ");	}		
		
		if (TAS_REGION_COUNT == TAS_SELECTED_COUNT){selectAllStateRegions(listBox, "TAS");	}
		else {selectIndividualRegions(listBox, items, itemArray, "TAS");	}
		
		if (VIC_REGION_COUNT == VIC_SELECTED_COUNT){selectAllStateRegions(listBox, "VIC");	}
		else { selectIndividualRegions(listBox, items, itemArray, "VIC ");	}		
		
		if (WA_REGION_COUNT == WA_SELECTED_COUNT){selectAllStateRegions(listBox, "WA");	}
		else { selectIndividualRegions(listBox, items, itemArray, "WA ");	}	
	
		selectIndividualRegions(listBox, items, itemArray, "AUS");
		selectStateDataOnly(listBox, itemArray);			

		
	}		
}

/*
 * selectAllStateRegions
 */
function selectAllStateRegions(listBox, abbrev){
	// select All Regions option
	for (i=0; i < selectedStates.length*2; i++){	
		if (listBox.options[i].text == abbrev + ' - All Regions'){
			listBox.options[i].selected = true;
		} else {
			listBox.options[i].selected = false;
		}
	}
		
	// deselect all options in selected state as these options areselected by default when
	// all regions is selected (above)
	for (i=selectedStates.length*2; i < listBox.length; i++){
		if (listBox.options[i].text.substring(0,3) == abbrev){
			listBox.options[i].selected = false;
		}		
	}	
}

/*
 * selectIndividualRegions
 */
function selectIndividualRegions(listBox, items, itemArray, abbrev){
	// deselect all statewide options
	for (i=0; i < selectedStates.length*2; i++){
		if(listBox.options[i].text.substring(0,3) == abbrev){
			listBox.options[i].selected = false;
		}	
	}
	
	// select specific regions
	for (i=selectedStates.length*2+1;i < listBox.length; i++){				
		if (listBox.options[i].text.substring(0,3) == abbrev){	
			for (j=0; j < itemArray.length; j++){
				if (itemArray[j] == listBox.options[i].value){
					listBox.options[i].selected = true;
				}
			}				
		}
	}	
}

/*
 * selectStateDataOnly
 */
function selectStateDataOnly(listBox, itemArray){
		for (i=0; i < selectedStates.length*2+1; i++){	
			for(j=0;j<itemArray.length;j++){					
				if (listBox.options[i].value == itemArray[j]){
					listBox.options[i].selected = true;
				}
			}
		}			
}

/*
 * reSelectItemsWithAnyHeader
 */
function reSelectItemsWithAnyHeader(listBox_cid, items){
	anySelected = false;
		listBox = document.getElementById(listBox_cid);	
	
	if (items.indexOf(',') > 0){
		// more than 1 option is selected			
		if (items.match(/,/g).length  >= listBox.length -2){
			listBox.options[0].selected = true;
			anySelected = true;
		}
	}
	
	if (!anySelected){	
		var itemsList = items.split(",");
		var itemsSelected = 0;
		
		for (i=0; i < listBox.options.length; i++){				
			//listBox.options[i].selected = false;
			
			for (j=0; j < itemsList.length; j++){			
				if(itemsList[j]==listBox.options[i].value){		
					
					listBox.options[i].selected = true;
					itemsSelected++;
				}
			}
		}
	}		
}

function searchScopeChanged(chkScope_cid, othScope_cid)
{	
	chkScope = document.getElementById(chkScope_cid);
	othScope = document.getElementById(othScope_cid);
	
	if (!chkScope.checked && !othScope.checked)	{
		othScope.checked = true;
	}
}

function useMyRegionChanged(chkMyRegion_cid, stateSelector_cid, regionSelector_cid)
{
	chkMyRegion = document.getElementById(chkMyRegion_cid);
	stateSelector = document.getElementById(stateSelector_cid);
	regionSelector = document.getElementById(regionSelector_cid);
	
	if (chkMyRegion.checked){
		stateSelector.disabled=true;
		regionSelector.disabled=true;	
	}else{
		stateSelector.disabled=false;
		regionSelector.disabled=false;
	}
}

// Validate that an item has been selected.
function validateList(listBox_cid, title){
	
	listBox = document.getElementById(listBox_cid);
	
	//alert(listBox.selectedIndex);
	if (listBox.selectedIndex == -1){
		alert('You must select at least one item from the ' + title + ' list');
		listBox.selectedIndex = 0;	
	}	
}

// Validate Form
function validateSearchForm (state_cid, region_cid, topic_cid, type_cid, keyword_cid) {
	state = document.getElementById(state_cid);
	region = document.getElementById(region_cid);
	topic = document.getElementById(topic_cid);
	type = document.getElementById(type_cid);
	keyword = document.getElementById(keyword_cid);
	
	if (keyword.value == ''){
		// no keyword has been entered
		// validate listboxes for a selection
		
		var faultItems = '';
		
		if (region.selectedIndex < 0){ faultItems += '+ Region\n';	}
		if (topic.selectedIndex < 0) { faultItems += '+ Topic\n'; }		
		if (type.selectedIndex < 0) { faultItems += '+ Type '; }
		
		if (faultItems.length > 0){			 
			alert('Please select a:\n' + faultItems);
			return false;
		}
	
	}else{
		// keyword has been entered
		// if region / topic has not been selected, select all option.
		
		if (region.selectedIndex < 0) {
			state.selectedIndex = 0;
			stateChanged(state_cid, region_cid);
		}
		
		if (topic.selectedIndex < 0) {
			topic.selectedIndex = 0;
		}
	}		
}
