arrRegions = [{"intRegionId":"38","strRegionName":"Nottinghamshire","arrLocations":[{"intLocationId":"28","strLocationName":"Arnold","strLocationNameWithPrefix":"Arnold","strRegionName":"Nottinghamshire"},{"intLocationId":"1695","strLocationName":"Gainsborough","strLocationNameWithPrefix":"Gainsborough","strRegionName":"Nottinghamshire"},{"intLocationId":"26","strLocationName":"Hucknall","strLocationNameWithPrefix":"Hucknall","strRegionName":"Nottinghamshire"},{"intLocationId":"1728","strLocationName":"Lincoln","strLocationNameWithPrefix":"Lincoln","strRegionName":"Nottinghamshire"},{"intLocationId":"27","strLocationName":"Long Eaton","strLocationNameWithPrefix":"Long Eaton","strRegionName":"Nottinghamshire"},{"intLocationId":"7","strLocationName":"Mansfield","strLocationNameWithPrefix":"Mansfield","strRegionName":"Nottinghamshire"},{"intLocationId":"8","strLocationName":"Newark","strLocationNameWithPrefix":"Newark","strRegionName":"Nottinghamshire"},{"intLocationId":"20","strLocationName":"Nottingham","strLocationNameWithPrefix":"Nottingham","strRegionName":"Nottinghamshire"},{"intLocationId":"50","strLocationName":"Other","strLocationNameWithPrefix":"Other","strRegionName":"Nottinghamshire"},{"intLocationId":"9","strLocationName":"Retford","strLocationNameWithPrefix":"Retford","strRegionName":"Nottinghamshire"},{"intLocationId":"1508","strLocationName":"Southwell","strLocationNameWithPrefix":"Southwell","strRegionName":"Nottinghamshire"},{"intLocationId":"29","strLocationName":"Sutton-In-Ashfield","strLocationNameWithPrefix":"Sutton-In-Ashfield","strRegionName":"Nottinghamshire"},{"intLocationId":"10","strLocationName":"Worksop","strLocationNameWithPrefix":"Worksop","strRegionName":"Nottinghamshire"}]}]

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

