arrRegions = [{"intRegionId":"63","strRegionName":"Wiltshire","arrLocations":[{"intLocationId":"183","strLocationName":"Andover","strLocationNameWithPrefix":"Andover","strRegionName":"Wiltshire"},{"intLocationId":"586","strLocationName":"Bradford-On-Avon","strLocationNameWithPrefix":"Bradford-On-Avon","strRegionName":"Wiltshire"},{"intLocationId":"591","strLocationName":"Calne","strLocationNameWithPrefix":"Calne","strRegionName":"Wiltshire"},{"intLocationId":"593","strLocationName":"Chippenham","strLocationNameWithPrefix":"Chippenham","strRegionName":"Wiltshire"},{"intLocationId":"600","strLocationName":"Cirencester","strLocationNameWithPrefix":"Cirencester","strRegionName":"Wiltshire"},{"intLocationId":"592","strLocationName":"Corsham","strLocationNameWithPrefix":"Corsham","strRegionName":"Wiltshire"},{"intLocationId":"590","strLocationName":"Devizes","strLocationNameWithPrefix":"Devizes","strRegionName":"Wiltshire"},{"intLocationId":"605","strLocationName":"Faringdon","strLocationNameWithPrefix":"Faringdon","strRegionName":"Wiltshire"},{"intLocationId":"162","strLocationName":"Fordingbridge","strLocationNameWithPrefix":"Fordingbridge","strRegionName":"Wiltshire"},{"intLocationId":"599","strLocationName":"Gillingham","strLocationNameWithPrefix":"Gillingham","strRegionName":"Wiltshire"},{"intLocationId":"598","strLocationName":"Hungerford","strLocationNameWithPrefix":"Hungerford","strRegionName":"Wiltshire"},{"intLocationId":"603","strLocationName":"Lyndhurst","strLocationNameWithPrefix":"Lyndhurst","strRegionName":"Wiltshire"},{"intLocationId":"594","strLocationName":"Malmesbury","strLocationNameWithPrefix":"Malmesbury","strRegionName":"Wiltshire"},{"intLocationId":"588","strLocationName":"Marlborough","strLocationNameWithPrefix":"Marlborough","strRegionName":"Wiltshire"},{"intLocationId":"134","strLocationName":"Melksham","strLocationNameWithPrefix":"Melksham","strRegionName":"Wiltshire"},{"intLocationId":"589","strLocationName":"Pewsey","strLocationNameWithPrefix":"Pewsey","strRegionName":"Wiltshire"},{"intLocationId":"166","strLocationName":"Romsey","strLocationNameWithPrefix":"Romsey","strRegionName":"Wiltshire"},{"intLocationId":"167","strLocationName":"Salisbury","strLocationNameWithPrefix":"Salisbury","strRegionName":"Wiltshire"},{"intLocationId":"595","strLocationName":"Shaftesbury","strLocationNameWithPrefix":"Shaftesbury","strRegionName":"Wiltshire"},{"intLocationId":"587","strLocationName":"Swindon","strLocationNameWithPrefix":"Swindon","strRegionName":"Wiltshire"},{"intLocationId":"601","strLocationName":"Tetbury","strLocationNameWithPrefix":"Tetbury","strRegionName":"Wiltshire"},{"intLocationId":"596","strLocationName":"Tidworth","strLocationNameWithPrefix":"Tidworth","strRegionName":"Wiltshire"},{"intLocationId":"132","strLocationName":"Trowbridge","strLocationNameWithPrefix":"Trowbridge","strRegionName":"Wiltshire"},{"intLocationId":"136","strLocationName":"Warminster","strLocationNameWithPrefix":"Warminster","strRegionName":"Wiltshire"},{"intLocationId":"135","strLocationName":"Westbury","strLocationNameWithPrefix":"Westbury","strRegionName":"Wiltshire"}]}]

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

