arrRegions = [{"intRegionId":"28","strRegionName":"Leicestershire","arrLocations":[{"intLocationId":"17","strLocationName":"Ashby-de-la-Zouch","strLocationNameWithPrefix":"Ashby-de-la-Zouch","strRegionName":"Leicestershire"},{"intLocationId":"1636","strLocationName":"Atherstone","strLocationNameWithPrefix":"Atherstone","strRegionName":"Leicestershire"},{"intLocationId":"11","strLocationName":"Coalville","strLocationNameWithPrefix":"Coalville","strRegionName":"Leicestershire"},{"intLocationId":"1701","strLocationName":"Grantham","strLocationNameWithPrefix":"Grantham","strRegionName":"Leicestershire"},{"intLocationId":"14","strLocationName":"Hinckley","strLocationNameWithPrefix":"Hinckley","strRegionName":"Leicestershire"},{"intLocationId":"1235","strLocationName":"Ibstock","strLocationNameWithPrefix":"Ibstock","strRegionName":"Leicestershire"},{"intLocationId":"21","strLocationName":"Leicester","strLocationNameWithPrefix":"Leicester","strRegionName":"Leicestershire"},{"intLocationId":"12","strLocationName":"Loughborough","strLocationNameWithPrefix":"Loughborough","strRegionName":"Leicestershire"},{"intLocationId":"32","strLocationName":"Lutterworth","strLocationNameWithPrefix":"Lutterworth","strRegionName":"Leicestershire"},{"intLocationId":"30","strLocationName":"Market Harborough","strLocationNameWithPrefix":"Market Harborough","strRegionName":"Leicestershire"},{"intLocationId":"1363","strLocationName":"Markfield","strLocationNameWithPrefix":"Markfield","strRegionName":"Leicestershire"},{"intLocationId":"13","strLocationName":"Melton Mowbray","strLocationNameWithPrefix":"Melton Mowbray","strRegionName":"Leicestershire"},{"intLocationId":"15","strLocationName":"Oadby","strLocationNameWithPrefix":"Oadby","strRegionName":"Leicestershire"},{"intLocationId":"18","strLocationName":"Oakham","strLocationNameWithPrefix":"Oakham","strRegionName":"Leicestershire"},{"intLocationId":"51","strLocationName":"Other","strLocationNameWithPrefix":"Other","strRegionName":"Leicestershire"},{"intLocationId":"31","strLocationName":"Rutland","strLocationNameWithPrefix":"Rutland","strRegionName":"Leicestershire"},{"intLocationId":"16","strLocationName":"Wigston","strLocationNameWithPrefix":"Wigston","strRegionName":"Leicestershire"}]}]

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