arrRegions = [{"intRegionId":"1","strRegionName":"Aberdeenshire","arrLocations":[{"intLocationId":"324","strLocationName":"Aberdeen","strLocationNameWithPrefix":"Aberdeen","strRegionName":"Aberdeenshire"},{"intLocationId":"325","strLocationName":"Aberdeen (City Centre)","strLocationNameWithPrefix":"Aberdeen (City Centre)","strRegionName":"Aberdeenshire"},{"intLocationId":"1627","strLocationName":"Aberlour","strLocationNameWithPrefix":"Aberlour","strRegionName":"Aberdeenshire"},{"intLocationId":"955","strLocationName":"Aboyne","strLocationNameWithPrefix":"Aboyne","strRegionName":"Aberdeenshire"},{"intLocationId":"1623","strLocationName":"Alford","strLocationNameWithPrefix":"Alford","strRegionName":"Aberdeenshire"},{"intLocationId":"983","strLocationName":"Ballater","strLocationNameWithPrefix":"Ballater","strRegionName":"Aberdeenshire"},{"intLocationId":"984","strLocationName":"Ballindalloch","strLocationNameWithPrefix":"Ballindalloch","strRegionName":"Aberdeenshire"},{"intLocationId":"1640","strLocationName":"Banchory","strLocationNameWithPrefix":"Banchory","strRegionName":"Aberdeenshire"},{"intLocationId":"1641","strLocationName":"Banff","strLocationNameWithPrefix":"Banff","strRegionName":"Aberdeenshire"},{"intLocationId":"1047","strLocationName":"Buckie","strLocationNameWithPrefix":"Buckie","strRegionName":"Aberdeenshire"},{"intLocationId":"1152","strLocationName":"Ellon","strLocationNameWithPrefix":"Ellon","strRegionName":"Aberdeenshire"},{"intLocationId":"1691","strLocationName":"Fochabers","strLocationNameWithPrefix":"Fochabers","strRegionName":"Aberdeenshire"},{"intLocationId":"1174","strLocationName":"Fraserburgh","strLocationNameWithPrefix":"Fraserburgh","strRegionName":"Aberdeenshire"},{"intLocationId":"1709","strLocationName":"Huntly","strLocationNameWithPrefix":"Huntly","strRegionName":"Aberdeenshire"},{"intLocationId":"1240","strLocationName":"Insch","strLocationNameWithPrefix":"Insch","strRegionName":"Aberdeenshire"},{"intLocationId":"1711","strLocationName":"Inverurie","strLocationNameWithPrefix":"Inverurie","strRegionName":"Aberdeenshire"},{"intLocationId":"1714","strLocationName":"Keith","strLocationNameWithPrefix":"Keith","strRegionName":"Aberdeenshire"},{"intLocationId":"1726","strLocationName":"Laurencekirk","strLocationNameWithPrefix":"Laurencekirk","strRegionName":"Aberdeenshire"},{"intLocationId":"1356","strLocationName":"Macduff","strLocationNameWithPrefix":"Macduff","strRegionName":"Aberdeenshire"},{"intLocationId":"1376","strLocationName":"Milltimber","strLocationNameWithPrefix":"Milltimber","strRegionName":"Aberdeenshire"},{"intLocationId":"1749","strLocationName":"Montrose","strLocationNameWithPrefix":"Montrose","strRegionName":"Aberdeenshire"},{"intLocationId":"1759","strLocationName":"Peterculter","strLocationNameWithPrefix":"Peterculter","strRegionName":"Aberdeenshire"},{"intLocationId":"323","strLocationName":"Peterhead","strLocationNameWithPrefix":"Peterhead","strRegionName":"Aberdeenshire"},{"intLocationId":"1525","strLocationName":"Stonehaven","strLocationNameWithPrefix":"Stonehaven","strRegionName":"Aberdeenshire"},{"intLocationId":"1532","strLocationName":"Strathdon","strLocationNameWithPrefix":"Strathdon","strRegionName":"Aberdeenshire"},{"intLocationId":"1789","strLocationName":"Turriff","strLocationNameWithPrefix":"Turriff","strRegionName":"Aberdeenshire"},{"intLocationId":"1596","strLocationName":"Westhill","strLocationNameWithPrefix":"Westhill","strRegionName":"Aberdeenshire"}]}]

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

