arrRegions = [{"intRegionId":"112","strRegionName":"Flintshire","arrLocations":[{"intLocationId":"1626","strLocationName":"Abergele","strLocationNameWithPrefix":"Abergele","strRegionName":"Flintshire"},{"intLocationId":"981","strLocationName":"Bagillt","strLocationNameWithPrefix":"Bagillt","strRegionName":"Flintshire"},{"intLocationId":"1048","strLocationName":"Buckley","strLocationNameWithPrefix":"Buckley","strRegionName":"Flintshire"},{"intLocationId":"415","strLocationName":"Deeside","strLocationNameWithPrefix":"Deeside","strRegionName":"Flintshire"},{"intLocationId":"1682","strLocationName":"Denbigh","strLocationNameWithPrefix":"Denbigh","strRegionName":"Flintshire"},{"intLocationId":"417","strLocationName":"Flint","strLocationNameWithPrefix":"Flint","strRegionName":"Flintshire"},{"intLocationId":"1226","strLocationName":"Holywell","strLocationNameWithPrefix":"Holywell","strRegionName":"Flintshire"},{"intLocationId":"1741","strLocationName":"Malpas","strLocationNameWithPrefix":"Malpas","strRegionName":"Flintshire"},{"intLocationId":"1747","strLocationName":"Mold","strLocationNameWithPrefix":"Mold","strRegionName":"Flintshire"},{"intLocationId":"299","strLocationName":"Prestatyn","strLocationNameWithPrefix":"Prestatyn","strRegionName":"Flintshire"},{"intLocationId":"1765","strLocationName":"Rhyl","strLocationNameWithPrefix":"Rhyl","strRegionName":"Flintshire"},{"intLocationId":"1777","strLocationName":"St. Asaph","strLocationNameWithPrefix":"St. Asaph","strRegionName":"Flintshire"},{"intLocationId":"1621","strLocationName":"Whitchurch","strLocationNameWithPrefix":"Whitchurch","strRegionName":"Flintshire"},{"intLocationId":"300","strLocationName":"Wrexham","strLocationNameWithPrefix":"Wrexham","strRegionName":"Flintshire"}]}]

AddPageLoadFunction(
	function(){
		var objVarElement = document.getElementById("QuickSearchRegion");
		
		if(objVarElement != null && objVarElement.options){			
			var objOption, objTextNode;

			var intCurrentRegionId = objVarElement.options[objVarElement.selectedIndex].value;		
			objVarElement.innerHTML="";
			
			var intNumRegions = arrRegions.length;
			var bolRegionFound = false;
			
			for(var i=0; i<intNumRegions;i++){
				objOption = document.createElement("option");
				objOption.value = arrRegions[i]["intRegionId"];
				if(intCurrentRegionId == arrRegions[i]["intRegionId"]){
					objOption.selected = "selected";
					bolRegionFound = true;
				}
				objTextNode = document.createTextNode(arrRegions[i]["strRegionName"]);
				objOption.appendChild(objTextNode);
				objVarElement.appendChild(objOption);
			}
			
			if(!bolRegionFound){
				intCurrentRegionId = objVarElement.options[objVarElement.selectedIndex].value;				
				QuickChangeRegionById(intCurrentRegionId);
			}					
			
			var objMyRules = { 
				"#QuickSearchRegion" : function(objElement){
					addEvent(objElement,"change",QuickChangeRegion);
				}
			};
			Behaviour.register(objMyRules);
			Behaviour.apply(objMyRules);
		}
	}
)

function QuickChangeRegion(objEvent){
	objEvent = PrepareEvent(objEvent);
	var intCurrentRegionId = objEvent.objTarget.options[objEvent.objTarget.selectedIndex].value;
	QuickChangeRegionById(intCurrentRegionId);
}

function QuickChangeRegionById(intRegionId){
	var objOption, objTextNode;
	
	var objVarElement = document.getElementById("QuickSearchLocation");
	objVarElement.innerHTML="";

	objOption = document.createElement("option");
	objOption.value = 0;
	objTextNode = document.createTextNode("All Locations");
	objOption.appendChild(objTextNode);
	objVarElement.appendChild(objOption);

	var intNumRegions = arrRegions.length;
	for(var i=0; i<intNumRegions;i++){
		if(intRegionId == arrRegions[i]["intRegionId"]){
			intCurrentRegion = i;
		}
	}
	
	var intNumLocations = arrRegions[intCurrentRegion]["arrLocations"].length;

	for(var i=0; i<intNumLocations;i++){
		objOption = document.createElement("option");
		objOption.value = arrRegions[intCurrentRegion]["arrLocations"][i]["intLocationId"];		
		objTextNode = document.createTextNode(arrRegions[intCurrentRegion]["arrLocations"][i]["strLocationName"]);
		objOption.appendChild(objTextNode);
		objVarElement.appendChild(objOption);
	}
}

