arrRegions = [{"intRegionId":"31","strRegionName":"Newcastle","arrLocations":[{"intLocationId":"241","strLocationName":"Birtley","strLocationNameWithPrefix":"Birtley","strRegionName":"Newcastle"},{"intLocationId":"236","strLocationName":"Blaydon-On-Tyne","strLocationNameWithPrefix":"Blaydon-On-Tyne","strRegionName":"Newcastle"},{"intLocationId":"73","strLocationName":"Blyth","strLocationNameWithPrefix":"Blyth","strRegionName":"Newcastle"},{"intLocationId":"243","strLocationName":"Boldon","strLocationNameWithPrefix":"Boldon","strRegionName":"Newcastle"},{"intLocationId":"75","strLocationName":"Castle Morpeth","strLocationNameWithPrefix":"Castle Morpeth","strRegionName":"Newcastle"},{"intLocationId":"231","strLocationName":"Cramlington","strLocationNameWithPrefix":"Cramlington","strRegionName":"Newcastle"},{"intLocationId":"246","strLocationName":"Felling","strLocationNameWithPrefix":"Felling","strRegionName":"Newcastle"},{"intLocationId":"70","strLocationName":"Gateshead","strLocationNameWithPrefix":"Gateshead","strRegionName":"Newcastle"},{"intLocationId":"248","strLocationName":"Gosforth","strLocationNameWithPrefix":"Gosforth","strRegionName":"Newcastle"},{"intLocationId":"244","strLocationName":"Jarrow","strLocationNameWithPrefix":"Jarrow","strRegionName":"Newcastle"},{"intLocationId":"232","strLocationName":"Longbenton","strLocationNameWithPrefix":"Longbenton","strRegionName":"Newcastle"},{"intLocationId":"56","strLocationName":"Newcastle upon Tyne","strLocationNameWithPrefix":"Newcastle upon Tyne","strRegionName":"Newcastle"},{"intLocationId":"245","strLocationName":"North Shields","strLocationNameWithPrefix":"North Shields","strRegionName":"Newcastle"},{"intLocationId":"71","strLocationName":"North Tyneside","strLocationNameWithPrefix":"North Tyneside","strRegionName":"Newcastle"},{"intLocationId":"199","strLocationName":"Other","strLocationNameWithPrefix":"Other","strRegionName":"Newcastle"},{"intLocationId":"247","strLocationName":"Ponteland","strLocationNameWithPrefix":"Ponteland","strRegionName":"Newcastle"},{"intLocationId":"235","strLocationName":"Ryton","strLocationNameWithPrefix":"Ryton","strRegionName":"Newcastle"},{"intLocationId":"230","strLocationName":"South Shields","strLocationNameWithPrefix":"South Shields","strRegionName":"Newcastle"},{"intLocationId":"72","strLocationName":"South Tyneside","strLocationNameWithPrefix":"South Tyneside","strRegionName":"Newcastle"},{"intLocationId":"240","strLocationName":"Stanley","strLocationNameWithPrefix":"Stanley","strRegionName":"Newcastle"},{"intLocationId":"76","strLocationName":"Tynedale","strLocationNameWithPrefix":"Tynedale","strRegionName":"Newcastle"},{"intLocationId":"233","strLocationName":"Tynemouth","strLocationNameWithPrefix":"Tynemouth","strRegionName":"Newcastle"},{"intLocationId":"229","strLocationName":"Wallsend","strLocationNameWithPrefix":"Wallsend","strRegionName":"Newcastle"},{"intLocationId":"74","strLocationName":"Wansbeck","strLocationNameWithPrefix":"Wansbeck","strRegionName":"Newcastle"},{"intLocationId":"242","strLocationName":"Washington","strLocationNameWithPrefix":"Washington","strRegionName":"Newcastle"},{"intLocationId":"238","strLocationName":"Whickham","strLocationNameWithPrefix":"Whickham","strRegionName":"Newcastle"},{"intLocationId":"234","strLocationName":"Whitley Bay","strLocationNameWithPrefix":"Whitley Bay","strRegionName":"Newcastle"}]}]

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);
	}
}
