arrRegions = [{"intRegionId":"83","strRegionName":"Lincolnshire","arrLocations":[{"intLocationId":"1624","strLocationName":"Alford","strLocationNameWithPrefix":"Alford","strRegionName":"Lincolnshire"},{"intLocationId":"988","strLocationName":"Barnetby","strLocationNameWithPrefix":"Barnetby","strRegionName":"Lincolnshire"},{"intLocationId":"990","strLocationName":"Barrow-Upon-Humber","strLocationNameWithPrefix":"Barrow-Upon-Humber","strRegionName":"Lincolnshire"},{"intLocationId":"992","strLocationName":"Barton-Upon-Humber","strLocationNameWithPrefix":"Barton-Upon-Humber","strRegionName":"Lincolnshire"},{"intLocationId":"1026","strLocationName":"Boston","strLocationNameWithPrefix":"Boston","strRegionName":"Lincolnshire"},{"intLocationId":"1027","strLocationName":"Bourne","strLocationNameWithPrefix":"Bourne","strRegionName":"Lincolnshire"},{"intLocationId":"1037","strLocationName":"Brigg","strLocationNameWithPrefix":"Brigg","strRegionName":"Lincolnshire"},{"intLocationId":"1086","strLocationName":"Cleethorpes","strLocationNameWithPrefix":"Cleethorpes","strRegionName":"Lincolnshire"},{"intLocationId":"1695","strLocationName":"Gainsborough","strLocationNameWithPrefix":"Gainsborough","strRegionName":"Lincolnshire"},{"intLocationId":"1701","strLocationName":"Grantham","strLocationNameWithPrefix":"Grantham","strRegionName":"Lincolnshire"},{"intLocationId":"1196","strLocationName":"Grimsby","strLocationNameWithPrefix":"Grimsby","strRegionName":"Lincolnshire"},{"intLocationId":"1229","strLocationName":"Horncastle","strLocationNameWithPrefix":"Horncastle","strRegionName":"Lincolnshire"},{"intLocationId":"1238","strLocationName":"Immingham","strLocationNameWithPrefix":"Immingham","strRegionName":"Lincolnshire"},{"intLocationId":"1728","strLocationName":"Lincoln","strLocationNameWithPrefix":"Lincoln","strRegionName":"Lincolnshire"},{"intLocationId":"1347","strLocationName":"Louth","strLocationNameWithPrefix":"Louth","strRegionName":"Lincolnshire"},{"intLocationId":"1355","strLocationName":"Mablethorpe","strLocationNameWithPrefix":"Mablethorpe","strRegionName":"Lincolnshire"},{"intLocationId":"1362","strLocationName":"Market Rasen","strLocationNameWithPrefix":"Market Rasen","strRegionName":"Lincolnshire"},{"intLocationId":"1489","strLocationName":"Scunthorpe","strLocationNameWithPrefix":"Scunthorpe","strRegionName":"Lincolnshire"},{"intLocationId":"1501","strLocationName":"Skegness","strLocationNameWithPrefix":"Skegness","strRegionName":"Lincolnshire"},{"intLocationId":"1774","strLocationName":"Sleaford","strLocationNameWithPrefix":"Sleaford","strRegionName":"Lincolnshire"},{"intLocationId":"489","strLocationName":"Spalding","strLocationNameWithPrefix":"Spalding","strRegionName":"Lincolnshire"},{"intLocationId":"1513","strLocationName":"Spilsby","strLocationNameWithPrefix":"Spilsby","strRegionName":"Lincolnshire"},{"intLocationId":"488","strLocationName":"Stamford","strLocationNameWithPrefix":"Stamford","strRegionName":"Lincolnshire"},{"intLocationId":"1579","strLocationName":"Ulceby","strLocationNameWithPrefix":"Ulceby","strRegionName":"Lincolnshire"},{"intLocationId":"475","strLocationName":"Wisbech","strLocationNameWithPrefix":"Wisbech","strRegionName":"Lincolnshire"},{"intLocationId":"1609","strLocationName":"Woodhall Spa","strLocationNameWithPrefix":"Woodhall Spa","strRegionName":"Lincolnshire"}]}]

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