arrRegions = [{"intRegionId":"66","strRegionName":"Buckinghamshire","arrLocations":[{"intLocationId":"676","strLocationName":"Amersham","strLocationNameWithPrefix":"Amersham","strRegionName":"Buckinghamshire"},{"intLocationId":"680","strLocationName":"Aylesbury","strLocationNameWithPrefix":"Aylesbury","strRegionName":"Buckinghamshire"},{"intLocationId":"677","strLocationName":"Beaconsfield","strLocationNameWithPrefix":"Beaconsfield","strRegionName":"Buckinghamshire"},{"intLocationId":"689","strLocationName":"Bedford","strLocationNameWithPrefix":"Bedford","strRegionName":"Buckinghamshire"},{"intLocationId":"564","strLocationName":"Berkhamsted","strLocationNameWithPrefix":"Berkhamsted","strRegionName":"Buckinghamshire"},{"intLocationId":"692","strLocationName":"Bicester","strLocationNameWithPrefix":"Bicester","strRegionName":"Buckinghamshire"},{"intLocationId":"687","strLocationName":"Bourne End","strLocationNameWithPrefix":"Bourne End","strRegionName":"Buckinghamshire"},{"intLocationId":"691","strLocationName":"Brackley","strLocationNameWithPrefix":"Brackley","strRegionName":"Buckinghamshire"},{"intLocationId":"685","strLocationName":"Buckingham","strLocationNameWithPrefix":"Buckingham","strRegionName":"Buckinghamshire"},{"intLocationId":"694","strLocationName":"Chalfont St. Giles","strLocationNameWithPrefix":"Chalfont St. Giles","strRegionName":"Buckinghamshire"},{"intLocationId":"675","strLocationName":"Chesham","strLocationNameWithPrefix":"Chesham","strRegionName":"Buckinghamshire"},{"intLocationId":"582","strLocationName":"Dunstable","strLocationNameWithPrefix":"Dunstable","strRegionName":"Buckinghamshire"},{"intLocationId":"688","strLocationName":"Gerrards Cross","strLocationNameWithPrefix":"Gerrards Cross","strRegionName":"Buckinghamshire"},{"intLocationId":"679","strLocationName":"Great Missenden","strLocationNameWithPrefix":"Great Missenden","strRegionName":"Buckinghamshire"},{"intLocationId":"563","strLocationName":"Hemel Hempstead","strLocationNameWithPrefix":"Hemel Hempstead","strRegionName":"Buckinghamshire"},{"intLocationId":"671","strLocationName":"Henley-On-Thames","strLocationNameWithPrefix":"Henley-On-Thames","strRegionName":"Buckinghamshire"},{"intLocationId":"678","strLocationName":"High Wycombe","strLocationNameWithPrefix":"High Wycombe","strRegionName":"Buckinghamshire"},{"intLocationId":"686","strLocationName":"Iver","strLocationNameWithPrefix":"Iver","strRegionName":"Buckinghamshire"},{"intLocationId":"682","strLocationName":"Leighton Buzzard","strLocationNameWithPrefix":"Leighton Buzzard","strRegionName":"Buckinghamshire"},{"intLocationId":"667","strLocationName":"Maidenhead","strLocationNameWithPrefix":"Maidenhead","strRegionName":"Buckinghamshire"},{"intLocationId":"668","strLocationName":"Marlow","strLocationNameWithPrefix":"Marlow","strRegionName":"Buckinghamshire"},{"intLocationId":"683","strLocationName":"Milton Keynes","strLocationNameWithPrefix":"Milton Keynes","strRegionName":"Buckinghamshire"},{"intLocationId":"684","strLocationName":"Newport Pagnell","strLocationNameWithPrefix":"Newport Pagnell","strRegionName":"Buckinghamshire"},{"intLocationId":"690","strLocationName":"Olney","strLocationNameWithPrefix":"Olney","strRegionName":"Buckinghamshire"},{"intLocationId":"681","strLocationName":"Princes Risborough","strLocationNameWithPrefix":"Princes Risborough","strRegionName":"Buckinghamshire"},{"intLocationId":"576","strLocationName":"Rickmansworth","strLocationNameWithPrefix":"Rickmansworth","strRegionName":"Buckinghamshire"},{"intLocationId":"643","strLocationName":"Staines","strLocationNameWithPrefix":"Staines","strRegionName":"Buckinghamshire"},{"intLocationId":"693","strLocationName":"Thame","strLocationNameWithPrefix":"Thame","strRegionName":"Buckinghamshire"},{"intLocationId":"565","strLocationName":"Tring","strLocationNameWithPrefix":"Tring","strRegionName":"Buckinghamshire"},{"intLocationId":"649","strLocationName":"Uxbridge","strLocationNameWithPrefix":"Uxbridge","strRegionName":"Buckinghamshire"},{"intLocationId":"665","strLocationName":"Windsor","strLocationNameWithPrefix":"Windsor","strRegionName":"Buckinghamshire"}]}]

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