arrRegions = [{"intRegionId":"30","strRegionName":"Greater Manchester","arrLocations":[{"intLocationId":"123","strLocationName":"Altrincham","strLocationNameWithPrefix":"Altrincham","strRegionName":"Greater Manchester"},{"intLocationId":"124","strLocationName":"Ashton-Under-Lyne","strLocationNameWithPrefix":"Ashton-Under-Lyne","strRegionName":"Greater Manchester"},{"intLocationId":"125","strLocationName":"Bolton","strLocationNameWithPrefix":"Bolton","strRegionName":"Greater Manchester"},{"intLocationId":"126","strLocationName":"Bury","strLocationNameWithPrefix":"Bury","strRegionName":"Greater Manchester"},{"intLocationId":"5","strLocationName":"Glossop","strLocationNameWithPrefix":"Glossop","strRegionName":"Greater Manchester"},{"intLocationId":"130","strLocationName":"Macclesfield","strLocationNameWithPrefix":"Macclesfield","strRegionName":"Greater Manchester"},{"intLocationId":"122","strLocationName":"Manchester","strLocationNameWithPrefix":"Manchester","strRegionName":"Greater Manchester"},{"intLocationId":"127","strLocationName":"Oldham","strLocationNameWithPrefix":"Oldham","strRegionName":"Greater Manchester"},{"intLocationId":"131","strLocationName":"Other","strLocationNameWithPrefix":"Other","strRegionName":"Greater Manchester"},{"intLocationId":"128","strLocationName":"Rochdale","strLocationNameWithPrefix":"Rochdale","strRegionName":"Greater Manchester"},{"intLocationId":"350","strLocationName":"Salford","strLocationNameWithPrefix":"Salford","strRegionName":"Greater Manchester"},{"intLocationId":"129","strLocationName":"Stockport","strLocationNameWithPrefix":"Stockport","strRegionName":"Greater Manchester"},{"intLocationId":"349","strLocationName":"Trafford","strLocationNameWithPrefix":"Trafford","strRegionName":"Greater Manchester"},{"intLocationId":"228","strLocationName":"Wigan","strLocationNameWithPrefix":"Wigan","strRegionName":"Greater Manchester"}]}]

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

