arrRegions = [{"intRegionId":"113","strRegionName":"Monmouthshire","arrLocations":[{"intLocationId":"529","strLocationName":"Abergavenny","strLocationNameWithPrefix":"Abergavenny","strRegionName":"Newport"},{"intLocationId":"953","strLocationName":"Abertillery","strLocationNameWithPrefix":"Abertillery","strRegionName":"Monmouthshire"},{"intLocationId":"1642","strLocationName":"Bargoed","strLocationNameWithPrefix":"Bargoed","strRegionName":"Monmouthshire"},{"intLocationId":"1649","strLocationName":"Blackwood","strLocationNameWithPrefix":"Blackwood","strRegionName":"Monmouthshire"},{"intLocationId":"523","strLocationName":"Caerleon","strLocationNameWithPrefix":"Caerleon","strRegionName":"Newport"},{"intLocationId":"526","strLocationName":"Caldicot","strLocationNameWithPrefix":"Caldicot","strRegionName":"Newport"},{"intLocationId":"209","strLocationName":"Chepstow","strLocationNameWithPrefix":"Chepstow","strRegionName":"Newport"},{"intLocationId":"1680","strLocationName":"Crickhowell","strLocationNameWithPrefix":"Crickhowell","strRegionName":"Monmouthshire"},{"intLocationId":"208","strLocationName":"Cwmbran","strLocationNameWithPrefix":"Cwmbran","strRegionName":"Newport"},{"intLocationId":"925","strLocationName":"Ebbw Vale","strLocationNameWithPrefix":"Ebbw Vale","strRegionName":"Newport"},{"intLocationId":"1705","strLocationName":"Hengoed","strLocationNameWithPrefix":"Hengoed","strRegionName":"Monmouthshire"},{"intLocationId":"524","strLocationName":"Magor","strLocationNameWithPrefix":"Magor","strRegionName":"Newport"},{"intLocationId":"528","strLocationName":"Monmouth","strLocationNameWithPrefix":"Monmouth","strRegionName":"Newport"},{"intLocationId":"1752","strLocationName":"New Tredegar","strLocationNameWithPrefix":"New Tredegar","strRegionName":"Monmouthshire"},{"intLocationId":"272","strLocationName":"Newport","strLocationNameWithPrefix":"Newport","strRegionName":"Newport"},{"intLocationId":"210","strLocationName":"Pontypool","strLocationNameWithPrefix":"Pontypool","strRegionName":"Monmouthshire"},{"intLocationId":"530","strLocationName":"Risca","strLocationNameWithPrefix":"Risca","strRegionName":"Newport"},{"intLocationId":"531","strLocationName":"Rogerstone","strLocationNameWithPrefix":"Rogerstone","strRegionName":"Newport"},{"intLocationId":"1788","strLocationName":"Tredegar","strLocationNameWithPrefix":"Tredegar","strRegionName":"Monmouthshire"},{"intLocationId":"1581","strLocationName":"Usk","strLocationNameWithPrefix":"Usk","strRegionName":"Monmouthshire"}]}]

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

