arrRegions = [{"intRegionId":"81","strRegionName":"Gloucestershire","arrLocations":[{"intLocationId":"980","strLocationName":"Badminton","strLocationNameWithPrefix":"Badminton","strRegionName":"Gloucestershire"},{"intLocationId":"1004","strLocationName":"Berkeley","strLocationNameWithPrefix":"Berkeley","strRegionName":"Gloucestershire"},{"intLocationId":"1013","strLocationName":"Blakeney","strLocationNameWithPrefix":"Blakeney","strRegionName":"Gloucestershire"},{"intLocationId":"1656","strLocationName":"Broadway","strLocationNameWithPrefix":"Broadway","strRegionName":"Gloucestershire"},{"intLocationId":"699","strLocationName":"Burford","strLocationNameWithPrefix":"Burford","strRegionName":"Gloucestershire"},{"intLocationId":"1669","strLocationName":"Cheltenham","strLocationNameWithPrefix":"Cheltenham","strRegionName":"Gloucestershire"},{"intLocationId":"1670","strLocationName":"Chipping Campden","strLocationNameWithPrefix":"Chipping Campden","strRegionName":"Gloucestershire"},{"intLocationId":"696","strLocationName":"Chipping Norton","strLocationNameWithPrefix":"Chipping Norton","strRegionName":"Gloucestershire"},{"intLocationId":"1079","strLocationName":"Cinderford","strLocationNameWithPrefix":"Cinderford","strRegionName":"Gloucestershire"},{"intLocationId":"600","strLocationName":"Cirencester","strLocationNameWithPrefix":"Cirencester","strRegionName":"Gloucestershire"},{"intLocationId":"1673","strLocationName":"Coleford","strLocationNameWithPrefix":"Coleford","strRegionName":"Gloucestershire"},{"intLocationId":"1130","strLocationName":"Drybrook","strLocationNameWithPrefix":"Drybrook","strRegionName":"Gloucestershire"},{"intLocationId":"1141","strLocationName":"Dursley","strLocationNameWithPrefix":"Dursley","strRegionName":"Gloucestershire"},{"intLocationId":"1143","strLocationName":"Dymock","strLocationNameWithPrefix":"Dymock","strRegionName":"Gloucestershire"},{"intLocationId":"1690","strLocationName":"Evesham","strLocationNameWithPrefix":"Evesham","strRegionName":"Gloucestershire"},{"intLocationId":"669","strLocationName":"Fairford","strLocationNameWithPrefix":"Fairford","strRegionName":"Gloucestershire"},{"intLocationId":"1699","strLocationName":"Gloucester","strLocationNameWithPrefix":"Gloucester","strRegionName":"Gloucestershire"},{"intLocationId":"670","strLocationName":"Lechlade","strLocationNameWithPrefix":"Lechlade","strRegionName":"Gloucestershire"},{"intLocationId":"1727","strLocationName":"Ledbury","strLocationNameWithPrefix":"Ledbury","strRegionName":"Gloucestershire"},{"intLocationId":"1343","strLocationName":"Longhope","strLocationNameWithPrefix":"Longhope","strRegionName":"Gloucestershire"},{"intLocationId":"1349","strLocationName":"Lydbrook","strLocationNameWithPrefix":"Lydbrook","strRegionName":"Gloucestershire"},{"intLocationId":"1351","strLocationName":"Lydney","strLocationNameWithPrefix":"Lydney","strRegionName":"Gloucestershire"},{"intLocationId":"1379","strLocationName":"Mitcheldean","strLocationNameWithPrefix":"Mitcheldean","strRegionName":"Gloucestershire"},{"intLocationId":"1397","strLocationName":"Newent","strLocationNameWithPrefix":"Newent","strRegionName":"Gloucestershire"},{"intLocationId":"1399","strLocationName":"Newnham","strLocationNameWithPrefix":"Newnham","strRegionName":"Gloucestershire"},{"intLocationId":"1766","strLocationName":"Ross-On-Wye","strLocationNameWithPrefix":"Ross-On-Wye","strRegionName":"Gloucestershire"},{"intLocationId":"1475","strLocationName":"Ruardean","strLocationNameWithPrefix":"Ruardean","strRegionName":"Gloucestershire"},{"intLocationId":"1772","strLocationName":"Shipston-On-Stour","strLocationNameWithPrefix":"Shipston-On-Stour","strRegionName":"Gloucestershire"},{"intLocationId":"1526","strLocationName":"Stonehouse","strLocationNameWithPrefix":"Stonehouse","strRegionName":"Gloucestershire"},{"intLocationId":"1537","strLocationName":"Stroud","strLocationNameWithPrefix":"Stroud","strRegionName":"Gloucestershire"},{"intLocationId":"601","strLocationName":"Tetbury","strLocationNameWithPrefix":"Tetbury","strRegionName":"Gloucestershire"},{"intLocationId":"1785","strLocationName":"Tewkesbury","strLocationNameWithPrefix":"Tewkesbury","strRegionName":"Gloucestershire"},{"intLocationId":"1595","strLocationName":"Westbury-On-Severn","strLocationNameWithPrefix":"Westbury-On-Severn","strRegionName":"Gloucestershire"},{"intLocationId":"1612","strLocationName":"Wotton-Under-Edge","strLocationNameWithPrefix":"Wotton-Under-Edge","strRegionName":"Gloucestershire"}]}]

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

