arrRegions = [{"intRegionId":"82","strRegionName":"Herefordshire","arrLocations":[{"intLocationId":"1657","strLocationName":"Bromyard","strLocationNameWithPrefix":"Bromyard","strRegionName":"Herefordshire"},{"intLocationId":"1659","strLocationName":"Bucknell","strLocationNameWithPrefix":"Bucknell","strRegionName":"Herefordshire"},{"intLocationId":"1673","strLocationName":"Coleford","strLocationNameWithPrefix":"Coleford","strRegionName":"Herefordshire"},{"intLocationId":"1679","strLocationName":"Craven Arms","strLocationNameWithPrefix":"Craven Arms","strRegionName":"Herefordshire"},{"intLocationId":"1706","strLocationName":"Hereford","strLocationNameWithPrefix":"Hereford","strRegionName":"Herefordshire"},{"intLocationId":"1717","strLocationName":"Kington","strLocationNameWithPrefix":"Kington","strRegionName":"Herefordshire"},{"intLocationId":"1727","strLocationName":"Ledbury","strLocationNameWithPrefix":"Ledbury","strRegionName":"Herefordshire"},{"intLocationId":"1301","strLocationName":"Leominster","strLocationNameWithPrefix":"Leominster","strRegionName":"Herefordshire"},{"intLocationId":"1737","strLocationName":"Ludlow","strLocationNameWithPrefix":"Ludlow","strRegionName":"Herefordshire"},{"intLocationId":"1742","strLocationName":"Malvern","strLocationNameWithPrefix":"Malvern","strRegionName":"Herefordshire"},{"intLocationId":"1763","strLocationName":"Presteigne","strLocationNameWithPrefix":"Presteigne","strRegionName":"Herefordshire"},{"intLocationId":"1766","strLocationName":"Ross-On-Wye","strLocationNameWithPrefix":"Ross-On-Wye","strRegionName":"Herefordshire"},{"intLocationId":"1784","strLocationName":"Tenbury Wells","strLocationNameWithPrefix":"Tenbury Wells","strRegionName":"Herefordshire"}]}]

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